﻿/* Image Carousel - Gavin Chapman */

//when document is ready
$("#bannerControls").append("&nbsp; Loading...");

$(window).load(function(){
	//set up variables
	bnr_current=1;
	bnr_paused=false;
	bnr_total=$("#carousel").find(".carouselImage").length;

	//hide banners and create banner links
	i=0;
	$("#bannerControls").html("");
	$("#bannerControls").append("<a style='float:left; width: 10px; text-align: center; margin-right: 10px;' href='javascript:jQuery.noop()' id='pauseButton' onclick='pauseBanner();'>||</a>");
//	$("#bannerControls").append("<a style='float:left; width: 10px; text-align: center; margin-right: 10px;' href='javascript:function(){}' id='pauseButton' onclick='pauseBanner();'>||</a>");

$("#carousel").find(".carouselImage").each(function(){
		i++;
		$(this).hide();
		$(this).attr("num",i);
		if(bnr_total>1)
			if(i==1)
				$("#bannerControls").append("<a style='border-left: 1px solid white;' href='javascript:jQuery.noop()' onclick='changeBanner($(this));return true;' rel='"+i+"' longdesc='"+$(this).attr("id")+"' title='"+$(this).attr("longdesc")+"'>"+i+"</a>")
			else
				$("#bannerControls").append("<a href='javascript:jQuery.noop()' onclick='changeBanner($(this));return false;' rel='"+i+"' longdesc='"+$(this).attr("id")+"' title='"+$(this).attr("longdesc")+"'>"+i+"</a>")
			
			//$("#bannerControls").append("<a href='#' onclick='changeBanner($(this))' rel='"+i+"'>"+i+"</a>")
	});
	
	
	
	//show banner #1	
	$(".carouselImage[num='"+bnr_current+"']").show();
	
	//get the time to live
	ttl=$(".carouselImage[num='"+bnr_current+"']").attr("rel");

	//if there's only one banner, remove the controls.
	if(bnr_total==1)
		$("#bannerControls").hide();
	else 
	{
		//otherwise show the first banner, set the first link and set up the callback
		$("#bannerControls").find("a[rel='"+bnr_current+"']").addClass("selected");
		$(".carouselImage[num='"+bnr_current+"']").animate({opacity: 1.0}, ttl*1000).fadeOut(150, function(){ bnrCallBack(); });
	
	}
	
	if(bannerId=getQueryString("banner")){
		if($("#"+bannerId).size())
		{
			//$("#bannerControls").hide();
			pauseBanner();
			$(".carouselImage[num='"+bnr_current+"']").stop(true,true).hide();
			$(".carouselImage[id='"+bannerId+"']").stop(true,true).show();
			
			$("#bannerControls").find("a[rel='"+bnr_current+"']").removeClass("selected");
			$("#bannerControls").find("a[longdesc='"+bannerId+"']").addClass("selected");
			bnr_current=$("#bannerControls").find("a[longdesc='"+bannerId+"']").attr("rel");
		}
	}
	
	
});

function getQueryString(id){
	qs=window.location.search.substr(1,window.location.search.length-1);
	queries=qs.split("&");
	content=false;
	for(x in queries)
	{
		tmp=queries[x].split("=");
		if(tmp[0].toLowerCase()==id.toLowerCase())
		{
			content=tmp[1];
		}
	}
	return content;
}

//Banner change function
function bnrCallBack(){
	if(!bnr_paused){
		//remove the selected class from the current banner link
		$("#bannerControls").find("a[rel='"+bnr_current+"']").removeClass("selected");
		
		//increment banner number and check it's not gone over the total, if it has, fade in 1st banner, stop.
		bnr_current++;
		if(bnr_current>bnr_total)
		{
			bnr_current=1;
			$(".carouselImage[num='"+bnr_current+"']").fadeIn(250);
			$("#bannerControls").find("a[rel='"+bnr_current+"']").addClass("selected");
			pauseBanner();
		}
		else
		{
			//add the selected class to the new banner link
			$("#bannerControls").find("a[rel='"+bnr_current+"']").addClass("selected");
			
			//set up the fade in for this and callback for the next banner
			$(".carouselImage[num='"+bnr_current+"']").fadeIn(250).animate({opacity: 1.0}, ttl*1000).fadeOut(150,function(){ bnrCallBack(); });
		}
	}
}

function pauseBanner(){
	if(!bnr_paused)
	{
		bnr_paused=true;
		$(".carouselImage[num='"+bnr_current+"']").stop(true,true).fadeIn();
		$("#pauseButton").html("►");	
	}
	else
	{
		bnr_paused=false;
		$(".carouselImage[num='"+bnr_current+"']").fadeIn(250).animate({opacity: 1.0}, 0).fadeOut(150,function(){ bnrCallBack(); });
		$("#pauseButton").html("||");		
	}
	
}

//manually clicked on banner function
function changeBanner(dom){
	
	//check we're not clicking on the same banner we're showing.
	if(bnr_current!=$(dom).attr('rel'))
	{
		//if not, then remove the class on the old banner link, add to the new one
		$("#bannerControls").find("a[rel='"+bnr_current+"']").removeClass("selected");
		$("#bannerControls").find("a[rel='"+$(dom).attr('rel')+"']").addClass("selected");

		//set up the fade and callback for the new old banner, fade in the new on callback.
		$(".carouselImage[num='"+bnr_current+"']").stop(true,true).fadeOut(150, function(){ $(".carouselImage[num='"+$(dom).attr('rel')+"']").stop(true,true).fadeIn(150); });
	}
	else //otherwise we do nothing except stop the current animation.
		$(".carouselImage[num='"+$(dom).attr('rel')+"']").stop(true,true);
		
	//set up for the next click (if there is one).
	bnr_current=$(dom).attr('rel');
}	


