GalmartSlideshow = new Class({
	initialize: function(thumbnails, slideshow_image, props) {
		this.props = Object.extend({
			images: [],
			template: '',
			thumbWidth: 118,
			thumbHeight: 118
		}, props || {});

		if (this.props.images.length < 1) { return; }

		this.thumbnails = $(thumbnails);
		this.slideshow_image = $(slideshow_image);

		if (this.thumbnails && this.slideshow_image)
		{
			this.currentPosition = 0;
			this.maxZIndex = 0;
			this.currentIndex = 0;

			this.fx = new Fx.Styles(this.thumbnails, {duration: 500, wait: false, transition: Fx.Transitions["Cubic"]["easeOut"]});
			this.fxSlideshowImage = new Fx.Styles(this.slideshow_image, {duration: 1000, wait: false, transition: Fx.Transitions["Cubic"]["easeOut"]});
			this.pendingEffects = [];
			this.previousIndex = -1;
			this.busy = false;
			this.start();
		}
	},
	cleanQueue: function(){
			var galmartSlideshow = this;
			var testAgain = false;
			if (galmartSlideshow.pendingEffects.length>=2)
			{
				if (galmartSlideshow.pendingEffects[0][0]==galmartSlideshow.pendingEffects[1][0] && 
						galmartSlideshow.pendingEffects[0][2]+galmartSlideshow.pendingEffects[1][2]==1)
				{
					galmartSlideshow.pendingEffects.shift();
					galmartSlideshow.pendingEffects.shift();
					testAgain = true;
				}
			}
			if (galmartSlideshow.pendingEffects.length>2)
			{
				if (galmartSlideshow.pendingEffects[0][1]==1 && 
						galmartSlideshow.pendingEffects[1][1]==0)
				{
					galmartSlideshow.pendingEffects.shift();
					galmartSlideshow.pendingEffects.shift();
					testAgain = true;
				}
			}
			if (galmartSlideshow.pendingEffects.length>3)
			{
				if (galmartSlideshow.pendingEffects[1][1]==1 && 
						galmartSlideshow.pendingEffects[2][1]==0)
				{
					t = galmartSlideshow.pendingEffects.shift();
					galmartSlideshow.pendingEffects.shift();
					galmartSlideshow.pendingEffects.shift();
					galmartSlideshow.pendingEffects.unshift(t);
					testAgain = true;
				}
			}
			
			if (testAgain) {galmartSlideshow.cleanQueue();}
	},
	runQueue: function(force){
		if (!this.busy || force)
		{
			var galmartSlideshow = this;
			galmartSlideshow.cleanQueue();
			
			if (galmartSlideshow.pendingEffects.length>0)
			{
				galmartSlideshow.busy = true;
				t = galmartSlideshow.pendingEffects.shift();
				if (t[2]==1)
					t[0].setStyle('z-index', ++galmartSlideshow.maxZIndex);
				t[0].effect('opacity', {duration: t[1]}).start(t[2]).chain(function() {galmartSlideshow.runQueue(true)});
			}
			else
			{
				galmartSlideshow.busy = false;
			}
		}
	},
	start: function(){
		
		var images = [];
		var galmartSlideshow = this;
		
		for (j = 0; j < this.props.images.length; j++) {
			var img = new Element('img');
			

			galmartSlideshow.slideshow_image.setHTML(
				galmartSlideshow.slideshow_image.innerHTML + 
				galmartSlideshow.props.template.replace(
						'###TITLE###', 
						galmartSlideshow.props.images[j][2]).replace(
									'###TEXT###', 
									galmartSlideshow.props.images[j][3]).replace(
									'###IMAGE###', 
									'<img src="'+galmartSlideshow.props.images[j][1]+'"/>').replace(
												'###ID###', 
												'pic'+j));
			$('pic'+j).setStyles({
				'z-index': j,
				'opacity': 0
			});


			images[j] = this.props.images[j][1];
			
			img.setProperty('src', this.props.images[j][0]);
			img.setProperty('width', this.props.thumbWidth);
			img.setProperty('height', this.props.thumbHeight);
			img.setProperty('title', this.props.images[j][2]);
			img.addEvent('click',function(){

				for (k = 0; k < galmartSlideshow.props.images.length; k++) {
					if (galmartSlideshow.props.images[k][0]==this.getProperty('src'))
					{
						nextIndex = k;
						break;
					}
				}
				if (galmartSlideshow.currentIndex != nextIndex)
				{
					galmartSlideshow.previousIndex = galmartSlideshow.currentIndex;
					galmartSlideshow.currentIndex = nextIndex;
					var firstEffect = [$('pic'+galmartSlideshow.previousIndex), 1000, 0];
					var secondEffect = [$('pic'+nextIndex), 1000, 1];

					if (firstEffect.length>0)
						galmartSlideshow.pendingEffects.push(firstEffect);
					if (secondEffect.length>0 && galmartSlideshow.loadedImages[nextIndex]!=null)
						galmartSlideshow.pendingEffects.push(secondEffect);
					galmartSlideshow.runQueue(false);
				}
			});
			img.injectInside(this.thumbnails);
		}
		
		galmartSlideshow.loadedImages = [];
		
		new Asset.images(images, {
			onProgress: function(i) {
				for (k = 0; k < galmartSlideshow.props.images.length; k++) 
					if (galmartSlideshow.props.images[k][1]==this.getProperty('src'))
					{
						i = k;
						break;
					}


				if (galmartSlideshow.slideshow_image.offsetHeight<$('pic'+k).offsetHeight)
				{
					galmartSlideshow.slideshow_image.setStyle('height', $('pic'+k).offsetHeight);
				}

				galmartSlideshow.maxZIndex=(i>galmartSlideshow.maxZIndex)?i:galmartSlideshow.maxZIndex;
				if (galmartSlideshow.loadedImages[i]!=this)
				{
					galmartSlideshow.loadedImages[i] = this;

					if (this.getProperty('src')==images[galmartSlideshow.currentIndex] &&
							galmartSlideshow.previousIndex != galmartSlideshow.currentIndex)
					{
						galmartSlideshow.previousIndex = galmartSlideshow.currentIndex;
						galmartSlideshow.pendingEffects.push([$('pic'+k), 1000, 1]);
						galmartSlideshow.runQueue(false);
					}
				}
			},
			onComplete: function() {
			}
		});
	}
});


