/* */
/*
* a slide show object
*/
function SlideShow(ssID, stID, scID, sooID, sspID, sskID, stbID, playImgURL, pauseImgURL, showToolbar)
{
	this.isInitialized  = false;
	this.allLoaded      = false;

	this.initialStart   = true;
	this.curSlide       = 0; // array key.
	this.lastMoveDir    = 1; // slideshow direction

	this.playImgURL     = playImgURL;
	this.pauseImgURL    = pauseImgURL;

	this.toHandle       = null;

	this.globName       = 'Slide_'+ssID+'_Show';

	this.imgList        = new Array();
	this.plc            = new Array(); // Pre Load Cache

	this.ssID           = ssID;
	this.stID           = stID;
	this.scID           = scID;
	this.sooID          = sooID;
	this.sspID          = sspID;
	this.sskID          = sskID;

	this.stbID          = stbID;

	this.showToolbar    = (typeof showToolbar == 'boolean') ? showToolbar: false;
}
SlideShow.prototype.slShowList  = new Array();
SlideShow.prototype.add = function( arg )
{
	return this.imgList.push( arg );
}
SlideShow.prototype.init        = function(rotationStep)
{
	if (this.isInitialized) { return; }

	SlideShow.prototype.slShowList[this.globName] = this;

	this.imgEL      = document.getElementById( this.ssID  );
	this.titleEL    = document.getElementById( this.stID  );
	this.captionEL  = document.getElementById( this.scID  );

	this.onOffEL    = document.getElementById( this.sooID );
	this.skipPrvEL  = document.getElementById( this.sspID );
	this.skipNxtEL  = document.getElementById( this.sskID );

	this.toolbarEL  = document.getElementById( this.stbID );

	this.playImage  = newImage( this.playImgURL );
	this.pauseImage = newImage( this.pauseImgURL );

	this.rotationStep = (rotationStep && rotationStep > 300) ? rotationStep: 2900;

	var cnt  = this.imgList.length;
	var self = this;

	for (var i = 0; i < cnt; i++) {
		this.plc.push( newImage( this.imgList[i].imgURL ) );
	}

	// javascript closure. advanced - wooo.
	(function(){
		if (self.imgEL) {
			self.imgEL.onclick = function() {
				self.showCurLink();
			}

			if (self.onOffEL) self.onOffEL.onclick = function() {
				self.startShow();
			}
			
			if (self.skipPrvEL) {
				self.skipPrvEL.style.display = 'none';
				self.skipPrvEL.onclick = function() {
					self.moveSlide(-1);
				}
			}
			
			if (self.skipNxtEL) { 
				self.skipNxtEL.style.display = 'none';
				self.skipNxtEL.onclick = function() {
					self.moveSlide(1);
				}
			}
		}
	})();

	if (this.toolbarEL) {
		if (!this.showToolbar) {
			this.toolbarEL.style.visibility = 'hidden';
		}
		this.toolbarEL.style.display = 'block';
	}
	this.isInitialized = true;
}
SlideShow.prototype.startShow   = function()
{
	if (!this.imgEL) return;//no elements no show

	if (this.toHandle) {
		return;
	}

	if (this.onOffEL) {
		this.onOffEL.style.backgroundImage = "url(" +this.pauseImgURL + ")";
	}

	var self = this;
	(function(){
		if (self.skipPrvEL) {
			self.skipPrvEL.style.display = 'none';
			self.skipNxtEL.style.display = 'none';

			self.onOffEL.onclick = function() {
			}
			self.stopShow();
		}
	})();

	if (!this.initialStart) {
		this.moveSlide();
	} else {
		this.initialStart = false;
	}
	
	this.toHandle   = setInterval('SlideShow.prototype.slShowList["'+this.globName+'"].moveSlide()', this.rotationStep);
}
SlideShow.prototype.stopShow    = function()
{
	clearInterval( this.toHandle );
	this.toHandle   = false;
	this.onOffEL.style.backgroundImage= "url(" + this.playImgURL + ")";

	var self = this;
	(function() {
		if (self.skipPrvEL) self.skipPrvEL.style.display = 'inline';
		if (self.skipNxtEL) self.skipNxtEL.style.display = 'inline';

		if (self.onOffEL) self.onOffEL.onclick = function() {
			self.startShow();
		}
	})();
}
SlideShow.prototype.showCurLink = function()
{
	if (typeof this.curSlide != 'undefined' &&
		typeof this.imgList  == 'object'    &&
		this.imgList[ this.curSlide ]       &&
		this.imgList[ this.curSlide ].imgLink)
	{
		if (typeof this.imgLinkWin == 'undefined' || !this.imgLinkWin || (this.imgLinkWin && this.imgLinkWin.closed)) {
			this.imgLinkWin = window.open('', 'Win_'+this.ssID);
		}

		this.imgLinkWin.location = this.imgList[ this.curSlide ].imgLink;
		this.imgLinkWin.focus();
	}
}
SlideShow.prototype.moveSlide   = function( move )
{
	switch (move) {
		case 1:     // next
		case -1:    // previous
		break;
		default:
		move = this.lastMoveDir;
	}

	var nextKey = this.curSlide + move;
	var lastKey = this.imgList.length - 1;

	var beenRoundOnce = false;


	while (!this.imgList[nextKey] /* && !this.imgLoaded(nextKey) */)  {
		if (lastKey == 0) {
			return;
		}

		if (nextKey < 0) {
			nextKey = lastKey;
		} else if (nextKey > lastKey) {
			nextKey = 0;
		} else {
			nextKey += move;
		}

		if (nextKey == this.curSlide) {
			if (beenRoundOnce) 
				return;
			
			beenRoundOnce = true;
		}
	}
	this.lastMoveDir = move;
	this.curSlide    = nextKey;

	this.imgEL.style.backgroundImage              = "url(" + this.imgList[nextKey].imgURL + ")";
	this.titleEL.innerHTML      = this.imgList[nextKey].imgTitle;
	this.captionEL.innerHTML    = this.imgList[nextKey].imgCaption;

	this.imgEL.style.cursor = (this.imgList[nextKey].imgLink)
	? 'pointer'
	: 'default'
	;
}
SlideShow.prototype.imgLoaded   = function( key )
{
	if (this.allLoaded) {
		return true;
	}

	if (key) {
		if (this.plc[key] instanceof Image) {
			return this.plc[key].completed;
		}
		return false;
	}

	var cnt    = this.plc.length;
	var gotErr = false;
	for (var i = 0; i < cnt; i++) {
		if (!this.plc[i].completed) {
			gotErr = true;
			break;
		}
	}

	if (gotErr) {
		return false;
	} else {
		this.allLoaded = true;
		return true;
	}
}

if (typeof newImage === 'undefined') {
	function newImage(arg)
	{
		if (document.images) {
			rslt = new Image();
			rslt.style.backgroundImage = "url(" + arg + ")";
			return rslt;
		}
	}
}

/* add each arg to the end of the array */
if ( typeof Array.push == 'undefined' ) {
	Array.prototype.push = function()
	{
		for ( var i = 0; i < arguments.length; i++ )
		{
			this[this.length] = arguments[i];
		}
		return this.length;
	}
}

