/**
 * This scripts provides three jobs related to dynamic dropdown
 * menus.
 *
 * At first 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.
 *
 * The second thing this script does is to define the width
 * of each dropdown menu. For that purpose we use the following
 * formula.
 *
 *    width of a menu = (clientWidth >= maxWidth ? fixedWidth :
 *                      clientwidth - (leftBlock + body padding) /4)
 * 
 * The third pretty job done here is to cut some long word by
 * germanspeakingclient ;)  
 *
 * A function is called when the menu width is smaller than the fixed
 * width. Then it walk through the entire menu structure to check if
 * some words have to be cut. Those words are defined in the wtc
 * array.
 *
 */

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

// number of menu
var numOfMenus = 4;


// words to cut
var wtc = new Array(
    "Mutterschafts"
  , "Versicherungs"
  , "Anschlussb"
);

// cut word after
var cwa = new Array();

cwa["Anschlussb"] = "Anschluss";

// cut type to word
var ctypew = new Array();

ctypew["Mutterschafts"] = 1;
ctypew["Versicherungs"] = 2;
ctypew["Anschlussb"]    = 3;


// general function that returns an element object
function $(id) {

    return document.getElementById(id);

}

// overwrite onload event
window.onload = function () {
    
    var substractToMenu1;
    var dynamicWidth = common_getDynamicMenuWidth();

    if (document.all) {

        substractToMenu1 = 5;

    } else {

        dynamicWidth -=  2;
        substractToMenu1 = 0;

    }

    $(1).style.width = (dynamicWidth - substractToMenu1) + "px";
    $(2).style.width = dynamicWidth + "px";
    $(3).style.width = dynamicWidth + "px";
    $(4).style.width = dynamicWidth + "px";

    if (cookies_getCookieByName("langId") == 2 && dynamicWidth < 193) {

       common_adjustNavigationLabels(dynamicWidth);

    }

}

function common_getDynamicMenuWidth() {

    return (document.body.clientWidth > 1015 ? 193 :
        (document.body.clientWidth -
        (241 + (document.body.clientWidth * 2 / 100))) / 4);

}

// adjusts Navigation label, say cut some long words.
// the function walk through the whole menu and check for
// word to cut.
function common_adjustNavigationLabels(dw) {
 
    var txt;
    var txtToRpl;
    var divElem;
    var cutType;

    if (dw > 180)
        cutType = 1;
    else if (dw > 145)
        cutType = 2;
    else 
        cutType = 3;
     
    for (i = 1; i <= numOfMenus; i++) {

         for (j = 0; j < $(i).childNodes.length; j++) {

             if ($(i).childNodes[j].nodeType == 1 &&
                 $(i).childNodes[j].tagName == "A") {

                 for(k = 0; k < $(i).childNodes[j].childNodes.length; k++) {

                     if ($(i).childNodes[j].childNodes[k].nodeType == 1) {

                         divElem = $(i).childNodes[j].childNodes[k];
                         txt     = divElem.firstChild.data;

                         for (w = 0; w <= wtc.length-1; w++) {

                              if (txt.indexOf(wtc[w]) != -1 &&
                                  cutType >= ctypew[wtc[w]]) {

                                  common_cutNavigationLabel(
                                      divElem
                                    , txt
                                    , (cwa[wtc[w]] != undefined ?
                                       cwa[wtc[w]] : wtc[w])
                                  );
                          
                             }

                         }

                     }

                 }

             }

         }

    }

}

function common_cutNavigationLabel(divElem, txt, txtToCut) {

   txt = txt.replace(txtToCut, txtToCut + "- ");
   divElem.firstChild.data = txt;

}

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, "/");

}

