/**************************************************************************
    Copyright (c) 2001-2003 Geir Landrö (drop@destroydrop.com)
    JavaScript Tree - www.destroydrop.com/hjavascripts/tree/
    Version 0.96    

    This script can be used freely as long as all copyright messages are
    intact.
**************************************************************************/

//************************************
// Push and pop not implemented in IE
//************************************
if(!Array.prototype.push) {
    function array_push() {
        for(var i=0;i<arguments.length;i++)
            this[this.length]=arguments[i];
        return this.length;
    }
    Array.prototype.push = array_push;
}

if(!Array.prototype.pop) {
    function array_pop(){
        if (this.length > 0)
            return this[this.length-1];
        else
            return null;
    }
    Array.prototype.pop = array_pop;
}

//************************************
// Tree
//************************************

// Arrays for nodes and icons
var rootNode    = new Node(0);
var nodes       = new Array();
var urlbase     = "/extension/oeuser/design/standard/"

function Node(id){
    this.id = id;
    this.caption = '';
    this.image = null;
    this.href = null;
    this.parent = null;
    this.opened = false;
    this.visible = true;
    this.children = Array();
}

function addChild(parent,node){
    parent.children.push(node);
    node.parent = parent;
}

function removeNode(node){
    for (var i=0; i<node.parent.children.length; i++){
        if (node.parent.children[i].id == node.id) 
            node.parent.children[i] = null;
    }
}

function getBrothers(node){
    var ret = new Array();
    for (var i=0; i<node.parent.children.length; i++){
        if (node.parent.children[i].id != node.id)
            ret.push(node.parent.children[i]);
    }
    return ret;
}

function getNode(id){
    for (var i=0; i<nodes.length; i++)
        if (nodes[i].id == id) return nodes[i];
    return null;
}

function showNode(node){
    node.visible=true;
    //if(node.children.length>0){
        for (var i=0;i<node.children.length;i++){
             var div = document.getElementById("div-node-"+node.children[i].id+"-content");
             if (div != null) div.style.display='';
        }
    //} else {
        var div = document.getElementById("div-node-"+node.id+"-content");
        if (div != null) div.style.display='';
    //}
}

function hideNode(node){
    node.visible=false;
    //if(node.children.length>0){
        for (var i=0;i<node.children.length;i++){
             var div = document.getElementById("div-node-"+node.children[i].id+"-content");
             if (div != null) div.style.display='none';
        }
    //} else {
        var div = document.getElementById("div-node-"+node.id+"-content");
        if (div != null) div.style.display='none';
    //}
}

function openNode(node,recurse){
    node.opened=true;
    var div = document.getElementById("div-node-"+node.id+"-container");
    var join = document.getElementById("join-node-" + node.id);

    if (div != null) div.style.display='';

    if (join != null){
        var bottom=''
        if (node.parent != null && isLast(node.parent, node.id)) 
            bottom='bottom'; 
        join.src = urlbase+"images/minus"+bottom+".gif";
    }

    if (recurse)
        for (var i=0; i<node.children.length; i++)
            openNode(node.children[i],recurse);
}

function closeNode(node,recurse){
    node.opened=false;
    var div = document.getElementById("div-node-"+node.id+"-container");
    var join = document.getElementById("join-node-" + node.id);

    if (div != null)div.style.display='none';

    if (join != null){
        var bottom=''
        if (node.parent != null && isLast(node.parent, node.id))
            bottom='bottom';
        join.src = urlbase+"images/plus"+bottom+".gif";
    }

    if (recurse)
        for (var i=0; i<node.children.length; i++)
            closeNode(node.children[i],recurse);
}

function getIndex(node){
    for (var i=0; i<nodes.length; i++)
        if (nodes[i].id == node.id) return i;
    return -1;
}

function getNode(id){
    for (var i=0; i<nodes.length; i++)
        if (nodes[i].id == id) return nodes[i];
    return null;
}


function isLast(parent, id){
    return parent.children[parent.children.length-1].id == id;
}

function createTree(_nodes, openFrom, rootCaption) {
    if (_nodes.length != 0) {
        buildTree(_nodes);
        if (rootCaption != null)
            rootNode.caption = rootCaption;
        document.write("<div id=\"div-node-"+rootNode.id+"\">"+rootNode.caption);
        for (var i=0; i<rootNode.children.length; i++)
            writeHtmlCode(rootNode.children[i]);
        document.write("</div>");
    
        if (openFrom != null) 
            setOpenFrom(openFrom);
        else
            openNode(rootNode,false);
    }
}

function buildTree(_nodes){
    nodes = new Array();
    rootNode = new Node(0);
    nodes.push(rootNode);
    
    for (var i=0; i<_nodes.length; i++){
        var vals = _nodes[i].split("|");
        var node = new Node(vals[0]);
        
        if (vals.length>2 && vals[2] != '') node.caption = vals[2];
        if (vals.length>3 && vals[3] != '') node.href = vals[3];
        if (vals.length>4 && vals[4] != '') node.image = vals[4];
        if (vals.length>5 && vals[5] != '') node.divClass = vals[5];
            
        var parentId = vals[1];
        var parent = getNode(parentId);

        if (parent != null)
            addChild(parent,node);
            
        nodes.push(node);
    }
}

function writeHtmlCode(node){

    document.write("<div id=\"div-node-"+node.id+"\">");
    
    var bottom = '';
    if (isLast(node.parent, node.id)) bottom='bottom';
    
    if (node.children.length>0) {
        document.write("<a href=\"javascript: openClose('"+node.id+"');\">");
        if (node.open)
            document.write("<img id=\"join-node-"+node.id+"\" src=\""+urlbase+"images/minus"+bottom+".gif\" align=\"absbottom\" alt=\"\" />");
        else
            document.write("<img id=\"join-node-"+node.id+"\" src=\""+urlbase+"images/plus"+bottom+".gif\" align=\"absbottom\" alt=\"\" />");
        document.write("</a>");
    } else {
        document.write("<img id=\"join-node-"+node.id+"\" src=\""+urlbase+"images/join"+bottom+".gif\" align=\"absbottom\" alt=\"\" />");
    }
    
    if (node.href != null && node.href != ''){
        document.write("<a href="+node.href+" onmouseover=\"window.status='"+node.caption+"';return true;\" onmouseout=\"window.status=' ';return true;\">");
    } else {
        document.write("<a href=\"javascript: showNews('"+node.id+"');\" onmouseover=\"window.status='"+node.caption+"';return true;\" onmouseout=\"window.status=' ';return true;\">");
    }
    
    if (node.image != null && node.image != '')
        document.write("<img src=\""+urlbase+"images/"+node.image+"\" align=\"absbottom\" alt=\"\" />");
    
    document.write(node.caption);
    document.write("</a><br />");
    
    if (node.children.length>0){
        document.write("<div id=\"div-node-"+node.id+"-container\" style=\"display:none;\">");
        for (var i=0; i<node.children.length; i++)
            writeHtmlCode(node.children[i]);
        document.write("</div>");
    }
    document.write("</div>");
}

// Opens or closes a node
function openClose(id) {
    var node = getNode(id);
    if (node == null)
        return;
    if (node.opened)
        closeNode(node,false);
    else
        openNode(node,false);
}

//Opens a subtree
function setOpenFrom(id){
    var node = getNode(id);
    openNode(node,true);
}

//*****************************
//Tree independent functions
//*****************************

// Show hide news depending on node
function showNews(id) {
    var node = getNode(id);
    if (node == null)
        return;

    for (var i=0; i<nodes.length; i++)
        hideNode(nodes[i]);
    showNode(node);
}
