var _imp_navi_nodes;
var _imp_navi_state;

function KTreeStatic_1(el, navi_nodes, navi_state) {
    _imp_navi_nodes     = navi_nodes;
    KTreeStatic_1_LoadState(navi_state);

    var rootid          = _imp_navi_nodes[""][0];
    var tree_definition = KTreeStatic_1_BuildTree(rootid, 0, true);
    el.appendChild(tree_definition);
    
    // set styles
    el.style.cursor     = "default";

    // Methoden (procedures)
    el.expand           = this.expand;
    el.expandAll        = this.expandAll;
    el.toggleExpand     = this.toggleExpand;
    el.select           = this.select;
    el.reloadChildren   = this.reloadChildren;

    // Methoden (function)
    el.getPath          = this.getPath;
    el.getChild         = this.getChild;
    el.getNode          = this.getNode;
    el.getLabel         = this.getLabel;
    el.getParent        = this.getParent;
    el.isExpanded       = this.isExpanded;
    el.GetFirstChildDIV = this.GetFirstChildDIV;

    // Methoden (event-handler)
    el.onclick          = this.click;

    // Callbacks

    // Konstanten

    // Eigenschaften
    el.selectedLabel    = null;
    el.noLinkToggle     = 0;

    return el;
}

////////////////////////////////////////////////////////////////////////////////
var KTreeStatic_1_BuildTree = function(id, depth, is_root) {  
    var node            = _imp_navi_nodes[id];
    var children_ids    = node[0];
    //var id              = node[1];
    var parentid        = node[2];
    var image           = node[3];
    var text            = node[4];
    var link            = node[5];
    var target          = node[6];
    var can_collapse    = node[7] == "-1" ? true : false;
    var is_expanded     = node[8] || !can_collapse;
    var has_children    = children_ids ? true : false;

    var expimg          = document.createElement("img");
    expimg.src          = has_children && can_collapse ? (is_expanded ? "/images/ktree/minus.png" : "/images/ktree/plus.png") : "/images/ktree/blank.png";
    
    var img             = document.createElement("img");
    img.src             = image;
    img.style.marginRight = "2px";
    
    var a               = document.createElement("a");
    a.href              = link;
    if (target) {
        a.target = target;
    }
    a.appendChild(document.createTextNode(text));
    
    var tree            = document.createElement("div");
    tree.className      = "ktree";
    tree.id             = "id" + id;
    tree.appendChild(expimg);
    tree.appendChild(img);
    tree.appendChild(a);

    if (children_ids) {
        var subdiv          = document.createElement("div");
        subdiv.className    = is_expanded ? "ktree" : "ktreec";
        subdiv.id           = "sub_id" + id;
        subdiv.style.marginLeft = is_root ? "" : "14px";

        var idx;
        for (idx = 0; idx < children_ids.length; idx++) {
            if (depth <= 1 || is_expanded) {
                var id = children_ids[idx];
                var subtree = KTreeStatic_1_BuildTree(id, depth + 1, false);
                subdiv.appendChild(subtree);
            }
        }
        
        tree.appendChild(subdiv);
    }
    
    return tree;
};

////////////////////////////////////////////////////////////////////////////////
KTreeStatic_1.prototype.click = function(e) {
    if (!e) { e = window.event; }
    if (!e) { return; }

    var el = e.srcElement || e.target;
    if (!el) { return; }


    var do_toggle = (el.tagName != "A" || !this.noLinkToggle);

    var srcElement = el;
    while (el && !el.id) { el = el.parentNode; }
    if (!el) { return; }

    if (srcElement.tagName == "IMG" && (srcElement.src.indexOf(".png") >= 0)) {
        if (do_toggle) {
            this.toggleExpand(el);
        }
    } else {
        if (el.id == "treelabel") {
            this.select(el);
            el = el.parentNode;
            while (el && !el.id) { el = el.parentNode; }
            if (do_toggle) { this.toggleExpand(el); }
        } else {
            this.select(el);
        }
    }
};

////////////////////////////////////////////////////////////////////////////////
KTreeStatic_1.prototype.getPath = function(node) {
    var path = "";
    while (node) {
        if (node.id && node.id != 'treelabel' && String(node.id).indexOf('sub_') != 0) {
            path = node.id + "/" + path;
        }
        node = node.parentNode;
    }
    return path;
};

////////////////////////////////////////////////////////////////////////////////
KTreeStatic_1.prototype.getChild = function(name, curNode, depth) {
    if (!curNode) { curNode = this; }
    if (depth > 0 && curNode.className && curNode.className.indexOf("ktree") >= 0 && curNode.id && String(curNode.id).indexOf("sub_") < 0) {
        if (curNode.id == name) { return curNode; }
        return null;
    }

    var nodes = curNode.childNodes;
    for (var idx = 0; idx < nodes.length; idx++) {
        var res = this.getChild(name, nodes[idx], depth + 1);
        if (res) { return res; }
    }
    return null;
};

////////////////////////////////////////////////////////////////////////////////
KTreeStatic_1.prototype.getNode = function(path) {
    var names = path.split("/");
    if (names[0] == this.id) { names.shift(); }

    var node = this;
    for (var idx = 0; idx < names.length && node; idx++) {
        node = this.getChild(names[idx], node, 0);
    }
    return node;
};

////////////////////////////////////////////////////////////////////////////////
KTreeStatic_1.prototype.getLabel = function(node) {
    if (node.id == "treelabel") { return node; }

    var nodes = node.childNodes;
    for (var idx = 0; idx < nodes.length; idx++) {
        var res = this.getLabel(nodes[idx]);
        if (res) { return res; }
    }
};

////////////////////////////////////////////////////////////////////////////////
KTreeStatic_1.prototype.getParent = function(node) {
    while (node = node.parentNode) {
        if (node.className && node.className.indexOf("ktree") >= 0) {
            return node;
        }
    }
    return null;
};

////////////////////////////////////////////////////////////////////////////////
KTreeStatic_1.prototype.expand = function(node) {
    this.toggleExpand(node, 1);
};

////////////////////////////////////////////////////////////////////////////////
KTreeStatic_1.prototype.expandAll = function(node) {
    this.expand(node);
    while (node = this.getParent(node)) {
        this.expand(node);
    }
};

////////////////////////////////////////////////////////////////////////////////
KTreeStatic_1.prototype.isExpanded = function() {
    var firstDIV = this.GetFirstChildDIV(this);
    if (!firstDIV) { return null; }
    return (firstDIV.className == "ktree");
};

////////////////////////////////////////////////////////////////////////////////
// liefert den ersten 'DIV'-Knoten eines bestimmten child-Knotens
// (ein DIV-Node muß folgende Attribute aufweisen: class="ktree*" und id="^sub_*")
KTreeStatic_1.prototype.GetFirstChildDIV = function(subnode) {
    if (subnode.className && subnode.className.indexOf("ktree") >= 0 && String(subnode.id).indexOf("sub_") == 0) {
        return subnode;
    }
    var nodes = subnode.childNodes;
    for (var idx = 0; idx < nodes.length; idx++) {
        var res = this.GetFirstChildDIV(nodes[idx]);
        if (res) { return res; }
    }
    return null;
};

////////////////////////////////////////////////////////////////////////////////
KTreeStatic_1.prototype.toggleExpand = function(node, doExpand) {
    var id          = node.id + "";
    var id_numeric  = id.replace("id", "");
    var childnodes  = node.childNodes;
    var childnode;
    for (var idx = 0; idx < childnodes.length; idx++) {
        var subnode = childnodes[idx];
        if (subnode.id == "sub_" + id) {
            childnode = subnode;
            break;
        }
    }

    if (!childnode)                         { return null; }
    if (doExpand == null)                   { doExpand = (childnode.className == "ktreec"); }
    if (doExpand && this.reloadChildren)    { this.reloadChildren(node, childnode); }

    childnode.className = (doExpand) ? "ktree" : "ktreec";
    if (doExpand) {
        _imp_navi_state[id_numeric] = 1;
    } else {
        delete _imp_navi_state[id_numeric];
    }
    KTreeStatic_1_SaveState(_imp_navi_state);

    var imgElement = node.firstChild;
    while (imgElement && (String(imgElement.src).indexOf("plus") < 0 && String(imgElement.src).indexOf("minus") < 0)) {
        imgElement = imgElement.nextSibling;
    }
    if (imgElement) {
        imgElement.src = (doExpand) ? imgElement.src.replace("plus", "minus") : imgElement.src.replace("minus", "plus");
    }
    return doExpand;
};

////////////////////////////////////////////////////////////////////////////////
// wird aufgerufen, wenn ein Childnode aufgeklappt wird
KTreeStatic_1.prototype.reloadChildren = function(node, childnode) {
    var count_children = childnode.childNodes.length;
    if (count_children) {
        return;
    }
    var id          = node.id.replace("id", "");
    var subtree     = KTreeStatic_1_BuildTree(id, 1, false);
    var firstdiv    = (subtree.getElementsByTagName("DIV"))[0];
    while (firstdiv.childNodes.length > 0) {
        var subnode = firstdiv.firstChild;
        childnode.appendChild(subnode);
    }
};

////////////////////////////////////////////////////////////////////////////////
KTreeStatic_1.prototype.select = function(label) {
    if (this.selectedLabel) {
        this.selectedLabel.style.color = "";
        this.selectedLabel.style.backgroundColor = "";
        if (this.selectedLabel.firstChild && this.selectedLabel.firstChild.style) {
            this.selectedLabel.firstChild.style.color = "";
        }
    }
    this.selectedLabel = this.getLabel(label);
    if (this.selectedLabel) {
        this.selectedLabel.style.color = "buttonhighlight";
        this.selectedLabel.style.backgroundColor = "highlight";
        if (this.selectedLabel.firstChild && this.selectedLabel.firstChild.style) {
            this.selectedLabel.firstChild.style.color = "buttonhighlight";
        }
    }
};

////////////////////////////////////////////////////////////////////////////////
KTreeStatic_1_SaveState = function(navistate) {
    var max_age = 365 * 24 * 60 * 60; // 1 Jahr
    var expires = new Date();
    var expires_end = expires.getTime() + max_age * 1000;
    expires.setTime(expires_end);
    var cookie_parts = [];
    cookie_parts.push("navistate=" + escape(Dump(navistate)));
    //cookie_parts.push("path=" + escape(unescape(location.pathname)));
    cookie_parts.push("path=/");
    cookie_parts.push("expires=" + expires.toGMTString());
    cookie_parts.push("max-age=" + max_age);
    var cookie_string = cookie_parts.join("; ");
//alert(cookie_string);    
    document.cookie = cookie_string;    
}

////////////////////////////////////////////////////////////////////////////////
//function getCookie(c_name) {
//    if (document.cookie.length > 0) {
//        var c_start = document.cookie.indexOf(c_name + "=");
//        if (c_start != -1) { 
//            c_start = c_start + c_name.length + 1; 
//            c_end = document.cookie.indexOf(";", c_start);
//            if (c_end == -1) {
//                c_end = document.cookie.length;
//            }
//            return unescape(document.cookie.substring(c_start,c_end));
//        } 
//    }
//    return "";
//}

////////////////////////////////////////////////////////////////////////////////
KTreeStatic_1_LoadState = function(a_navistate) {
    _imp_navi_state = a_navistate;
    for (expandedid in _imp_navi_state) {
        //console.log("expandid=" + expandedid);
        try {
            _imp_navi_nodes[expandedid][8] = 1;
        }
        catch (e) {}
    }
}

