/**
* 
* Copyright 2005, addObject.com. All Rights Reserved
* Author Jack Hermanto, www.addobject.com
*
* By using this script means you are agree with the following term
* 1. You can use this script on your application or website but you MUST
* not delete this header.
* 2. You are not allowed to include only part of the script into your application 
* or website.
* 3. If you includes this scripts in commercial product, you must obtains
* copy of license from www.addobject.com
*/

var nlsTree = new Object();

function NlsTree(tId) {
    this.tId = "Eyob";
    this.opt = new StdOpt();
    this.ico = new StdIco("img/");
    this.nLst = new Object()
    this.tRef = "";
    this.rt = null;
    this.selNd = null;
    this.nCnt = 0;

    if (nlsTree[this.tId]!=null) {
        alert("The tree with id " + this.tId + " already exist, please change the tree id.");
    } else {
        nlsTree[this.tId] = this;
    }

    return this;
}

function StdIco(path) {
    this.pnb = path+"plusnb.gif";
    this.pb  = path+"plusb.gif";
    this.mnb = path+"minusnb.gif";
    this.mb  = path+"minusb.gif";
    this.opf = path+"folderopen2.gif";
    this.clf = path+"folder.gif";
    this.chd = path+"leaf.gif";
    this.rot = path+"root.gif";
    this.lnb = path+"lineang.gif";
    this.lb  = path+"lineints.gif";
    this.lin = path+"line.gif";
    this.bln = path+"blank.gif";
    return this;
}

function StdOpt() {
    this.trg = "_self";
    this.sort = "asc";
    return this;
}

function NlsNode(orgId, capt, url, ic, exp, xtra) {
    this.orgId = orgId;
    this.id = "";
    this.capt = capt;
    this.url = (url==null || url=="") ? "javascript:void(0);" : url;
    this.ic = (ic==null || ic=="")?null:ic.split(",");
    this.exp = exp==null ? false : exp;
    this.xtra = xtra ==null ? false : xtra;

    this.nx = null; this.pv = null; this.fc = null; this.lc = null; this.pr = null;
    this.equals = function (nd) {
        return (this.id == nd.id);
    }
    return this;
}

NlsTree.prototype.genIntId = function (id) {return this.tId+id;}
NlsTree.prototype.genOrgId = function(intId) { return intId.substr(this.tId.length); }
NlsTree.prototype.compareNode = function(aN, bN) { return (aN.capt >= bN.capt); }

NlsTree.prototype.add = function(id, prn, capt, url, ic, exp, xtra) {
    var nNd = new NlsNode(((id==null||id=="")?("int"+ (++this.nCnt)):id), capt, url, ic, exp, xtra);
    nNd.id = this.genIntId(nNd.orgId);
    if (this.nLst[nNd.id]!=null) 
	{ 
	confirm("There is an urjent Training under the title " + capt + "");  
	}
    this.nLst[nNd.id] = nNd;
    if (this.rt==null) { this.rt = nNd; } else {
        var pnd = this.nLst[this.genIntId(prn)];
        if (pnd==null ) { alert("Parent node " + prn + " not found!!"); return; }
        nNd.pr = pnd;
        if (pnd.lc==null) {
            pnd.fc = nNd; pnd.lc = nNd; 
        } else {
            var t=pnd.fc;
            if (this.opt.sort!="no") { 
                do {
                    if (this.opt.sort=="asc" ? this.compareNode(t, nNd) : this.compareNode(nNd, t)) break;
                    t = t.nx;
                } while (t!=null);
                if (t!=null) {
                    if (t.pv==null) { t.pv=nNd; pnd.fc=nNd; } else { nNd.pv=t.pv; t.pv.nx=nNd; t.pv=nNd; }
                    nNd.nx=t;
                }
            }
            if (this.opt.sort=="no" || t==null) { nNd.pv = pnd.lc; pnd.lc.nx = nNd; pnd.lc = nNd; }
        }
    }
    return nNd;
}

NlsTree.prototype.append = function(id, prn, capt, url, ic, exp, xtra) {
    var nd = this.add(id, prn, capt, url, ic, exp, xtra);
    this.reloadNode(prn);
}

NlsTree.prototype.remove = function(id) {
    var rNd = (id!=null ? this.nLst[this.genIntId(id)] : this.selNd);
    if (rNd!=null) {
        if (this.rt.equals(rNd)) { this.rt=null; this.nLst=new Object(); this.selNd=null; return rNd};
        if (rNd.equals(this.selNd)) this.selNd = null;
        var pr = rNd.pr;
        if (pr.lc.equals(rNd)) pr.lc=rNd.pv; 
        if (pr.fc.equals(rNd)) pr.fc=rNd.nx;
        if (rNd.pv!=null) rNd.pv.nx=rNd.nx; 
        if (rNd.nx!=null) rNd.nx.pv=rNd.pv;
        rNd.next=null;rNd.pv=null;rNd.pr=null;
        this.loopTree(rNd, "this.nLst[sNd.id]=null");
        this.reloadNode(this.genOrgId(pr.id));
    }
    return rNd;
}

NlsTree.prototype.getSelNode = function() {
    return this.selNd;
}

NlsTree.prototype.genANode = function(sNd) {
    var ev=""; var st=""; var treeName=this.tRef + "nlsTree." + this.tId;
    var ip = (sNd.nx!=null? this.ico.lb: this.ico.lnb);
    var sv = treeName + ".selectNode(\"" + sNd.id + "\");";
    if (sNd.fc) {
        ev = treeName + ".toggleNode(\"" + sNd.id + "\");";
        st = treeName + ".selNToggle(\"" + sNd.id + "\");";
        ip = (sNd.nx!=null? (sNd.exp?this.ico.mb:this.ico.pb): (sNd.exp?this.ico.mnb:this.ico.pnb));
    } else sNd.exp=false;
    var s = (sNd.pr==null ? "": "<img id=ip_" + sNd.id + " style='vertical-align:bottom' src='" + ip + "' " + (sNd.fc==null ? "" : "onclick='"+ ev +"'") + ">") +
        "<img id=ic_" + sNd.id + " style='vertical-align:bottom' src='" + (sNd.ic!=null? sNd.ic[0]:(sNd.fc!=null)?this.ico.clf:this.ico.chd)  + "' onclick='"+ sv + treeName + ".treeOnClick();' ondblclick='" + st + treeName + ".treeOnDblClick()' onmouseover='" + treeName + ".treeOnMouseOver()' onmousemove='"+ treeName +".treeOnMouseMove()' onmouseout='"+ treeName +".treeOnMouseOut()' >" +
        "</td><td valign=middle nowrap><a target=\"" + this.opt.trg + "\" href=\""+sNd.url+"\" id=ac_" + sNd.id + " class='node' unselectable='on' onclick='" + sv + treeName + ".treeOnClick();' ondblclick='" + st + treeName + ".treeOnDblClick()' onmouseover='" + treeName + ".treeOnMouseOver()' onmousemove='"+ treeName +".treeOnMouseMove()' onmouseout='"+ treeName +".treeOnMouseOut()' >" + sNd.capt + "</a></td>"
    var n=sNd.pr;
    while (n != null && !n.equals(this.rt)) {
        s = "<img style='vertical-align:bottom' src='"+(n.nx!=null ? this.ico.lin : this.ico.bln)+"'>" + s;
        n=n.pr;
    }
    s="<td nowrap>"+s;
    return "<table cellpadding='0' cellspacing='0' border='0'><tr>" + s + "</tr></table>";
}

NlsTree.prototype.genNodes = function(sNd, incpar) {
    var s = incpar ? ("<div id='"+sNd.id+"' unselectable='on'>" + this.genANode(sNd) +
            "</div><div style='display:" + (sNd.fc && sNd.exp?"block":"none") + "' id='ch_" + sNd.id + "'>") : "";
    if (sNd.fc !=null) {
        var chNode = sNd.fc;
        do {
            s=s+this.genNodes(chNode, true);
            chNode = chNode.nx;
        } while (chNode != null)
    }
    s= incpar ? (s+"</div>") : s;
    return s;
}

NlsTree.prototype.showTree = function() {
    return this.genNodes(this.rt, true);
}

NlsTree.prototype.reloadNode = function(id) {
    var intId = this.genIntId(id);
    var s = this.genNodes(this.nLst[intId], false);
    var dvN = NlsGetElementById("ch_"+intId);
    dvN.innerHTML = s;
    if (dvN.innerHTML=="") dvN.style.display="none";
    s = this.genANode(this.nLst[intId]);
    dvN = NlsGetElementById(intId)
    dvN.innerHTML = s;
    if (this.selNd!=null) {var sId=this.selNd.id; this.selNd=null; this.selectNode(sId); }
}

NlsTree.prototype.selNToggle = function(id) {
    this.toggleNode(id); //this.selectNode(id);
}

NlsTree.prototype.selectNode = function (id) {
    var ac=null;var ic=null;var sNd=null;
    sNd = this.selNd;
    if (sNd!=null) {
        ac = NlsGetElementById("ac_" + sNd.id);
        ic = NlsGetElementById("ic_" + sNd.id);
        ic.src = sNd.ic!=null ? sNd.ic[0] :(sNd.fc!=null?this.ico.clf:this.ico.chd);
        ac.className = "node";
    }
    sNd = this.nLst[id];
    this.selNd = sNd;
    ac = NlsGetElementById("ac_" + id);
    ic = NlsGetElementById("ic_" + id);
    ic.src = sNd.ic!=null ? (sNd.ic[1]!=null?sNd.ic[1]:sNd.ic[0]) : (sNd.fc!=null?this.ico.opf:this.ico.chd);
    ac.className = "selnode";
}

NlsTree.prototype.toggleNode = function(id) {
    var nd = NlsGetElementById("ch_" + id);
    var ip = NlsGetElementById("ip_" + id);
    var sNd = this.nLst[id];
    if (sNd.exp) {
        sNd.exp = false;
        nd.style.display="none";
        if (ip!=null && sNd.fc!=null) ip.src=sNd.nx ? this.ico.pb : this.ico.pnb;
    } else {
        sNd.exp = true;
        nd.style.display="block";
        if (ip!=null && sNd.fc!=null) ip.src=sNd.nx ? this.ico.mb : this.ico.mnb;
    }
}

NlsTree.prototype.getNodeById = function(id) {
  return this.nLst[this.genIntId(id)]
}

NlsTree.prototype.loopTree = function(sNd, act) {
    eval(act);
    if (sNd.fc !=null) {
        var chNode = sNd.fc;
        do {
            this.loopTree(chNode, act);
            chNode = chNode.nx;
        } while (chNode != null)
    }
}

NlsTree.prototype.treeOnClick = function() { /*PUT YOUR CODE HERE OR ASSIGN NEW EVENT HANDLER*/ }
NlsTree.prototype.treeOnDblClick = function() { /*PUT YOUR CODE HERE OR ASSIGN NEW EVENT HANDLER*/ }
NlsTree.prototype.treeOnMouseOver = function () { /*PUT YOUR CODE HERE OR ASSIGN NEW EVENT HANDLER*/ }
NlsTree.prototype.treeOnMouseMove = function () { /*PUT YOUR CODE HERE OR ASSIGN NEW EVENT HANDLER*/ }
NlsTree.prototype.treeOnMouseOut = function () { /*PUT YOUR CODE HERE OR ASSIGN NEW EVENT HANDLER*/ }

/**Cross browser related methods*/
function NlsGetElementById(id) {
    if (document.all) {
        return document.all(id);
    } else
    if (document.getElementById) {
        return document.getElementById(id);
    }
}
