/**
 *	Adapted from the Basic Slideshow App (http://www.icommunicate.co.uk/articles/all/simple_slide_show_for_prototype_scriptaculous_38/)
 *	
 *	Which was adapted from from Tom Doyle by Isotope Communications Ltd, Dec 2008
 *	http://www.tomdoyletalk.com/2008/10/28/simple-image-gallery-slideshow-with-scriptaculous-and-prototype/
 *	
 *
 */

var homepagePromo = new Class.create();

homepagePromo.prototype = {
	
	initialize : function (options){
		var slidesArray = [];
		$$("#homepage-promo .promo").each(function(s) { slidesArray.push(s.id) } );
		this.slides = slidesArray;
		this.iImageId = 0;
		if ( this.slides ) {
			this.numOfImages	= this.slides.length;
			if ( !this.numOfImages ) {
				alert('No slides?');
			}
		}
		
		this.play = new PeriodicalExecuter(this.goNext.bind(this), 8);
	},
	
	// The Fade Function
	swapImage: function (x,y) {
		$(this.slides[x]) && $(this.slides[x]).appear({ duration: 1 });
		$(this.slides[y]) && $(this.slides[y]).fade({ duration: 0 });
	},
	
	goToPromo: function (selected_promo_number) {
		this.play.stop();
		
		var imageShow, imageHide;

		imageShow = (selected_promo_number - 1)
		imageHide = this.iImageId;

		this.swapImage(imageShow,imageHide);
		this.iImageId = imageShow;
	  this.play = new PeriodicalExecuter(this.goNext.bind(this), 8);
	},
	
	goNext: function () {
		var imageShow, imageHide;

		imageShow = this.iImageId+1;
		imageHide = this.iImageId;

		if (imageShow == this.numOfImages) {
			this.swapImage(0,imageHide);
			this.iImageId = 0;
		} else {
			this.swapImage(imageShow,imageHide);
			this.iImageId++;
		}
	},
	
	goPrevious: function () {
		var imageShow, imageHide;

		imageShow = this.iImageId-1;
		imageHide = this.iImageId;

		if (this.iImageId == 0) {
			this.swapImage(this.numOfImages-1,imageHide);
			this.iImageId = this.numOfImages-1;				
		} else {
			this.swapImage(imageShow,imageHide);	
			this.iImageId--;
		}
	}
	
};
