/*
    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 bulletSlideShow = Class.create();

// implementing the class
bulletSlideShow.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.slideSelectors = Array();
		this.slideShowDelay = 9000;
		this.articleLink = "";
		this.slideInfoZone = "";
		this.allow_auto_slideShow = true;

		element.style.display="block";
		this.maxIter = data.length;

		if (data.length>1)
		{
			this.slideNavZone = document.createElement('div');
			this.slideNavZone.className = 'slideNavZone';
			this.slideNavZone.innerHTML = '';
			//element.appendChild(this.slideNavZone);
		}

		
		for(i=0;i<data.length;i++)
		{
			this.articleLink = document.createElement('a');
			this.articleLink.className = 'global';
			this.articleLink.href = data[i][1];
			
			element.appendChild(this.articleLink);

			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="top center";

			this.articleLink.appendChild(currentImg);
			currentImg.currentOpacity = new fx.Opacity(currentImg, {duration: 400});
			currentImg.setStyle('opacity',0);
			this.slideElements[parseInt(i)] = currentImg;
			
			// Create selector div
			var slideSelector = document.createElement('a');
			//slideSelector.id = 'sli_'+i;
			slideSelector.className = 'emptycircle';
//			slideSelector.href = 'http://www.clubic.com';
			slideSelector.href = 'javascript:void(0);';
			slideSelector.style.display = 'block';
//			slideSelector.id=parseInt(i);
//			slideSelector.onclick =  this.pushSelectSlideShow.bind(this, 12);
			slideSelector.onclick =  this.pushSelectSlideShow.pass(parseInt(i), this);
/*
			fctname="action";
			var str = "function "+fctname+"() { slideshow.pushSelectSlideShow("+parseInt(i)+");}";
			eval(str);
			slideSelector.onclick = eval(fctname);
*/
			this.slideNavZone.appendChild(slideSelector);

			this.slideSelectors[parseInt(i)] = slideSelector;

		}
		if (data.length>1)
		{

		//Create Player
		this.playImg = document.createElement('a');
		this.playImg.className = 'play';
		this.playImg.href = "#";
		this.playImg.onclick = this.play.bind(this);

		this.stopImg = document.createElement('a');
		this.stopImg.className = 'stop';
		this.stopImg.href = "#";
		this.stopImg.onclick = this.stop.bind(this);
		
		this.playImg.style.display = this.allow_auto_slideShow ? 'none' : 'block';
		this.stopImg.style.display = this.allow_auto_slideShow ? 'block' : 'none';
//		this.playImg.onclick = function(){alert("toto play");}.bind(this);
//		this.stopImg.onclick = function(){alert("toto stop");}.bind(this);
		
		this.slideNavZone.appendChild(this.playImg);
		this.slideNavZone.appendChild(this.stopImg);
		
/*   		var leftArrow = document.createElement('a');
			leftArrow.className = 'left';
			leftArrow.onclick = this.pushPrevSlideShow.bind(this);
			element.appendChild(leftArrow);
			
			var rightArrow = document.createElement('a');
			rightArrow.className = 'right';
			rightArrow.onclick = this.pushNextSlideShow.bind(this);
			element.appendChild(rightArrow);
*/
		}

		
/*		this.slideInfoZone = document.createElement('div');
		this.slideInfoZone.className = 'slideInfoZone';
		this.articleLink.appendChild(this.slideInfoZone);
		this.slideInfoZone.style.opacity = 0;
*/
		if (data.length>1) {
			element.appendChild(this.slideNavZone);
		}

		this.slideInfoZone = document.createElement('div');
		this.slideInfoZone.className = 'slideInfoZone';
		element.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);
	},
	stop: function() {
		this.allow_auto_slideShow=false;
		this.playImg.style.display = this.allow_auto_slideShow ? 'none' : 'block';
		this.stopImg.style.display = this.allow_auto_slideShow ? 'block' : 'none';

		window.clearTimeout(this.timeoutID_showInfoSlideShow);
		window.clearTimeout(this.timeoutID_hideInfoSlideShow);
		window.clearTimeout(this.timeoutID_nextSlideShow);
	},
	play: function() {
		this.allow_auto_slideShow=true;
		this.playImg.style.display = this.allow_auto_slideShow ? 'none' : 'block';
		this.stopImg.style.display = this.allow_auto_slideShow ? 'block' : 'none';
		setTimeout(this.showInfoSlideShow.bind(this),1000);
		setTimeout(this.hideInfoSlideShow.bind(this),this.slideShowDelay-1000);
		setTimeout(this.nextSlideShow.bind(this),this.slideShowDelay);
	},
	startSlideShow: function() {
		this.lastIter = this.maxIter - 1;
		this.currentIter = 0;
		this.slideShowInit = 0;
		this.slideElements[parseInt(this.currentIter)].setStyle('opacity', 1);
		this.slideSelectors[parseInt(this.currentIter)].className = 'fullcircle';
		
		setTimeout(this.showInfoSlideShow.bind(this),1000);
		setTimeout(this.hideInfoSlideShow.bind(this),this.slideShowDelay-1000);
		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)(1);
	},
	pushSelectSlideShow: function(position) {
//alert(toto);	
//		var position=toto.target.id;
		this.stop();
		this.lastIter = this.currentIter;
		this.currentIter=position;

		this.slideShowInit = 0;
		this.doSlideShow.bind(this)(1);
	},
	selectSlideShow: function(position) {
		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);
	},	
	doSlideShow: function(position) {
			if (this.slideShowInit == 1)
			{
				setTimeout(this.startSlideShow.bind(this),10);
			} else {
				if (position == 1)
				{
					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);
					}
				} else {  /* position = -1 */
					if (this.currentIter != this.maxIter - 1) {
						this.slideElements[parseInt(this.currentIter)].setStyle('opacity',1);
						this.slideElements[parseInt(this.lastIter)].currentOpacity.custom(1, 0);
					} else {
						this.slideElements[parseInt(this.currentIter)].currentOpacity.options.onComplete = function() {
							this.slideElements[parseInt(this.lastIter)].setStyle('opacity',0);
						}
						this.slideElements[parseInt(this.currentIter)].currentOpacity.custom(0, 1);
					}
				}
				this.slideSelectors[parseInt(this.lastIter)].className = 'emptycircle';
				this.slideSelectors[parseInt(this.currentIter)].className = 'fullcircle';

			this.timeoutID_showInfoSlideShow = window.setTimeout(this.showInfoSlideShow.bind(this),1000);
			if(this.allow_auto_slideShow)
			{			this.timeoutID_hideInfoSlideShow = window.setTimeout(this.hideInfoSlideShow.bind(this),this.slideShowDelay-1000);		
						this.timeoutID_nextSlideShow = window.setTimeout(this.nextSlideShow.bind(this),this.slideShowDelay);
			}
	
		}
	},
	showInfoSlideShow: function() {
/*	this.articleLink.removeChild(this.slideInfoZone);
		//this.slideShowElement.removeChild(this.slideInfoZone);
		this.slideInfoZone = document.createElement('div');
		this.slideInfoZone.className = 'slideInfoZone';
		this.slideInfoZone.styles = new fx.Styles(this.slideInfoZone);
		this.slideInfoZone.setStyle('opacity',0);
		var slideInfoZoneDiv = document.createElement('div');
		slideInfoZoneDiv.className = 'InfoZone';

//		var slideInfoZoneTitle = document.createElement('h2');
//		slideInfoZoneTitle.innerHTML = this.slideShowData[this.currentIter][2]
//		slideInfoZoneDiv.appendChild(slideInfoZoneTitle);
		var slideInfoZoneDescription = document.createElement('p');
		slideInfoZoneDescription.innerHTML = this.slideShowData[this.currentIter][3];
		slideInfoZoneDiv.appendChild(slideInfoZoneDescription);
		this.slideInfoZone.appendChild(slideInfoZoneDiv);
		
		this.articleLink.appendChild(this.slideInfoZone);
		this.articleLink.href = this.slideShowData[this.currentIter][1];
		
		
		//this.slideShowElement.appendChild(this.slideInfoZone);
		this.slideInfoZone.styles.custom({'opacity': [0, 0.7], 'height': [0, this.slideInfoZone.getStyle('height', true)]});
	*/	
		//this.articleLink.href = this.slideShowData[this.currentIter][1];
		//this.articleLink.href = "www.clubic.com";
		this.slideInfoZone.innerHTML=this.slideShowData[this.currentIter][2];
	},
	hideInfoSlideShow: function() {
//		this.slideInfoZone.styles.custom({'opacity': [0.7, 0]});
		//this.slideInfoZone.styles.custom({'opacity': [0.7, 0], 'height': [this.slideInfoZone.getStyle('height', true), 0]});
		this.slideInfoZone.innerHTML='';
	},
	goTo: function(num) {
		this.currentIter = num;
		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.setOpacity(0);
		}
		this.doSlideShow.bind(this)();
	}
};

function initbulletSlideShow(element, data) {
	var slideshow = new bulletSlideShow(element, data);
}

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