var isDOM = (document.getElementById ? true : false); 
var isIE4 = ((document.all && !isDOM) ? true : false);
var isNS4 = (document.layers ? true : false);

function getObject(id)	{
	if (isDOM) return document.getElementById(id);
	if (isIE4) return document.all[id];
	if (isNS4) return document.layers[id];
}

function hideSubmenu(id)
{
    var submenu = getObject(id);
    
    if (submenu.hideTimer)
        clearTimeout(submenu.hideTimer);
    
    submenu.hideTimer = setTimeout(function()
    {
        submenu.style.display = 'none';
    }, 100);
}

//popup submenu
function getCurrentPosition(e) {
    var current = e;
    var currentTop = 0;
    var currentLeft = 0;
    var currentHeight = current.offsetHeight;
    var currentWidth = current.offsetWidth;

    while (current) {
        currentLeft += current.offsetLeft;
        currentTop += current.offsetTop;
        current = current.offsetParent;
    }
    return { left: currentLeft, top: currentTop, width: currentWidth, height: currentHeight };
}

function getClientWidth() {
    return document.compatMode == 'CSS1Compat' && !window.opera ? document.documentElement.clientWidth : document.body.clientWidth;
}

function ShowSubmenu(e, id) {
    var submenu = getObject(id);
    
    if (submenu.hideTimer)
        clearTimeout(submenu.hideTimer);
    
    var current = getCurrentPosition(e);
    var docWidth = getClientWidth();
	
	jQSubmenu = $("#"+id);
	
    jQSubmenu.css('right', docWidth - (current.left + current.width) - 3);
	
    jQSubmenu.css('top',  current.top + current.height);
	
    jQSubmenu.css('display','block');
}

function showWithTimer(e) {
    if (e.hideTimer)
        clearTimeout(e.hideTimer);
        
    e.style.display = 'block';
}

function show(e) {
        e.style.display = 'block';
}
function hide(e) {
        e.style.display = 'none';
}

// скрывает/раскрывает подразделы меню
function ChangeMenuState(id)	
{	
	for (var i = 0; i < menu_items.length; ++i)
	{
	    var item = getObject(menu_items[i]);
	    
	    if (null == item)
	        continue;
	        
	    if (item.id != id)
		    item.style.display = "none";
        else
            item.style.display = item.style.display == "none" ? "block" : "none";
	}
}

// скрывает/раскрывает подразделы вложенных таблиц
function ChangeTableState(id)	
{
    var item = getObject('tbl' + id);
    var img = getObject('img' + id);
    
    if (item.style.display == "none" || item.style.display == "")
    {
        item.style.display =  "block";
        img.src = img.src.replace('arr_blue','arr_blue_r');
    }
    else
    {
        item.style.display =  "none";
        img.src = img.src.replace('arr_blue_r','arr_blue');
    }
}


