/**
 * @author bdgeorge
 * Content Slide Show with external controls
 * Note: all 'Slides' must start with a class 'Slide_x' where x is the number eg Slide_1, Slide_2 etc
 */
var wh_slideshow = {
	fadeOutTime:1000,
	fadeInTime:1000,
	leftSide:"leftSide",
	rightSide:"rightSide",
	selector:"#SlideShow div.slide",
	init: function(){
		//Find our Slideshow on the page and attach a counter to it to keep track of which Slide we are on, start on Slide 1
		mySlide = $("#SlideShow")
		mySlide.counter = 0;
		mySlide.nextSlide = 1;
		
		//Count how many Slides we have on the page and save that as max count
		//All Slides must start with a class name 'Slide_x' where x is the Slide number
		mySlide.maxcount = $(wh_slideshow.selector).length - 2;	
		mySlide.images = $(wh_slideshow.selector);
		//Initialise the counter text
		$('.counter').text("Image "+eval(mySlide.counter +1) +"/"+eval(mySlide.counter +2)+ " of " + eval(mySlide.maxcount + 2));	
		
		//Create a last Slide function, this will hide the current Slide and show the next one, if we are on the last Slide then go to the first
		$("#SlideShow a.slideleft").click(function(){
			wh_slideshow.lastSlide();
			
		});	
		$("#SlideShow a.slideright").click( function(){
			wh_slideshow.nextSlide();
		}); 
		//Add a click to the image as well
		$("#SlideShow .slide img").click( function(){
			wh_slideshow.nextSlide();
		}); 		
	},
	_advance:function(dir,step){
		//calculate the next Slide
		if (dir == 'back'){
		mySlide.counter <= 0 ? mySlide.nextSlide=mySlide.maxcount: mySlide.nextSlide = mySlide.counter - step;			
		} else {
		mySlide.counter >= mySlide.maxcount ? mySlide.nextSlide=0: mySlide.nextSlide = mySlide.counter + step;				
		}

		$(mySlide.images[mySlide.counter]).fadeOut(wh_slideshow.fadeOutTime,function(){
			$(this).removeClass(wh_slideshow.leftSide +" "+ wh_slideshow.rightSide);
		});	
		
		$(mySlide.images[mySlide.counter+1]).fadeOut(wh_slideshow.fadeOutTime,function(){
			$(this).removeClass(wh_slideshow.leftSide +" "+ wh_slideshow.rightSide);
		});				
			
		$(mySlide.images[mySlide.nextSlide]).addClass(wh_slideshow.leftSide).fadeIn(wh_slideshow.fadeInTime);	
		$(mySlide.images[mySlide.nextSlide + 1]).addClass(wh_slideshow.rightSide).fadeIn(wh_slideshow.fadeInTime);			
		mySlide.counter=mySlide.nextSlide;			
		$('.counter').text("Image "+eval(mySlide.counter +1) +"/"+eval(mySlide.counter +2)+ " of " + eval(mySlide.maxcount + 2));		
		return false;		
	},
	lastSlide: function(){
		wh_slideshow._advance('back',2);		
	},
	nextSlide: function(){
		wh_slideshow._advance('next',2);		
	}
}

$(document).ready(function(){		
wh_slideshow.init();
});