var hideMenuTimer;
var showMenuTimer;
var my_parent;
var lastOpenFromNavBar=false;
var lastEl;
var menuArr=new Array;
var mainTagName = 'LI';

function findPosX(obj)
{
    var curleft = 0;
    if (obj.tagName=='A')  curleft=-obj.offsetWidth;
    if (obj.offsetParent)
    {
        while (obj.offsetParent)
        {
            curleft += obj.offsetLeft;
            obj = obj.offsetParent;
        }
    }
    else if (obj.x)
    curleft += obj.x;
    return curleft;
}

function findPosY(obj)
{
    var curtop = 1;
    if (obj.tagName==mainTagName) curtop=-1;
    else if (obj.tagName=='A') curtop=obj.offsetHeight;
    
    if (obj.offsetParent)
    {
        while (obj.offsetParent)
        {
            curtop += parseInt(obj.offsetTop);
            obj = obj.offsetParent;
        }
    }
    else if (obj.y) curtop += obj.y;
    return curtop;
}

var productMenu = function()
{
    clearTimeout(showMenuTimer);

    if (!my_parent.id) return;

    if (my_parent.tagName=='A')
    {
        hideMenu();
        lastOpenFromNavBar=true;
    }
    if (menuArr[my_parent.id]) return;
    this.menu=null;
    this.my_parent=my_parent;
    this.id=my_parent.id;
    this.next=null;

    if (my_parent.tagName=='DIV')
    {
        if (my_parent.className == 'top') my_parent.className="top sel";
        else if (my_parent.className == 'active top') my_parent.className="active top";
        else if (my_parent.className != "active") my_parent.className="sel";
    }

    if (productionArr[this.id]) listArr=productionArr;
    else listArr=productionDivArr;

    this.level=1;
    for (mn in menuArr)
    {
        if (productionDivArr[menuArr[mn].id])
        {
            if (productionDivArr[menuArr[mn].id][this.id])
            {
                menuArr[mn].next=this;
                this.level+=menuArr[mn].level;
            }
            else
            {
                if (!menuArr[mn].next) 	menuArr[mn].close();
            }
        }
        else menuArr[mn].close();
    }
    for (mn in menuArr) if (menuArr[mn].level>=this.level) menuArr[mn].close();

    this.menu=document.createElement('div');
    this.menu.className="floatmenu level"+levelsArr[this.id];
    if (my_parent.nodeName == mainTagName) {
        this.menu.style.left=findPosX(my_parent) + "px";
        this.menu.style.top= 116 + "px";
        if (my_parent.className == 'active hover') this.menu.className += ' active';  
    }
    else {
        this.menu.style.left=findPosX(my_parent) + my_parent.offsetWidth - 15 + "px";
        this.menu.style.top=findPosY(my_parent) + "px";    
    }

    if (productionDivArr[this.id])
    {
        div = document.createElement('DIV');
        div.className = 'tb';
        this.menu.appendChild(div);

        div2 = document.createElement('DIV');
        div2.className = 'mainBg';
        this.menu.appendChild(div2);

        var ii = 0;
        for (arr in productionDivArr[this.id])
        {
            div = document.createElement('DIV');
            div.innerHTML=""+productionDivArr[this.id][arr];
            div.id=arr;
            if (activesArr[div.id] == 1) {
                div.className = 'active';
                if (ii == 0) div.className += ' top';
            }
            else {
                if (ii == 0) div.className += ' top';
            }
            div.onmousemove=new Function("onMouseMove(this)");
            div.onmouseout=new Function("onMenuOut(this)");
            if (productionDivArr[div.id] || productionArr[div.id]) {
                // есть подменю
                div.onclick=new Function("onFloatMenuClick(this)");
            } else {
                div.onclick=new Function("redirect(this)");
            }
            div.onmouseover=new Function("onFloatMenuOver(this)");
            div2.appendChild(div);
            ii++;
        }
        div = document.createElement('DIV');
        div.className = 'bb';
        this.menu.appendChild(div);
    }
    if (productionArr[this.id])
    {
        for (arr in productionArr[this.id])
        {
            div=document.createElement('DIV');
            div.innerHTML=""+productionArr[this.id][arr];
            div.id=arr;
            div.onmousemove=new Function("onMouseMove(this)");
            div.onmouseout=new Function("onMenuOut(this)");
            div.onclick=new Function("onMenuClick(this)");
            div.onmouseover=new Function("clearTimeout(showMenuTimer);");
            this.menu.appendChild(div);
        }
    }
    document.body.appendChild(this.menu);
//    if ((document.body.clientHeight+document.body.scrollTop)<(parseInt(this.menu.style.top)+this.menu.offsetHeight))
//    {
//        this.menu.style.top=(document.body.clientHeight+document.body.scrollTop-this.menu.offsetHeight);
//        if (parseInt(this.menu.style.top)<document.body.scrollTop) this.menu.style.top=document.body.scrollTop;
//    }
//    if ((document.body.clientWidth+document.body.scrollLeft)<(parseInt(this.menu.style.left)+this.menu.offsetWidth))
//    {
//        this.menu.style.left=parseInt(this.menu.style.left)-350;
//        if (parseInt(this.menu.style.left)<document.body.scrollLeft) this.menu.style.left=document.body.scrollLeft;
//    }
    menuArr[this.id]=this;
}

productMenu.prototype.close=function()
{
    if (this.my_parent.tagName=='DIV')
    {
        if (this.my_parent.className == 'top sel') this.my_parent.className = 'top';
        if (this.my_parent.className != 'active' && this.my_parent.className != 'top' && this.my_parent.className != 'active top') this.my_parent.className="";
    }
    if (this.my_parent.tagName==mainTagName)
    {
        var topmenu=document.getElementById("topMenu");
        for (var i=0; i < topmenu.childNodes.length; i++) {
            if (topmenu.childNodes[i].nodeName == 'LI' && topmenu.childNodes[i].id != "") {
                $('#'+topmenu.childNodes[i].id).removeClass('hover');
            }
        }
    }

    document.body.removeChild(this.menu);
    //  this.menu.outerHTML='';
    delete menuArr[this.id];
}

function onMenuClick(el)
{
    hideMenu();
    window.location=linkUrl+(el.id.replace(/[^0-9\/]/g, ""))+"/";
}

function onMouseMove()
{
    clearTimeout(hideMenuTimer);
}

function onMenuOut(el)
{
    clearTimeout(showMenuTimer);
    clearTimeout(hideMenuTimer);
    hideMenuTimer=setTimeout("hideMenu()",3000);
}

function hideMenu()
{
    clearTimeout(showMenuTimer);
    clearTimeout(hideMenuTimer);
    for (mn in menuArr)  menuArr[mn].close();
}

function onFloatMenuClick(el)
{
    clearTimeout(showMenuTimer);
    my_parent=el;
    new productMenu();
}

function onFloatMenuOver(el)
{
    clearTimeout(showMenuTimer);
    my_parent=el;
    showMenuTimer=setTimeout("new productMenu()",700);
}

function onLeftMenuClick(el)
{
    if (lastOpenFromNavBar)
    {
        hideMenu();
        lastOpenFromNavBar=false;
    }
    my_parent=el;
    new productMenu();
}
function bodyMouseDown(ev)
{
    ev=ev?ev:window.event;
    el=ev.srcElement?ev.srcElement:ev.target;
    if (el.tagName!='DIV') hideMenu();
}
function redirect(el) {
    location.href = hrefsArr[el.id];
}
document.body.onmousedown=bodyMouseDown;