$('html').addClass('js');


// GLOBAL VARIABLES
var droplink_navigation_timer;
var droplink_navigation_timer_duration = 300;
var containerWidth = 980;

	
$(document).ready(function () {

    //Main navigation dropdown
    showDroplinkNavigation();

    //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();
    }

});


/* TOP NAVIGATION DROPDOWN */
function showDroplinkNavigation() {
    $('#main_navigation > ul.menu > li').hover(
	    function () {

	        window.clearTimeout(droplink_navigation_timer);
	        hideDroplinkNavigation();
	        // find main_nav width
	        // var parentMenuWidth = $(this).parent().outerWidth();
	        // find dropdown width
	        // var dropdownWidth = $('.dropdown_container', this).outerWidth();
	        // available space to right of dropdown
	        // var availableSpace = (containerWidth - dropdownWidth);
	        // increment position evenly between menu items
	        // var totalMenuItems = $(this).parent().children().length;
	        // var listItemNumber = $(this).parent().children().index(this);
	        // var offsetIncrements = (availableSpace + 53) / totalMenuItems;
	        // var itemLeftPosition = $(this).position().left;
	        // find how much overflow
	        // var overflowAmount = parentMenuWidth - itemLeftPosition - dropdownWidth;

	        // adjust position accordingly
			// var totalOffset = ((overflowAmount + (listItemNumber * offsetIncrements)) - availableSpace);

			var totalOffset = 0;

			$('.dropdown_container', this).css('left', totalOffset);

	        //  refresh cufon
	        Cufon.refresh('#main_navigation > ul.menu > li > a', { hover: true });
	        // show menu
	        $('.dropdown_container', this).show();
	        // add selected class
	        $(this).addClass('selected');
	        // add arrow (delay for hoverIntent)
	        $('#main_navigation > ul.menu > li.selected > a').addClass('arrow_down');
	        // stop carousel if present
	        var carouselPresent = $('#carousel_container').length;
	        if (carouselPresent > 0) {
	            $('#carousel_images').cycle('pause');
	        };
	    },
		function () {
		    if ($(this).hasClass('selected')) {
		        droplink_navigation_timer = window.setTimeout('hideDroplinkNavigation("#main_navigation > li.selected")', droplink_navigation_timer_duration);
		    } else {
		        hideDroplinkNavigation();
		    };
		    // resume carousel if present
		    var carouselPresent = $('#carousel_container').length;
		    if (carouselPresent > 0) {
		        $('#carousel_images').cycle('resume');
		    };
		});
};
function hideDroplinkNavigation(current_id) {
    $('#main_navigation ul.menu li a').removeClass('arrow_down');
    $('#main_navigation ul.menu li div.dropdown_container').hide();
    $('#main_navigation ul.menu li').removeClass('selected');
    Cufon.refresh('#main_navigation > ul.menu > li > a', { hover: true });
};


//Homepage Carousel 
function initCarousel() {
    // Global vars
    var padding = 40;
    var panelHeight = 139;

    $('#carousel_images').cycle({
        fx: 'fadeout',
        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');
    });

}
