// JavaScript Document   
// Copyright Scorchsoft 2010.  www.scorchsoft.co.uk
// Slideshow Fader v1.1

	var first = 1;
	var last = 3;
	//var delay = 8000;
	var delay = 1000;
	var jqueryFadeTime = 850;
	var current = 1;
	var normcolor = "#fff";
	var highcolor = "#f00";
	
	//this variable handles if a link has been pressed
	var changeSlide = -1;
	
	
	//show_slideshow();
	/**
	*	@first the first frame to be displayer
	*	@last the last frame to be displayed
	*	@delay the period in milliseconds to wait between each frame showing
	*	
	**/
	function show_slideshow(first, last, delay, normcolor, highcolor) {
		this.first = first;
		this.last = last;
		this.delay = delay;
		this.normcolor = normcolor;
		this.highcolor = highcolor;
	 	setTimeout(set_slideshow(), delay);
	 
	}
	// usage: show_slideshow(first frame, end frame, delay time);

	function set_slide(slideNum){
		
		changeSlide = slideNum;
		var i = first;		
		//hides all other slides, only fades the visible one
		while( i <=last){
		
		 	$('#slideshow' + i).hide()
			
			if(i != changeSlide){
				//unhilights all of the links
				$('#slidelink_' + i).css("background-color",normcolor);
			}
			
			i++;
		}
		//changes the link that is highlighted to match the current frame
		$('#slidelink_' + changeSlide).css("background-color",highcolor);
		$('#slideshow' + changeSlide ).show();
		changeSlide = slideNum;
	}

	
	
	function fadeItIn(current){
		
		//this check prevents 2 divs being shown at once if a 
		//link is clicked whilst the slide is fading out.
		//this function is called from a settimeout so if a link is pressed when
		//the timeout has been initialised then it will fade in anyway.
		if(changeSlide == -1){ 
			
			$('#slideshow' + current ).fadeIn();
			$('#slidelink_' + current).css("background-color",highcolor);
		}
	}
	function set_slideshow() {
		 return (function() {
			 //alert('#slideshow' + f);
			 
			 
			 //only fades out if a link hasn't been pressed
			 if(changeSlide == -1){
				 
				 i = first;
				 while( i <=last){
					 
					 $('#slideshow' + i).fadeOut();
				 	 $('#slidelink_' + i).css("background-color",normcolor);
					 i++;
				 }
				 

				//so that the slideshow loops
				 if (current == last) { 
					current = first; 
				} else { 
						current = current + 1; 
				}
				 
				 
				 setTimeout("fadeItIn(" + current + ");", jqueryFadeTime);
				 //setTimeout("$('#slideshow" + current + "').fadeIn();", jqueryFadeTime);
				 //$('#slidelink_' + current).css("background-color","#a31a1a");
				 
			 }else{
				
				//current needs to be updated if a link is pressed
				 current = changeSlide;
				 changeSlide = -1;
			 }
			
			 setTimeout(set_slideshow(), delay + jqueryFadeTime);
			 
		 })
	}
	
