﻿HtmlElementRoller.ACCELERATOR_CLOSE = 1.25;
HtmlElementRoller.ACCELERATOR_OPEN = .55;

function HtmlElementRoller()
{
	
}

HtmlElementRoller.closeElement = function(e, n)
{
	
	e.style.overflowY 	= "hidden";
	
	var h = HtmlElementRoller.getElementHeight(e);
	
	if (!n)
		n = h * .06;
		
	n = n * HtmlElementRoller.ACCELERATOR_CLOSE;
	
	if (h - n > 0)
	{
		e.style.height = (h - n) + "px";
		setTimeout(function(){HtmlElementRoller.closeElement(e, n);}, 50);
	}
	else
	{
		e.style.height 			= "0px";
		e.parentNode.className 	= "closed";
		HtmlElementRoller.saveState(e.id, "closed", 14);
		return true;
	}

}

/*

@t = the target height where the open will stop. 

*/
HtmlElementRoller.openElement = function(e, n, t)
{
	
	e.style.overflowY 	= "hidden";
	
	// calculate the target height if not provided
	
	if (!t)
	{
		// adjust the parent so it hides the child 
		// as we calculate the target height
		
		peh = e.parentNode.style.height;
		if (!peh)
		{
			pe = HtmlElementRoller.getElementHeight(e.parentNode) + "px";
			e.parentNode.style.height = pe;
		}
		e.parentNode.style.overflowY = "hidden";
		
		e.style.height = "";
		e.style.display = "";
		
		t = HtmlElementRoller.getElementHeight(e);
		e.style.height = "0px";
		
		// put the parent back the way we found it
		
		if (!peh)
			e.parentNode.style.height = null;
	}
	
	// calculate the step change if not provided.
	
	if (!n)
		n = t * .45;
	else	
		n = n * HtmlElementRoller.ACCELERATOR_OPEN;

	if (n < 8)
		n = 8;
		
	// get the current height
	
	var h = HtmlElementRoller.getElementHeight(e);

	if (h + n < t)
	{
		e.style.height 		= (h + n) + "px";
		setTimeout(function(){HtmlElementRoller.openElement(e, n, t);}, 50);
	}
	else
	{
		e.style.height 			= null;
		e.parentNode.className 	= "open";
		HtmlElementRoller.saveState(e.id, "open", 14);
		return true;
	}
	
}

HtmlElementRoller.saveState = function (name,value,days) {

	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

HtmlElementRoller.getElementHeight = function(e)
{
	
	if (typeof e.innerHeight != 'undefined')
		return e.innerHeight;
	else
		return e.clientHeight;
	
}
