﻿function showSubmenu(menuId, submenuDivId, targetUrl)
{
    if (visibleSubmenuId != '')
    {
        $get(visibleSubmenuId).className = 'Hidden';
    }
    if (visibleMenuId != '' && $get(visibleMenuId).className != 'MmLnkSelectedCurrent MmLnk')
    {
        $get(visibleMenuId).className = 'MmLnk';
    }
    
    var submenuDiv = $get(submenuDivId);
    var container = submenuDiv.parentNode;
    
    submenuDiv.className = 'Submenu';
    
    if ($get(menuId).className != 'MmLnkSelectedCurrent MmLnk')
    {
        $get(menuId).className = 'MmLnkSelected';
    }
    
    visibleMenuId = menuId;
    visibleSubmenuId = submenuDivId;
     
    var topEl;
    var el = container;
    var absLeft = 0;
    while (el)
    {
        topEl = el;
        absLeft = absLeft + el.offsetLeft;
        el = el.offsetParent;
    }
    
    //alert("Submenu width: " + submenuDiv.clientWidth + "\nleft: " + absLeft + "\nMax widht: " + topEl.clientWidth);
    
    var windowWith = topEl.clientWidth;
    var pageWith = 970;
    var leftPageMargin = (windowWith < 970)
        ? 0
        : (windowWith - 970) / 2;
        
    //submenuDiv.style["left"] = "-" + (absLeft - leftPageMargin) + "px";
        
    var overflow = absLeft + submenuDiv.clientWidth - (leftPageMargin + pageWith);
    
    if (overflow > 0)
    {
        submenuDiv.style["left"] = "-" + (overflow + 3) + "px";
    }
    else
    {
        submenuDiv.style["left"] = "0px";
    }
}