/*--------------------------------------------------|
| pTree 2.05 | www.destroydrop.com/javascript/tree/ |
|---------------------------------------------------|
| Copyright (c) 2002-2003 Geir Landrí°¹í¾ |*/

// Node object

function Pnode(id, pid, open) {
    this.id = id;
    this.pid = pid;
    this._io = open || false;
}

// Tree object

function pTree(objName) {
    this.icon = {
        nlPlus			: 'js_menu/img/nolines_plus.png',
        nlMinus			: 'js_menu/img/nolines_minus.png'
    }
    
    this.obj = objName;
    this.aNodes = [];
    this.rozwin = false;
}


// Adds a new node to the node array
pTree.prototype.add = function(id, pid, open) {
    this.aNodes[this.aNodes.length] = new Pnode(id, pid, open);
}


// Open/close all nodes
pTree.prototype.openAll = function() {
    this.rozwin = !this.rozwin;
    this.oAll(this.rozwin);
    document.rozwin[0].value = (this.rozwin)?"zwiÅ„ wszystkie":"rozwiÅ„ wszystkie";
}

//pTree.prototype.closeAll = function() {
//    this.oAll(false);
//}




// Highlights the selected node
pTree.prototype.s = function(id) {
    eNew = document.getElementById("s" + id);
    eNew.className = "Sel";
}


// Toggle Open or close
pTree.prototype.o = function(ID) {
    for (var n=0; n<this.aNodes.length; n++) {
        if (this.aNodes[n].id == ID) {
            var cn = this.aNodes[n];
            this.nodeStatus(!cn._io, ID);
            cn._io = !cn._io;
            this.closeLevel(cn);
				break;
        }
    }
}


// Open or close all nodes
pTree.prototype.oAll = function(status) {
    for (var n=0; n<this.aNodes.length; n++) {
        var ID = this.aNodes[n].id;
        this.nodeStatus(status, ID);
        this.aNodes[n]._io = status;
    }
}



// Closes all nodes on the same level as certain node
pTree.prototype.closeLevel = function(node) {
    for (var n=0; n<this.aNodes.length; n++) {
        if (this.aNodes[n].pid == node.pid && this.aNodes[n].id != node.id && this.aNodes[n]._io == true) {
            this.nodeStatus(false, this.aNodes[n].id);
            this.aNodes[n]._io = false;
            this.closeAllChildren(this.aNodes[n]);
        }
    }
}

// Closes all children of a node
pTree.prototype.closeAllChildren = function(node) {
    for (var n=0; n<this.aNodes.length; n++) {
        if (this.aNodes[n].pid == node.id) {
            if (this.aNodes[n]._io) this.nodeStatus(false, this.aNodes[n].id);
            this.aNodes[n]._io = false;
            this.closeAllChildren(this.aNodes[n]);
        }
    }
}

// Change the status of a node(open or closed)
pTree.prototype.nodeStatus = function(stan, ID) {
   eDiv	= document.getElementById("c" + ID);
  	eJoin	= document.getElementById("i" + ID);
	eJoin.src = (stan)?this.icon.nlMinus:this.icon.nlPlus;
   eDiv.style.display = (stan) ? 'block': 'none';
}


