/**
 * This scripts provides three jobs related to dynamic dropdown
 * menus.  It drives a dynamic navigation of dropdown menus.
 * Each menu is opened when the user
 * drive his mouse on a related label, say the menu
 * opener, say a link with an "onmouseover" event.
 *
 * By opening a menu, an eventually precendtyl opened one
 * have to be close.
 *
 * The menu is closed if the user just drive the mouse on
 * the opener element, then out.
 *
 */

// element id sent in a time out function
var tid;

// number of menu
var numOfMenus = 4;

// general function that returns an element object
function $(id) {
    return document.getElementById(id);
}

function common_close(n) {

    $(n).style.visibility = 'hidden';
    $(n).style.display = 'none';

}

function common_open(n) {

    $(n).style.visibility='visible';
    $(n).style.display='block';

}

function common_hide(n, timeout) { 
    
    tid = setTimeout("common_close('" + n + "')", timeout);

}


function common_show(n) {
 
    clearTimeout(tid);
    common_open(n);

}

function common_drive(n) {

    var state = $(n).style.visibility;

    for(i = 1; i <= numOfMenus; i++) {

        if((state == 'visible') && (i != n)) {

            common_close(i, 1000);

        }

    }

}

function common_keep(n) {

    var state = $(n).style.visibility;
    if (state == "visible") {

        clearTimeout(tid);

    }

}


function common_closeAll() {

    var state = "";
    for(i = 1; i <= numOfMenus; i++) {

        common_close(i, 20);

    }

}


function common_driveElementDisplay(cookieName, elemId, n) {

    var operation = "";
    var state = $(elemId).style.visibility;

    if (state == "hidden") {
        
        $('cat_' + n + '_drive').style.backgroundImage=
            'url(../tpl/img/less.png)';
        common_open(elemId);
        operation = "open";

    } else  {

        $('cat_' + n + '_drive').style.backgroundImage=
            'url(../tpl/img/more.png)';
        common_close(elemId);
        operation = "close";
    }

    common_updateViewStateCookie(cookieName, operation, n);

}
 
function common_updateViewStateCookie(cookieName, operation, n) {

    oldState  = (cookies_getCookieByName(cookieName) ?
                 cookies_getCookieByName(cookieName) * 1 : 0);

    elemValue = Math.pow(2, (n)) * (operation == "open" ? 1 : -1);

    newState = (oldState + elemValue) - 0;

    cookies_storeCookie(cookieName, newState, "/");

}



function common_highlight_option(n, FWROOT) {

    var imgFolder = FWROOT + "tpl/img/";
    var opt       = $("option_" + n);
    var aTagOpt   = $("aTagOpt_" + n);

    opt.style.cursor='pointer';
    opt.style.backgroundImage='url(' + imgFolder + 'bgOptionOn.png)';
    opt.style.backgroundColor='#333';

    // aTagOpt.style.color = '#f66';
    aTagOpt.style.color = '#fff';

}

function common_reset_option(n) {

    var opt       = $("option_" + n);
    var aTagOpt   = $("aTagOpt_" + n);

    opt.style.cursor = '';
    opt.style.backgroundImage = '';
    opt.style.backgroundColor='';
    
    aTagOpt.style.color = '';

}
