function push() {
  var sub = this.length;
    for (var i = 0; i < push.arguments.length; ++i) {
      this[sub] = push.arguments[i];
      sub++;
  }
}

if (!Array.prototype.push) Array.prototype.push = push;

function forAllIn(arr, f, arg) {
    var z = arr;
    for(i = 0; i < z.length; i++) {
        var el = z[i];
        f(el, arg);
    }
}


function select(name, f) {
    var rez = [];
    var to = 0;
    if(document.all) {
        z =  document.all.tags(name);
    } else if (document.getElementsByTagName) { 
        z = document.getElementsByTagName(name);
    } else {
        z = []; // for Netscape 4.xx does not work ;)
    }
    for(i = 0; i < z.length; i++) {
        var el = z[i];
        if (f(el)) rez.push(el);
    }
    return rez;
}

var hrefCached = null;

function controlMenu(href) {
    //Speeding up IE5 considerably in TECH REPORTS
    if (hrefCached == href) {
        return;
    }
    else {
        hrefCached = href;
    }

    if (href.href) {
        href = href.href;
    }
    menuName=pageof(href);
    suffix=suffixof(href);

    hideAllExcept(menuName, suffix);
}

function hideExcept(x, except) {
    var menuId = x.id;
    x.style.display = menuId.indexOf(except) == -1 ?  "none" : "block";
}
function hideMainExcept(x, except) {
    var menuId = x.id;
    x.style.backgroundColor = menuId.indexOf(except) == -1 ? "#0084c6" :  "#93a4b5";
}


var divs;
var tds;
var as;


function initSelections() {
    if (divs) {return;}
    divs = select("DIV", function (x) {return x.id.indexOf("SubMenu") != -1;});
    tds = select("TD", function (x) {return x.id.indexOf("MainMenu") != -1;});
    as = select("A", function (x) {return x.id.indexOf("SubMenuRef") != -1;});
}

function hideAllExcept(menuName, suffix) {
    initSelections();

    forAllIn(divs, hideExcept, menuName);
    forAllIn(tds, hideMainExcept, menuName);
    forAllIn(as, setUnderline, suffix);
}

function inspectStyle(elm){
    if (elm.style) {
        var str = "";
        for (var i in elm.style) {
            str += i + ": " + elm.style[i] + "; ";
        }
        alert(str);
    }
}

function afterlast(s,d) {
    i = s.lastIndexOf(d);
    if (-1 == i) {
        return s;
    }
    return s.substring(1 + i);
}

function upto(s,d) {
    i = s.indexOf(d);
    if (-1 == i) {
        return s;
    }
    return s.substring(0, i);
}

function pageof(s) {
    s = afterlast(s, "/");
    s = upto(s, "?");
    return s;
}

function currentPage() {
    return currentSuffix();
}

function suffixof(s) {
    s = afterlast(s, "/");
    return s;
}

function currentSuffix() {
    s = suffixof(location.href);
    if (s == "") return "index?lang=en";
    return s;
}

function setUnderline(x, sign) {
    x.style.textDecoration =  x.href.indexOf(sign) != -1 ? "underline" : "none";
}

var outMainMenu = 1;
var outSubMenu = 1;
var callCount = 0;
function resetMenu() {
    initSelections();

    callCount--;
    if (outMainMenu && outSubMenu && callCount == 0) {
        controlMenu(currentPage());
//        forAllIn(as, setUnderline, currentSuffix());
    }
}

function enterMain() {
   outMainMenu = 0;
}
function leaveMain() {
   outMainMenu = 1;
   callCount++;
   setTimeout("resetMenu();", 1500);
}
function enterSub() {
   outSubMenu = 0;
}
function leaveSub() {
   outSubMenu = 1;
   callCount++;
   setTimeout("resetMenu();", 1500);
}


