// ----------------------------------
//
// SlideShow.js
//
// Created by Martin Andres
// Requires jquery.js
//
// ----------------------------------

var SlideShow = new Object();

SlideShow = function(options) {
	this.debugWin = '';

	this.debug		= false;		if(options.debug)		this.debug		= options.debug;
	this.width		= "";			if(options.width)		this.width		= options.width;
	this.height		= "";			if(options.height)		this.height		= options.height;
	this.effect		= "fade";		if(options.effect)		this.effect		= options.effect;
	this.duration	= 4;			if(options.duration)	this.duration	= options.duration;
	this.label		= "slideshow";	if(options.label)		this.label		= options.label;

	this.data = new Array();

	this.maxNum = 0;
	this.curNum = -1;
	this.intObj = 0;
	this.intDoneObj = 0;
	this.hasText = false;
	this.inEffect = false;
	this.effectDuration = 2;
	if (this.duration > 0 && this.duration < this.effectDuration ) this.effectDuration = this.duration - 1;

	this.tog = "a";
	this.layA = this.label + "_laya";
	this.layB = this.label + "_layb";
	this.imgA = "";
	this.imgB = "";
	this.txtA = "";
	this.txtB = "";
	this.divA = "";
	this.divB = "";

	if (this.effect!='') this.tog = 'b';

	this.debugDraw("width=["+this.width+"]");
	this.debugDraw("height=["+this.height+"]");
	this.debugDraw("effect=["+this.effect+"]");
	this.debugDraw("duration=["+this.duration+"]");
	this.debugDraw("label=["+this.label+"]");
	this.debugDraw("debug=["+this.debug+"]");

	var html = "";
	html+="<div id='"+this.label+"' style='overflow:hidden;width:"+this.width+"px;height:"+this.height+"px;'>";
	html+="<div class='loading'><span>Loading...</span></div>";
	html+="</div>";

	document.write(html);
	this.debugDraw("html=["+html+"]");

}

SlideShow.prototype = {
	
	load: function() {
		this.debugDraw("load()");
		
		i = this.data.length;
		this.data[i] = new Object();
		this.data[i].src = arguments[0];
		if (arguments.length>1) {
			html = arguments[1];
			// parse out quotes
			p = html.indexOf("'"); if (p==0) p = html.indexOf('"');
			if (p>0) {
				html = html.substr(p+1,999);
				p = html.indexOf("'"); if (p==0) p = html.indexOf('"');
				html = html.substr(0,p);
			}
			this.data[i].text = html;
		} else {
			this.data[i].text = "";
		}
		if (this.data[i].text != "") this.hasText = true;
		
		jQuery("<img>").attr("src", this.data[i].src);

		//this.MM_preloadImages(this.data[i].src);

		this.debugDraw("data["+i+"].src=["+this.data[i].src+"] .text=["+this.data[i].text+"]");
	},

	transition: function(dir) {
		this.debugDraw("------------");
		this.debugDraw("transition("+dir+")");
		clearTimeout(this.intDoneObj);

		var thisObj = this;

		if (dir==1) {
			this.curNum++;
		} else {
			this.curNum--;
		}
		this.debugDraw("curNum=["+this.curNum+"] tog=["+this.tog+"] effect=["+this.effect+"]");

		if (this.curNum > this.maxNum) this.curNum = 0;
		if (this.curNum < 0) this.curNum = this.maxNum;

		if (this.tog == "a") {
			this.tog = "b";
			$('#'+this.label+'_imgb').attr('src',this.data[this.curNum].src);
			$('#'+this.label+'_txtb').html(this.data[this.curNum].text);
			lay1 = this.layA;
			lay2 = this.layB;
		} else {
			this.tog = "a";
			$('#'+this.label+'_imga').attr('src',this.data[this.curNum].src);
			$('#'+this.label+'_txta').html(this.data[this.curNum].text);
			lay1 = this.layB;
			lay2 = this.layA;
		}
	

		if (this.effect=="") {
			if (this.tog == "a") {
				$('#'+this.label+'_laya').css("display","block");
				$('#'+this.label+'_layb').css("display","none");
			} else {
				$('#'+this.label+'_laya').css("display","none");
				$('#'+this.label+'_layb').css("display","block");
			}
		}
		if (this.effect=="fade") {
			this.inEffect = true;
			if (this.tog == "a") {
				$('#'+this.label+'_laya').fadeIn("slow",function(){thisObj.transitionDone()});
				$('#'+this.label+'_layb').fadeOut("slow");
			} else {
				$('#'+this.label+'_laya').fadeOut("slow");
				$('#'+this.label+'_layb').fadeIn("slow",function(){thisObj.transitionDone()});
			}
		}
		if (this.effect=="slide") {
			this.inEffect = true;
			if (this.tog == "a") {
				//$('#'+this.label+'_laya').slideUp("slow");
				$('#'+this.label+'_layb').slideDown("slow");
			} else {
				//$('#'+this.label+'_laya').slideDown("slow");
				$('#'+this.label+'_layb').slideDown("slow");
			}
		}
		if (this.effect=="zoom") {
			this.inEffect = true;
			Effect.Grow(lay1, { duration: this.effectDuration });
			Effect.Shrink(lay2, { duration: this.effectDuration });
		}


		if (this.duration > 0) this.intObj = window.setTimeout(function() { thisObj.transition(1); }, 1000 * this.duration, this);
//		if (this.effect!="") this.intDoneObj = window.setTimeout(function() { thisObj.transitionDone(); }, 1000 * this.effectDuration, this);

//		var timeoutFunc1 = function() { thisObj.transition(1); };
//		var timeoutFunc2 = function() { thisObj.transitionDone(); };
//		if (this.duration > 0) this.intObj = window.setTimeout(timeoutFunc1, 1000 * this.duration, this);
//		if (this.effect!="") this.intDoneObj = window.setTimeout(timeoutFunc2, 1000 * this.effectDuration, this);

//		if (this.duration > 0) this.intObj = window.setTimeout(function(thisObj) { thisObj.transition(1); }, 1000 * this.duration, this);
//		if (this.effect!="") this.intDoneObj = window.setTimeout(function(thisObj) { thisObj.transitionDone(); }, 1000 * this.effectDuration, this);

//		if (this.duration > 0) this.intObj = window.setTimeout("alert('a');SlideShow.transition(1)", 1000 * this.duration, this);
//		if (this.effect!="") this.intDoneObj = window.setTimeout("alert('b');SlideShow.transitionDone()", 1000 * this.effectDuration, this);
	},

	transitionDone: function() {
		this.debugDraw("transitionDone()");
		this.inEffect = false;
	},

	move: function(dir) {
		this.debugDraw("move("+dir+")");
		clearTimeout(this.intObj);
		clearTimeout(this.intDoneObj);

		if (this.inEffect == false) {
			this.transition(dir);
		}
	},

	start: function() {
		this.debugDraw("write()");
	
		if (this.data.length>0) {

			var html = "";
			html+="<div id='"+this.label+"_laya' style='position:absolute;width:"+this.width+"px;height:"+this.height+"px;display:none;'><img id='"+this.label+"_imga' src='"+this.data[1].src+"'><div id='"+this.label+"_txta' class='text'>"+this.data[1].text+"</div></div>";
			html+="<div id='"+this.label+"_layb' style='position:absolute;width:"+this.width+"px;height:"+this.height+"px;display:none;'><img id='"+this.label+"_imgb' src='"+this.data[1].src+"'><div id='"+this.label+"_txtb' class='text'>"+this.data[1].text+"</div></div>";

			$('#'+this.label).html(html);

			this.imgA = $('#'+this.label+'_imga');
			this.imgB = $('#'+this.label+'_imgb');
			this.txtA = $('#'+this.label+'_txta');
			this.txtB = $('#'+this.label+'_txtb');
			this.divA = $('#'+this.label+'_laya');
			this.divB = $('#'+this.label+'_layb');
/*	
			this.debugDraw("imgA.src=["+this.imgA.src+"]");
			this.debugDraw("layA=["+this.layA+"]");
			this.debugDraw("divA.style.display=["+this.divA.style.display+"]");
			this.debugDraw("imgB.src=["+this.imgB.src+"]");
			this.debugDraw("layB=["+this.layB+"]");
			this.debugDraw("divB.style.display=["+this.divB.style.display+"]");
*/	
			this.maxNum = this.data.length - 1;
			this.debugDraw("maxNum=["+this.maxNum+"]");
	
			if (this.data.length>1) {
				this.transition(1);
			}
		}
	},


	debugDraw: function(txt) {
		if (this.debug==true) {
			if (this.debugWin=='') {
				this.debugWin = window.open('','Debug','top=0,left=0,width=500,height=700,scrollbars=1');
				this.debugWin.document.write("<pre>");
			}
			txt = txt.replace(/</g,'&lt;').replace(/>/g,'&gt;')
			this.debugWin.document.write(txt+"\n");
		}
	}

}
