function load(location){
    if(document.images){
        var image = new Image();
        image.src = location;
        return image;
    }
}

function getElementPosition(oElement, oMenu){
    var res = new Array(0, 0, 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
    var doCount = true;
    do{
        if(pStopObj == oElement){
            doCount = false;
            res[2] = res[0];
            res[3] = res[1];
        }
        if(doCount){
            res[0] += oElement.offsetLeft;
            res[1] += oElement.offsetTop;
        }else{
            res[2] += oElement.offsetLeft;
            res[3] += oElement.offsetTop;
        }
    }while((oElement = oElement.offsetParent) != null);
    if(doCount){
        res[2] = res[0];
        res[3] = res[1];
    }
    return res;
}

function positioningMenu(smNum, relateToParentX, relateToParentY, deltaX, deltaY, directionX, directionY){
    var result = new Array(relateToParentX, relateToParentY, deltaX, deltaY, directionX, directionY);
    
    if(typeof(directionX) == 'undefined'){
        directionX = 'right';
    }
    if(typeof(directionY) == 'undefined'){
        directionY = 'down';
    }

    if(typeof(relateToParentX) == 'undefined'){
        relateToParentX = 'right';
    }
    if(typeof(relateToParentY) == 'undefined'){
        directionY = 'top';
    }

    if(typeof(deltaX) == 'undefined'){
        deltaX = 0;
    }
    if(typeof(deltaY) == 'undefined'){
        deltaY = 0;
    }

    var menuId = "sub_menu_" + smNum;
    var menuObj = document.getElementById(menuId);
    var parentId = "j" + smNum;
    var parentObj = document.getElementById(parentId);
    
    var currentLeft = document.body.scrollLeft;
    var currentRight = currentLeft + document.body.clientWidth;
    var currentTop = document.body.scrollTop;
    var currentBottom = currentTop + document.body.clientHeight;

    var parentPosition = getElementPosition(parentObj, menuObj);
    var parentWidth = parentObj.offsetWidth;
    var parentHeight = parentObj.offsetHeight;
    
    menuObj.style.position = 'absolute';
    menuObj.style.left = parentPosition[0] + parentWidth;
    menuObj.style.top = parentPosition[1] + parentHeight;
    menuObj.style.display = 'block';
    var menuWidth = menuObj.offsetWidth;
    var menuHeight = menuObj.offsetHeight;
    
    var flipType;
    
    // Vertical flip
    flipType = 0;
    if(/*>*/ directionX == 'right' && relateToParentX == 'right' && /*at right*/ parentPosition[2] + parentWidth + deltaX + menuWidth > currentRight && /*at left*/ parentPosition[2] - menuWidth - deltaX > currentLeft){
        relateToParentX = 'left';
        flipType = 1;
    }else if(/*>*/ directionX == 'right' && relateToParentX == 'left' && /*at right*/ parentPosition[2] + deltaX + menuWidth > currentRight && /*at left*/ parentPosition[2] + parentWidth - menuWidth - deltaX > currentLeft){
        relateToParentX = 'right';
        flipType = 1;
    }else if(/*<*/ directionX == 'left' && relateToParentX == 'left' && /*at left*/ parentPosition[2] - menuWidth - deltaX < currentLeft && /*at right*/ parentPosition[2] + parentWidth + deltaX + menuWidth < currentRight){
        relateToParentX = 'right';
        flipType = 2;
    }else if(/*<*/ directionX == 'left' && relateToParentX == 'right' && /*at left*/ parentPosition[2] + parentWidth - menuWidth - deltaX < currentLeft && /*at right*/ parentPosition[2] + deltaX + menuWidth < currentRight){
        relateToParentX = 'left';
        flipType = 2;
    }
    if(flipType == 1){
        directionX = 'left';
        deltaX = -deltaX;
    }else if(flipType == 2){
        directionX = 'right';
        deltaX = -deltaX;
    }

    // Horizontal flip
    flipType = 0;
    if(/*v*/ directionY == 'down' && relateToParentY == 'top' && /*at bottom*/ parentPosition[3] + deltaY + menuHeight > currentBottom && /*at top*/ parentPosition[3] + parentHeight - menuHeight - deltaY > currentTop){
        relateToParentY = 'bottom';
        flipType = 1;
    }else if(/*v*/ directionY == 'down' && relateToParentY == 'bottom' && /*at bottom*/ parentPosition[3] + parentHeight + deltaY + menuHeight > currentBottom && /*at top*/ parentPosition[3] - menuHeight - deltaY > currentTop){
        relateToParentY = 'top';
        flipType = 1;
    }else if(/*^*/ directionY == 'up' && relateToParentY == 'bottom' && /*at top*/ parentPosition[3] + parentHeight - deltaY - menuHeight < currentTop && /*at bottom*/ parentPosition[3] + deltaY + menuHeight < currentBottom){
        relateToParentY = 'top';
        flipType = 2;
    }else if(/*^*/ directionY == 'up' && relateToParentY == 'top' && /*at top*/ parentPosition[3] - deltaY - menuHeight < currentTop && /*at bottom*/ parentPosition[3] + parentHeight + deltaY + menuHeight < currentBottom){
        relateToParentY = 'bottom';
        flipType = 2;
    }
    if(flipType == 1){
        directionY = 'up';
        deltaY = -deltaY;
    }else if(flipType == 2){
        directionY = 'down';
        deltaY = -deltaY;
    }

    // Set X menu position
    var leftPosition = deltaX;
    if(relateToParentX == 'left' && directionX == 'left'){
        leftPosition -= menuWidth;
    }else if(relateToParentX == 'right' && directionX == 'right'){
        leftPosition += parentWidth;
    }else if(relateToParentX == 'right' && directionX == 'left'){
        leftPosition += parentWidth - menuWidth;
    }else if(relateToParentX == 'center'){
        leftPosition += parentWidth/2;
    }
    leftPosition = Math.max(-parentPosition[2], leftPosition);
    
    // Set Y menu position
    var topPosition = deltaY;
    if(relateToParentY == 'top' && directionY == 'up'){
        topPosition -= menuHeight;
    }else if(relateToParentY == 'bottom' && directionY == 'down'){
        topPosition += parentHeight;
    }else if(relateToParentY == 'bottom' && directionY == 'up'){
        topPosition += parentHeight - menuHeight;
    }else if(relateToParentY == 'center'){
        topPosition += parentHeight/2;
    }
    topPosition = Math.max(-parentPosition[3], topPosition);
    
    // Correct X position
    var leftPositionA = parentPosition[2] + leftPosition;
    if(leftPositionA > currentRight - menuWidth){
        deltaLeft = leftPositionA - currentLeft;
        deltaRight = leftPositionA + menuWidth  - currentRight;
        if(deltaRight <= deltaLeft){
            leftPosition -= deltaRight;
        }else{
            leftPosition -= deltaLeft;
        }
    }
    
    // Correct Y position
    var topPositionA = parentPosition[3] + topPosition;
    if(topPositionA > currentBottom - menuHeight){
        deltaTop = topPositionA - currentTop;
        deltaBottom = topPositionA + menuHeight - currentBottom;
        if(deltaBottom <= deltaTop){
            topPosition -= deltaBottom;
        }else{
            topPosition -= deltaTop;
        }
    }

    menuObj.style.left = parentPosition[0] + leftPosition;
    menuObj.style.top = parentPosition[1] + topPosition;
    
    return new Array(relateToParentX, relateToParentY, deltaX, deltaY, directionX, directionY);
}

var hTmMenuHide = null;
var hTmSubMenuHide = {"init":0};
var openedMenusStack = Array();

function showMenu(smNum, parentNum, relateToParentX, relateToParentY, deltaX, deltaY, directionX, directionY){
    var menuObj = document.getElementById("sub_menu_" + smNum);
    if(menuObj != null){
        clearTimeout(hTmMenuHide);
        clearTimeout(hTmSubMenuHide[smNum]);
        
        hideMenuById(parentNum, true, true);
        positioningMenu(smNum, relateToParentX, relateToParentY, deltaX, deltaY, directionX, directionY);
        document.getElementById("sub_menu_" + smNum).style.display = 'block';
        openedMenusStack.push(smNum);
    }
}

function hideMenu(smNum){
    var menuObj = document.getElementById("sub_menu_" + smNum);
    if(menuObj != null){
        menuObj.style.display = 'none';
        swapImage(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();
    
    // Switch to next block if you have some problems with div hiding caused special custom processing
    /*oDivs = document.getElementsByTagName("DIV");
    for(i = 0; i < oDivs.length; i++){
        if(oDivs[i].id.substr(0, 9) == "sub_menu_"){
            hideMenu(oDivs[i].id.substr(9));
        }
    }*/
} 

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 ck(num,state){
}
function smclick(){
    hideMenuAll();
}

/* IMAGE PROCESSOR */

var prevImgSrc = Array();
// imgOver is not null = over, null = out
function swapImage(smNum, imgOver){
    if(document.images['main_menu_img_'+smNum] && imgOver != ''){
        prevImgSrc[smNum] = document.images['main_menu_img_'+smNum].src;
        document.images['main_menu_img_'+smNum].src = imgOver;
    }else if((typeof(imgOver) == 'undefined' || imgOver == '') && document.images['main_menu_img_'+smNum] && prevImgSrc[smNum] != null && document.images){
        document.images['main_menu_img_'+smNum].src = prevImgSrc[smNum];
    }
}
