/* 
 * this is a plain version that is compatible with IE 7
 * copyright DelArte 2005 - 2010
 * write a flexible menu structure
 * get data from xml source
 * layout based on css
 * and possible override from webapplication
 */


function cssmenu() {

    var that=this;

    cssmenu.prototype.write = function(elementid) {
        this.elementid = elementid;
        this.loaddata(null, norgid);
    }

    cssmenu.prototype.loaddata = function(callbackfunction, norgid) {

        this.loaddatacallback = callbackfunction || function () { };
        var request = new ajaxObject("menudata.jsv", this.process);
        request.update('or=' + norgid, 'POST', true);
    }

    cssmenu.prototype.process = function(resptxt, respstatus, respxml) {

        if (respstatus != 200) {
            alert("probleem bij het ophalen van het menu");
            return;
        }
        that.writemenu(respxml);
    }

    cssmenu.prototype.writemenu = function(xml) {

        var emenuspace = document.getElementById(this.elementid);

        //        var emn = emenuspace.appendDivChild("", "mn_menu_v2");
        var emn = emenuspace.appendChild(document.createElement("div"));
        emn.setAttribute("class", "mn_menu_v2");
        emn.setAttribute("className", "mn_menu_v2");

        //        var xmenu = xml.getFirstChildByTagName("menu");
        var xmenu;

        for ( var xitm, i = 0; ( xitm = xml.childNodes.item(i) ); i++ )
        {
            if ( xitm.nodeName == "menu")
            {
                xmenu = xitm;
                break;
            }
        }


        // var xitems = xmenu.getChildrenByTagName("item");
        var xitems = [];

        for ( var xitm, i = 0; ( xitm = xmenu.childNodes.item(i) ); i++ )
        {
            if ( xitm.nodeName == "item" )
                xitems.push(xitm);
        }


        for (var i = 0; i < xitems.length; i++) {
            this.writeitem(emn, xitems[i], true);
        }
    }

    cssmenu.prototype.writeitem = function(epar, xml, lfirst) {

        var eitem;
        if (lfirst) {
            // load the global style
            this.globalstyle = new mnstyle();
            this.globalstyle.readxml(xml);


            eitem = epar;

        } else {
            // set class
            var csubclass= xml.getAttribute("class");
            if (csubclass == undefined) {
                csubclass = "mn_item_v2";
            }

            //            eitem = epar.appendDivChild("", csubclass);
            eitem = epar.appendChild(document.createElement("div"));
            eitem.setAttribute("class", csubclass);
            eitem.setAttribute("className", csubclass);

            // set hyperlink
            // var url = xml.getChildText("url");
            var url = "";

            for ( var xitm, i = 0; ( xitm = xml.childNodes.item(i) ); i++ )
            {
                if ( xitm.nodeName == "url" )
                {
                    xnode = xitm;
                    break;
                }
            }

            if (xnode != undefined) {
                //url = xnode.getText();
                if (xnode.hasChildNodes()) {
                    url = xnode.childNodes[0].nodeValue;
                }

            }



            if (url != undefined) {
                if (url.length > 0) {
                    // url = url.jw_addUrlParam("or", norgid);

                    var csep = "?";
                    // set right separator
                    if (url.indexOf(csep) != -1) csep = "&";

                    // only add if not yet there
                    if (url.indexOf("or" + "=") == -1) {
                        url += csep + "or" + "=" + norgid;
                    }
                    
                    //   eitem.setAttribute("onclick", "window.location.href='" + url + "';");
                    // compatible with IE 7 and seems to run in real browsers also
                    eitem.onclick = function() {
                        window.location.href = url;
                    };
                }
            }

            // set name
            // var name = xml.getChildText("name");
            var name = "";
            // var xnode = xml.getFirstChildByTagName("name");
            var xnode;

            for ( var xitm, i = 0; ( xitm = xml.childNodes.item(i) ); i++ )
            {
                if ( xitm.nodeName == "name" )
                {
                    xnode = xitm;
                    break;
                }
            }



            if (xnode != undefined) {
                // name = xnode.getText();
                if (xnode.hasChildNodes()) {
                    name = xnode.childNodes[0].nodeValue;
                }


            }


            if (name.length > 0) eitem.innerHTML = name;

            // prepare alternate styles
            var sty = new mnstyle();
            sty.initglobal(this.globalstyle);
            sty.readxml(xml);

            // set altenate css class
            if (sty.getcssclass() != undefined) {
                // eitem.setClassAttribute(sty.getcssclass());
                eitem.setAttribute("class", sty.getcssclass());
                eitem.setAttribute("className", sty.getcssclass());
            }

            // set style
            var cstyle = sty.writetostyle();
            if (cstyle.length > 0) {
                //   eitem.setAttribute("style", cstyle);
                if (sty.width != undefined) eitem.style.width       = sty.width;
                if (sty.height != undefined) eitem.style.height     = sty.height;

                if (sty.bi != undefined) eitem.style.border         = sty.bi + "px solid;";
                if (sty.bic != undefined) eitem.style.borderColor   = sty.bic;
                if (sty.pb != undefined) eitem.style.paddingBottom  = sty.pb;
                if (sty.pl != undefined) eitem.style.paddingLeft    = sty.pl;
                if (sty.pr != undefined) eitem.style.paddingRight   = sty.pr;
                if (sty.pt != undefined) eitem.style.paddingTop     = sty.pt;
                if (sty.uc != undefined) eitem.style.color          = sty.uc;
                if (sty.ucbg != undefined) eitem.style.backgroundColor = sty.ucbg;
            }

            var cmsover = sty.writemouseover();
            var cmsout = sty.writemouseout();            
        }

        // if has submenu
        //   var xitems = xml.getChildrenByTagName("item");

        var xitems = [];

        for ( var xitm, i = 0; ( xitm = xml.childNodes.item(i) ); i++ )
        {
            if ( xitm.nodeName == "item" )
                xitems.push(xitm);
        }


        if (xitems.length > 0) {

            var esm = eitem;
            if (!lfirst) {

                // actions to show/ hide submenu
                // eitem.setAttribute("onmouseover", "switchsubmenu(this, true);" + cmsover);
                // eitem.setAttribute("onmouseout", "switchsubmenu(this, false);" + cmsout);


                // IE 7 compatible
                eitem.onmouseover = function() {
                    switchsubmenu(this, true);
                    if (sty.sc != undefined) this.style.color= sty.sc;
                    if (sty.scbg != undefined)  this.style.backgroundColor = sty.scbg;
                };
                eitem.onmouseout = function() {
                    switchsubmenu(this, false);
                    if (sty.uc != undefined) this.style.color= sty.uc;
                    if (sty.ucbg != undefined) this.style.backgroundColor=sty.ucbg;
                };

                // append submenu
                csubclass= xml.getAttribute("class");
                if (csubclass == undefined) {
                    csubclass = "mn_sub_v2";
                }
                // esm = eitem.appendDivChild("", csubclass);
                esm = eitem.appendChild(document.createElement("div"));
                esm.setAttribute("class", csubclass);
                esm.setAttribute("className", csubclass);


                // esm.setAttribute("onmouseout", "this.style.display='none';");
                esm.onmouseout = function() {
                    this.style.display='none';
                };
            }
            // write items in submenu
            for (var i = 0; i < xitems.length; i++) {
                
                this.writeitem(esm, xitems[i], false);
            }
        } else {
            // if (cmsover.length > 0) eitem.setAttribute("onmouseover", cmsover);
            // if (cmsout.length > 0) eitem.setAttribute("onmouseout", cmsout);

            // IE 7 compatible
            eitem.onmouseover = function() {
                if (sty.sc != undefined) this.style.color= sty.sc;
                if (sty.scbg != undefined)  this.style.backgroundColor = sty.scbg;
            }

            eitem.onmouseout = function() {
                if (sty.uc != undefined) this.style.color= sty.uc;
                if (sty.ucbg != undefined) this.style.backgroundColor=sty.ucbg;
            }

        }        
    }
}

function mnstyle() {

    mnstyle.prototype.readxml = function (xml) {

        // var csettings = xml.getChildText("settings");
        var csettings = "";
        //        var xnode = this.getFirstChildByTagName("settings");
        var xnode;

        for ( var xitm, i = 0; ( xitm = xml.childNodes.item(i) ); i++ )
        {
            if ( xitm.nodeName == "settings" )
            {
                xnode = xitm;
                break;
            }
        }
        if (xnode != undefined) {
            // csettings = xnode.getText();

            if (xnode.hasChildNodes()) {
                csettings = xnode.childNodes[0].nodeValue;
            }
            

        }
        if (csettings.length > 0) this.readsettings(csettings);
    }

    mnstyle.prototype.initglobal = function(gst) {

        if (gst.cssclass  != undefined) this.cssclass = gst.cssclass;
        if (gst.width     != undefined) this.width = gst.width;
        if (gst.height    != undefined) this.height = gst.height;
        if (gst.bi        != undefined) this.bi = gst.bi;       // inside, border of item
        if (gst.bic       != undefined) this.bic = gst.bic;
        if (gst.bo        != undefined) this.bo = gst.bo;       // outside, border of (sub) menu
        if (gst.boc       != undefined) this.boc = gst.boc;
        if (gst.cs        != undefined) this.cs = gst.cs;
        if (gst.cson      != undefined) this.cson = gst.cson;
        if (gst.cssc      != undefined) this.cssc = gst.cssc;
        if (gst.pb        != undefined) this.pb = gst.pb;       // padding
        if (gst.pl        != undefined) this.pl = gst.pl;
        if (gst.pr        != undefined) this.pr = gst.pr;
        if (gst.pt        != undefined) this.pt = gst.pt;
        if (gst.sc        != undefined) this.sc = gst.sc;
        if (gst.scbg      != undefined) this.scbg = gst.scbg;   // selected background color
        if (gst.uc        != undefined) this.uc = gst.uc;       // unselected font color
        if (gst.ucbg      != undefined) this.ucbg = gst.ucbg;   // unselected background color
        if (gst.sl        != undefined) this.sl = gst.sl;       // shift of submenu
        if (gst.st        != undefined) this.st = gst.st;
        if (gst.ac        != undefined) this.ac = gst.ac;
    }


    mnstyle.prototype.writemouseover = function() {
        var code="";
        if (this.sc != undefined) code += "this.style.color='" + this.sc + "';";
        if (this.scbg != undefined) code += "this.style.backgroundColor='" + this.scbg + "';";
        return(code);
    }

    mnstyle.prototype.writesubmenustyle = function() {
        var cstyle = "";
        if (this.bo  != undefined) cstyle += "border:" + this.bo + "px solid;";
        if (this.boc != undefined) cstyle += "border-color:" + this.boc + ";";
        if (this.sl  != undefined && this.width != undefined) cstyle += "left:" + (this.width + this.sl)  + ";";
        if (this.st  != undefined && this.height != undefined) cstyle += "top:" + (-1 * (this.height - this.st)) + ";";
        return(cstyle);
    }

    mnstyle.prototype.writemouseout = function() {
        var code="";
        if (this.uc != undefined) code += "this.style.color='" + this.uc + "';";
        if (this.ucbg != undefined) code += "this.style.backgroundColor='" + this.ucbg + "';";
        return(code);
    }


    mnstyle.prototype.getcssclass = function() {
        return(this.cssclass);
    }

    mnstyle.prototype.writetostyle = function() {

        var cstyle = "";

        //   if (this.cssclass  != undefined) cstyle += ":" + this.cssclass + ";";
        if (this.width != undefined) cstyle += "width:" + this.width + ";";
        if (this.height != undefined) cstyle += "height:" + this.height + ";";
        if (this.bi != undefined) cstyle += "border:" + this.bi + "px solid;";
        if (this.bic != undefined) cstyle += "border-color:" + this.bic + ";";

        /// ///       if (this.cs        != undefined) cstyle += ":" + this.cs + ";";
        //       if (this.cson      != undefined) cstyle += ":" + this.cson + ";";
        /// //       if (this.cssc      != undefined) cstyle += ":" + this.cssc + ";";
        if (this.pb != undefined) cstyle += "padding-bottom:" + this.pb + ";";
        if (this.pl != undefined) cstyle += "padding-left:" + this.pl + ";";
        if (this.pr != undefined) cstyle += "padding-right:" + this.pr + ";";
        if (this.pt != undefined) cstyle += "padding-top:" + this.pt + ";";
        if (this.uc != undefined) cstyle += "color:" + this.uc + ";";
        if (this.ucbg != undefined) cstyle += "background-color:" + this.ucbg + ";";
        //    if (this.ac        != undefined) cstyle += ":" + this.ac + ";";
       
        return(cstyle);
    }


    mnstyle.prototype.readsettings = function(csettings) {
        var asin = csettings.split(";");
        var aset = new Array();
        for (var j = 0; j < asin.length; j++) {
            var as = asin[j].split(":");
            aset[j] = new Array();
            aset[j][0] = as[0];
            aset[j][1] = as[1];
        }

        j = 0;
        // optional name value pairs in fixed sequence
        if (aset[j][0] == "cssclass") {
            this.cssclass = aset[j][1];
            j++
        }
        if (aset[j][0] == "width") {
            this.width = aset[j][1] * 1;
            j++
        }
        if (aset[j][0] == "height") {
            this.height = aset[j][1] * 1;
            j++
        }
        // border inside
        if (aset[j][0] == "bi") {
            this.bi = aset[j][1] * 1;
            j++
        }
        // border inside color
        if (aset[j][0] == "bic") {
            this.bic = aset[j][1];
            j++
        }
        // border outside
        if (aset[j][0] == "bo") {
            this.bo = aset[j][1] * 1;
            j++
        }
        // border oustide color
        if (aset[j][0] == "boc") {
            this.boc = aset[j][1];
            j++
        }
        if (aset[j][0] == "cs") {
            this.cs = aset[j][1];
            j++
        }
        if (aset[j][0] == "cson") {
            this.cson = aset[j][1];
            j++
        }
        if (aset[j][0] == "cssc") {
            this.cssc = aset[j][1];
            j++
        }
        if (aset[j][0] == "pb") {
            this.pb = aset[j][1] * 1;
            j++
        }
        if (aset[j][0] == "pl") {
            this.pl = aset[j][1] * 1;
            j++
        }
        if (aset[j][0] == "pr") {
            this.pr = aset[j][1] * 1;
            j++
        }
        if (aset[j][0] == "pt") {
            this.pt = aset[j][1] * 1;
            j++
        }
        if (aset[j][0] == "sc") {
            this.sc = aset[j][1];
            j++
        }
        if (aset[j][0] == "scbg") {
            this.scbg = aset[j][1];
            j++
        }
        if (aset[j][0] == "uc") {
            this.uc = aset[j][1];
            j++
        }
        if (aset[j][0] == "ucbg") {         
            this.ucbg = aset[j][1];
            j++
        }
        if (aset[j][0] == "sl") {
            this.sl = aset[j][1] * 1;
            j++
        }
        if (aset[j][0] == "st") {
            this.st = aset[j][1] * 1;
            j++
        }
        if (aset[j][0] == "ac") {
            this.ac = aset[j][1];
            j++
        }
    }

}

function switchsubmenu(epar, lon) {

    var cdisp = "block";
    if (!lon) cdisp = "none";

    var echildren = epar.getElementsByTagName("div");
    if (echildren.length > 0) {
        echildren[0].style.display = cdisp;
    }
}

function go(caction, curl) {
    document.getElementById('navaction').value = caction;
    if (curl.length > 0) document.getElementById('navform').action = curl;
    document.getElementById('navform').submit();
}


