/*--------------------------------------------------|
| 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.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);
}



// 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;
        }
    }
}


// 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);
   eDiv.style.display = (stan) ? '': 'none';
}
