/*
    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(eImage, eText, data, freq) {
		this.currentIter = 0;
		this.lastIter = 0;
		this.maxIter = 0;
		this.slideShowElement = eImage;
		this.slideShowData = data;
		this.slideShowInit = 1;
		this.slideElements = Array();
		this.slideShowDelay = freq;
		this.articleLink = "";
		this.slideInfoZone = "";
		
		this.articleText = eText;
		
		eImage.style.display="block";

		this.articleLink = document.createElement('a');
		this.articleLink.className = 'global';
		eImage.appendChild(this.articleLink);
		this.articleLink.href = "";
		
		this.articleTextLink = document.createElement('a');
		this.articleTextLink.className = 'global';
		eText.appendChild(this.articleTextLink);
		this.articleTextLink.href = "";
		this.articleTextLink.textContent = "";
		
		
		this.articleImg = document.createElement('img');
		this.articleImg.className = "slideElement";
		this.articleLink.appendChild(this.articleImg);

		this.maxIter = data.length;
		
		this.loadingElement = document.createElement('div');
		this.loadingElement.className = 'loadingElement';
		this.articleLink.appendChild(this.loadingElement);
		
		this.showInfoSlideShow();
		
		this.doSlideShow();
	},
	destroySlideShow: function(eImage) {
		var myClassName = eImage.className;
		var newElement = document.createElement('div');
		newElement.className = myClassName;
		eImage.parentNode.replaceChild(newElement, eImage);
	},
	startSlideShow: function() {
		this.loadingElement.style.display = "none";
		this.lastIter = this.maxIter - 1;
		this.currentIter = 0;
		this.slideShowInit = 0;
		
		this.articleImg.src = this.slideShowData[this.currentIter][0];
		this.articleImg.alt = this.slideShowData[this.currentIter][3];
		
		this.articleTextLink.href = this.slideShowData[this.currentIter][1];
		this.articleTextLink.innerHTML = this.slideShowData[this.currentIter][3];
		
		this.articleLink.href = this.slideShowData[this.currentIter][1];
		
		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)();
	},
	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 {
			this.articleImg.src = this.slideShowData[this.currentIter][0];
			this.articleImg.alt = this.slideShowData[this.currentIter][3];
			
			this.articleTextLink.href = this.slideShowData[this.currentIter][1];
			this.articleTextLink.innerHTML = this.slideShowData[this.currentIter][3];
		
			this.articleLink.href = this.slideShowData[this.currentIter][1];
			
			setTimeout(this.nextSlideShow.bind(this),this.slideShowDelay);
		}
	},
	showInfoSlideShow: function() {
	},
	hideInfoSlideShow: function() {
	}
};

function initTimedSlideShow(eImage, eText, data, freq) {
	var slideshow = new timedSlideShow(eImage, eText, data, freq);
}

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}