var showcaseSlideShow = Class.create();

showcaseSlideShow.prototype = {
	initialize: function(element, data) {
		this.currentIter = 0;
		this.lastIter = 0;
		this.maxIter = 0;
		this.slideShowElement = element;
		this.slideShowData = data;
		this.slideShowInit = 1;
		this.slideElements = Array();

		element.style.display="block";

		this.maxIter = data.length;
		for(i=0;i<data.length;i++)
		{
			var currentImg = document.createElement('div');
			currentImg.className = "slideElement";
			currentImg.style.position="absolute";
			currentImg.style.left="0px";
			currentImg.style.top="0px";
			currentImg.style.margin="0px";
			currentImg.style.border="0px";
			currentImg.style.backgroundImage="url('" + data[i][0] + "')";
			currentImg.style.backgroundPosition="center center";

			element.appendChild(currentImg);
			currentImg.currentOpacity = new fx.Opacity(currentImg, {duration: 800});
			currentImg.currentOpacity.set(0);
			this.slideElements[i] = currentImg;
		}
		currentImg.currentOpacity = new fx.Opacity(currentImg, {duration: 700});
		currentImg.currentOpacity.set(0);
		
		this.loadingElement = document.createElement('div');
		this.loadingElement.className = 'loadingElement';
		element.appendChild(this.loadingElement);
		this.doSlideShow(1);
	},
	destroySlideShow: function(element) {
		var myClassName = element.className;
		var newElement = document.createElement('div');
		newElement.className = myClassName;
		element.parentNode.replaceChild(newElement, element);
	},
	pushNextSlideShow: function() {
		setTimeout(this.nextSlideShow.bind(this),800);
	},
	pushPrevSlideShow: function() {
		setTimeout(this.prevSlideShow.bind(this),800);
	},
	startSlideShow: function() {
		this.loadingElement.style.display = "none";
		this.lastIter = this.maxIter - 1;
		this.currentIter = 0;
		this.slideShowInit = 0;
		this.slideElements[this.currentIter].currentOpacity.set(1);
	},
	nextSlideShow: function() {
		this.lastIter = this.currentIter;
		this.currentIter++;
		if (this.currentIter >= this.maxIter)
		{
			this.currentIter = 0;
			this.lastIter = this.maxIter - 1;
		}
		this.slideShowInit = 0;
		this.doSlideShow.bind(this)(1);
	},
	prevSlideShow: function() {
		this.lastIter = this.currentIter;
		this.currentIter--;
		if (this.currentIter <= -1)
		{
			this.currentIter = this.maxIter - 1;
			this.lastIter = 0;
		}
		this.slideShowInit = 0;
		this.doSlideShow.bind(this)(2);
	},
	doSlideShow: function(position) {
		if (this.slideShowInit == 1)
		{
			imgPreloader = new Image();
			imgPreloader.onload=function(){
				setTimeout(this.startSlideShow.bind(this),10);
			}.bind(this);
			imgPreloader.src = this.slideShowData[0][0];
		} else {
			if (position == 1)
			{
				if (this.currentIter != 0) {
					this.slideElements[this.currentIter].currentOpacity.options.onComplete = function() {
						this.slideElements[this.lastIter].currentOpacity.set(0);
					}.bind(this);
					this.slideElements[this.currentIter].currentOpacity.custom(0, 1);
				} else {
					this.slideElements[this.currentIter].currentOpacity.set(1);
					this.slideElements[this.lastIter].currentOpacity.custom(1, 0);
				}
			} else {
				if (this.currentIter != this.maxIter - 1) {
					this.slideElements[this.currentIter].currentOpacity.set(1);
					this.slideElements[this.lastIter].currentOpacity.custom(1, 0);
				} else {
					this.slideElements[this.currentIter].currentOpacity.options.onComplete = function() {
						this.slideElements[this.lastIter].currentOpacity.set(0);
					}.bind(this);
					this.slideElements[this.currentIter].currentOpacity.custom(0, 1);
				}
			}
		}
	},
	goTo: function(num) {
		if((num-1) == this.currentIter) return;
		this.lastIter = this.currentIter;
		this.currentIter = num-1;
		//if (num == 0) this.lastIter = this.maxIter-1;
		//else this.lastIter = num-1;
		this.slideShowInit = 0;
		for(i=0;i<this.maxIter;i++)
		{
			this.slideElements[i].currentOpacity.set(0);
		}
		this.doSlideShow.bind(this)();
	}
};

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}