$(document).ready(function(){
	SetLogo();
	InitFlashlets();
	InitNatterTicker();
	CreateTabs();
	ImagesPagination();
	TabFormInit();
	DropDownListOpen();
	DropDownListClose();
	ToggleShows();
	SetSiteMapButton();
	PrintPage();
});

// GLOBAL FUNCTIONS ===============================================================
function SetSiteMapButton(){
	$("#sitemap-btn").click(function(){
		$("#sitemap-holder").slideToggle("fast");
		return false;
	});
	
	$("#sitemap-holder").hide();
}

function SetLogo(){
	/* hooks Nick logo up to call the flash via externalInterface */
	$("#nick-logo a").hover(
		function(){
			$("#navigation .home").addClass("home-selected");
			flashMovie = document.getElementById("homeAnim");
			flashMovie.popHome();
		},
		function(){
			$("#navigation .home").removeClass("home-selected");
			flashMovie = document.getElementById("homeAnim");
			flashMovie.closeHome();
		}
	);
}

function InitFlashlets(){
	// Set the home anchor to id as home
	$("#navigation ul li a.home").attr("id","home");
	// home anim in nav --------------------------
	if ($("#nick-logo.nickjr").length > 0)
	{
		var so = new SWFObject("/Flash/Flashlets/HomeAnimNickJr.swf", "homeAnim", "106", "87", "8", "#000000");
	} else {
		var so = new SWFObject("/Flash/Flashlets/homeAnim.swf", "homeAnim", "106", "87", "8", "#000000");
	}
	so.addParam("wmode", "transparent");
	so.write("home");
	$("#home").css({ textIndent:"0" });
	$("#home").css({ background:"none" });
	window.homeAnim = document.getElementById('homeAnim');
}

function InitNatterTicker(){
	
	if ($("#natterTicker").length > 0)
	{
	
		//$("#natterTicker").wrap("<div id='natterMask'></div>");
		$("#natterTicker").clone().appendTo("#natterMask").attr("id","natterTicker2");
		var theWidth = 0;
		$("#natterTicker li").each(
			function(){
				theWidth += ( $(this).width() + 20 );
			}
		);
		$("#natterTicker").width(theWidth);
		$("#natterTicker2").css( {"left":theWidth} );
		$("#natterTicker2").css( {"bottom":0} );
		$("#natterTicker2").width(theWidth);
		
		$("#natterMask").hover(
			function(){
				$("#natterTicker").stop();
				$("#natterTicker2").stop();
			},
			function(){
				AnimNatter();
			}
		);
		
		AnimNatter();
	}
}

function AnimNatter(){
	
	var theWidth = $("#natterTicker").width();
	var thePos = parseInt( $("#natterTicker").css("left").replace("px", "") );
	
	var animTime = ( (theWidth + thePos) / 70)*1000;
	
	var animTo = 0 - theWidth;
	$("#natterTicker").animate({"left":animTo}, animTime, "linear");
	
	var animTo2 = 0;
	$("#natterTicker2").animate({"left":animTo2}, animTime, "linear", function(){ ResetNatterPos(); });

}

function ResetNatterPos(){
	$("#natterTicker").css( {"left":0} );
	$("#natterTicker2").css( {"left":$("#natterTicker").width()} );
	AnimNatter();
}

function CreateTabs()
{
	// Step 1 - Check there is more than 1 pod in tab module
	// Step 2 - Remove h2 headers and replace with tab list items
	// Step 3 - Create tab click events
	// Step 4 - Display the correct tab otherwise show first
	
	// Create list of Useful Variables
	var TabHtmlList = "<ul class=\"tabs\"></ul>";
	var TabHtmlListItem;
	var TabIndex = 0;
	
	// STEP 1
	if($(".tabs-list h2").length == 2)
	{
	
	// STEP 2
		$(".tabs-list").prepend(TabHtmlList);
		$(".tabs-list h2").each(function()
		{
			if(TabIndex == 0)
			{
				TabHtmlListItem = "<li class=\"tab-"+$(this).text().toLowerCase()+"\"><a class=\"selected\" href=\"#\">"+$(this).text()+"</a></li>";
				$(".tabs-list > ul").append(TabHtmlListItem);
				$(this).remove();
			} else {
				TabHtmlListItem = "<li class=\"tab-"+$(this).text().toLowerCase()+"\"><a href=\"#\">"+$(this).text()+"</a></li>";
				$(".tabs-list > ul").append(TabHtmlListItem);
				$(this).remove();
			}
			TabIndex++;
		});
		
	// STEP 3
		$(".tabs-list .tabs a").click(function()
		{
			var TabSelected = $(this).parent().attr("class").replace("tab-","");
			$(".tabs-list > div").hide();
			$(".tabs-list > div."+TabSelected).show();
			$(".tabs-list .tabs a").removeClass("selected");
			$(this).addClass("selected")
			return false;
		});
		
	// STEP 4

		$(".tabs-list > div").hide()
		
		if($(".tabs-list li.selected").length > 0) {
			// Hide all tab content modules
			$("ul.tabs > li > a").removeClass("selected");
			
			// Show only the content module that has the selected movie
			$(".tabs-list > div:has(.selected)").show();
			
			// Select the correct tab
			// NOTE: Limitation to only 2 tab modules...
			if($(".tabs-list > div:odd:has(.selected)").length > 0){
				$(".tabs-list .tabs a:odd").addClass("selected");
			} else {
				$(".tabs-list .tabs a:even").addClass("selected");
			}
			
		}  else {
			$(".tabs-list > div:first").show();
		}
	}
}

function ImagesShow(Start,End)
{
	var ImageNumber = 0;	
	$(".tabs-list > .images > .non-scroll > ul > li").each(function()
	{
		if(ImageNumber >= Start && ImageNumber < End)
		{
			$(this).show();
		} else {
			$(this).hide();
		}
		ImageNumber++;
	});
}

// Pod Tab and Pod Pagination ===============================================================
function ImagesPageSelectUpdate(PageNum)
{
	var PageSelectUpdate = parseInt(PageNum) - 1;
	$(".tabs-list > .images .pod-pagination li").removeClass();
	$(".tabs-list > .images .pod-pagination li:eq(" + PageSelectUpdate + ")").addClass("selected");
}

function ImagesPagination()
{

	// Step 1 - Estabish number of pages
	// Step 2 - Create necessary html
	// Step 3 - Show only the required images
	// Step 4 - Create pagination click events
	
	// Step 1
	var ImagesDisplayNum = 9;
	var ImagesNum = $(".tabs-list > .images li").length;
	var ImagesPagesMaxNum = Math.ceil(ImagesNum / ImagesDisplayNum);
	var ImageStart = 0;
	
	// Step 2
	$(".tabs-list > .images > .non-scroll > ul").css("height","228px");

	if(ImagesNum > 9){
		
		var PaginationHtml = "<div class=\"pod-pagination\">" +
			"<a class=\"prev\" href=\"#\">Prev</a>" +
			"<ul>" +
			"</ul>" +
			"<a class=\"next\" href=\"#\">Next</a>" +
			"</div>";
		
		$(".tabs-list > .images").append(PaginationHtml);
		
		for(var x = 1; x <= ImagesPagesMaxNum; x++)
		{
			if(x == 1){
				var PaginationHtmlListItem = "<li class=\"selected\"><a href=\"#\">" + x + "</a></li>";
			} else {
				var PaginationHtmlListItem = "<li><a href=\"#\">" + x + "</a></li>";
			}
			$(".tabs-list > .images .pod-pagination ul").append(PaginationHtmlListItem);
		}

		// Step 3
		ImagesShow(ImageStart,ImagesDisplayNum);
		
		// Step 4
		// Page number links
		$(".tabs-list > .images .pod-pagination ul li a").click(function()
		{
			var ImagePageSelected = $(this).text();
			var ImageEnd = $(this).text() * ImagesDisplayNum;
			var ImageStart = ImageEnd - ImagesDisplayNum;
			ImagesShow(ImageStart,ImageEnd);
			ImagesPageSelectUpdate(ImagePageSelected);
			return false;
		});
		
		// Next and previous page links
		$(".tabs-list > .images .pod-pagination > a").click(function()
		{
			var CurrentPageNum = $(".tabs-list > .images .pod-pagination li.selected a").text();
			var PrevPageNum = parseInt(CurrentPageNum) - 1;
			var NextPageNum = parseInt(CurrentPageNum) + 1;
			
			if($(this).attr("class") == "next"){
				if(NextPageNum <= ImagesPagesMaxNum){		
					var ImageEnd = NextPageNum * ImagesDisplayNum;
					var ImageStart = ImageEnd - ImagesDisplayNum;
					ImagesShow(ImageStart,ImageEnd);
					ImagesPageSelectUpdate(NextPageNum);
				}
				return false;
			}
			
			if($(this).attr("class") == "prev"){
				if(PrevPageNum > 0){		
					var ImageEnd = PrevPageNum * ImagesDisplayNum;
					var ImageStart = ImageEnd - ImagesDisplayNum;
					ImagesShow(ImageStart,ImageEnd);
					ImagesPageSelectUpdate(PrevPageNum);
				}
				return false;
			}
		});
	}
}

//  Tab Form Scripts Start ===============================================================

	function TabFormInit()
	{
		TabFormShowButton();
		TabFormCloseButton()

		if ($("#success.tab-form, fieldset.tab-form .error span:visible, fieldset.tab-form span:visible .error").length > 0) {
		    TabFormShow();
		}
		else {
		    TabFormHide();
		}
		
		$("a.tab-form").toggle(function(){
			TabFormShow()
			return false;
		},function()
		{
			TabFormHide();
			return false;
		});
	}

	function TabFormHide()
	{
		$("a.tab-form").removeClass("tab-selected");
		$("fieldset.tab-form").hide();
	}

	function TabFormShow()
	{
		$("a.tab-form").addClass("tab-selected");
		$("fieldset.tab-form").show();
	}

	function TabFormCloseButton()
	{

		var CloseHtml = "<a class=\"close\" href=\"#\">Close</a>";
		$(".general-detail .content fieldset.tab-form").append(CloseHtml)
		
		$("fieldset.apply a.close").click(function(){
			TabFormHide()
			$(".general-detail .overview .apply a.selected").removeClass("selected");
			
			return false;
		});
	}

	function TabFormShowButton(){
		$("a.tab-form-button").click(function(){
			TabFormShow();
	});
}

//  Drop Down List ===============================================================

function DropDownListOpen()
{
	$("h2.all-shows a, a.winners-archive").click(function()
	{
		$(".drop-down-list").toggle();
		return false;
	});
}

// Close the option to view all Nick shows
function DropDownListClose()
{
	$(".drop-down-list a.close").click(function()
	{
		DropDownListHide();
		return false;
	});
}

// Hide the list of other Nick shows
function DropDownListHide()
{
	$(".drop-down-list").hide();
}

//  Toggle Shows ===============================================================

function ToggleShows(){	
	$("#header #navigation li a.shows").click(function()
	{

		// Only display Top Show if user is not on the Show page
		if($(".shows-all").length == 0 ){
			
			if($("#header #navigation li a.shows.close-shows").length != 0){
				$("#header #navigation li a.shows.close-shows").removeClass("close-shows");
				closeShows();
				return false;
			}
			// Check which nick site is being visited
			var ShowsUrl = $(this).attr("href");
			var ShowsTopUrl = ShowsUrl + "TopShows.aspx";
			
			$("#header #navigation li a.shows").addClass("close-shows");
			// Set up Variables - html code for close link
			var containHtml	=	"<img class=\"loading\" src=\"/images/Global/loadingAnimation.gif\" alt=\"loading\" />" +
								"<div class=\"shows\"></div>" +
								"<div class=\"options\">" + 
									"<a class=\"view-all\" href=\"" + ShowsUrl + "\">View all shows</a>" +
									"<a class=\"close\" href=\"#\">Close</a>" +
								"</div>";
			
			// Has the shows already been open? If not create content
			if($(".shows-top a.close").length == 0) 
			{
				// Append the "contain" container
				$(".shows-top .contain").append(containHtml);
				$(".shows-top .options a.close").bind("click",
					function(event){
						closeShows();
						return false;
					}
				);
				
				$(".shows-top .contain div.shows").hide();
				$.ajax(
				{
					url: ShowsTopUrl,
					dataType: 'html',
					success: function(html){
						$(".loading").hide()
						$(".shows-top .contain div.shows").append(html)
						$(".shows-top .contain div.shows").fadeIn();
					}
				});
				
				// Display show navigation - slide down
				$(".shows-top").slideDown(500);
				
				return false;
			} 
			else 
			{
				$(".shows-top .contain div.shows").fadeIn();
				$(".shows-top").slideDown(500);
			}
			
			// Remove alt tag from img
			$(".shows-top ul li img").removeAttr("alt","title");
						
			return false;
		}
	});
}

function closeShows(){
	$(".shows-top .contain .shows").fadeOut();
	$(".shows-top").slideUp(500);
	if($("#header #navigation li a.shows.close-shows").length != 0){
		$("#header #navigation li a.shows.close-shows").removeClass("close-shows");
	}
}

//  Tab Form Scripts Start ===============================================================

function PrintPage(){
	$(".print").click(function(){
		window.print();
		return false;
	});
}