
jQuery(function($) { // maybe change to doc ready?

// fading vars -------------------------------------------------

var ImageArray = []; // array to hold the src of the backgroun img

var ImageArragEnd = ImageArray.length;

var BannerLable = 0; // use to lable everything

var counter = 0; // counts were the banner is upto

var BGcounter = 0; // counts were the banner background src is upto

var LGcounter = 1; // counts were the lights are up to...

var timer; // timer val

// -- start functions ------------------------------------------     

//fading labler
$("#fader").children().each(function(){ // lables everything and add to array so can be use for fading. 
	var BannerSRC = $(this).children().attr("src"); 
	BannerLable ++; // jsut a counter for the class adder
	$(this).addClass("image"+ BannerLable +""); // adds class like "image1", "image2"
	ImageArray.push(BannerSRC) // push then 
	ImageArragEnd = ImageArray.length; // updating the array
	
});
   

// test to see if there is 1 or more images ---------------------
if(ImageArragEnd == 1){ 
	//do nothing!
	}else{
    
    timer = setTimeout(eval("fadingFun"),"5000");  // rrestart the functions
	
	 $(".light").animate({opacity: '0.4'}, 0), // restarter 
     $(".light1").animate({opacity: '1'}, 1000); //.. shows box one the hides it so i can be faded in! *failsafe

}


// fading FUNction -----------------------------------------------
function fadingFun(){ 

	 //UPDAT PUT BOTH INTO A FUCNTION as a call back of each....
	 checker();
	 counter ++;
	 BGcounter ++;
	 LGcounter ++;
 
	 
	 $("#fader").css("background-image","url("+ ImageArray[BGcounter] +")");	// get this working
	 $("#fader").children().fadeOut(0), // restarter 
     $(".image"+ counter +"").fadeIn(0).fadeOut(2000), //.. shows box one the hides it so i can be faded in! *failsafe
	 $(".light").animate({opacity: '0.4'}, 300), // restarter 
     $(".light"+ LGcounter +"").animate({opacity: '1'}, 1000); //.. shows box one the hides it so i can be faded in! *failsafe
    timer = setTimeout(eval("fadingFun"),"5000");  // rrestart the functions
    
}

// click on light
$(".light").click(function(){
	// build in a return false switch!!!
	$(".light").stop();
    clearTimeout(timer);
	var altnumb = $(this).attr("alt"); 
	checker(); // CHECKS IF VARS ARE PAST THE ARRAY LENTGHT
	counter = [BGcounter + 1];
	BGcounter = [altnumb - 1 ] 


	 $("#fader").children().fadeOut(0); // restarter 
     $(".image"+ counter +"").fadeIn(0).fadeOut(2000); //.. shows box one the hides it so i can be faded in! *failsafe
	 $("#fader").css("background-image","url("+ ImageArray[BGcounter] +")");	// get this working
	 $(".light").animate({opacity: '0.4'}, 300), // restarter 
     $(this).animate({opacity: '1'}, 1000); //.. shows box one the hides it so i can be faded in! *failsafe
	
	
	BGcounter = [altnumb -1];
	LGcounter = altnumb;

	counter = BGcounter 
    timer = setTimeout(eval("fadingFun"),"5000");  // rrestart the functions
	
});



// function to correct any overflow in var




function checker(){

	if(counter >= [ImageArragEnd] ){  // check were its upto in the line and if its the last one then *restart*
		counter = 0;
	 }
	 if(BGcounter >= [ImageArragEnd - 1]){  // check were its upto in the line and if its the last one then *restart*
		BGcounter = -1;
	 }
	 if(LGcounter >= [ImageArragEnd ]	 ){  // check were its upto in the line and if its the last one then *restart*
		LGcounter = 0;
	 }

}	 




$("#switch_light").css("cursor","e-resize");
/// switcch 
var switcher = "on";
$("#switch_light").attr("src", "/content/images/sidebar/light_red.png"); 


$("#switch").click(function(){

if(switcher == "on"){ 

$("#switch_light").attr("src", "/content/images/sidebar/on_light.png"); 
$("#switch_light").css("cursor","w-resize");
	 switcher = "off";
     $("#switch_light").animate({opacity: '0.4'}, 1000), //.. shows box one the hides it so i can be faded in! *failsafe
	 $("#posters").fadeOut(300, function(){
	 
	  	 $("#events_list").fadeIn(300); // restarter 
	 }); // restarter 
	 $(this).removeClass("on");
	 $(this).addClass("off");

} else if(switcher == "off"){ 

$("#switch_light").attr("src", "/content/images/sidebar/light_red.png"); 
$("#switch_light").css("cursor","e-resize");

	 switcher = "on";
     $("#switch_light").animate({opacity: '1'}, 1000), //.. shows box one the hides it so i can be faded in! *failsafe
	 $("#events_list").fadeOut(300, function(){
	 
	  	 $("#posters").fadeIn(300); // restarter 
	 }); // restarter 
	 $(this).removeClass("off");
	 $(this).addClass("on");

}

});






});
