/**
*	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 vslider('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 vslider(contentWrapperId, contentWrapperMaxHeight)
{
	// Default variables
		this.containerObj = null;
		this.containerId = null;
		this.containerHeight = null;
		this.containerCurrentHeight = null;
		
		this.minHeight = 1;
		
		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.containerHeight = contentWrapperMaxHeight;
		this.containerCurrentHeight = contentWrapperMaxHeight;
	
	// Constructor
		this.containerObj = document.getElementById(contentWrapperId);
		if (this.containerObj == null)
			{ alert("Cannot find ["+contentWrapperId+"]"); return null; }
			
		this.recalcScrollSpeed();
	
}

vslider.prototype.setContainerHeight = function(myContainerHeight)
{
	this.containerHeight = myContainerHeight;
	this.recalcScrollSpeed();
}

vslider.prototype.setSlideTotalSteps = function(myTotalSteps)
{
	this.slideTotalSteps = myTotalSteps;
	this.recalcScrollSpeed();
}

vslider.prototype.recalcScrollSpeed = function()
{
	if (this.containerHeight % 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"
				+"Height = "+this.containerHeight+"\r\n"
				+"Steps = "+this.slideTotalSteps+"\r\n"
				+"Remainder="+(this.containerHeight % this.slideTotalSteps));
	
	this.scrollSpeed = this.containerHeight / this.slideTotalSteps;
}

vslider.prototype.show = function()
{
	this.direction = 1;
	if (!this.running()) this.start();
}

vslider.prototype.showNow = function()
{
	this.direction = 1;
	this.containerCurrentHeight = this.containerHeight;
	this.containerObj.style.height = this.containerHeight;
	if (this.running()) this.stop();
}

vslider.prototype.hide = function()
{
	this.direction = 0;
	if (!this.running()) this.start();
}

vslider.prototype.hideNow = function()
{
	this.direction = 0;
	this.containerCurrentHeight = this.minHeight;
	this.containerObj.style.height = this.containerCurrentHeight;
	if (this.running()) this.stop();
}

vslider.prototype.switchAction = function()
{
	this.direction = this.direction == 0 ? 1 : 0;
	if (!this.running()) this.start();
}

vslider.prototype.stepClose = function(repaint)
{
	var result = true;
	if (repaint==null) repaint = true;
	
	this.containerCurrentHeight -= this.scrollSpeed;
	if (this.containerCurrentHeight < this.minHeight)
	{
		this.containerCurrentHeight = this.minHeight;
		result = false;
	}
	
	// Update
	if (repaint) this.repaint();
	
	// Done
	return result;
}

vslider.prototype.stepOpen = function(repaint)
{
	var result = true;
	if (repaint==null) repaint = true;
	
	this.containerCurrentHeight += this.scrollSpeed;
	if (this.containerCurrentHeight > this.containerHeight)
	{
		this.containerCurrentHeight = this.containerHeight;	
		result = false;
	}
	
	// Update
	if (repaint) this.repaint();
	
	// Done
	return result;
}

vslider.prototype.repaint = function()
{
	this.containerObj.style.height = this.containerCurrentHeight;
}

vslider.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();
		}
	}
}

vslider.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);
}

vslider.prototype.stop = function()
{
	if (this.running())
		window.clearInterval(this.timerId);
	this.timerId = null;
}

vslider.prototype.running = function()
{
	return this.timerId!=null ? true : false;
}