/*************************************************
*  Выпадающее меню для сайта napitki.ru          *
*                                                *
*  ADT Web Solitions, 20.04.2010                 *
*  Грипинский Сергей                             *
*  kagami@adt.ru, kagami-sorano@mail.ru          *
*  icq 224-799-719                               *
*                                                *
*************************************************/

var timerid = 0;
var lifetime = 500; //ms
function showChildMenu(event)
{
	if($(event.target).context.nodeName != 'A')
		var ownerid = $(event.target).attr('id');
	else
		var ownerid = $(event.target).parent().attr('id');
	hideAllOtherMenu();
	var father = document.getElementById(ownerid);
	if (father != null) 
	{
		var child = document.getElementById(ownerid.replace(/father/,'child'));
		if (child != null) 
		{
			var fatherpos = getElementPosition(father.id) ;
			var childleft = fatherpos.left + fatherpos.width;
			var childtop = fatherpos.top - (fatherpos.height/2);
			child.style.left = childleft + 'px';
			child.style.top = childtop + 'px';
			child.style.display = 'block';
			clearTimeout(timerid);
		}
	}
}
function hideChildMenu(event) 
{
	var ownerid = $(event.target).attr('id');
	var father = document.getElementById(ownerid);
	if (father != null) 
	{
		var child = document.getElementById(ownerid.replace(/father/,'child'));
		if (child != null) 
		{
			var str_exec = "hideNow('" + ownerid.replace(/father/,'child') + "')";
			timerid = setTimeout(str_exec,lifetime);
		}
	}
}
function holdChildMenu(event) 
{
	clearTimeout(timerid);
}
function forgetChildMenu(event)
{
	var childname = $(event.target).attr('id');
	var str_exec = "hideNow('" + childname + "')";
	timerid = setTimeout(str_exec,lifetime);
}
function hideNow(elemid)
{
	document.getElementById(elemid).style.display = 'none';
}
function hideAllOtherMenu() 
{
$('.dropdownmenu').each(function(i){$(this).css('display','none');});
/*	var mother = document.getElementById('mother');
	if (mother != null) 
	{
		var children = mother.children;
		for (i=0; i<children.length; i++) 
		{
			children[i].style.display = 'none';
		}
	}
*/
}
function getElementPosition(elemId)
{
    var elem = document.getElementById(elemId);
    var w = elem.offsetWidth, h = elem.offsetHeight, l = 0, t = 0;
    while (elem)
    {
        l += elem.offsetLeft;
        t += elem.offsetTop;
        elem = elem.offsetParent;
    }
    return {"left":l, "top":t, "width": w, "height":h};
}


$(document).ready(function(){
	$('.toplinemenu').bind('mouseover',showChildMenu);
	$('.toplinemenu').bind('mouseout',hideChildMenu);
	$('.dropdownmenu').bind('mouseover',holdChildMenu);
	$('.dropdownmenu').bind('mouseout',forgetChildMenu);
});