$(window).load(
function () {
	var $selectedLink;
	var clicked = false;
	var bannerIndex = 0;
	var bannerCount;
	var textColour = "#1B1B1B";
	
	//get the top nav scripts (including the search box blur script)
	$.getScript("http://www.leicester.gov.uk/includes/scripts/top-nav.js");
	//$.getScript("includes/scripts/top-nav.js");
	
	/* accessibility scripts */
	$.getScript("http://www.leicester.gov.uk/includes/scripts/jquery.cookie.js", function() {
	    $.getScript("http://www.leicester.gov.uk/includes/scripts/accessibility.js");
    });
	
	/* 
	 multi panel 
	(this is done inline below)
	*/
    //$.getScript("http://www.leicester.gov.uk/includes/scripts/multi-panel.js");
	
	$.getScript("http://www.leicester.gov.uk/includes/scripts/link-fixer.js");	
	
	/*loadMobileScripts();
	
	function loadMobileScripts(){
	    if(parseInt($(window).width()) <= 480){
	        $.getScript("includes/scripts/mobile.js");
	        $(window).unbind("resize");
	    }else {
	        $(window).resize(function(){
	            loadMobileScripts();
	        });
	    }
	}*/

		
	$("div#banner-text-outer div.banner-text h2 a").click(
		function(){
			if($selectedLink != this){
				clicked = true;
				var index = $("div#banner-text-outer div.banner-text h2 a").index(this);
				bannerIndex = index;
				SelectBanner(index);
				SetTextColour($(this));
				$selectedLink = this;
			}
			return false;
		}
	);
	
	function SetTextColour($element){
		textColour = $element.attr("class");
		if(textColour == "")
			textColour = "#1B1B1B";
			
		//set the text colour based on the class of the link pressed
		$("div#banner-text-outer div.banner-text a").css("color", textColour);
	}
	
	function SelectBanner(bannerIndex){
		var $banners = $("div#banner-text-outer div");
		var $textLinks = $("div#banner-text-outer div.banner-text a:nth-child(2)");
		
		//remove the class 'selected' from all banners
		$banners.removeClass("selected");
		//select the correct banner
		$banners.eq(bannerIndex).addClass("selected");
		//reset the colours of the text links and headings
		$("div#banner-text-outer div.banner-text a").attr("style", "");
		//hide all visible text links
		(":visible", $textLinks).stop().hide();
		
		//if the selected banners text link is not visible, slide it down
		$("div#banner-text-outer div.selected a:not(:visible):nth-child(2)").stop().slideDown(500);
		$("div#banner-inner a.banner").each(
			function(){
				var z_index = $(this).css("z-index");
				z_index--;
				$(this)
				.attr("style", "")
				.css("z-index", z_index)
				.removeClass("selected");
			}
		);
		var $selectedBanner = $("div#banner-inner a.banner").eq(bannerIndex);
		$selectedBanner.stop()
		.css("z-index", 4)
		.hide()
		.fadeIn(300)
		.addClass("selected");

	}
	
	//multi-panel stuff	
	$("div.multi-panel div.element-panel-alpha-header-inner a").click(
	function(){
		var linkIndex = $("div.multi-panel div.element-panel-alpha-header-inner a").index(this);
		$("div.multi-panel div.element-panel-alpha-header-inner a").removeClass("selected");
		$(this).addClass("selected");
		var $contentPanels = $("div.multi-panel div.element-panel-alpha-body-content-inner");
		$contentPanels.removeClass("selected");
		$contentPanels.eq(linkIndex).addClass("selected");
		return false;
		
	});
	
	//home page multi-panel
	var $topTasks = $("div#top-tasks ul#headings h3");
	$topTasks.hover(
		function(){
			$topTasks.parent().removeClass("hover");
			$(this).parent().addClass("hover");
		}, 
		function(){
		}
	);
	
	$("div.top-task li").hover(
	function(){
	    $("div",$(this)).stop().animate({ top:"0px"}, 300);
	        
	},
	function(){
	    $("div",$(this)).stop().animate({ top:"85px"}, 300);    
	});
	
	//home page a-z
	$("ul#a-z li a").click(
		function(){
			if(!$(this).parent().hasClass("selected")){
				$(this).parent().addClass("selected").siblings().removeClass("selected");
				var areaToAddTo = $("div#atozTopLinks");
				var target = "div#" + $(this).text();
				var pageElementToLoad = "atoz_topListings.html " + target;
				$(areaToAddTo).load(pageElementToLoad,
					function(){
						$(this)
						.hide()
						.slideDown(350);
					}								  
				); 
			}
			
			return false;
		}
	  );
	
	
	//home page services
	var $serviceHeaders = $("div#tasks-panel ul.lgnl h3");
	var $expandLink = "<a href=\"#\" class=\"expand\" title=\"show top links for this section\">expand</a>";
	$serviceHeaders.before($expandLink);
	$("ul.lgnl li a.expand").click(
		function(){
		    $("ul.lgnl ul").css({
		        "position"  : "absolute",
			    "left"      : "-9999px"
		    });
			if(!$(this).parent().hasClass("selected")){
			    $(this).siblings("ul").css({
			        "position"  : "relative",
			        "left"      : "0px"
			        });
			    //.show();
			    $("ul.lgnl li").removeClass("selected");
			    $(this).parent().addClass("selected");
			}else{
			    $("ul.lgnl li").removeClass("selected");
			    //$("ul.lgnl ul").attr("style", "");
			}
			return false;
		}
	);
		
		
	//home page whats on panel
	var $whatsOnListItems = $("div#whats-on-panel ul li");
	$whatsOnListItems.show();
	
	$("<div class=\"whats-on-scroller\"></div>").appendTo($("div#whats-on-panel div.element-panel-beta-body-content-inner"));
	for(var i=1; i<=$whatsOnListItems.length;i++){
	    var $pagerIcon = $("<a href=\"#\">"+ i + "</a>")
	    .click(function(){
	        var $eventListItem = $("p.whats-on-date", $whatsOnListItems.eq(parseInt($(this).text())-1));
	        scrollWhatsOnList($eventListItem.position().top, parseInt($(this).text())-1);
	        $(this).addClass("selected")
	        .siblings().removeClass("selected");
	        return false;
	    })
	    .appendTo($("div.whats-on-scroller"));
	    if(i==1)
	        $pagerIcon.addClass("selected");
	}
	
	$("p.whats-on-date", $whatsOnListItems)
	.addClass("clickable")
	.click(function(){
	    scrollWhatsOnList($(this).position().top, $whatsOnListItems.index($(this).parent()));
	});
	
	function scrollWhatsOnList($top, $scrollIconIndex){
	    $("div#whats-on-panel ul").animate({
	        top: -$top + 58
	    },500);	 
	    $("div.whats-on-scroller a").eq($scrollIconIndex).addClass("selected")
	        .siblings().removeClass("selected");
	}

	
});
