// ===========================================================================================
// Copyright 2010
// by Asset Generation Elite Services, Inc.
// ===========================================================================================
function slideObj(title, image, lynk, text, attr)
{
	this.title = title;					// title above Image
	this.image = image;					// Image file
	this.lynk  = lynk;					// URL to go to when clicked
	this.text  = text;					// Text below Image
	this.attr  = attr;					// Attribution of Text
}

function ssObj(name, progression, num_cycles, delay_ms, opt, linkbase, imagebase)
{
	this.tid      = 0;
	this.elem_id  = null;
	this.slide    = new Array();		// Array of slideshow items
	this.history  = new Array();		// This holds a recent-history of the slides shown
	this.display  = 0;					// This is the history-index that has the currently-displayed slide num

	this.name      = 'ss_' + name;		// Name of the Spec
	this.prog      = progression;		// How to choose next slide: num = StepSize, "rnd", "prnd", 
	this.num       = num_cycles;		// How many cycles to execute before stopping: num or "continuous"
	this.running   = true;				// If False, Slide Show is paused
	this.delay     = delay_ms;			// How long to delay between "slides"
	this.faderate  = 1000;				// how long to experience the fade transition
	this.fadestep  = 10;				// num steps for full fade effect
	this.fadedir   = -1;				// negative means fade-out, positive means fade-in, zero means no fading at all
	this.opa       = 0;					// opacity right now
	this.speed     = (opt & 0x0100)? true:false;	// Buttons for Faster/Slower
	this.pause     = (opt & 0x0010)? true:false;	// Buttons for Pause
	this.ffrev     = (opt & 0x0001)? true:false;	// Buttons for Forward/Reverse
	this.linkBase  = linkbase;			// pre-pend to item Link-to URLs
	this.imageBase = imagebase;			// pre-pend to item Image-path URLs
	this.imagedir  = "";
}

ssObj.prototype.slideAdd = function(title, image, lynk, text, attr)
{
	this.slide.push(new slideObj(title, image, lynk, text, attr));
}

ssObj.prototype.timerCancel = function()
{
	if (this.tid != 0)
	{
		var tiddly = this.tid;
		this.tid = 0;
		clearInterval(tiddly);
		// TBD:  Set up pause-button to restart-timer
	}
}

ssObj.prototype.timerStart = function(delta)
{
	var thisObj = this;
	
	this.timerCancel();
	if (this.running)
		this.tid = setTimeout(function() { thisObj.step(); }, delta);
}

ssObj.prototype.runToggle = function()
{
	var el;
	if (this.running)		// Currently Running...so Pause
	{
		this.running = false;
		this.timerCancel();
		this.opa = 100;
		objOpacitySet(this.elem_id, this.opa);	// Re-assert complete Opacity
		if (this.pause)
		{	el = document.getElementById(this.name+'_togg');
			el.src = this.imagedir + 'ss_runn.gif';
			el.alt = "continue";
		}
	}
	else					// Already Paused....set back into motion
	{
		if (this.pause)
		{	el = document.getElementById(this.name+'_togg');
			el.src = this.imagedir + 'ss_pause.gif';
			el.alt = "pause";
		}
		this.running = true;
		this.step();
	}
}

ssObj.prototype.speedSlower = function()
{
	this.delay += 500;

	this.timerStart();
}

ssObj.prototype.speedFaster = function()
{
	this.delay -= 500;
	if (this.delay < 0)
		this.delay = 0;

	this.timerStart();
}

ssObj.prototype.historyFore = function()
{
	if (this.running)
		this.runToggle();

	this.chooseNext();
	this.displayCurrent();
}

ssObj.prototype.historyBack = function()
{
	if (this.running)
		this.runToggle();

	this.choosePrev();
	this.displayCurrent();
}

ssObj.prototype.choosePrev = function()
{
	this.display++;
	if (this.display >= this.history.length)
		this.display = this.history.length;
}

ssObj.prototype.chooseNext = function()
{
	if (this.display > 0)
	{
		// If we are looking into the history, then advance toward "now"
		this.display--;
	}
	else
	{
		var prev = 0;		// Default if no history yet
		var next;

		// Determine the Previous Slide Index
		if (this.history.length > 0)
			prev = this.history[0];
			
		// Follow spec to select next "now" slide index
		if ((this.prog == "rnd") || (this.prog == "prnd"))
		{
			var i;
			do
			{
				next = Math.floor(Math.random() * this.slide.length);
				if (this.prog == "rnd")
					break;

				// For "prnd" we must verify that we don't match anything in history
				for(i = 0 ; i < this.history.length ; i++)
				{
					if (next == this.history[i])
						break;
				}
			}
			while(i < this.history.length)

		}
		else
		{
			// "Step-by N" with wrap-around
			next = (prev + this.prog + this.slide.length) % (this.slide.length);
		}

		// Add selection to Front of History
		this.history.unshift(next);
		// Truncate End of History if it's too long
		if (this.history.length > ((this.slide.length / 3) + 1))
			this.history.pop();
	}
}

ssObj.prototype.displayCurrent = function()
{
	var curr = this.slide[this.history[this.display]];
	var iHTML = "";

	if (curr.lynk != "")
	{	iHTML += '<a href="'+this.linkBase+curr.lynk+'" target="ss_other_'+this.name+'">\n';
	}
	if (curr.title != "")
	{	iHTML += '<div id="tytle">'+curr.title+'</div><br>\n';
	}
	if (curr.image != "")
	{	iHTML += '<img width="60%" src="'+this.imageBase+curr.image+'" /><br>\n';
	}
	if (curr.lynk != "")
	{	iHTML += '</a><br>\n';
	}
	if ((curr.text != "") || (curr.attr != ""))
	{	iHTML += '<div id="'+this.name+'_biz_descr">\n';
		if (curr.text != "")
		{	iHTML += curr.text + '\n';
		}
		if (curr.attr != "")
		{	iHTML += '<br/><br/>-- ' + curr.attr + '\n';
		}
		iHTML += '</div>\n';
	}
	// Update the Slideshow element on the page
	this.elem_id.innerHTML = iHTML;
}

ssObj.prototype.scheduleNextUpdate = function(delta)
{
	if (this.num == 0)
		return;

	if ((this.num != "continuous") && (this.num > 0))
		this.num--;

	this.timerStart(delta);
}

ssObj.prototype.step = function()
{
	var waite = 50;
	
	if (this.fadedir == 0)			// No Fade Effect shouyld be used
	{
		this.chooseNext();
		this.displayCurrent();
		waite = this.delay;
	}
	else							// Using a Fade Effect
	{
		if (this.fadedir == -1)		// Fading out
		{
			if (this.opa == 0)		// We're fully faded out
			{
				objOpacitySet(this.elem_id, this.opa);	// Re-assert complete transparancy
				this.chooseNext();
				this.displayCurrent();
				this.fadedir = 1;
			}
			else					// Still fading out
			{
				this.opa -= (100 / this.fadestep);
				if (this.opa < 0) this.opa = 0;
				objOpacitySet(this.elem_id, this.opa);	// set new opacity
				waite = Math.floor(this.faderate / this.fadestep);
			}
		}
		if (this.fadedir == 1)		// Fading in
		{
			if (this.opa == 100)	// We're fully faded in
			{
				this.fadedir = -1;
				waite = this.delay;
			}
			else					// Still fading
			{
				this.opa += (100 / this.fadestep);
				if (this.opa > 100) this.opa = 100;
				objOpacitySet(this.elem_id, this.opa);	// set new opacity
				waite = Math.floor(this.faderate / this.fadestep);
			}
		}
	}
	this.scheduleNextUpdate(waite);
}

ssObj.prototype.position = function(imagedir)
{
	this.imagedir = imagedir;
	document.write('<div id="'+this.name+'_outer"><center>\n');

	if (this.speed || this.pause || this.ffrev)
	{	document.write('<div><center>\n');
		if (this.speed)
			document.write('<img id="'+this.name+'_slow" src="'+this.imagedir+'ss_down.gif"  alt="slower"/>\n');
		if (this.ffrev)
			document.write('<img id="'+this.name+'_back" src="'+this.imagedir+'ss_back.gif"  alt="previous"/>\n');
		if (this.pause)
			document.write('<img id="'+this.name+'_togg" src="'+this.imagedir+'ss_pause.gif" alt="pause"/>\n');
		if (this.ffrev)
			document.write('<img id="'+this.name+'_fore" src="'+this.imagedir+'ss_fore.gif"  alt="next"/>\n');
		if (this.speed)
			document.write('<img id="'+this.name+'_fast" src="'+this.imagedir+'ss_uppp.gif"  alt="faster"/>\n');
		document.write('</center></div>\n');
	}

	document.write('<div id="'+this.name+'_biz"></div>\n</center></div>\n');
	this.elem_id = document.getElementById(this.name+'_biz');
		
	// Add event handlers for the control buttons (if any)
	if (this.speed || this.pause || this.ffrev)
	{
		var el;
		var thisObj = this;
		if (this.speed)
		{	el = document.getElementById(this.name+'_fast');
			el.onclick = function() { thisObj.speedFaster(); };
			el = document.getElementById(this.name+'_slow');
			el.onclick = function() { thisthisObj.speedSlower(); };
		}
		if (this.ffrev)
		{	el = document.getElementById(this.name+'_fore');
			el.onclick = function() { thisObj.historyFore(); };
			el = document.getElementById(this.name+'_back');
			el.onclick = function() { thisObj.historyBack(); };
		}
		if (this.pause)
		{	el = document.getElementById(this.name+'_togg');
			el.onclick = function() { thisObj.runToggle(); };
		}
	}
}

//Example:  (This can be done outside of a function for in-line execution)
function exampleSlideShow()
{
	var myss = new ssObj("Example", "prnd", "continuous", 10000, 0x0111,
				  		 "http://www.yahoo.com/", "ImageDir");

	myss.slideAdd("Title1","Imagefile1.jpg","lynk1.php","Image Num 1","by Dude 1");
	myss.slideAdd("Title2","Imagefile2.jpg","lynk2.php","Image Num 2","by Dude 2");
	myss.slideAdd("Title3","Imagefile3.jpg","lynk3.php","Image Num 3","by Dude 3");
	myss.slideAdd("Title4","Imagefile4.jpg","lynk4.php","Image Num 4","by Dude 4");
	myss.slideAdd("Title5","Imagefile5.jpg","lynk5.php","Image Num 5","by Dude 5");

	myss.position("Common/Images/");
	myss.step();
}
