/**
*	Author: @Thomas Ene
*
*	Audit:	
*	03/06/2008, v0.0.1
*		- First Draft
*	04/13/2008, v0.0.2
*		- Various improvements and bug fixing
*
*	Important: (this example might not work!!!)
*		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, IE7
*/

function hslider(contentWrapperId, contentWrapperMaxWidth)
{
	// Default variables
		this.containerObj = null;
		this.containerId = null;
		this.containerWidth = null;
		this.containerCurrentWidth = null;
		
		this.minWidth = 0;
		
		this.direction = 1; // 0 - closing, 1 - opening
		
		this.timerId = null;
		this.timeoutSpeed = 40;
		
		this.slideTotalSteps = 1;
		this.scrollSpeed = 40; // To be recalculated in a sec
		
	// Container
		this.containerId = contentWrapperId;
		this.containerWidth = contentWrapperMaxWidth;
		this.containerCurrentWidth = contentWrapperMaxWidth;
	
	// Constructor
		this.containerObj = document.getElementById(contentWrapperId);
		if (this.containerObj == null)
			{ alert("Cannot find ["+contentWrapperId+"]"); return null; }
			
		this.recalcScrollSpeed();

}

hslider.prototype.setContainerWidth = function(myContainerWidth)
{
	this.containerWidth = myContainerWidth;
	this.recalcScrollSpeed();
}

hslider.prototype.setSlideTotalSteps = function(myTotalSteps)
{
	this.slideTotalSteps = myTotalSteps;
	this.recalcScrollSpeed();
}

hslider.prototype.recalcScrollSpeed = function()
{
	if (this.containerWidth % this.slideTotalSteps != 0)
		alert("To avoid display bugs and flickering use a number of steps\r\n"
				+"which generates a null remainder!\r\n"+"\r\n"
				+"Width = "+this.containerWidth+"\r\n"
				+"Steps = "+this.slideTotalSteps+"\r\n"
				+"Remainder="+(this.containerWidth% this.slideTotalSteps));
	
	this.scrollSpeed = this.containerWidth / this.slideTotalSteps;
}

hslider.prototype.show = function()
{
	this.direction = 1;
	if (!this.running()) this.start();
}

hslider.prototype.showNow = function()
{
	this.direction = 1;
	this.containerCurrentWidth = this.containerWidth;
	this.containerObj.style.width = this.containerWidth;
	if (this.running()) this.stop();
}

hslider.prototype.hide = function()
{
	this.direction = 0;
	if (!this.running()) this.start();
}

hslider.prototype.hideNow = function()
{
	this.direction = 0;
	this.containerCurrentWidth = this.minWidth;
	this.containerObj.style.width = this.containerCurrentWidth;
	if (this.running()) this.stop();
}

hslider.prototype.switchAction = function()
{
	this.direction = this.direction == 0 ? 1 : 0;
	if (!this.running()) this.start();
}

hslider.prototype.stepClose = function(repaint)
{
	var result = true;
	if (repaint==null) repaint = true;
	
	this.containerCurrentWidth -= this.scrollSpeed;
	if (this.containerCurrentWidth < this.minWidth)
	{
		this.containerCurrentWidth = this.minWidth;
		result = false;
	}
	
	// Update
	if (repaint) this.repaint();
		
	// Done
	return result;
}

hslider.prototype.stepOpen = function(repaint)
{
	var result = true;
	if (repaint==null) repaint = true;
	
	this.containerCurrentWidth += this.scrollSpeed;
	if (this.containerCurrentWidth > this.containerWidth)
	{
		this.containerCurrentWidth = this.containerWidth;
		result = false;
	}
	
	// Update
	if (repaint) this.repaint();
		
	// Done
	return result;
}

hslider.prototype.repaint = function()
{
	this.containerObj.style.width = this.containerCurrentWidth;
}

hslider.prototype.sliding = function()
{
	//alert('a');
	if (!this.running()) return;
	
	//alert(parseFloat('399px'));
	//alert(this.containerObj.clientWidth);
	
	if (this.direction == 0)
	{
		if (!this.stepClose())
		{
			//alert('stopping '+this.containerObj.clientWidth);
			this.stop();
		}
	}
	
	if (this.direction == 1)
	{
		if (!this.stepOpen())
		{
			//alert('stopping '+this.containerObj.clientWidth);
			this.stop();
		}
	}
}

hslider.prototype.start = function()
{
	//if (typeof(eval(this.timer)) != "undefined")
	if (this.running())
	{
		alert("hscroller->start: Object is already running "+
			"("+this.containerId+") !");
		return;
	}

	var _this = this;
	this.timerId = window.setInterval(	function(){ _this.sliding(); },
										_this.timeoutSpeed);
}

hslider.prototype.stop = function()
{
	if (this.running())
		window.clearInterval(this.timerId);
	this.timerId = null;
}

hslider.prototype.running = function()
{
	return this.timerId!=null ? true : false;
}