function myMenu(id) {
    this.id = id;
    this.type = "array";
    this.deffirstTDClass = "menutd";
    this.deffirstAClass = "menua";
    this.defTDClass = "tdclass";
    this.defAClass = "menuaa";
    this.divClass = "menudiv";
    this.defTarget = "_self";
    this.equalWidth = false;
    this.table = null;
    this.OP = false;
    this.FF = false;
    this.IE = false;
    this.opened = null;
    this.timeOut = 1000;
    
    this.browserType = function() {
        if (navigator.userAgent.toLowerCase().indexOf("opera") != -1){
            this.OP = true;
        }
        if (navigator.userAgent.toLowerCase().indexOf("msie") != -1){
            this.IE = true;
        }
        if (navigator.userAgent.toLowerCase().indexOf("firefox") != -1){
            this.FF = true;
        }
    }
    this.setClass = function(elem,classname) {
        if (this.FF){
            elem.setAttribute("class",classname);
        } else if (this.IE) {
            elem.className = classname;
        } else if (this.OP) {
            elem.className = classname;
        }
    }
    this.getMenuItems = function(items) {
        switch (this.type) {
            case "XML":
            break;
            case "array":
                if (items == null) {
                    alert("Menu items are not available!");
                    return false;
                } else {
                    this.menuItems = items;
                    this.getMenuItemsfromArray();
                }
            break;
        }
    }
    this.getMenuItemsfromXML = function (params) {
    }
    this.getMenuItemsfromArray = function () {
        for (i in this.menuItems) {
            if (i < this.menuItems.length) {
                this.menuItems[i] = this.checkItem(this.menuItems[i],true);
            }
        }
    }
    this.checkItem = function (item,first) {
        if ((item[3] == null)||(item[3] == "")){
            if (first != null) {
                item[3] = this.deffirstTDClass;
            } else {
                item[3] = this.defTDClass;
            }
        }
        if ((item[4] == null)||(item[4] == "")){
            if (first != null) {
                item[4] = this.deffirstAClass;
            } else {
                item[4] = this.defAClass;
            }
        }
        if ((item[5] == null)||(item[5] == "")){
            item[5] = this.defTarget;
        }
        if (item[1] == "") {
            item[1] = "#";
            item[5] = "_self";
        }
        if ((item[2] != null) && (is_array(item[2]))) {
            for (i = 0;i < item[2].length;i++) {
                item[2][i] = this.checkItem(item[2][i]);
            }
        } else {
            item[2] = "";
        }
        return item;
    }
    this.createTable = function () {
        var table = document.createElement("table");
        table.setAttribute("cellpadding",0);
        table.setAttribute("cellspacing",0);
        table.setAttribute("border",0);
        table.setAttribute("style","width:100%;");
        var tbody = document.createElement("tbody");
        var tr = document.createElement("tr");
        for (i in this.menuItems) {
        if (i < this.menuItems.length) {
            var item = this.menuItems[i];
            var td = document.createElement("td");
            this.setClass(td,item[3]);
            var menuPoint = document.createElement("a");
            this.setClass(menuPoint,item[4]);
            menuPoint.setAttribute("href",item[1]);
            menuPoint.setAttribute("target",item[5]);
            menuPoint.style.display = "block";
            menuPoint.style.width = "100%";
            menuPoint.setAttribute("style","display:block;width:100%;height:100%;");
            menuPoint.innerHTML = "&nbsp;" + item[0] + "&nbsp;";
            menuPoint.onmouseover = new Function (this.id + ".openFirst(" + i + ");");
            menuPoint.onmouseout = new Function (this.id + ".time=setTimeout('" + this.id + ".closeFirst()'," + this.timeOut +");");
            this.setClass(menuPoint,item[4]);
            td.appendChild(menuPoint);
            tr.appendChild(td);
        }
        }
        tbody.appendChild(tr);
        table.appendChild(tbody);
        this.table = table;
    }
    this.openFirst = function (id) {
        if (document.getElementById) {
            var t = document.getElementsByTagName("div");
            for (i = 0;i < t.length;i++){
                //alert(t[i].id + ' -> ' + t[i].innerHTML);
            }
        }
        if (this.opened == id) {
            clearTimeout(this.time);
        } else if (this.opened != null){
            this.closeFirst();
        }
        this.opened = id;
        if (is_array(this.menuItems[id][2])) {
            var x = document.getElementById(this.id + "_firstSubmenu_" + id);
            if (x) {
                x.style.visibility='visible';
            }
        }
    }
    this.closeFirst = function () {
        clearTimeout(this.time);
        if ((this.opened != null)&&(this.opened != 'undefined')&&(is_array(this.menuItems[this.opened][2]))){
            var x = document.getElementById(this.id + "_firstSubmenu_" + this.opened);
            if (x) {
                x.style.visibility='hidden';
            }
            this.opened = null;
        }
    }
    this.formatTable = function () {
        if (this.equalWidth) {
            var w = 0;
            var tds = document.getElementById(this.id + "_contentdiv").childNodes[0].getElementsByTagName("td");
            for (i = 0;i < tds.length;i++){
                if (w < tds[i].offsetWidth) {
                    w = tds[i].offsetWidth;
                }
            }
            for (i = 0;i < tds.length;i++){
                tds[i].style.width = w + 'px';
                tds[i].style.textAlign = 'center';
            }
        } else {
            document.getElementById(this.id + "_contentdiv").childNodes[0].style.width = "100%";
        }
        document.getElementById(this.id + "_contentdiv").style.visibility = 'visible';
    }
    this.createFirstSubs = function () {
        var t = document.getElementById(this.id + "_contentdiv").childNodes[0];
        var tds = t.getElementsByTagName("a");
        for (i = 0;i < tds.length;i++) {
            if ((this.menuItems[i][2] != null)&&(is_array(this.menuItems[i][2]))) {
                var pos = findPos(tds[i]);
                pos[1] += tds[i].parentNode.offsetHeight;
                var table = document.createElement("table");
                table.setAttribute("border",0);
                table.setAttribute("cellpadding",0);
                table.setAttribute("cellspacing",0);
                var tbody = document.createElement("tbody");
                for (j = 0;j < this.menuItems[i][2].length;j++) {
                    var tr = document.createElement("tr");
                    var td = document.createElement("td");
                    this.setClass(td,this.menuItems[i][2][j][3]);
                    td.setAttribute("style","white-space:nowrap;");
                    td.setAttribute("nowrap","nowrap");
                    var menuPoint = document.createElement("a");
                    this.setClass(menuPoint,this.menuItems[i][2][j][4]);
                    menuPoint.setAttribute("href",this.menuItems[i][2][j][1]);
                    menuPoint.setAttribute("target",this.menuItems[i][2][j][5]);
                    menuPoint.innerHTML = "&nbsp;" + this.menuItems[i][2][j][0] + "&nbsp;";
                    menuPoint.style.display = "block";
                    menuPoint.style.width = "100%";
                    menuPoint.setAttribute("style","display:block;width:100%;height:100%;");
                    td.appendChild(menuPoint);
                    tr.appendChild(td);
                    tbody.appendChild(tr);
                }
                table.appendChild(tbody);
                var div = document.createElement("div");
                this.setClass(div,this.divClass);
                div.setAttribute("id",this.id + "_firstSubmenu_" + i);
                div.id = this.id + "_firstSubmenu_" + i;
                div.name = this.id + "_firstSubmenu_" + i;
                div.appendChild(table);
                div.style.position = "absolute";
                div.style.top = pos[1] + "px";
                div.style.left = pos[0] + "px";
                if (this.OP) {
                div.style.visibility = "visible";
                } else {
                div.style.visibility = "hidden";
                }
                div.style.border = "0px";
                div.onmouseover = new Function ("if (" + this.id + ".time) {clearTimeout(" + this.id + ".time);}");
                div.onmouseout = new Function (this.id + ".time=setTimeout('" + this.id + ".closeFirst()'," + this.timeOut + ");");
                if (this.OP) {
                    document.appendChild(div);
                } else {
                    document.body.appendChild(div);
                }
                var max = 0;
                var tds1 = document.getElementById(this.id + "_firstSubmenu_" + i).childNodes[0].getElementsByTagName("td");
                for (j = 0;j < tds1.length;j++){
                    if (tds1[j].offsetWidth > max) {
                        max = tds1[j].offsetWidth;
                    }
                }
                if (max < tds[i].offsetWidth) {
                    document.getElementById(this.id + "_firstSubmenu_" + i).childNodes[0].style.width = max + "px";
                }
            }
        }
    }
    this.createSubs = function (item) {
    }
    this.show = function(params) {
        document.write("<div id='" + this.id + "_contentdiv' style='border:0px solid red;visibility:hidden'></div>");
        this.browserType();
        this.getMenuItems(params);
        this.createTable();
        document.getElementById(this.id + "_contentdiv").appendChild(this.table);
        if (window.onload){
            var ol = window.onload.toString();
            var pattern = new RegExp("\{([^\}]*\)}");
            ol = ol.match(pattern)[1] + ' ' + this.id + ".formatTable();\n" + this.id + ".createFirstSubs();\n";
        } else {
            var ol = this.id + ".formatTable();\n" + this.id + ".createFirstSubs();\n";
        }
        var func = function () {eval(ol);}
        window.onload  = func;
        //document.getElementById(this.id + "_contentdiv").style.visibility = 'visible';
    }
}

function is_array( mixed_var ) {
    var key = '';
    if (!mixed_var) {
        return false;
    }
    if (typeof mixed_var === 'object') {
        return true;
        if (mixed_var.hasOwnProperty) {
            for (key in mixed_var) {
                if (false === mixed_var.hasOwnProperty(key)) {
                    return false;
                }
            }
        }
        return true;
    }
    return false;
}
function print_r(theObj) {
    if(theObj.constructor == Array || theObj.constructor == Object){
        document.write("<ul>")
        for(var p in theObj){
            if(theObj[p].constructor == Array || theObj[p].constructor == Object){
                document.write("<li>["+p+"] => "+typeof(theObj)+"</li>");
                document.write("<ul>")
                print_r(theObj[p]);
                document.write("</ul>")
            } else {
                document.write("<li>["+p+"] => "+theObj[p]+"</li>");
            }
        }
        document.write("</ul>")
    }
}
function findPos(obj) {
    var curleft = curtop = 0;
    if (obj.offsetParent) {
        do {
            curleft += obj.offsetLeft;
            curtop += obj.offsetTop;
            } while (obj = obj.offsetParent);
        return [curleft,curtop];
    }
}
