﻿jQuery(document).ready(function()
{
	/*** Begin Initialization Code ***/

	// If we're in IE6...
	if(jQuery.browser.msie && jQuery.browser.version == "6.0")
	{
		// ...turn off all animations
		jQuery.fx.off = true;
	}

	/*** End Initialization Code ***/

	/*** Begin Menu Rollover Behavior ***/

	jQuery("ul.nav li").mouseenter(function()
	{
		if(!jQuery(this).hasClass("onPage") && !jQuery(this).hasClass("onPageRight") && !jQuery(this).hasClass("onPageLeft"))
		{
			jQuery(this).addClass("on");
		}
	}).mouseleave(function()
	{
		if(!jQuery(this).hasClass("onPage") && !jQuery(this).hasClass("onPageRight") && !jQuery(this).hasClass("onPageLeft"))
		{
			jQuery(this).removeClass("on");
		}
	});

	/*** End Menu Rollover Behavior ***/

	// Set the intial indexes for the slide show
	var currentIndex = 1;
	var priorIndex = 0;

	// Set animation properties
	var storySlideOff =
	{
		paddingTop: "6px",
		marginTop: "0px",
		height: "52px"
	};

	var storySlideOn =
	{
		paddingTop: "7px",
		marginTop: "-18px",
		height: "69px"
	};

	var storySlideRollover =
	{
		paddingTop: "7px",
		marginTop: "-9px",
		height: "60px"
	};

	var storyBackdropOff =
	{
		height: "64px",
		opacity: "0.0"
	};

	var storyBackdropOn =
	{
		height: "82px",
		opacity: "1.0"
	};

	var storyBackdropRollover =
	{
		height: "73px",
		opacity: "1.0"
	};

	var tabOff =
	{
		height: "0px",
		opacity: "0.0"
	};

	var opacityOn =
	{
		opacity: "1.0"
	};

	var opacityOff =
	{
		opacity: "0.0"
	};

	var animationOptions =
	{
		duration: 150
	};

	/*** Begin Carousel Behaviors ***/

    // Engage behaviors only if the carousel elements exist
    if (jQuery("ul.homeStorySlides").length > 0) {
        // Show slides
        jQuery("li.slide2, li.slide3").show();

        // Engage auto slideshow
        var slideShowInterval = setInterval(function () {
            // Roll over the current index
            jQuery("ul.homeStories li").eq(currentIndex).addClass("on").animate(storySlideOn, jQuery.extend(true, { queue: false }, animationOptions)).children("div.backdrop").animate(storyBackdropOn, jQuery.extend(true, { queue: false }, animationOptions)).next("div.storyContent").children("div.leftImage").children("div.white").animate(opacityOn, jQuery.extend(true, {}, animationOptions));

            // Roll down prior index
            jQuery("ul.homeStories li").eq(priorIndex).removeClass("on").animate(storySlideOff, jQuery.extend(true, { queue: false }, animationOptions)).children("div.backdrop").animate(storyBackdropOff, jQuery.extend(true, { queue: false }, animationOptions)).next("div.storyContent").children("div.leftImage").children("div.white").animate(opacityOff, jQuery.extend(true, {}, animationOptions)); ;

            // Fade out slides
            jQuery("li.homeStorySlide").animate(opacityOff, jQuery.extend(true, {}, animationOptions));

            // Center proper slide
            jQuery("li.homeStorySlide").each(function () {
                jQuery(this).animate({ top: ((jQuery(this).index() - currentIndex) * 300) + "px" }, { duration: 0 });
            });

            // Fade in slides
            jQuery("li.homeStorySlide").animate(opacityOn, jQuery.extend(true, {}, animationOptions));

            // Deal with the currentIndex var
            if (currentIndex == 2) {
                currentIndex = 0;
            }
            else {
                currentIndex++;
            }

            // Deal with priorIndex var
            if (priorIndex == 2) {
                priorIndex = 0;
            }
            else {
                priorIndex++;
            }
        }, 5000);

        // Cycle through the home stories and do the positioning
        jQuery("li.homeStorySlide").each(function () {
            // Set the top offset
            jQuery(this).css("top", (jQuery(this).index() * 300) + "px");

            // Make slides visible
            jQuery("ul.homeStorySlides li.homeStorySlide").show();
        });

        // Home Stories Click Behavior
        jQuery("ul.homeStories li").click(function () {
            // Clear the slideshow interval
            clearInterval(slideShowInterval);

            // Only execute if the current slide isn't "on"
            if (!jQuery(this).hasClass("on")) {
                // Remove "on" class from all homeStory LIs
                jQuery("ul.homeStories li").removeClass("on");

                // Set the selected index
                var storyIndex = jQuery(this).index();

                // Fade out current slide
                jQuery("li.homeStorySlide").animate(opacityOff, jQuery.extend(true, {}, animationOptions));

                // Center proper slide
                jQuery("li.homeStorySlide").each(function () {
                    jQuery(this).animate({ top: ((jQuery(this).index() - storyIndex) * 300) + "px" }, { duration: 0 });
                });

                // Fade in current slide
                jQuery("li.homeStorySlide").animate(opacityOn, jQuery.extend(true, {}, animationOptions));

                // Animate all other indexes to an "off" state
                jQuery("ul.homeStories li").each(function () {
                    if (storyIndex != jQuery(this).index()) {
                        // Animate slide to off state
                        jQuery(this).animate(storySlideOff, jQuery.extend(true, {}, animationOptions)).children("div.backdrop").animate(storyBackdropOff, jQuery.extend(true, {}, animationOptions)).next("div.storyContent").children("div.leftImage").children("div.white").animate(opacityOff, jQuery.extend(true, {}, animationOptions));
                    }
                    else {
                        // Animate slide to on state
                        jQuery(this).addClass("on").animate(storySlideOn, jQuery.extend(true, {}, animationOptions)).children("div.backdrop").animate(storyBackdropOn, jQuery.extend(true, {}, animationOptions)).next("div.storyContent").children("div.leftImage").children("div.white").animate(opacityOn, jQuery.extend(true, {}, animationOptions));
                    }
                });
            }
        });

        // Slideshow hovering function
        jQuery("ul.homeStories li").mouseenter(function () {
            if (!jQuery(this).hasClass("on")) {
                jQuery(this).stop().animate(storySlideRollover, jQuery.extend(true, {}, animationOptions)).children("div.backdrop").stop().animate(storyBackdropRollover, jQuery.extend(true, {}, animationOptions)).next("div.storyContent").children("div.leftImage").children("div.white").stop().animate(opacityOn, jQuery.extend(true, {}, animationOptions));
            }
        }).mouseleave(function () {
            if (!jQuery(this).hasClass("on")) {
                jQuery(this).stop().animate(storySlideOff, jQuery.extend(true, {}, animationOptions)).children("div.backdrop").stop().animate(storyBackdropOff, jQuery.extend(true, {}, animationOptions)).next("div.storyContent").children("div.leftImage").children("div.white").stop().animate(opacityOff, jQuery.extend(true, {}, animationOptions));
            }
        });
    }

	/*** Begin Tab Specific Behavior ***/

	// Only bind elements if the tab element is available
	if(jQuery("div.tab").length > 0)
	{
		// Tab hovering behavior
		jQuery("div.tab").mouseenter(function()
		{
			if(!jQuery(this).hasClass("on"))
			{
				jQuery(this).children("div.onState").stop().animate(opacityOn, jQuery.extend(true, {}, animationOptions));
			}
		}).mouseleave(function()
		{
			if(!jQuery(this).hasClass("on"))
			{
				jQuery(this).children("div.onState").stop().animate(opacityOff, jQuery.extend(true, {}, animationOptions));
			}
		});

		// Cycle through the tab states and resize their markers
		jQuery("div.tab div.onState").each(function()
		{
			jQuery(this).css(
			{
				width: jQuery(this).parent("div.tab").outerWidth() + "px",
				height: jQuery(this).parent("div.tab").outerHeight() + "px"
			});
		});
	}

	/*** End Tab Specific Behavior ***/

	/*** Begin Case Study Link Behavior ***/

	// Only bind these behaviors if the case study links area exists in the DOM
	if(jQuery("div.caseStudyLinks").length > 0)
	{
		// Record the original index the arrow is on
		var originalArrowIndex = jQuery("div.caseStudyLinks").find("a.on").parent("p.link").index() - 1;

		// Show the correct arrow
		jQuery("div.caseStudyLinks").children("div.arrow").children("p.link").eq(originalArrowIndex).children("img.orangeArrow").css({opacity: "1.0"});
		
		// Hide all other arrows
		jQuery("div.caseStudyLinks").children("div.arrow").children("p.link").not(":eq(" + originalArrowIndex + ")").children("img.orangeArrow").css({opacity: "0.0"});
		
		// Loop through the link list and make sure the heights correlate
		jQuery("div.caseStudyLinks").children("div.arrow").children("p.link").each(function()
		{
			// Save this current item's index
			var currentIndex = jQuery(this).index() - 1;
			
			// Assign the proper height
			jQuery(this).css({height: jQuery(this).parent("div.arrow").next("div.links").children("p.link").eq(currentIndex).height() + "px"});
		});
		
		// Bind hovering behavior
		jQuery("div.links p.link a").mouseover(function()
		{
			// Clear fade timeout
			if(typeof(linkFade) === "number")
			{
				// Clear timeout variable
				clearTimeout(linkFade);

				// Delete timeout variable
				delete linkFade;
			}

			// Fade all arrows out
			jQuery("img.orangeArrow").animate({opacity: "0.0"}, {queue: false, duration: 250});

			// Fade the target arrow in
			jQuery("img.orangeArrow").eq(jQuery(this).parent("p.link").index() - 1).animate({opacity: "1.0"}, {queue: false, duration: 250});
		}).mouseout(function()
		{
			// Fade all arrows out
			jQuery("img.orangeArrow").animate({opacity: "0.0"}, {queue: false, duration: 250});

			// Fade the original arrow in
			linkFade = setTimeout(function()
			{
				jQuery("img.orangeArrow").eq(originalArrowIndex).animate({opacity: "1.0"}, {queue: false, duration: 250});
			}, 250);
		});
	}

/*** End Case Study Link Behavior ***/

/*** Begin Flex Item Modal Behavior ***/

// Close event binding
jQuery("#colorbox p.close a").live("click", function () {
    jQuery.fn.colorbox.close();
});

// Initialize event binding
jQuery("a.modalLink").colorbox(
	{
	    width: "528px",
	    height: "50px",
	    iframe: false,
	    onComplete: function () {
	        // Remove scrollbars
	        jQuery("#cboxLoadedContent").css({ overflow: "hidden" });

	        // Populate the title for the modal
	        var mediaInfoElement = jQuery.fn.colorbox.element().parent("div.mediaContainer").find("div.mediaInfo");

	        // Populate needed data
	        var mediaParams =
			{
			    mediaTitle: jQuery.fn.colorbox.element().attr("title"),
			    mediaCopy: mediaInfoElement.children("div.mediaCopy").html(),
			    mediaMarkup: mediaInfoElement.children("div.mediaParams").children("span.mediaMarkup").html()
			};

	        // Populate the modal items...
	        jQuery("#colorbox div.mediaContainer").html(mediaParams.mediaMarkup);
	        jQuery("#colorbox div.mediaDescription").html(mediaParams.mediaCopy);
	        jQuery("#colorbox p.title").text(mediaParams.mediaTitle);

	        // Resize event
	        jQuery("div#colorbox").ready(function () {
	            // Calculate height resize value
	            if (jQuery.browser.msie && jQuery.browser.version == "6.0") {
	                // IE 6's rules
	                var resizeHeight = (jQuery("div#cboxContent div#modalContentBody").height() + 35) + "px";
	            }
	            else {
	                // Everyone else's rules
	                var resizeHeight = (jQuery("div#cboxContent div#modalContentBody").height() + 30) + "px";
	            }

	            jQuery.fn.colorbox.resize({ height: resizeHeight, speed: 250 });

	            // Set visibility timeout
	            setTimeout(function () {
	                jQuery("div#modalContentBody").show();
	            }, 525);
	        });
	    }
	});

/*** End Flex Item Modal Behavior ***/
});
