var slideShow = Class.create({

	initialize: function(showNumber,imageArray,duration) {
		// Preload Images
		this.preloadedImages = imageArray.each(function(image, index) {
			index = new Image();
			index.src = image.full_path;
		});

		this.count = 1;
		this.imageArray = imageArray.toArray();
		this.showNumber = showNumber;
		this.duration = duration;

		this.imageHolder1 = $('slide' + showNumber + 'a');
		this.imageHolder2 = $('slide' + showNumber + 'b');
		this.showNumber = showNumber;
		this.lastImage = imageArray[imageArray.length -1];
    this.firstImage = imageArray[0];
		this.imageHolder1.setStyle('background-image: url(' + this.firstImage.full_path + ')');
		//$('slideshowCaptionText'+this.showNumber).update(this.lastImage.name);

		this.imageHolder1.show();
		this.imageHolder2.hide();

		this.imageHolder1.setStyle('zindex: 6');
		this.imageHolder2.setStyle('zindex: 5');

		new PeriodicalExecuter(this.cycle.bind(this),duration);
	},
	cycle: function() {

		if(!this.count) {
			this.count = 1;
		}

		if(!this.imageHolder1.visible())
		{
			this.hiddenHolder = this.imageHolder1;
			this.visibleHolder = this.imageHolder2;
		} else {
			this.hiddenHolder = this.imageHolder2;
			this.visibleHolder = this.imageHolder1;
		}

		imageIndex = this.count -1;

		//currentImage = this.preloadedImages[imageIndex].full_path;
		this.hiddenHolder.setStyle('background-image: url(' + this.imageArray[imageIndex].full_path + ')');
		//$('slideshowCaptionText'+this.showNumber).update(this.imageArray[imageIndex].name);
		//$('slideshowCaptionText'+this.showNumber).setStyle('z-index: 7');
		this.visibleHolder.setStyle('z-index: 6');
		this.hiddenHolder.setStyle('z-index: 5');
		this.hiddenHolder.show();
		this.visibleHolder.setOpacity(1);
		new Effect.Fade(this.visibleHolder,{duration: this.duration - 1, afterFinish: this.afterFade.bind(this)});

		if(this.count == this.imageArray.length)
		{
			this.count = 1;
		} else {
			this.count = this.count + 1;
		}

	},

	afterFade: function(){
		this.visibleHolder.hide();

	}
});



