
var timer_id = 0;
var moused_out_div = 0;
var old_content;


var sub_menu;

    var menuLinks;

            
var sub_menu_parent;

var jsmenu_root;


function menu_init()
{
    old_content = jQuery("#old_content");
    sub_menu = jQuery("#submenu");
    menuLinks = jQuery(".jsmenu");
    sub_menu_parent = jQuery("#submenu_parent");
    jsmenu_root = jQuery("#jsmenu_root");

    sub_menu.hover(postponeSwap, timeOut_subMenu);
    menuLinks.hover(swapMenu,timeOut);

    if (jsmenu_root)   jsmenu_root.mouseover(swapMenu_root);
}



jQuery(document).ready(menu_init);



function swapMenu_root(){

    var target = jQuery(this)
    clearTimeout(timer_id);


    menuLinks.each( function( ) {
    var dom_element = this;
            unselect(dom_element);
    } );


    sub_menu.html(old_content.html());
}



function select(element)
{
    element.parentNode.style.backgroundColor=community_lightest;
    jQuery('#submenu').addClass("highlight_submenu");
    //element.parentNode.style.borderColor = sub_menu.getAttribute("backgroundColor") + " " + sub_menu.getAttribute("backgroundColor");

}
function unselect(element)
{
    element.parentNode.style.backgroundColor="#ffffff";
    jQuery('#submenu').removeClass("highlight_submenu");

}

function swapMenu(){

    var target = this;
    clearTimeout(timer_id);

    if (moused_out_div) {
        unselect(moused_out_div.firstChild);
    }
    select(target);
    var content = jQuery("#"+target.innerHTML);
    if (content.get(0))  sub_menu.html(content.html());
    else sub_menu.html("<ul id='submenus'><li></li></ul>");
    moused_out_div = target.parentNode;
}

function postponeSwap(){
    clearTimeout(timer_id);
}



function timeOut(){
 
    var current_div = 0;
    if (moused_out_div) current_div = moused_out_div;
    timer_id = setTimeout(switchBack,500);

    function switchBack() {
        timer_id = 0;
      unselect(current_div.firstChild);

        if (current_div == moused_out_div) {
             if (old_content) sub_menu.html(old_content.html());
             else sub_menu.html("<ul id='submenus'><li></li></ul>");
        }
    }

}

function timeOut_subMenu() {

    var current_div = 0;
    if (moused_out_div) current_div = moused_out_div;
    timer_id = setTimeout(switchBack,500);

    function switchBack() {
        timer_id = 0;
        if (current_div.firstChild) {
            unselect(current_div.firstChild);
        }
        if (current_div == moused_out_div) {
             if (old_content) sub_menu.html(old_content.html());
             else sub_menu.html("<ul id='submenus'><li></li></ul>");
        }
    }
}

