function getElementPosition(oElement, oMenu){
    var res = new Array(0, 0);
    // Detect absolute or relative parent offset
    pStopObj = null;
    pObj = oMenu.parentNode;
    while(pObj != null && typeof(pObj.style) != "undefined"){
        if(pObj.style.position == "absolute" || pObj.style.position == "relative"){
            pStopObj = pObj;
            break;
        }
        pObj = pObj.parentNode;
    }
    // Get coordinates
    do{
        if(pStopObj == oElement)
            break;
        res[0] += oElement.offsetLeft;
        res[1] += oElement.offsetTop;
    }while((oElement = oElement.offsetParent) != null);
    return res;
}

function positioningMenu(smNum, relateToParentX, relateToParentY, deltaX, deltaY){
    var menuId = "sm" + smNum;
    var menuObj = document.getElementById(menuId);
    var parentId = "i" + smNum;
    var parentObj = document.getElementById(parentId);
    
    var parentPosition = getElementPosition(parentObj, menuObj);

    if(typeof(deltaX) == "undefined")
        deltaX = 0;
    if(relateToParentX == "right" || relateToParentX == "center"){
        parentWidth = parentObj.offsetWidth;
        if(relateToParentX == "center")
            parentWidth = parentWidth/2;
        deltaX += parentWidth;
    }
    
    if(typeof(deltaY) == "undefined")
        deltaY = 0;
    if(relateToParentY == "bottom" || relateToParentY == "center"){
        parentHeight = parentObj.offsetHeight;
        if(relateToParentY == "center")
            parentHeight = parentHeight/2;
        deltaY += parentHeight;
    }
    
    menuObj.style.position = 'absolute';
    menuObj.style.left = parentPosition[0] + deltaX + 'px';
    menuObj.style.top = parentPosition[1] + deltaY + 'px';
}

var hTmMenuHide = null;
var hTmSubMenuHide = {"init":0};
var prevImgSrc = Array();
var openedMenusStack = Array();

function showMenu(smNum, parentNum, relateToParentX, relateToParentY, deltaX, deltaY, imgOver){
    var menuObj = document.getElementById("sm" + smNum);

    if(menuObj != null){
        clearTimeout(hTmMenuHide);
        clearTimeout(hTmSubMenuHide[smNum]);
        
        hideMenuById(parentNum, true, true);
        positioningMenu(smNum, relateToParentX, relateToParentY, deltaX, deltaY);
      
        document.getElementById("sm" + smNum).style.display = 'block';
        openedMenusStack.push(smNum);
    }
}

function hideMenu(smNum){
    var menuObj = document.getElementById("sm" + smNum);
    if(menuObj != null){
        menuObj.style.display = 'none';
        images = document.getElementsByTagName("IMG");
        if(document.images && document.images['main_menu_img_'+smNum] && prevImgSrc[smNum] != null){
            document.images['main_menu_img_'+smNum].src = prevImgSrc[smNum];
        }
    }
}

function hideMenuById(smNum, isIdParent, hideAllIfNotFound){
    if(smNum == 0){
        hideMenuAll();
    }else{
        var removeFromPos = -1;
        for(i = 0; i < openedMenusStack.length; i++){
            if(openedMenusStack[i] == 0)
                break;
            if(removeFromPos == -1 && openedMenusStack[i] == smNum){
                removeFromPos = i;
                if(isIdParent){
                    removeFromPos += 1;
                    continue;
                }
            }
            if(removeFromPos > -1){
                hideMenu(openedMenusStack[i]);
            }
        }
        if(hideAllIfNotFound && removeFromPos == -1){
            hideMenuAll();
        }else if(removeFromPos > -1 && removeFromPos < openedMenusStack.length){
            openedMenusStack.splice(removeFromPos, openedMenusStack.length-removeFromPos);
        }
    }
} 

function hideMenuAll(){
    for(i = openedMenusStack.length-1; i >= 0; i--){
        hideMenu(openedMenusStack[i]);
    }
    openedMenusStack = new Array();
} 

function hideMenuAllByTimeout(){
    hTmMenuHide = setTimeout('hideMenuAll()', 500);
}

function hideMenuIdByTimeout(smNum){
    hTmSubMenuHide[smNum] = setTimeout('hideMenuById('+smNum+', false, false)', 250);
}

/* HTML handlers */
function mon(smNum, smParentId){
    clearTimeout(hTmMenuHide);
    if(typeof(smNum) != "undefined" && smNum > 0){
        clearTimeout(hTmSubMenuHide[smNum]);
    }
    if(typeof(smParentId) != "undefined" && smParentId > 0){
        clearTimeout(hTmSubMenuHide[smParentId]);
    }
}

function moff(smNum, evt){
    hideMenuAllByTimeout();
    if(typeof(smNum) != "undefined"){
        hideMenuIdByTimeout(smNum);
    }
    if(typeof(evt) != "undefined"){
        if(typeof(evt.cancelBubble) != "undefined"){
            evt.cancelBubble = true;
            if(typeof(evt.stopPropagation) == "function")
                evt.stopPropagation();
        }
    }
}

function submoff(menuId){
    hideMenuIdByTimeout(menuId);
}

function smclick(){
    hideMenuAll();
}

function menuOnOff(num) {
 el = document.getElementById('pd' + num);
 if (el.style.display == 'block') {
  el.style.display = 'none';
 } else {
  el.style.display = 'block';
 }
}
