var timeout          = 1000;
var closetimer		 = 0;
var ddmenuitem       = 0;
var ddMenuId         = 0;
var afterClose       = null;
var currentTweener   = null;
var siteTotalWidth   = null;//$('site_container').getCoordinates().width;
var windowTotalWidth = null;//window.getSize().x;
var leftPos          = null;//Math.floor(((windowTotalWidth / 2) - (siteTotalWidth / 2)));
// open hidden layer
function mopen(id)
{	
//	$('sub_cat').innerHTML = (id);
	if($(id) == null){
		return;
	}
	
	// cancel close timer
	mcancelclosetime();
	
	if(ddMenuId == id)return;
	if(ddMenuId != id)animateClose();

	// close old layer
	mclose(null);

	hideInactive();
	// show layer
	animateOpen(id);
}

// close showed layer
function mclose()
{
	ddMenuId = null;
	animateClose();
}

function animateOpen(id)
{
	if($(id) == null)return;
	
	if((leftPos + siteTotalWidth) < ($(id).getCoordinates().left + $(id).getCoordinates().width))
	{
		$(id).setStyle('width', '90px');
	}
	
	tween = new Tween(document.getElementById(id).style,'height',Tween.regularEaseOut,0,getContainerHeight(id),0.25,'px');
	tween.stop();
	tween.start();
		
	document.getElementById(id).style.visibility = 'visible';
	
//	$(id).setStyle('opacity', 9);
	
	ddMenuId = id;
}

function animateClose()
{
	hideInactive();	
	
	afterClose = ddMenuId;
	ddMenuId = null;
}

function onMenuOpen()
{
}

function onMenuClosed()
{
	if(afterClose != null)
	{
		afterClose = null;
	}
	afterClose = null;
}

// go close timer
function mclosetime()
{
	closetimer = window.setTimeout('mclose()', timeout);
}

// cancel close timer
function mcancelclosetime()
{
	if(closetimer)
	{
		window.clearTimeout(closetimer);
		closetimer = null;
	}
}
/**
* Returns the actual height
*
* @param {String}id: The id of the element whose height will be returned
*/
function getContainerHeight(id)
{
	var height=0;
	
	for( var node = $(id).firstChild; node != null; node = node.nextSibling )
	{
		height += node.offsetHeight;
	}
	return height;
}

function hideInactive()
{
	//Adds events to all Elements with the class name 'email'.
	els = $(document.body).getElements('div.dd_container');
	for(var i = 0; i < els.length; i++){
		if(els[i].id != ddMenuId){document.getElementById(els[i].id).style.visibility = 'hidden';}
	}
}

// close layer when click-out
document.addEvent('domready', 
function (){
	hideInactive();
	document.addEvent('click', function(){ddMenuId=null; hideInactive();}); 
});