/*
*	Author: @Thomas Ene
*
*	Audit:	
*	03/06/2008, v0.0.1
*		- First Draft

	Important:
		Example
				<div id="contentWrapper" style="overflow:hidden;width:370px;height:150px;background-color:blue;float:right;">
					<div id=style="width:370px;background-color:green;">
						abcd 1234<br>
						abcd 1234<br>
						abcd 1234<br>
					</div>
				</div>
				<script>
					var slider = new hslider('contentWrapper', 370);
					slider.switchAction();
				</script>
				
		Notes:
			Mandatory:
				- the content wrapper layer (div) must specify: overflow:hidden;
			Recommended:
				- both the content wrapper and the content layers (divs) should specify a fixed width

*	Tested with: FF2, Opera9.2, IE6
*/

function hslider(contentWrapperId, contentWrapperMaxWidth)
{
	// Container
		this.containerId = contentWrapperId;
		this.containerWidth = contentWrapperMaxWidth;
		this.containerCurrentWidth = contentWrapperMaxWidth;
		this.containerObj = null;
		
	// Default variables
		this.minWidth = 0;
		
		this.timerId = null;
		this.running = false;
		
		this.direction = 1; // 0 - closing, 1 - opening
		
		this.timeoutSpeed = 30;
		this.scrollSpeed = 40;
	
	// Constructor
		var tobj;
		
		tobj = document.getElementById(contentWrapperId);
		if (tobj!=null) this.containerObj = tobj;
		
		if (this.containerObj == null)
			{ alert("Cannot find ["+containerId+"]"); return null; }
	
}

hslider.prototype.show = function()
{
	this.direction = 1;
	if (!this.running) this.start();
}

hslider.prototype.hide = function()
{
	this.direction = 0;
	if (!this.running) this.start();
}

hslider.prototype.switchAction = function()
{
	this.direction = this.direction == 0 ? 1 : 0;
	if (!this.running) this.start();
}

hslider.prototype.sliding = function()
{
	//alert('a');
	if (!this.running) return;
	
	//alert(parseFloat('399px'));
	//alert(this.containerObj.clientWidth);
	
	if (this.direction == 0)
	{
		if (this.containerObj.clientWidth > this.minWidth)
		{
			this.containerCurrentWidth -= this.scrollSpeed;
			if (this.containerCurrentWidth < this.minWidth)
				this.containerCurrentWidth = this.minWidth;
			this.containerObj.style.width = this.containerCurrentWidth;
		}
		else
		{
			//alert('stopping '+this.containerObj.clientWidth);
			this.stop();
		}
	}
	
	if (this.direction == 1)
	{
		if (this.containerObj.clientWidth < this.containerWidth)
		{
			this.containerCurrentWidth += this.scrollSpeed;
			if (this.containerCurrentWidth > this.containerWidth)
				this.containerCurrentWidth = this.containerWidth;
			this.containerObj.style.width = this.containerCurrentWidth;
			
		}
		else
		{
			//alert('stopping '+this.containerObj.clientWidth);
			this.stop();
		}
	}
}

hslider.prototype.start = function()
{
	//if (typeof(eval(this.timer)) != "undefined")
	if (this.timerId != null)
	{
		alert("hscroller->start: Object is already running "+
			"("+this.containerId+") !");
		return;
	}

	var _this = this;
	this.timerId = window.setInterval(	function(){ _this.sliding(); },
										_this.timeoutSpeed);
	this.running = true;
}

hslider.prototype.stop = function()
{
	if (this.timerId != -1)
		window.clearInterval(this.timerId);
	this.timerId = null;
	this.running = false;
}