// Site wide js file
// Author: Amersham websites

$(document).ready(function(){
			
		//navigation call first 
		$(".navAccess").find("li.disabled a").click(function(){
			
			//remove all other info spans
			//$("span.errMes").fadeOut("fast", function() {
					$("span.errMes").remove();
				//  });
			var thisEl = $(this);
			
			
			if (!thisEl.parent().find(".errMes").length) {
				var errorHTML = '<span class="errMes">Only available to trade, please sign or or <a href="/contactus_temp.asp">contact us</a> to find out more.</span>';	
				thisEl.parent().append(errorHTML);
			}
			//handle disabled click
			thisEl.parent().find(".errMes").css({marginLeft:"-65px"}).animate({
				top:-70,
				opacity:0.9
				},
				{
				  duration: 500,
				  easing: "easeOutBack"
				});
			
			//fade it out again
			setTimeout(function(){ 
								
				thisEl.parent().find(".errMes").fadeOut("slow", function() {
					thisEl.parent().find(".errMes").remove();
				  });
								
			}, 3000);
				
			return false;
			
		});	
		
		//if error message
		
		if ($(".loginerror").length) {
			setTimeout(function(){ 
								
				$(".loginerror").fadeOut("slow");
								
			}, 5000);
		}
		

		//main rotating banners		
		if ($("#mainBanners ul li").length>1) {
			setupRotatingBanners({curr:1});
		}
	});

	function setupRotatingBanners(opts){

		var cfg = {
			rbSpeed:	4000,
			rbCurr:		1,	//current is also first to show
			rbLength:	$("#mainBanners").find(">ul>li").length
		}
		cfg = $.extend(cfg,opts);

		
		//set up style dependancy apply non accessible styles
						
		$("#mainBanners")
		.find(">ul>li").css({
			position:	"absolute",
			left:		0,
			top:		0
		});

		//set up the initial display
		$("#mainBanners").find(">ul>li").each(function(i){
			if (i!=(cfg.rbCurr-1)) {
				$(this).fadeTo(0,0);						
			}
		});
		
		//control html/function				
		var contHtml = '<div class="control"><ul>';
		$("#mainBanners").find(">ul>li").each(function(i){
			contHtml += '<li>'+(i+1)+'</li>';	
		});
		contHtml += '</ul></div>';


		$("#mainBanners").append(contHtml);
		$("div.control").find("li").each(function(i){					
				
				$(this).click(function(){			
					//go to selected banner
					theloop(i+1);
					//set the next banner
					if (i<(cfg.rbLength-1))	{
						cfg.rbCurr = i+2;
					}
					else cfg.rbCurr = 1;
					//start the rotation again						
					clearInterval(t);							
					t=setInterval(function (){								
						theloop(cfg.rbCurr);
					}, cfg.rbSpeed);
					
				});
		});


		//start loop of rotation
		t=setInterval(function (){ 
			theloop(cfg.rbCurr);
		}, cfg.rbSpeed);

		//the loop
		function theloop(loopTarget) {						
			$("#mainBanners").find(">ul>li").each(function(i){
				if (i!=(loopTarget-1)) {
					$(this).fadeTo("slow",0);						
				}
				else {
					$(this).fadeTo("slow",1);
				}						 
			});
			//set current to new value
			if (loopTarget < cfg.rbLength) {
				cfg.rbCurr = loopTarget+1;
			}
			else {
				//reset current
				cfg.rbCurr = 1;
			}
			//console.log("Boom");
		}

						
	}
