function toggle_section(w) {
    var container = w.parentNode.parentNode;
    // seek the menu-section-inner node
    for(var i=0; i<container.childNodes.length; i++) {
        var node = container.childNodes[container.childNodes.length-i-1];
        if(node.className=='menu_section_inner')
            break;
    }
    if(node.style.display!='none') {
        // if the node is not hidden, hide it and append a transparent shadow above the title
        node.style.display = 'none';
        var shadow = document.createElement('div');
        shadow.className = 'menu_section_title_shadow_alpha';
        shadow.innerHTML = '';
        container.appendChild(shadow);
        w.parentNode.style.backgroundImage = 'url(images/menu_section_title_plus.gif)';
    } else {
        // otherwise, show it and remove the transparent shadow
        node.style.display = 'block';
        container.removeChild(container.childNodes[container.childNodes.length-1]);
        w.parentNode.style.backgroundImage = 'url(images/menu_section_title_minus.gif)';
    }
    return false;
}

function find_position(obj) {
    // get the absolute position of an element
    var curleft = curtop = 0;
    if (obj.offsetParent) {
        curleft = obj.offsetLeft
        curtop = obj.offsetTop
        while (obj = obj.offsetParent) {
            curleft += obj.offsetLeft
            curtop += obj.offsetTop
        }
    }
    return [curleft,curtop];
}

var inside = 0;

function show_submenu(w) {
    inside = true;
    // put a ul on the position (128, 0) of his parent li element
    var pos = find_position(w);
    var li = w.parentNode;
    // cleaning other opened panels
    close_child_submenus(li);
    // opening the item's child
    var ul_list = li.getElementsByTagName('ul');
    if(ul_list.length>0) {
        ul = ul_list[0];
        if(li.className=='l0') {
            ul.style.left = (pos[0] + 138) + 'px';
            ul.style.top = pos[1] + 'px';
            ul.style.display = 'block';
        } else {
            /* on the second level of the menu the ul uses the parent ul as
             * reference, this way, we can't use the element position, we hack
             * it using the default with for ul. */
            ul.style.left = '158px';
            ul.style.top = li.offsetTop + 'px';
            ul.style.display = 'block';
        }
    }
}

function hide_submenu(w) {

    inside = false;
    var li = w.parentNode;
    timerID = setTimeout(function() { if(!inside) close_all_submenus() }, 750);

}

function close_child_submenus(li) {
    var ul_container = li.parentNode;
    var opened_panels = ul_container.getElementsByTagName('ul');
    for(var i=0; i<opened_panels.length; i++)
        opened_panels[i].style.display = 'none';
}

function close_all_submenus() {
    var menu = document.getElementById('menu');
    opened_panels = menu.getElementsByTagName('ul');
    for(var i=0; i<opened_panels.length; i++)
        opened_panels[opened_panels.length-i-1].style.display = 'none';
}


function check_height(stage) {
    if(typeof(stage)=='undefined')
        stage = 1;
    var menu = document.getElementById('menu_wrapper');
    var content = document.getElementById('content');
    //alert(menu.offsetHeight + ' - ' + content.offsetHeight)
    if (menu.offsetHeight > content.offsetHeight + 300) {
        var brands = document.getElementById('brands_menu');
        var bestsellers = document.getElementById('bestsellers_menu');
        if(stage==1) {
            toggle_section(bestsellers);
            return check_height(2);
        } else {
            toggle_section(brands);
        }
    }
    return false;
}

