/* */
/*
 * 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()
{
    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 );

    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(){
        self.onOffEL.onclick = function() {
            self.startShow();
        }
        self.skipPrvEL.style.display = 'none';
        self.skipPrvEL.onclick = function() {
            self.moveSlide(-1);
        }
        self.skipNxtEL.style.display = 'none';
        self.skipNxtEL.onclick = function() {
            self.moveSlide(1);
        }
    })();

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

    this.onOffEL.src= this.pauseImgURL;

    var self = this;
    (function(){
        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()', 2900);
}
SlideShow.prototype.stopShow    = function()
{
    clearInterval( this.toHandle );
    this.toHandle   = false;
    this.onOffEL.src= this.playImgURL;

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

        self.onOffEL.onclick = function() {
            self.startShow();
        }
    })();
}
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;

    //alert('this.curSlide = '+this.curSlide);
    //alert('nextKey = '+nextKey);
    //alert('lastKey = '+lastKey);

    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) {
                alert('bad: '+this.imagesPLC.length);
                return;
            }
            beenRoundOnce = true;
        }
    }

    //alert(nextKey);

    this.lastMoveDir = move;
    this.curSlide    = nextKey;

//    for (i in this.imgList[nextKey]) {
//        alert( 'nextKey' + i + ':' + this.imgList[nextKey][i] );
//    }

    this.imgEL.src              = this.imgList[nextKey].imgURL;
    this.titleEL.innerHTML      = this.imgList[nextKey].imgTitle;
    this.captionEL.innerHTML    = this.imgList[nextKey].imgCaption;
}
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.src = arg;
            return rslt;
        }
    }
}

if (typeof joinFunctions === 'undefined') {
	function joinFunctions()
	{
		var newfunc = '';
		for (var c = 0;c < arguments.length;c++) {
			if ( arguments[c] ) {
				var func=arguments[c].toString();
				newfunc+=func.substr(func.indexOf("{")-1);
			}
		}
		return new Function(newfunc);
	}
}

/* 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;
    }
}


