function Node(id, pid, name, url, title, target, icon, iconOpen, open) {

this.id = id;

this.pid = pid;

this.name = name;

this.url = url;

this.title = title;

this.target = target;

this.icon = icon;

this.iconOpen = iconOpen;

this._io = open || false;

this._is = false;

this._ls = false;

this._hc = false;

this._ai = 0;

this._p;

};



// Tree object

function dTree(objName) {

this.config = {

target: null,

folderLinks: false,

useSelection: false,

useCookies: false,

useLines: false,

useIcons: false,

useStatusText: false,

closeSameLevel: false,

inOrder: false

}

this.icon = {

root: '',

folder: '',

folderOpen: '',

node: '',

empty: '/cn/images/empty.gif',

line: '',

join: '',

joinBottom: '',

plus: '',

plusBottom: '',

minus: '',

minusBottom: '',

nlPlus: '',

nlMinus: ''

};

this.obj = objName;

this.aNodes = [];

this.aIndent = [];

this.root = new Node(-1);

this.selectedNode = null;

this.selectedFound = false;

this.completed = false;

this.IsSelect = false;

};



// 添加新的节点到节点阵列

dTree.prototype.add = function(id, pid, name, url, title, target, icon, iconOpen, open) {

this.aNodes[this.aNodes.length] = new Node(id, pid, name, url, title, target, icon, iconOpen, open);

};




// 树输出到页面

dTree.prototype.toString = function() {

var str = '<div class="dtree">\n';

if (document.getElementById) {

if (this.config.useCookies) this.selectedNode = this.getSelected();

str += this.addNode(this.root);

} else str += '浏览器不支持';

str += '</div>';

if (!this.selectedFound) this.selectedNode = null;

this.completed = true;

return str;

};



// 创建树结构


dTree.prototype.addNode = function (pNode) {

    var str = '';

    var n = 0;

    if (this.config.inOrder) {
        n = pNode._ai;
    }

    for (n; n < this.aNodes.length; n++) {

        if (this.aNodes[n].pid == pNode.id) {

            var cn = this.aNodes[n];

            cn._p = pNode;

            cn._ai = n;

            this.setCS(cn);

            if (!cn.target && this.config.target) cn.target = this.config.target;

            if (cn._hc && !cn._io && this.config.useCookies) cn._io = this.isOpen(cn.id);

            if (!this.config.folderLinks && cn._hc) cn.url = null;

            if (this.config.useSelection && cn.id == this.selectedNode && !this.selectedFound) {

                cn._is = true;

                this.selectedNode = n;

                this.selectedFound = true;

            }

            str += this.node(cn, n);

            if (cn._ls) break;

        }

    }

    return str;

};

//' + ((this.root.id == node.pid) ? 'dTreeNode1' : '

// 创建节点图标，URL和文字


dTree.prototype.node = function (node, nodeId) {

    var str = '<div  class="dTreeNode" id="a' + node.id + '">';

    if (node.url) {

        str += '<a  onmouseover="javascript: ' + this.obj + '.o(' + nodeId + ');" onmouseout="javascript: ' + this.obj + '.out(' + nodeId + ');" id="s' + this.obj + nodeId + '" class="' + ((this.config.useSelection) ? ((node._is ? 'nodeSel' : 'node')) : 'node') + '" href="' + node.url + '"';

        if (node.title) str += ' title="' + node.title + '"';

        if (node.target) str += ' target="' + node.target + '"';

        if (this.config.useStatusText) str += ' onmouseover="window.status=\'' + node.name + '\';return true;" onmouseout="window.status=\'\';return true;" ';

        if (this.config.useSelection && ((node._hc && this.config.folderLinks) || !node._hc))

            str += ' onclick="javascript: ' + this.obj + '.s(' + nodeId + ');"';

        str += '>';

    }

    else if ((!this.config.folderLinks || !node.url) &&  node.pid != this.root.id)

        str += '<a onmouseover="javascript: ' + this.obj + '.o(' + nodeId + ');" onmouseout="javascript: ' + this.obj + '.out(' + nodeId + ');"  class="node">';


    str += node.name;

    if (node.url || (!this.config.folderLinks || !node.url) ) str += '</a>';



    str += '</div>';


    if (node._hc) {

        str += '<div class="dTreeNode1">';

        str += '<div id="d' + this.obj + nodeId + '" class="' + ((nodeId == 0) ? 'clip' : 'clip1') + '" style="display:' + ((this.root.id == node.pid || node._io) ? 'block' : 'none') + ';">';

        str += this.addNode(node);

        str += '</div>';

        str += '</div>';


    }

    this.aIndent.pop();

    return str;

};





// 检查是否有任何一个子节点，如果它是最后一个节点


dTree.prototype.setCS = function(node) {

var lastId;

for (var n=0; n<this.aNodes.length; n++) {

if (this.aNodes[n].pid == node.id) node._hc = true;

if (this.aNodes[n].pid == node.pid) lastId = this.aNodes[n].id;

}

if (lastId==node.id) node._ls = true;

};





// 切换打开


dTree.prototype.o = function (id) {

    var cn = this.aNodes[id];
    var eDiv = document.getElementById('d' + this.obj + id);
    

    if (eDiv != null) {


        if (cn._hc == true) {
//            pDiv.style.background = "#ccc";
            eDiv.style.display = "block";
            
            this.closeLevel(cn);
        }
    } else {
        if (cn._hc == false) {

//            pDiv.style.background = "#ccc";
            this.closeLevel(cn);
        }
    }


  
};



dTree.prototype.out = function (id) {
    //    this.isSelect = false;

    var cn = this.aNodes[id];
    var eDiv = document.getElementById('d' + this.obj + id);
    

    var cnt = this.aNodes[cn.id];
    

    if (eDiv != null) {

        
            
        

        if (cn._hc == true) {
//            pDiv.style.background = "#ccc";
            this.closeLevel(cn);
        }
    } else {
        if (cn._hc == false) {
           
//            pDiv.style.background = "#fff";
            this.closeLevel(cn);
         
        }
    }

   
};

//改变一个节点（打开或关闭）状态


dTree.prototype.nodeStatus = function (status, id, bottom) {

    eDiv = document.getElementById('d' + this.obj + id);


    if (this.config.useIcons) {

        eIcon = document.getElementById('i' + this.obj + id);

        eIcon.src = (status) ? this.aNodes[id].iconOpen : this.aNodes[id].icon;

    }

    eDiv.style.display = (status) ? 'block' : 'none';
    

};





// 如果push和pop是不是由浏览器执行


if (!Array.prototype.push) {

Array.prototype.push = function array_push() {

for(var i=0;i<arguments.length;i++)

this[this.length]=arguments[i];

return this.length;

}

};

if (!Array.prototype.pop) {

Array.prototype.pop = function array_pop() {

lastElement = this[this.length-1];

this.length = Math.max(this.length-1,0);

return lastElement;

}

};

// 关闭所有节点上作为同一级别的某些节点


dTree.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]._hc) {

            this.nodeStatus(false, n, this.aNodes[n]._ls);

            this.aNodes[n]._io = false;

            this.closeAllChildren(this.aNodes[n]);

        }

    }

}

// 关闭一个节点的所有子


dTree.prototype.closeAllChildren = function (node) {

    for (var n = 0; n < this.aNodes.length; n++) {

        if (this.aNodes[n].pid == node.id && this.aNodes[n]._hc) {

            if (this.aNodes[n]._io) this.nodeStatus(false, n, this.aNodes[n]._ls);

            this.aNodes[n]._io = false;

            this.closeAllChildren(this.aNodes[n]);

        }

    }

}

