/*
    This file is part of JonDesign's SmoothSlideshow v2.0.

    JonDesign's SmoothSlideshow is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    JonDesign's SmoothSlideshow is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Foobar; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

    Main Developer: Jonathan Schemoul (JonDesign: http://www.jondesign.net/)
    Contributed code by:
    - Christian Ehret (bugfix)
    - Simon Willison (addLoadEvent)
*/

// declaring the class
var timedSlideShow = Class.create();

// implementing the class
timedSlideShow.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();
		this.slideShowDelay = 4000;
		this.articleLink = "";
		this.slideInfoZone = "";
		this.selectedIter = 0;
		
		element.style.display="block";

		this.articleLink = document.createElement('div');
		this.articleLink.className = 'global';
		element.appendChild(this.articleLink);
		//this.articleLink.href = "";

		this.maxIter = data.length;
		for(i=0;i<data.length;i++)
		{
			var currentImg = document.createElement('a');
			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";
			currentImg.href=data[i][1];

			this.articleLink.appendChild(currentImg);
			currentImg.currentOpacity = new fx.Opacity(currentImg, {duration: 400});
			currentImg.setStyle('opacity',0);
			this.slideElements[parseInt(i)] = currentImg;
		}
		
		this.loadingElement = document.createElement('div');
		this.loadingElement.className = 'loadingElement';
		this.articleLink.appendChild(this.loadingElement);

		this.slideInfoZoneBG = document.createElement('div');
		this.slideInfoZoneBG.className = 'slideInfoZoneBG';
		this.slideInfoZoneBG.style.opacity = 0.7;
		this.slideInfoZoneBG.style.backgroundColor = '#' + data[0][3];
		this.articleLink.appendChild(this.slideInfoZoneBG);

		this.slideInfoZoneACC = document.createElement('div');
		this.slideInfoZoneACC.className = 'slideInfoZoneACC';
		this.slideInfoZoneACC.style.opacity = 0.7;
		this.articleLink.appendChild(this.slideInfoZoneACC);
		
		this.slideInfoZone = document.createElement('div');
		this.slideInfoZone.className = 'slideInfoZone';
		this.articleLink.appendChild(this.slideInfoZone);

		this.doSlideShow();
	},
	destroySlideShow: function(element) {
		var myClassName = element.className;
		var newElement = document.createElement('div');
		newElement.className = myClassName;
		element.parentNode.replaceChild(newElement, element);
	},
	startSlideShow: function() {
		this.loadingElement.style.display = "none";
		this.lastIter = this.maxIter - 1;
		this.currentIter = 0;
		this.slideShowInit = 0;
		this.slideElements[parseInt(this.currentIter)].setStyle('opacity', 1);
		setTimeout(this.showInfoSlideShow.bind(this),10);
		this.time1=setTimeout(this.switchInfoSlideShow.bind(this),this.slideShowDelay);
		this.time2=setTimeout(this.nextSlideShow.bind(this),this.slideShowDelay);
	},
	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)();
	},
	gotoSelectedSlide: function() {
		var ss = this.slideshow;
		ss.lastIter = ss.currentIter;
		var si = this.selectedIter;
		if (si==ss.lastIter+1) return;
		var marginChange = new fx.Style(ss.slideInfoZoneACC, 'margin-top', {duration:300, onComplete:function(){
			ss.currentIter=si-1;
		}});
		var colorChange = new fx.Color(ss.slideInfoZoneBG, 'background-color', {duration:500});
		colorChange.toColor(ss.slideShowData[si-1][3]);
		
		marginChange.custom(ss.currentIter*26, (this.selectedIter-1)*26);
		ss.currentIter=si-1;
		ss.slideShowInit = 0;
		if (ss.currentIter != 0) {
			ss.slideElements[parseInt(ss.currentIter)].currentOpacity.options.onComplete = function() {
				ss.slideElements[parseInt(ss.lastIter)].setStyle('opacity',0);
			}.bind(ss);
			ss.slideElements[parseInt(ss.currentIter)].currentOpacity.custom(0, 1);
		} else {
			ss.slideElements[parseInt(ss.currentIter)].setStyle('opacity',1);
			ss.slideElements[parseInt(ss.lastIter)].currentOpacity.custom(1, 0);
		}
		clearTimeout(ss.time1);
		clearTimeout(ss.time2);
		ss.time1=setTimeout(ss.switchInfoSlideShow.bind(ss),ss.slideShowDelay);
		ss.time2=setTimeout(ss.nextSlideShow.bind(ss),ss.slideShowDelay);
	},
	doSlideShow: function() {
		if (this.slideShowInit == 1)
		{
			imgPreloader = new Image();
			// once image is preloaded, start slideshow
			imgPreloader.onload=function(){
				setTimeout(this.startSlideShow.bind(this),10);
			}.bind(this);
			imgPreloader.src = this.slideShowData[0][0];
		} else {
			if (this.currentIter != 0) {
				this.slideElements[parseInt(this.currentIter)].currentOpacity.options.onComplete = function() {
					this.slideElements[parseInt(this.lastIter)].setStyle('opacity',0);
				}.bind(this);
				this.slideElements[parseInt(this.currentIter)].currentOpacity.custom(0, 1);
			} else {
				this.slideElements[parseInt(this.currentIter)].setStyle('opacity',1);
				this.slideElements[parseInt(this.lastIter)].currentOpacity.custom(1, 0);
			}
			//setTimeout(this.showInfoSlideShow.bind(this),1000);
			this.time1=setTimeout(this.switchInfoSlideShow.bind(this),this.slideShowDelay);
			this.time2=setTimeout(this.nextSlideShow.bind(this),this.slideShowDelay);
		}
	},
	showInfoSlideShow: function() {
		this.articleLink.removeChild(this.slideInfoZone);
		this.slideInfoZone = document.createElement('div');
		this.slideInfoZoneBG.styles = new fx.Styles(this.slideInfoZoneBG);
		this.slideInfoZoneBG.setStyle('opacity',0.7);
		this.slideInfoZoneACC.styles = new fx.Styles(this.slideInfoZoneACC);
		this.slideInfoZoneACC.setStyle('opacity',0.7);
		this.slideInfoZoneTitle=new Array();
		this.slideInfoZoneLink=new Array();
		this.slideInfoZoneNumber=new Array();
		for(var i=0;i<this.slideShowData.length;i++)
		{
			this.slideInfoZoneTitle[i] = document.createElement('h2');
			this.slideInfoZoneNumber[i] = document.createElement('div');
			this.slideInfoZoneNumber[i].className= 'number';
			this.slideInfoZoneLink[i] = document.createElement('div');
			this.slideInfoZoneLink[i].className= 'slideBlock';
			this.slideInfoZoneLink[i].slideshow= this;
			this.slideInfoZoneLink[i].selectedIter= i+1;
			this.slideInfoZoneLink[i].onclick = this.gotoSelectedSlide;
			this.slideInfoZoneNumber[i].innerHTML=i+1;
			this.slideInfoZoneTitle[i].innerHTML=this.slideShowData[i][2];
			this.slideInfoZoneLink[i].appendChild(this.slideInfoZoneNumber[i]);
			this.slideInfoZoneLink[i].appendChild(this.slideInfoZoneTitle[i]);
			this.slideInfoZone.appendChild(this.slideInfoZoneLink[i]);
		}
		this.articleLink.appendChild(this.slideInfoZone);
		//this.articleLink.href = this.slideShowData[this.currentIter][1];
		this.slideInfoZone.className = 'slideInfoZone';
	},
	switchInfoSlideShow: function() {
		var marginChange = new fx.Style(this.slideInfoZoneACC, 'margin-top', {duration:300});
		if (this.currentIter+1==this.maxIter) var nexti=0;
		else var nexti=this.currentIter+1;
		marginChange.custom(this.currentIter*26, nexti*26);
		var colorChange = new fx.Color(this.slideInfoZoneBG, 'background-color', {duration:500});
		colorChange.toColor(this.slideShowData[nexti][3])
		//alert(this.slideShowData[nexti][3]);
		//this.slideInfoZone.styles.custom({'opacity': [0.7, 0]});
	}
};

function initTimedSlideShow(element, data) {
	var slideshow = new timedSlideShow(element, data);
}

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