/*--------------------------------------------------|
| dTree 2.05 | www.destroydrop.com/javascript/tree/ |
|---------------------------------------------------|
| Copyright (c) 2002-2003 Geir Landroe          |
|                                                  |
| This script can be used freely as long as all     |
| copyright messages are intact.                    |
|                                                   |
| Updated: 17.04.2003                               |
|--------------------------------------------------*/

//--------------------------------------------------------
// Node object
//--------------------------------------------------------
function Node(id, pid, name, open, check, map, wms, legend, usemap, mapscmin, mapscmax, usemapscmin, usemapscmax) {
    this.id = id;                  // own id
    this.pid = pid;                // parent id
    this.name = name;
    this.check = check || false;   // checked?
    this._io = open || false;      // open or closed
    this._ls = false;              // last sibling
    this._hc = false;              // has children?
    this._ai = 0;                  // own index
    this._pi;                      // parent index
    this.map = map;                // map string
    this.wms = wms;                // WMS name
    this.legend = legend;          // Legend
    this.usemap = usemap;          // Usemap?
    this.clickable = true;         // clickable?
    this.openable = true;          // openable?
    this.active = true;            // active?
    this.visible = true;           // visible?
    this.mapscmin = mapscmin;      // minimum mapscale
    this.mapscmax = mapscmax;      // maximum mapscale
    this.usemapscmin = usemapscmin;// minimum usemapscale
    this.usemapscmax = usemapscmax;// maximum usemapscale
// Hennef Spezial
if (pid==0) this.openable=false;
if (pid==0 || pid==9) this.clickable=false;
if (id==37 || id==63) this.clickable=false;
if (id==3) this.visible=false;
};

//--------------------------------------------------------
// Tree object
//--------------------------------------------------------
function dTree(objName, style, lines, text) {
    this.config = {
        useLines: true,
        clickText: true
    }
    if (style=="classic") {
      this.icon = {
        node: 'img/page.gif',
        empty: 'img/pfeil-klein_empty.gif',
        join: 'img/join.gif', 
        joinBottom: 'img/joinbottom.gif', 
        plus: 'img/pfeil-klein_rechts.gif',    
        plusBottom: 'img/pfeil-klein_rechts.gif',
        minus: 'img/pfeil-klein_unten.gif',
        minusBottom: 'img/pfeil-klein_unten.gif',
        nlPlus: 'img/pfeil-klein_rechts.gif',
        nlMinus: 'img/pfeil-klein_unten.gif',
        checkon: 'images/haekchen_on.gif',
        checkoff: 'images/haekchen_off.gif',
        checkoninactive: 'images/haekchen_on_grau.gif',
        checkoffinactive: 'images/haekchen_off_grau.gif',
        checkempty: 'images/haekchen_empty.gif'      
      };
    }
    else { // original
      this.icon = {
        root: 'img2/base.gif',
        folder: 'img2/folder.gif',
        folderOpen: 'img2/folderopen.gif',
        node: 'img2/page.gif',
        empty: 'img2/empty.gif',
        line: 'img2/line.gif',
        join: 'img2/join.gif',
        joinBottom: 'img2/joinbottom.gif',
        plus: 'img2/plus.gif',
        plusBottom: 'img2/plusbottom.gif',
        minus: 'img2/minus.gif',
        minusBottom: 'img2/minusbottom.gif',
        nlPlus: 'img2/nolines_plus.gif',
        nlMinus: 'img2/nolines_minus.gif',
        checkon: 'images/haekchen_on.gif',
        checkoff: 'images/haekchen_off.gif',
        checkoninactive: 'images/haekchen_on_grau.gif',
        checkoffinactive: 'images/haekchen_off_grau.gif',
        checkempty: 'images/haekchen_empty.gif'   
      };
    }
    if (lines==true) this.config.useLines=true;
    else this.config.useLines=false;
    if (text==true) this.config.clickText=true;
    else this.config.clickText=false;
    this.obj = objName;
    this.aNodes = [];
    this.aIndent = [];
    this.root = new Node(-1);
    this.completed = false;
};

//--------------------------------------------------------
// Adds a new node to the node array
//--------------------------------------------------------
dTree.prototype.add = function(id, pid, name, open, checked, map, wms, legend, usemap, mapscmin, mapscmax, usemapscmin, usemapscmax) {
    this.aNodes[this.aNodes.length] = new Node(id, pid, name, open, checked, map, wms, legend, usemap, mapscmin, mapscmax, usemapscmin, usemapscmax);
};

//--------------------------------------------------------
// Creates the tree structure
//--------------------------------------------------------
dTree.prototype.buildTree = function(pNode) {
    var n=0;
    if (!pNode) pNode=this.root;
    for (n; n<this.aNodes.length; n++) {
        if (this.aNodes[n].pid == pNode.id) { // find all sons
            var cn = this.aNodes[n];
            cn._pi = pNode;
            cn._ai = n;
            this.setCS(cn);
            this.buildTree(cn);
            if (cn._ls) break;
        }
    }
// Hennef
    this.mapbunt=this.aNodes[2].map;
    this.mapsw=this.aNodes[3].map;
};

//--------------------------------------------------------
// Checks if a node has any children and if it is the last sibling
//--------------------------------------------------------
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;
};

//--------------------------------------------------------
// Outputs the tree to the page
//--------------------------------------------------------
dTree.prototype.toString = function() {
    var str = '<div class="dtree">\n';
    if (document.getElementById) {
      str += this.addNode(this.root);
//      str += "<hr width=\"95%\" style=\"margin-top: 4px; margin-bottom:4px; height: 0px;\">";

// ====================================================================================================================
//   Hier werden einige Layer fest verankert und nicht ueber die karten.xml implementiert!!!
// ====================================================================================================================
//    str += '<img src="' + this.icon.empty + '" alt=""><img name=\"check\" id=\"locations\" style="padding-bottom:3px; padding-top:3px;" value=\"\" src='+icon+' onClick=\"my_toggle_locations();\"> Ausflugspunkte<br>';
//      str += '<img src="' + this.icon.empty + '" alt=""><img name=\"check\" id=\"blackwhite\" style="padding-bottom:3px; padding-top:3px;" value=\"\" src='+icon+' onClick=\"my_toggle_blackwhite();\"> Schwarz/Weiss Modus<br>';
//      str += '<img src="' + this.icon.empty + '" alt=""><img name=\"check\" id=\"hauptstrnamen\" style="padding-bottom:3px; padding-top:3px;" value=\"\" src='+icon+' onClick=\"my_toggle_hauptstr();\"> Hauptstra&szlig;ennamen<br>';
//      str += '<img src="' + this.icon.empty + '" alt=""><img name=\"check\" id=\"nebenstrnamen\" style="padding-bottom:3px; padding-top:3px;" value=\"\" src='+icon+' onClick=\"my_toggle_nebenstr();\"> Nebenstra&szlig;ennamen';
// ====================================================================================================================
    }
    else str += 'Browser not supported.';
    str += '</div>';
    this.completed = true;
    return str;
};

//--------------------------------------------------------
// Runs through the tree
//--------------------------------------------------------
dTree.prototype.addNode = function(pNode) {
    var str = '';
    var n=0;
    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);
            str += this.node(cn, n);
            if (cn._ls) break;
        }
    }
    return str;
};

//--------------------------------------------------------
// Creates the node icon and text
//--------------------------------------------------------
dTree.prototype.node = function(node, nodeId) {
    var str = '', onclick='';
    if (node.visible==true) {
      var str = '<div class="dTreeNode">';
      if (node.openable==true) str += this.indent(node, nodeId);
      if (node.pid != this.root.id) {
          if (node.active==true) { 
              if (node.check==true) icon=this.icon.checkon; else icon=this.icon.checkoff;
              onclick='onclick=\"javascript: ' + this.obj + '.setLayercheck('+nodeId+',false);\" style=\"cursor:pointer\"';
          }
          else {
              if (node.check==true) icon=this.icon.checkoninactive; else icon=this.icon.checkoffinactive;
              onclick='style=\"cursor:default\"';
          }
          if (node.clickable==true) str += '<img name=\"check\" style="padding-bottom:3px; padding-top:3px;" value=\"\" id=\"node'+nodeId+'\" src='+icon+' '+onclick+'>';
          if (this.config.clickText && node.clickable==true) {
              str += '<span id=\"text'+nodeId+'\" '+onclick+' class=\"'+(node.active==true?'active':'inactive')+'\"> '+node.name+'</span>';
          }
          else str += ' '+node.name;
      }
      str += '</div>';
      if (node._hc) {
        str += '<div id="d' + this.obj + nodeId + '" class="clip" style="display:' + ((this.root.id == node.pid || node._io) ? 'block' : 'none') + ';">';
        str += this.addNode(node);
        str += '</div>';
      }
    }
    this.aIndent.pop();
    return str;
};

//--------------------------------------------------------
// Adds the empty and line icons
//--------------------------------------------------------
dTree.prototype.indent = function(node, nodeId) {
    var str = '';
    if (this.root.id != node.pid) {
//      for (var n=1; n<this.aIndent.length; n++)
        for (var n=(node._hc?0:1); n<this.aIndent.length; n++)
           str += '<img src="' + ( (this.aIndent[n] == 1 && this.config.useLines) ? this.icon.line : this.icon.empty ) + '" alt="" />';
            (node._ls) ? this.aIndent.push(0) : this.aIndent.push(1);
        if (node._hc) {
            str += '<img id="j' + this.obj + nodeId + '" src="';
            if (!this.config.useLines) str += (node._io) ? this.icon.nlMinus : this.icon.nlPlus;
            else str += ( (node._io) ? ((node._ls && this.config.useLines) ? this.icon.minusBottom : this.icon.minus) : ((node._ls && this.config.useLines) ? this.icon.plusBottom : this.icon.plus ) );
            str += '" alt="" onClick=\"javascript: ' + this.obj + '.o('+nodeId+');\" style=\"cursor:pointer\"/>';
        } else str += '<img src="' + ( (this.config.useLines) ? ((node._ls) ? this.icon.joinBottom : this.icon.join ) : this.icon.empty) + '" alt="" />';
    }
    return str;
};

//--------------------------------------------------------
// Adds the empty and line icons
//--------------------------------------------------------
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;
};

//--------------------------------------------------------
// Toggle Open or close
//--------------------------------------------------------
dTree.prototype.o = function(id) {
    var cn = this.aNodes[id];
    if (cn._io==true) {
      if (this.checkAllChildren(id,true)) return;
    }
    this.nodeStatus(!cn._io, id, cn._ls);
    cn._io = !cn._io;
    my_statistik("title="+encodeURI(cn.name)+"&open="+cn._io+"&action=layer_open");
    eventID=2;
};

//--------------------------------------------------------
// Checks if all children of a node are selected or unselected
//--------------------------------------------------------
dTree.prototype.checkAllChildren = function(node, check) {
    for (var n=0; n<this.aNodes.length; n++) {
        if (this.aNodes[n].pid == node) {
            if (this.aNodes[n].check==check || this.checkAllChildren(n,check)) return true;
        }
    }
    return false;
}

//--------------------------------------------------------
// Open or close all nodes
//--------------------------------------------------------
dTree.prototype.oAll = function(status) {
    for (var n=0; n<this.aNodes.length; n++) {
        if (this.aNodes[n]._hc && this.aNodes[n].pid != this.root.id) {
            this.nodeStatus(status, n, this.aNodes[n]._ls);
            this.aNodes[n]._io = status;
        }
    }
};

//--------------------------------------------------------
// Opens the tree to a specific node
//--------------------------------------------------------
dTree.prototype.openTo = function(nId, bFirst) {
    if (!bFirst) {
        for (var n=0; n<this.aNodes.length; n++) {
            if (this.aNodes[n].id == nId) {
                nId=n;
                break;
            }
        }
    }
    var cn=this.aNodes[nId];
    if (cn.pid==this.root.id || !cn._pi) return;
    cn._io = true;
    if (this.completed && cn._hc) this.nodeStatus(true, cn._ai, cn._ls);
    this.openTo(cn._pi._ai, true);
};

//--------------------------------------------------------
// Change the status of a node(open or closed)
//--------------------------------------------------------
dTree.prototype.nodeStatus = function(status, id, bottom) {
    eDiv= document.getElementById('d' + this.obj + id);
    eJoin= document.getElementById('j' + this.obj + id);
    eJoin.src = (this.config.useLines)?
        ((status)?((bottom)?this.icon.minusBottom:this.icon.minus):((bottom)?this.icon.plusBottom:this.icon.plus)):
        ((status)?this.icon.nlMinus:this.icon.nlPlus);
    eDiv.style.display = (status) ? 'block': 'none';
};

//--------------------------------------------------------
// If Push and pop is not implemented by the browser
//--------------------------------------------------------
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;
    }
};
var numcheck=0;
var maxcheck=4;
//--------------------------------------------------------
// Called when a checkbox is checked or unchecked
//--------------------------------------------------------
dTree.prototype.setLayercheck = function(id,single) {
    if (!my_checkWMSComplete()) return;
    if (this.aNodes[id].active==false) return;
    var check=this.aNodes[id].check;
if (check==false && id>9 && numcheck==maxcheck) { alert("Sie koennen nur maximal "+maxcheck+" Karten des Umweltatlas gleichzeitig auswaehlen!"); return; }
if (check==false && id>9) numcheck++;
if (check==true && id>9) numcheck--;
    if (check==true) check=false; else check=true;
    this.aNodes[id].check=check;
    if (check) document.getElementById('node'+id).src=this.icon.checkon;
    else document.getElementById('node'+id).src=this.icon.checkoff;
    if (this.aNodes[id].legend!="") this.showLegend(id);
    if (single==true) return;
    this.specialcheck(id,check);
    this.toggleAllChildren(id, check);
    if (this.aNodes[id].wms=="2") my_update_maplayers(true); else my_update_maplayers();
    my_statistik("title="+encodeURI(this.aNodes[id].name)+"&check="+check+"&action=layer_select");
    my_setMapRequest(0);
    eventID=2;
};

//--------------------------------------------------------
// Toggle Check/Uncheck of all children of a node
//--------------------------------------------------------
dTree.prototype.toggleAllChildren = function(node, check) {
    for (var n=0; n<this.aNodes.length; n++) {
        if (this.aNodes[n].pid == node) {
            //document.getElementById('node'+this.aNodes[n].id).check=check; 
            if (check==true) { document.getElementById('node'+this.aNodes[n].id).src=this.icon.checkon; this.aNodes[n].check=true; }
            else { document.getElementById('node'+this.aNodes[n].id).src=this.icon.checkoff; this.aNodes[n].check=false; }
            if (this.aNodes[n].legend!="") this.showLegend(n);
            this.specialcheck(n, check);
            if (this.aNodes[n]._hc) { this.toggleAllChildren(n,check); }
        }
    }
};

dTree.prototype.showLegend = function(id) {
    var show=true;
    for (var n=0; n<this.aNodes.length; n++) {
       if ((n!=id) && (this.aNodes[n].legend==this.aNodes[id].legend) && (this.aNodes[n].check==true) && (this.aNodes[n].active==true)) show=false;
    }
    if (show==true) my_subShow(this.aNodes[id].legend, this.aNodes[id].check);
}

dTree.prototype.getMaps = function(wms,sep,indent,usemap) {
    var str=""; var str2="";
    for (var n=0; n<this.aNodes.length; n++) {
       if (this.aNodes[n].check==true && this.aNodes[n].active==true && this.aNodes[n].map.length>1 && this.aNodes[n].wms==wms) {
          if (usemap==false || (this.aNodes[n].usemap==true && this.aNodes[n].usemapscmin<=myScale && this.aNodes[n].usemapscmax>=myScale)) {
             if (str.length==0) { 
                str=indent+this.aNodes[n].map+indent; str2="logicallayer";
             }
             else { str+=sep+indent+this.aNodes[n].map+indent; str2+=",logicallayer"; }
          }
       }
    }
    return new Array(str,str2);
};


dTree.prototype.setLayers = function(layer) {
    var i, n;
    for (n=0; n<this.aNodes.length; n++) { 
       if (this.aNodes[n].pid!=this.root.id && this.aNodes[n].check==true) {
           this.aNodes[n].check=false; 
           document.getElementById('node'+this.aNodes[n].id).src=this.icon.checkoff;
       }
    }
/*
    blackwhite=false;
    document.getElementById("blackwhite").src=d.icon.checkoff;
    hauptstrassen=false;

    document.getElementById("hauptstrnamen").src=d.icon.checkoff;
    nebenstrassen=false;
    document.getElementById("nebenstrnamen").src=d.icon.checkoff;
*/
    this.oAll(false);
    layers=layer.split(',');
    for (i=0; i<layers.length; i++) {
/*
       if (layers[i]=="blackwhite") {
          blackwhite=true;
          document.getElementById("blackwhite").src=d.icon.checkon;
       }
       else if (layers[i]=="hauptstrassen") {
          hauptstrassen=true;
          document.getElementById("hauptstrnamen").src=d.icon.checkon;
       }
       else if (layers[i]=="nebenstrassen") {
          nebenstrassen=true;
          document.getElementById("nebenstrnamen").src=d.icon.checkon;
       }
*/
       for (n=0; n<this.aNodes.length; n++) {
          if (this.aNodes[n].pid!=this.root.id && this.aNodes[n].map==layers[i]) { 
             this.aNodes[n].check=true; 
             document.getElementById('node'+this.aNodes[n].id).src=this.icon.checkon;
          }
       }
    }
};

dTree.prototype.mapcheck = function(layername) {
    if (layername==null || layername.length<=1) return true;
    for (var n=0; n<this.aNodes.length; n++) {
       if (this.aNodes[n].map==layername && this.aNodes[n].check==true && this.aNodes[n].active==true) return true;
    }
    return false;
};

dTree.prototype.scalecheck = function(scale) {
    for (n=1; n<this.aNodes.length; n++) {
        if (this.aNodes[n].visible==true) {
            if (this.aNodes[n].mapscmin<=scale && this.aNodes[n].mapscmax>=scale) {
                if (this.aNodes[n].active==false) this.activate(n, true);
            }
            else {
                if (this.aNodes[n].active==true) this.activate(n, false);
           }
        }
    }
}

dTree.prototype.activate = function(id, active) {
    if (active==true) {
        if (this.aNodes[id].clickable==true) {
            if (this.aNodes[id].check==true) document.getElementById('node'+id).src=this.icon.checkon;
            else document.getElementById('node'+id).src=this.icon.checkoff;
            document.getElementById('text'+id).className='active';
            document.getElementById('node'+id).style.cursor="pointer";
            document.getElementById('text'+id).style.cursor="pointer";
        }
        this.aNodes[id].active=true;
    }
    else {
        if (this.aNodes[id].clickable==true) {
            if (this.aNodes[id].check==true) document.getElementById('node'+id).src=this.icon.checkoninactive;
            else document.getElementById('node'+id).src=this.icon.checkoffinactive;
            document.getElementById('text'+id).className='inactive';
            document.getElementById('node'+id).style.cursor="default";
            document.getElementById('text'+id).style.cursor="default";
        }
        this.aNodes[id].active=false;
    }
}


/* Hennef-Spezial-Button-Funktionen */
dTree.prototype.specialcheck = function(id, check) {
   // Buttons 2,3,4, 5 und 6 sind Radio-Buttons (schliessen sich gegenseitig aus) 
   if (id==2 || id==3 || id==4 || id==5 || id==6  && check==true) {
       if (id!=2 && this.aNodes[2].check==true) this.setLayercheck(2,true);
       if (id!=3 && this.aNodes[3].check==true) this.setLayercheck(3,true);
       if (id!=4 && this.aNodes[4].check==true) this.setLayercheck(4,true);
       if (id!=5 && this.aNodes[5].check==true) this.setLayercheck(5,true);
       if (id!=6 && this.aNodes[6].check==true) this.setLayercheck(6,true);
    }
    // Wird der Button 1 (Basiskarten) ausgewaehlt, werden die Buttons 2,6 und 7 gesetzt
//    else if (id==1 && check==true) { 
//       if (this.aNodes[2].check==false) this.setLayercheck(2,true);
//       if (this.aNodes[3].check==true) this.setLayercheck(3,true);
//       if (this.aNodes[4].check==true) this.setLayercheck(4,true);
//       if (this.aNodes[5].check==true) this.setLayercheck(5,true);
//       if (this.aNodes[6].check==false) this.setLayercheck(6,true);
//       if (this.aNodes[7].check==false) this.setLayercheck(7,true);
//       if (this.aNodes[8].check==true) this.setLayercheck(8,true);
//    }
    // Wird eine Umweltkarte ausgewaehlt, so wird der Stadtplan auf schwarz/weiss gestellt,
    // wird die letzte Umweltkarte abgewaehlt, wird wieder zurueck auf bunt gestellt
    else if (id>8) {
       if (check==true) { this.aNodes[2].map=this.mapsw; }
       else {
          var last=true;
          for (var n=9; n<this.aNodes.length; n++) if (this.aNodes[n].check==true) last=false;
          if (last==true) { this.aNodes[2].map=this.mapbunt; }
       }
    }
    // Fuer das Luftbild wird ein Copyright-Hinweis auf der Karte eingeblendet
    if (this.aNodes[4].check==true) myWMS[0][0]=wms2+"&imgexit=myWMS.userexits.img_copyright"; else myWMS[0][0]=wms2;
    // Fuer Biotope wird ein Copyright-Hinweis unter der Karte eingeblendet
    if (id==11) {
       if (check==true) document.getElementById("Copyright_Biotope").style.display="inline";
       else document.getElementById("Copyright_Biotope").style.display="none";
    }
    my_infobox_close();

};
