 // JavaScript Slideshow Tool 1.0
 //
 // Copyright (c) 2007 Martin Knowles

function down(strId, strPrefix)
{
    document.getElementById(strPrefix + strId).src = 'buttons/tbar' + strPrefix + '-r_' + strId + '.gif';
}
function up(strId, strPrefix)
{
    document.getElementById(strPrefix + strId).src = 'buttons/tbar' + strPrefix + '_' + strId + '.gif';
}
    
function MKPMSlideshow()
{
    var strHTML;
    this.images = new Array();
    this.defaultDuration = 3000;
    this.running = false;
    this.imgId = 0;
    if (!document.getElementById || !document.createElement) return;
	this.show = document.getElementById('ss_show');
    this.show.style.overflow = 'hidden';
    this.show.style.position = 'relative';
    this.badgeTitle = document.getElementById('ss_badgeTitle');
    this.badgePos = document.getElementById('ss_badgePos');
    this.panels = new Array();
    this.panels[0] = document.createElement("img");
    this.panels[1] = document.createElement("img");    
    this.panels[0].style.display = this.panels[1].style.display = 'none';
    this.panels[0].style.position = this.panels[1].style.position = 'absolute';
    this.panels[0].style.top = this.panels[1].style.top = '0';
    this.panels[0].style.left = this.panels[1].style.left = '0';
    this._setOpacity(this.panels[0],1);
    this._setOpacity(this.panels[1],0);
    this.paused = false;
	this.wndSizeY = document.height;
    this.wndSizeX = document.width;
    this.livePanel = this.panels[0];
    this.nextPanel = this.panels[1];
    this.show.appendChild(this.panels[0]);
    this.show.appendChild(this.panels[1]);
    if (!document._mk_shows) document._mk_shows = new Array();
    this.ssid = document._mk_shows.length;
    document._mk_shows[document._mk_shows.length] = this;
}
    
MKPMSlideshow.prototype.addImage = function(strTitle, strURL, nDuration)
{
	this.images[this.images.length] = [strTitle, strURL, nDuration];
}

MKPMSlideshow.prototype.setSize = function(nWidth, nHeight)
{
    this.show.style.width = nWidth + 'px';
    this.show.style.height = nHeight + 'px';
}

MKPMSlideshow.prototype.showImage = function()
{
    this.nextPanel.src = this.images[this.imgId][1];
    this.nextPanel.style.display = 'block';
	if (this.paused)
	{
		document._mk_nextfade(this.ssid);
	}
	else
	{
		this.hTO = setTimeout("document._mk_nextfade("+this.ssid+")",this.images[this.imgId][2] ? this.images[imgId][2] : this.defaultDuration);
	}
}

MKPMSlideshow.prototype.pause = function()
{
    if (!this.paused)
    {
		this.paused = true;
		clearTimeout(this.hTO);
		delete this.hTO;
		if (this.hFTO && this.livePanel.panelOpacity == 1)
		{
			clearTimeout(this.hFTO);
			delete this.hFTO;
		}
	}
	else
	{
		this.paused = false;
		this.imgId = (this.imgId + 1) % this.images.length;
		this._fadeIn();	
	}
}

MKPMSlideshow.prototype.start = function()
{
    this.imgId = 0;
    this.nextPanel.src = this.images[this.imgId][1];
    this.nextPanel.style.display = 'block';
    //preload the next image before asking for it
    var preloadImg;
    preloadImg = new Image();
    preloadImg = this.images[this.imgId][1];
    this.hFTO = setTimeout("document._mk_nextfade("+this.ssid+")",50);
}

MKPMSlideshow.prototype.next = function(bPause)
{
    this.imgId = (this.imgId + 1) % this.images.length;
	if (bPause)
	{
		clearTimeout(this.hTO);
		delete this.hTO;	
		this.paused = true;
	}   
    this.showImage();
}

MKPMSlideshow.prototype.prev = function(bPause)
{
    this.imgId = (this.imgId - 1) % this.images.length;
	if (this.imgId < 0) this.imgId = this.images.length - 1;
    if (bPause)
	{
		clearTimeout(this.hTO);
		delete this.hTO;
		this.paused = true;
		
	}
	this.showImage();
}

MKPMSlideshow.prototype.where = function()
{
    return (this.images.length ? ((1 + this.imgId) + " of " + this.images.length) : "No images");
}

MKPMSlideshow.prototype._fadeIn = function()
{
    if (this.livePanel.panelOpacity <= 0)
    {
        this.livePanel = this.nextPanel;
        this.nextPanel = this.panels[(this.nextPanel == this.panels[0] ? 1 : 0)];
        if (this.badgeTitle) this.badgeTitle.innerHTML = this.images[this.imgId][0];
        if (this.badgePos) this.badgePos.innerHTML = this.where();
        this._setOpacity(this.nextPanel, 0);
        if (!this.paused) this.next();
        return;
    }
	this._setOpacity(this.livePanel, this.livePanel.panelOpacity - (this.paused ? .1 : .05));
	this._setOpacity(this.nextPanel, this.nextPanel.panelOpacity + (this.paused ? .1 : .05));
	this.hFTO = setTimeout("document._mk_nextfade("+this.ssid+")",(this.paused ? 10 : 30));
}
MKPMSlideshow.prototype._setOpacity = function(obj, nOp)
{
    var nRealOp = (nOp > .99 ? .99 : nOp);
    obj.panelOpacity = nOp;
    obj.style.opacity = nRealOp;
    obj.style.MozOpacity = nRealOp;
    obj.style.filter = 'alpha(opacity=' + (nRealOp*100) + ')';
}

document._mk_nextfade = function(ssid)
{
    document._mk_shows[ssid]._fadeIn();
}
