jQuery(function () {
    modalWindow();
    roundedCorners();
    easySlidr();
    selectSliderPanel();
    toolTip();
    builderPanel();
    bannerSlider();
    bubbleLogin();
});

function roundedCorners() {
    var window, disneyDiv, products, builder;
    window = $('.window');

    if (window.length > 0) {        
        window.css('background', '#fff').corner("3px");
        window.parent().css({ background: '#bfbfbf', padding: '1px' }).corner("3px");
    }


    disneyDiv = $('.disney div.inner');
    if (disneyDiv.length > 0) {
        disneyDiv.css('background', '#fff').corner("3px").parent().css({ background: '#bebebe', padding: '1px' }).corner("3px");
    }

    products = $('.product_area div.center li a')
    if (products.length > 0) { $('.product_area div.center li a').corner("3px"); }


    builder = $('ul.builder_group_select li a')
    if (builder.length > 0) { builder.corner("3px"); }
};

function modalWindow() {
    //When you click on a link with class of poplight and the href starts with a # 
    if ($('a.modal[href^=#]'))
        $('a.modal[href^=#]').click(function (e) {
            e.preventDefault(); // Cancel the link behavior

            var popID = $(this).attr('rel'); //Get Popup Name
            var popURL = $(this).attr('href'); //Get Popup href to define popup location (eg. ?w=300&amp;t=100&amp;=100)

            //Pull Query & Variables from href URL
            var query = popURL.split('?');
            var dim = query[1].split('&');
            var popWidth = dim[0].split('=')[1]; // Get the first query string value
            var popHeight = dim[1].split('=')[1];
            var popTop = dim[2].split('=')[1];
            var popLeft = dim[3].split('=')[1];
            var popOpacity = dim[4].split('=')[1];
            var popCenter = dim[5].split('=')[1];

            //Center or not to center the popup
            if (popCenter == 'no') {
                //Fade in the Popup and add close button
                $('#' + popID).fadeIn().css({ 'width': Number(popWidth), 'height': Number(popHeight) }).prepend('<a href="#" class="close"><img src="/images/icon_close_pop.png" class="btn_close" title="Close Window" alt="Close" /></a>');
                $('.window').fadeIn().css({ 'width': Number(popWidth), 'height': Number(popHeight) });

                //Apply window positioning
                $('#' + popID).css({ 'left': Number(popLeft) });
                $('.modal_container').css({ 'top': Number(popTop) });
            } else if (popCenter == 'yes') {
                //Fade in the Popup and add close button
                $('#' + popID).fadeIn().css({ 'width': Number(popWidth) }).prepend('<a href="#" class="close"><img src="/images/icon_close_pop.png" class="btn_close" title="Close Window" alt="Close" /></a>');

                //Define margin for center alignment (vertical + horizontal) - we add 10 to the height/width to accomodate for the padding + border width defined in the css
                var popMargTop = ($('#' + popID).height() + 10) / 2;
                var popMargLeft = ($('#' + popID).width() + 10) / 2;

                //Apply Margin to Popup
                $('#' + popID).css({
                    'margin-top': -popMargTop,
                    'margin-left': -popMargLeft,
                    'position': 'fixed', 'top': '50%', 'left': '50%'
                });
            }

            //Fade in Background
            $('body').append('<div id="fade"></div>'); //Add the fade layer to bottom of the body tag.
            if (popOpacity == 'on') {
                $('#fade').css({ 'filter': 'alpha(opacity=60)' }).css({ opacity: 0.6 }).fadeIn(); //Fade in the fade layer 
            } else if (popOpacity == 'off') {
                $('#fade').css({ 'filter': 'alpha(opacity=0)' }).css({ opacity: 0 }).fadeIn(); //Fade in the fade layer 
            }
            return false;
        });

    //Close Popups and Fade Layer
    if ($('a.close, #fade, input.closeme'))
        $('a.close, #fade, input.closeme').live('click', function () { //When clicking on the close or fade layer...
            $('#fade, .window_container').fadeOut(function () { $('#fade, a.close').remove(); }); //fade them both out

            return false;
        });
};

function easySlidr() {
    if ($("#slider") && $("#slider").easySlider)
        $("#slider").easySlider({
            auto: false,
            continuous: true
        });
};

function selectSliderPanel() {
    if ($('.product_slider div#slider ul'))
        $('.product_slider div#slider ul').click(function (e) {
            // unhighlight the previous selection
            $('.product_slider div#slider ul li div.active').removeClass('active');
            // highlight the selection
            $(e.target).closest('li div').addClass('active');
            return false;
        });
};

function toolTip() {
    if ($('#tooltip'))
        $('#tooltip').click(function (e) {
            e.preventDefault(); // Cancel the link behavior
            $('.tooltip_bubble').toggleClass('show');
            return false;
        });
};

function builderPanel() {
    if ($('.slider_controls span a'))
        $('.slider_controls span a').click(function () {

            $('.builder_panel').animate({ top: '-=562', height: '+=560' }, "slow", function () {
                $('.builder_panel div.builder_tabs').css('display', 'block');
            }).css('display', 'block');

            $('body').append('<div id="fade"></div>'); //Add the fade layer to bottom of the body tag.
            $('#fade').css({ 'filter': 'alpha(opacity=70)' }).css({ opacity: 0.7 }).fadeIn(); //Fade in the fade layer 
            return false;
        });
    if ($('.builder_panel span a, a.close_panel, #fade'))
        $('.builder_panel span a, a.close_panel, #fade').live('click', function (e) {
            e.preventDefault(); // Cancel the link behavior

            $('.builder_panel').animate({ top: '+=562', height: '-=560' }, "slow", function () {
                $('.builder_panel').css('display', 'none');
                $('.builder_panel div.builder_tabs').css('display', 'none');
            });

            $('#fade').fadeOut(function () { $('#fade').remove(); }); //fade them both out

            return false;
        });
};


//----------------------------------------------------------------------------------------------------------------------------------------------------------------------------//
//banner slider --------------------------------------------------------------------------------------------------------------------------------------------------------------// 

function bannerSlider() {

    $(".slider li").hide();
    $(".slider li").css({ position: "absolute" });
    $(".slider li").eq(0).show();

    //Simple Slider by Soh Tanaka
    //Set Default State of each porfolio piece
    if ($(".slider_paging")) $(".slider_paging").show();
    if ($(".slider_paging a")) $(".slider_paging a").first().addClass("active");

    //Get size of slide, how many there are, then determine the size of the slider.
    if ($(".slider_window") && $("ul.slider li")) {
        var sliderWidth = $(".slider_window").width();
        var sliderSum = $("ul.slider li").size();
        var sliderFull = sliderWidth * sliderSum;

        //Adjust the image reel to its new size
        //$(".slider").css({ 'width': sliderFull });

        //Paging + Slider Function
        rotate = function () {
            var triggerID = $active.attr("rel") - 1; //Get number of times to slide
            var sliderPosition = triggerID * sliderWidth; //Determines the distance the slider reel needs to slide

            var oldSelection = $(".slider_paging a.active").index();

            $(".slider_paging a").removeClass('active'); //Remove all active class
            $active.addClass('active'); //Add active class (the $active is declared in the rotateSwitch function)

            var newSelection = $(".slider_paging a.active").index();

            $(".slider li").eq(oldSelection).fadeOut('slow');
            $(".slider li").eq(newSelection).fadeIn('slow');

            //Slider Animation
            //$(".slider").animate({ left: -sliderPosition }, 804);
        };
    }

    //Rotation + Timing Event
    rotateSwitch = function () {
        play = setInterval(function () { //Set timer - this will repeat itself every 3 seconds
            if ($('.slider_paging a.active')) {
                $active = $('.slider_paging a.active').next();

                if ($active.length === 0 && $('.slider_paging a:first')) { //If paging reaches the end...
                    $active = $('.slider_paging a:first'); //go back to first
                }


                rotate(); //Trigger the paging and slider function
            }
        }, 6000); //Timer speed in milliseconds (3 seconds)  
    };

    rotateSwitch(); //Run function on launch

    //On Hover
    if ($(".slider li")) $(".slider li").hover(function () {
        clearInterval(play); //Stop the rotation
    }, function () {
        rotateSwitch(); //Resume rotation
    });

    //On Click
    if ($(".slider_paging a")) $(".slider_paging a").click(function () {
        $active = $(this); //Activate the clicked paging
        //Reset Timer
        clearInterval(play); //Stop the rotation
        rotate(); //Trigger rotation immmediately
        rotateSwitch(); //Resume rotation
        return false; //Prevent browser jump to link anchor
    });
};
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------------//

// FLYOUT LOGIN BOX --------------------------------------------
function bubbleLogin() {
    // IE7 and below browser check.  Disables popup Register bubble.
    if ($.browser) {
        if (!($.browser.msie) || !(parseInt($.browser.version, 10) < 8)) {
            $('#bubbleLogin').click(function (e) {
                e.preventDefault(); // Cancel the link behavior
                //$(e.target).closest('li').addClass('active');
                $('li.login_bt span').toggleClass('show');
                $('body').append('<div id="fade"></div>');                 

                $('#fade').css({ 'filter': 'alpha(opacity=20)' }).css({ 'opacity': '0.2', 'z-index': '1' }).fadeIn(); //Fade in the fade layer
                $('#fade').live('click', function () { //When clicking on fade layer...
                    //$(e.target).closest('li').removeClass('active');
                    $('#fade').fadeOut(function () { $('#fade').remove(); }); //fade out
                    $('li.login_bt span').removeClass('show');
                });
            });

            //If Enter key is pressed, send the login form
            $('.login_body').keypress(function (event) {
                if (event.which == 13) {
                    $("#" + HEADERLOGINSUBMITBUTTONID).click();
                }
            });
        };
    }
    return false;
};
