$('html').addClass('js');


// GLOBAL VARIABLES
var droplink_navigation_timer;
var droplink_navigation_timer_duration = 300;
var containerWidth = 910;


$(document).ready(function () {



    //Homepage Carousel
    var homepage_carousel = $('#carousel_container').length;
    if (homepage_carousel) {
        //----- Content functions -----//
        // Assign image and content divs unique ids
        $('#carousel_images div').each(function (id) {
            $(this).attr("id", "slide_" + (id + 1));
        });
        $('#carousel_content div:not(div.text)').each(function (id) {
            $(this).attr("id", "content_" + (id + 1));
        });
        initCarousel();
    }

});





//Homepage Carousel 
function initCarousel() {
    // Global vars
    var padding = 40;
    var panelHeight = 139;

    $('#carousel_images').cycle({
        fx: 'scrollHorz',
        speed: 900,
        timeout: 6000,
        easing: 'easeInOutSine',
        next: '#next',
        prev: '#prev',
        before: onBefore,
        after: onAfter
    });

    // Hide content onLoad
    $('#carousel_content div').hide();

    // Set default width of panel
    setDefaultSlidePanelWidth();
    function setDefaultSlidePanelWidth() {
        var defaultPanelWidth = ($('#carousel_content div#content_1 div.text').width()) + padding;
        $('#carousel_content_panel').css("width", defaultPanelWidth);
    }

    function getSlidePanelWidth(slideToLoad) {
        var nextSlideId = parseInt(slideToLoad);
        var nextSlideContent = $('#carousel_content div#content_' + nextSlideId + ' div.text');
        var nextSlideWidth = nextSlideContent.width() + padding + "px";
        animatePanels(nextSlideId, nextSlideContent, nextSlideWidth);
    }

    // Animate transition
    function animatePanels(nextSlideId, nextSlideContent, nextSlideWidth) {
        // If IE don't animate fade
        if (jQuery.browser.msie) {
            // Fade out text
            $('#carousel_content div').fadeOut(0);
            // Animate panel
            $('#carousel_content_panel').animate({ width: nextSlideWidth }, {
                duration: 650,
                easing: 'easeInOutSine'
            });
            // Fade in text
            $('#carousel_content div').delay(950).fadeIn(0);
        } else {
            // Fade out text
            $('#carousel_content div').fadeOut(300);
            // Animate panel
            $('#carousel_content_panel').delay(300).animate({ width: nextSlideWidth }, {
                duration: 650,
                easing: 'easeInOutSine'
            });
            // Fade in text
            $('#carousel_content div').delay(850).fadeIn(300);
        };
    }


    //----- Transition functions -----//
    // before animation starts
    function onBefore(curr, next, opts) {
        getSlidePagination(curr, next, opts);
        slideToLoad = $(next).attr("id").slice(-1);
        getSlidePanelWidth(slideToLoad);
        // fade down navigation elements [or hide in IE]
        if (!jQuery.browser.msie) {
            $('a#next, a#prev').fadeTo(50, "0.3");
            $('#slide_counter').fadeTo(50, "0");
        } else {
            $('a#next, a#prev').addClass("animated");
            $('#slide_counter').css("color", "#96959D");
        };
        // show click blockers
        $('div#next_block, div#prev_block').show();
    }
    // after animation completes
    function onAfter(curr, next, opts) {
        getSlidePagination(curr, next, opts);
        var currentId = $(this).attr("id").slice(-1);
        // shift content to show relevant slide
        var slideShift = -(currentId - 1) * panelHeight;
        $('#carousel_content div').css("top", slideShift);
        // fade up navigation elements [or show in IE]
        if (!jQuery.browser.msie) {
            // wait for animation to finish
            setTimeout(function () {
                $('a#next, a#prev').fadeTo(300, "1");
                $('#slide_counter').fadeTo(300, "1");
                // hide click blockers
                $('div#next_block, div#prev_block').hide();
            }, 300);
        } else {
            $('a#next, a#prev').removeClass("animated");
            $('#slide_counter').css("color", "#fff");
            // hide click blockers
            $('div#next_block, div#prev_block').hide();
        };
    }

    // Generate pagination
    function getSlidePagination(curr, next, opts) {
        var currentSlide = (opts.currSlide + 1);
        var captionCurrent = currentSlide + '/' + opts.slideCount;
        $('#slide_counter p').html(captionCurrent);
    }

    // Stop slideshow once navigation has been interacted with
    $('a#next, a#prev').click(function () {
        $('#carousel_images').cycle('pause');
    });

}
