jQuery.noConflict();
jQuery(document).ready(function($) {

	if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))) {
		return false;
	}

	// Lets add close buttons to all of the level2 menus
	$(".level2").append("<button class=\"smallCloseButton\">Close</button>");
	$(".level2 > .smallCloseButton").click(function() {
	  closeSubNav();
	  return false;
	});


	// When a top level nav link is clicked...
	$("#mainNavigation > .globalMenu > li > a").click(function(){
	  clickedNav($(this).parent());
	});

	// This keeps slideshows from messing up the content area height when exiting subnav – used in closeSubNavs()
	slideshowHeight = $(".SlideShowContainer").height();


	// When a top level breadcrumb link is clicked...
	$("div.breadcrumbs > a:nth-child(2)").click(function(){
		clickedItemText = $(this).html(); // Get the text content of the link
		mainNavItem = $("#mainNavigation > .globalMenu > li > a").filter(function(index) {
			return $(this).html() == clickedItemText; // Return the corrosponding #mainNavigation item
		}).parent();
		clickedNav(mainNavItem);
	});


	//Functions

	function clearSubs() {
		$("#mainNavigation > .boxTop").fadeOut(500);
		$("#mainNavigation > .boxBottom").fadeOut(500);
		$("#mainNavigation .level2").fadeOut(500);
		$("#mainNavigation").removeClass("subNav");

		// Set all sub navs activeSubNav attribute to false
		$("#mainNavigation > ul > li").attr("activeSubNav", false);
	}
	
	
	function activateSubNav(item) {
	
	        $("body").addClass("animating");
	
			// Clear the current sub nav or .contentBox to get ready for the clicked one,
			clearSubs();
			$("#mainContent .contentBox").fadeOut(500);
			// If we're on home
			if($("body.home").length > 0)
				$("#mainContent #subFeatures").fadeOut(500);
	
	        // Resize the content box for the sub nav
	        resizeContentContainer($("#mainNavigation ."+item+" .level2"), 500, 70);
	
	        // Place a transparent div in front of the navigation so the user can't interact while animation is happening
	        $("#mainNavigation > .boxTop").before("<span id=\"topLevelCover\"></span>");
	
	        // Remove the transparent div after 700ms – animations should be finished by then
	        setTimeout(
	            function() {
	                $("#topLevelCover").remove();
	            },
	            800
	        );
	
	
	        // Display the subNav item
	        setTimeout(
	            function() {
	                $("#mainNavigation > .boxTop").fadeIn(500);
	                $("#mainNavigation > .boxBottom").fadeIn(500);
	                $("#mainNavigation ."+item+" .level2").fadeIn(500);
	                $("#mainNavigation").addClass("subNav");
	                $("#mainNavigation ."+item).attr("activeSubNav", true);
	            },
	            750
	        );
	
	        setTimeout(
	            function() {
	                $("body").removeClass("animating");
	            },
	            1250
	        );
	
	}
	
	
	function clickedNav(clickedItem) {
	
	        // If there is no level2 nav under this item, exit
	        if(!$(clickedItem).children(".level2").attr("class")) return false;
	
	        // If the clicked item is already the active subNav, exit
	        if($(clickedItem).attr("activeSubNav") == "true") return false;
	
	        // Activate this item's level2
	        activateSubNav(
	            $(clickedItem)
	            .attr("class")
	            .replace(/selectedPageRoot/g, "")
	            .replace(/selectedPage/g, "")
	            .replace(/hasChildren/g, "")
	            .replace(/ /g, "")
	        );
	
	    }
	
	
	function closeSubNav() {
	
	    $("body").addClass("animating");
	
	
	    // Get rid of subNavs
	    clearSubs();
	
		// If we're on inside
		if($("body.inside").length > 0) {
			// See if a slideshow on the page is 'collapsed' and, if so, set extraHeight to the original slideshow height
			if ($(".SlideShowContainer").css("height") == "0px") {
				extraHeight = slideshowHeight + 40;
				}
			else {
				extraHeight = 40;
				}

			// Resize the content box for #insideContent
			resizeContentContainer($("#mainContent #insideContent.contentBox"), 500, extraHeight);

			// Bring finside content back after timeout (to allow for animations)
			setTimeout(function() {
				$("#mainContent #insideContent.contentBox").fadeIn(500);
				},
				750
			);
		}
	
		// If we're on home
		else if($("body.home").length > 0) {
			// Resize the content box for the featured content
			resizeContentContainer($("#mainContent #featured.contentBox"), 500, 216);
			
			// Bring feature back after timeout (to allow for animations)
			setTimeout(function() {
				$("#mainContent #featured.contentBox").fadeIn(500);
				$("#mainContent #subFeatures").fadeIn(500);
				},
				750
			);
		}
	
	
	    setTimeout(
	        function() {
	            $("body").removeClass("animating");
	        },
	        1250
	    );
	
	}
	
	
	function resizeContentContainer(box, timeout, extraSpace) {
	// Resizes content container as content changes
	
	
	    // Set default value for extra space added beneath the content
	    extraSpace = typeof(extraSpace) != 'undefined' ? extraSpace : 40;
	
	    // Set variable for total height of content container
	    var height = $(box).height() + extraSpace;
	
	    // Lock the content container height until the animation starts
	    $("#mainContent > .contents").height($("#mainContent > .contents").height());
	
	    // Animate height change
	    setTimeout(
	        function() {
	            $("#mainContent > .contents").animate(
	            { height: height },
	            400,
	            "swing"
	            );
	        },
	        timeout
	    );
	}



	// Watch link on home page

	// Lets add a close button to the video container
	$("#videoContainer").append("<button class=\"smallCloseButton\">Close</button>");
	$("#videoContainer > .smallCloseButton").click(function() {
		closeHomeVideo();
		return false;
	});


	/*$("#mediaControls_watch a").click(function() {

		$("body").addClass("animating");

		// Get rid of featured and subNavs
		clearSubs();
		$("#mainContent .contentBox").fadeOut(500);
		$("#mainContent #subFeatures").fadeOut(500);

		resizeContentContainer($("#mainContent #videoContainer"), 500);

		setTimeout(
			function() {
				$("#videoContainer > .boxTop").fadeIn(500);
				$("#videoContainer > .boxBottom").fadeIn(500);
				$("#videoContainer").fadeIn(500);
			},
			750
		);


		setTimeout(
			function() {
				$("body").removeClass("animating");
			},
			1250
		);

		return false;

	});*/


	// Watch link Functions

	function closeHomeVideo() {
	
		$("body").addClass("animating");
	
		$("#videoContainer > .boxTop").fadeOut(500);
		$("#videoContainer > .boxBottom").fadeOut(500);
		$("#videoContainer").fadeOut(500);
	
		// Resize the content box for the featured content
		resizeContentContainer($("#mainContent #featured.contentBox"), 500, 216);
	
		// Bring feature back after timeout (to allow for animations)
		setTimeout(function() {
			$("#mainContent #featured.contentBox").fadeIn(500);
			$("#mainContent #subFeatures").fadeIn(500);
			},
			750
		);
	
	
		setTimeout(
			function() {
				$("body").removeClass("animating");
			},
			1250
		);
	
	}

});


