// ===========================================================================================
// Copyright 2010
// by Asset Generation Elite Services, Inc.
// ===========================================================================================
function tableuObj(name, progression, num_cycles, delay_ms, opt)
{
	this.tid      = 0;
	this.elem     = null;				// (Eventually) holds the element id for this tableu's "div" HTML element
    this.shuffle  = null;				// (Eventually) holds the shuffle Object that manages scenez, cardz, and lox
	this.kontent  = new Array();		// Lyst of content (scenes) for this tableu
	this.disp_opt = "";					// display_options when rendering content to cardz
	this.key      = null;				// if non-null, keyword for search-narrowed scene lyst given to shuffle object

	this.name      = name;				// Name of the HTML div Element for this tableu (may not exist until after this call)
	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, tableu does not automatically cycle
	this.delay     = delay_ms;			// How long to delay between auto-cycle
	this.xrate     = 1000;				// how long to experience the xlate transition
	this.dir       = 1;					// how far and which direction to step after each interval
	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.imagedir  = "";
	
//	test_alert(TABITZ_OBJ_CREATE, "Created Tableu Obj called '"+this.name+"'");
}

tableuObj.prototype.Open = function(tclass)
{
	echo('<div class="' + tclass + '" id="'+this.name+'"'+"\n");

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

}

tableuObj.prototype.elemSetup = function()
{
	this.elem = objById(this.name);

	if (this.elem)
	{
		// 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(); };
			}
		}
	}
	else
	{
		windowload_todo_list.funkAdd(new funcObj(this, tableuObj.prototype.elemSetup));
	}
}

tableuObj.prototype.Close = function()
{
	echo('</div>'+"\n");
	this.elemSetup();
}

tableuObj.prototype.DrawAll = function(tclass, eclass)
{
	this.Open(tclass);
	this.shuffle.posDrawAll(eclass);
	this.Close();
}

tableuObj.prototype.Start = function(mystepdir)
{
	this.dir = mystepdir;
	this.shuffle.Start(1);
	this.timerStart(this.delay);
}

tableuObj.prototype.shuffleAdd = function(shuff)
{
    this.shuffle  = shuff;
}

tableuObj.prototype.sceneAdd = function(scene)
{
	if (scene == null)
		return;
    this.kontent.push(scene);
}

tableuObj.prototype.sceneDel = function(scene)
{
	if (scene != null)
	{
		SceneFind(this.kontent, scene, true);	// Find and remove the scene from our lyst
	}
	return(scene);
}

tableuObj.prototype.sceneDelByCode = function(kode)
{
	return(SceneFindByCode(this.kontent, kode, true));	// Find and remove the scene from our lyst
}

tableuObj.prototype.keySet = function(keyword)
{
    this.key  = keyword;
}

tableuObj.prototype.sceneGive2Shuffle = function()
{
	var giveLyst = this.kontent.slice();						// copy the array

	if (this.prog == "prnd" || this.prog == "rnd")
	{
		var i;
		var rindex;
		var lm;
		for(i=0; i < giveLyst.length; i++)
		{
			rindex = randomInt(0, giveLyst.length - 1);
			lm = giveLyst.splice(rindex, 1);					// pluck from somewhere
			giveLyst.unshift(lm[0]);							// move it to beginning
		}
		giveLyst.reverse();										// reverse Array order
		for(i=0; i < giveLyst.length; i++)
		{
			rindex = randomInt(0, giveLyst.length - 1);
			lm = giveLyst.splice(rindex, 1);					// pluck from somewhere
			giveLyst.unshift(lm[0]);							// move it to beginning
		}
	}
	this.shuffle.sceneLystApply(giveLyst);
}

// ===========================================================================================

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

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

tableuObj.prototype.runToggle = function()
{
	var el;
	if (this.running)		// Currently Running...so Pause
	{
		this.running = false;
		this.timerCancel();
		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();
	}
}

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

	this.timerStart();
}

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

	this.timerStart();
}

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

	this.shuffle.slide(1, true, this.xrate);
}

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

	this.shuffle.slide(-1, true, this.xrate);
}

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

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

	this.timerStart(delta);
}

tableuObj.prototype.step = function()
{
	this.shuffle.slide(this.dir, true, this.xrate);
	this.scheduleNextUpdate(this.delay);
}

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

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

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

// ===========================================================================================
