function CatTreeItem(pDeep, pParent, pChildren, pId, pIdParent, pIdCat, pNameShort, pNameLong, pUrl, pViewPosition, pSelected, pChecked) {
  var deep;
  var parent;
  var children;
  var id;
  var idParent;
  var idCat;
  var nameShort;
  var nameLong;
  var url;
  var viewPosition;
  var selected;
  var checked;
  var oldSelected;
  var descendants;
  var catInMap;

  var self = this;

  this.initialize = function(pDeep, pParent, pChildren, pId, pIdParent, pIdCat, pNameShort, pNameLong, pUrl, pViewPosition, pSelected, pChecked) {
    deep = pDeep;
    parent = pParent;
    children = pChildren;
    id = pId;
    idParent = pIdParent;
    idCat = pIdCat;
    nameShort = pNameShort;
    nameLong = pNameLong;
    url = pUrl;
    viewPosition = pViewPosition;
    selected = pSelected;
    checked = pChecked;
    oldSelected = selected;
    descendants = 0;
    catInMap = 10;
  }

  //---------------------------------------------------------------------------

  this.evalDescendants = function() {
    descendants = 0;

    if(children != null) {
      for(var i=0 ; i<children.length ; i++) {
        descendants += children[i].evalDescendants() + 1;
      }
    }

    return descendants;
  }

  this.setDeep = function(pDeep) {
    deep = pDeep;
  }

  this.getDeep = function() {
    return deep;
  }

  this.setParent = function(pParent) {
    parent = pParent;
  }

  this.getParent = function() {
    return parent;
  }

  this.setChildren = function(pChildren) {
    children = pChildren;
  }

  this.getChildren = function() {
    return children;
  }

  this.setId = function(pId) {
    id = pId;
  }

  this.getId = function() {
    return id;
  }

  this.setIdParent = function(pIdParent) {
    idParent = pIdParent;
  }

  this.getIdParent = function() {
    return idParent;
  }

  this.setIdCat = function(pIdCat) {
    idCat = pIdCat;
  }

  this.getIdCat = function() {
    return idCat;
  }

  this.setNameShort = function(pNameShort) {
    nameShort = pNameShort;
  }

  this.getNameShort = function() {
    return nameShort;
  }

  this.setNameLong = function(pNameLong) {
    nameLong = pNameLong;
  }

  this.getNameLong = function() {
    return nameLong;
  }

  this.setUrl = function(pUrl) {
    url = pUrl;
  }

  this.getUrl = function() {
    return url;
  }

  this.setViewPosition = function(pViewPosition) {
    viewPosition = pViewPosition;
  }

  this.getViewPosition = function() {
    return viewPosition;
  }

  this.setSelected = function(pSelected) {
    selected = pSelected;
  }

  this.getSelected = function() {
    return selected;
  }

  this.setOldSelected = function(pOldSelected) {
    oldSelected = pOldSelected;
  }

  this.getOldSelected = function() {
    return oldSelected;
  }

  this.setDescendants = function(pDescendants) {
    descendants =  pDescendants;
  }

  this.getDescendants = function() {
    return descendants;
  }

  this.setChecked = function(pChecked) {
    checked =  pChecked;
  }

  this.getChecked = function() {
    return checked;
  }

  this.setCatInMap = function(pcatInMap) {
//    alert("pcatInMap = " + pcatInMap);
    catInMap =  pcatInMap;
  }

  this.getCatInMap = function() {
    return catInMap;
  }

  this.toString = function() {
    return "deep: " + deep +
           " - parent: " + (parent instanceof CatTreeItem ? parent.getId() : 0) +
           " - children: " + (children instanceof Array ? children.length : 0) +
           " - id: " + id +
           " - idParent: " + idParent +
           " - idCat: " + idCat +
           " - nameShort: " + nameShort +
           " - nameLong: " + nameLong +
           " - url: " + url +
           " - viewPosition: " + viewPosition +
           " - selected: " + selected +
           " - checked: " + checked +
           " - oldSelected: " + oldSelected +
           " - descendants: " + descendants +
           " - catInMap: " + catInMap;
  }

  //---------------------------------------------------------------------------

  this.initialize(pDeep, pParent, pChildren, pId, pIdParent, pIdCat, pNameShort, pNameLong, pUrl, pViewPosition, pSelected, pChecked);
}

//=============================================================================

function CatTree(pFieldResult) {
  var fieldResult;
  var callbackChecked;

  var item;
  var foundItem;
  var subSelectionId;

  var self = this;

  this.initialize = function(pFieldResult) {
    fieldResult = pFieldResult;
    callbackChecked = null;

    item = null;
    foundItem = null;
    subSelectionId = -1;
  }

  //---------------------------------------------------------------------------

  this.findItemForId = function(item, id) {
    if(item instanceof CatTreeItem) {
      if(item.getId() == id) {
        if(foundItem == null) {
          foundItem = item;
        }
      }
      var children = item.getChildren();
      if(children instanceof Array) {
        for(var i=0 ; i<children.length ; i++) {
          child = children[i];
          if(child instanceof CatTreeItem) {
            self.findItemForId(child, id);
          }
        }
      }
    }
  }

  this.copyOldSelectedToSelected = function(item) {
    if(item instanceof CatTreeItem) {
      var oldSelected = item.getOldSelected();
      //console.info("id " + item.getNameShort());
      //console.info(item);
      //console.info("selected " + item.getSelected());
      //console.info("oldSelected " + oldSelected);
      item.setSelected(oldSelected);
      //console.info("selected " + item.getSelected());
      //console.info(item);
      //console.info("---");
      var children = item.getChildren();
      if(children instanceof Array) {
        for(var i=0 ; i<children.length ; i++) {
          self.copyOldSelectedToSelected(children[i]);
        }
      }
    }
  }

  this.copySelectedToOldSelected = function(item) {
    if(item instanceof CatTreeItem) {
      item.setOldSelected(item.getSelected());
      var children = item.getChildren();
      if(children instanceof Array) {
        for(var i=0 ; i<children.length ; i++) {
          self.copySelectedToOldSelected(children[i]);
        }
      }
    }
  }

  this.parentForItemAndDeep = function(item, deep) {
    var parentDeep = null;
    var parent = item.getParent();
    while(parent instanceof CatTreeItem) {
      //console.info("p "+parent);
      if(parent.getDeep() == deep) {
        parentDeep = parent;
      }
      parent = parent.getParent();
    }
    return parentDeep;
  }

  this.postEval = function(evalItem) {
    if(evalItem instanceof CatTreeItem) {
      evalItem.evalDescendants();
      var children = evalItem.getChildren();
      if(children instanceof Array) {
        for(var i=0 ; i<children.length ; i++) {
          child = children[i];
          if(child instanceof CatTreeItem) {
            child.setParent(evalItem);
            self.postEval(child);
          }
        }
      }
    }
  }

  this.eval = function(items) {
//      alert("eval!" + items);
    var itemsArray;
    var itemArray;
    var oldDeep;
    var aktDeep;
    var id;
    var idParent;
    var idCat;
    var nameShort;
    var nameLong;
    var url;
    var viewPosition;
    var selected;
    var checked;
    var catTreeItemArray;
    var catTreeItem;
    var firstIndent;

    catTreeItemArray = new Array();

    itemsArray = items.split("\t");
    if(itemsArray.length > 1) {
      subSelectionId = parseInt(itemsArray[0]);
      oldDeep = parseInt(itemsArray[1]);

      if(!(catTreeItemArray[oldDeep] instanceof Array)) {
        catTreeItemArray[oldDeep] = new Array();
      }

      for(var i=2 ; i<itemsArray.length ; i++) {
        itemArray = itemsArray[i].split("\|");

        if(itemArray.length == 10) {
          aktDeep = parseInt(itemArray[0]);
          id = parseInt(itemArray[1]);
          idParent = parseInt(itemArray[2]);
          idCat = parseInt(itemArray[3]);
          nameShort = itemArray[4];
          nameLong = itemArray[5];
          url = itemArray[6];
          viewPosition = parseInt(itemArray[7]);
          selected = parseInt(itemArray[8]);
          checked = parseInt(itemArray[9]);

          if(!(catTreeItemArray[aktDeep] instanceof Array)) {
            catTreeItemArray[aktDeep] = new Array();
          }

          catTreeItem = new CatTreeItem(aktDeep, null, null, id, idParent, idCat, nameShort, nameLong, url, viewPosition, selected, checked);

          if(oldDeep < aktDeep) {
            // eine Ebene tiefer
            catTreeItemArray[aktDeep] = new Array();
            catTreeItemArray[aktDeep].push(catTreeItem);
          } else {
            if(oldDeep > aktDeep) {
              // eine Ebene höher
              catTreeItem.setChildren(catTreeItemArray[oldDeep]);
              catTreeItemArray[oldDeep] = new Array();
              catTreeItemArray[aktDeep].push(catTreeItem);
            } else {
              // gleiche Ebene
              catTreeItemArray[oldDeep].push(catTreeItem);
            }
          }

          oldDeep = aktDeep;
        }
      }
    }

    if(catTreeItemArray.length > 0 && catTreeItemArray[0].length > 0) {
      item = catTreeItemArray[0][0];
      self.postEval(item);
    }
  }

  //---------------------------------------------------------------------------

  this.evalResult  = function(item, indent, result) {
    if(item instanceof CatTreeItem) {
      var children = item.getChildren();
      if(children instanceof Array) {
        for(var i=0 ; i<children.length ; i++) {
          result = self.evalResult(children[i], indent + 1, result);
        }
      }
    }

    if(firstIndent) {
      firstIndent = false;
      result += subSelectionId + "\t" + indent + "\t";
    }

    result += indent + "|" +
              item.getId() + "|" +
              item.getIdParent() + "|" +
              item.getIdCat() + "|" +
              item.getNameShort() + "|" +
              item.getNameLong() + "|" +
              item.getUrl() + "|" +
              item.getViewPosition() + "|" +
              item.getSelected() + "|" +
              item.getChecked() + "\t";

    return result;
  }

  this.updateResult  = function(pSubSelection) {
    var indent;
    var result;

    if(pSubSelection instanceof CatTreeItem) {
      subSelectionId = pSubSelection.getId();
    } else {
      subSelectionId = -1;
    }
    firstIndent = true;
    indent = 0;
    result = self.evalResult(item, indent, "");

    //document.cookie = "result=" + result + "; path=/";
    //alert(document.cookie);

    $(fieldResult).value = result;

    return result;
  }

  this.getResult  = function() {
    return this.updateResult(item);
  }

  //---------------------------------------------------------------------------

  this.evalCheckedDeep = function(item, result) {
    if(item instanceof CatTreeItem) {
      var children = item.getChildren();
      if(children instanceof Array) {
        if(children.length > 0) {
          for(var i=0 ; i<children.length ; i++) {
            result = self.evalCheckedDeep(children[i], result);
          }
        } else {
          result += item.getId() + ",";
        }
      } else {
        result += item.getId() + ",";
      }
    }
    return result;
  }

  this.evalCheckedOrFlag = function(item, flag, result) {
    //console.info(item);
    if(item instanceof CatTreeItem) {
      if(item.getSelected() && (item.getChecked() || flag)) {
        var parentSelected = 0;
        var parent = item.getParent();
        while(parent instanceof CatTreeItem) {
          if(parent.getDeep() > 1) {
            parentSelected |= parent.getSelected();
          }
          parent = parent.getParent();
        }
        if(parentSelected == 0) {
          //console.info(item);
          result = self.evalCheckedDeep(item, result);
        }
      } else {
        var children = item.getChildren();
        if(children instanceof Array) {
          for(var i=0 ; i<children.length ; i++) {
            result = self.evalCheckedOrFlag(children[i], flag, result);
          }
        }
      }
    }
    return result;
  }

  this.handleChecked = function() {
    var result = self.evalCheckedOrFlag(item, false, "");
    //alert(result);
    if(callbackChecked != null) {
      callbackChecked(result);
    }
  }

  this.findItem = function(id) {
    foundItem = null;
    self.findItemForId(item, id);
    return foundItem;
  }

  this.getItem = function() {
    return item;
  }

  this.getSubSelectionId = function() {
    return subSelectionId;
  }

  this.setCallbackChecked = function(pCallbackChecked) {
    callbackChecked = pCallbackChecked;
  }

  //---------------------------------------------------------------------------

  this.initialize(pFieldResult);
}

//=============================================================================

function CatTreeDisplay(pCatTree, pDivSub, pDocSub, pDivCfg, pDocCfg, pTrCfg1, pTdCfg1, pTrCfg2, pTdCfg2, pCallbackLink, pCallbackMore, pCallbackClose, pCallbackCatPage) {
  var ID_SUB_OFFSET = 1000;

  var catTree;
  var catTreeItem;
  var divSub;
  var docSub;
  var divCfg;
  var docCfg;
  var trCfg1;
  var tdCfg1;
  var trCfg2;
  var tdCfg2;
  var callbackLink;
  var callbackMore;
  var callbackClose;
  var callbackCatPage;

  var cfgSelection;
  var subSelection;

  var maxCfgCat;
  var maxSubCat;

  var self = this;

  this.initialize = function(pCatTree, pDivSub, pDocSub, pDocCfg, pDivCfg, pTrCfg1, pTdCfg1, pTrCfg2, pTdCfg2, pCallbackLink, pCallbackMore, pCallbackClose, pCallbackCatPage) {
//      alert("initialize CatTreeDisplay");
    catTree = pCatTree;
    catTreeItem = pCatTree.getItem();
    divSub = pDivSub;
    docSub = pDocSub;
    divCfg = pDivCfg;
    docCfg = pDocCfg;
    trCfg1 = pTrCfg1;
    tdCfg1 = pTdCfg1;
    trCfg2 = pTrCfg2;
    tdCfg2 = pTdCfg2;
    callbackLink = pCallbackLink;
    callbackMore = pCallbackMore;
    callbackClose = pCallbackClose;
    callbackCatPage = pCallbackCatPage;

    maxCfgCat = 10;
    maxSubCat = 4;

    cfgSelection = null;
    subSelection = catTree.findItem(catTree.getSubSelectionId());
  }

  //---------------------------------------------------------------------------

  this.findCb = function(id) {
    var elements = docSub.elements;
    var cb = null;
    for(var i=0 ; i<elements.length ; i++) {
      if(elements[i].type == "checkbox") {
        if(elements[i].id == id) {
          cb = elements[i];
        }
      }
    }
    if(cb == null) {
      elements = docCfg.elements;
      for(var i=0 ; i<elements.length ; i++) {
        if(elements[i].type == "checkbox") {
          if(elements[i].id == id) {
            cb = elements[i];
          }
        }
      }
    }
    return cb;
  }

  this.setCfgCbAndItem = function(id, value) {
    var cb = self.findCb(id);
    if(cb != null) {
      cb.checked = value;
      cb.defaultChecked = value;
    }
    var item = catTree.findItem(id);
    if(item instanceof CatTreeItem) {
      item.setSelected(value);
    }
  }

  // Setzt aktuelle Cb/Item und alle Nachkommen
  this.setCfgCbAndItemAndDescendants = function(item, value) {
    if(item instanceof CatTreeItem) {
      self.setCfgCbAndItem(item.getId(), value);

      var children = item.getChildren();
      if(children instanceof Array) {
        for(var i=0 ; i<children.length ; i++) {
          self.setCfgCbAndItemAndDescendants(children[i], value);
        }
      }
    }
  }

  // Setzt aktuelle Cb/Item und alle Nachkommen und alle Vorfahren
  this.setCfgCbAndItemAndDescendantsAndAncestor = function(id) {
    var item = catTree.findItem(id);
    //console.info(item);
    if(item instanceof CatTreeItem) {
      var cb = self.findCb(item.getId());
      if(cb != null) {
        //console.info(cb.checked);
        self.setCfgCbAndItemAndDescendants(item, cb.checked ? 1 : 0);

        var parent = item.getParent();
        while(parent instanceof CatTreeItem) {
          //console.info(parent);
          var cbParent = self.findCb(parent.getId());
          if(cbParent != null) {
            //console.info(cbParent.checked);
            selectedChildren = 1;
            var children = parent.getChildren();
            if(children instanceof Array) {
              for(var i=0 ; i<children.length ; i++) {
                var child = children[i];
                if(child instanceof CatTreeItem) {
                  selectedChildren &= child.getSelected();
                }
              }
            }
            self.setCfgCbAndItem(parent.getId(), selectedChildren ? 1 : 0);
          }
          parent = parent.getParent();
        }
      }
    }
  }

  this.setSubCbAndItem = function(id, value) {
    var cb = self.findCb(ID_SUB_OFFSET + parseInt(id));
    if(cb != null) {
      cb.checked = value;
      cb.defaultChecked = value;
    }
    var item = catTree.findItem(id);
    if(item instanceof CatTreeItem) {
      item.setChecked(value);
    }
  }

  // Setzt aktuelle Sub Cb/Item und alle Nachkommen
  this.setSubCbAndItemAndDescendants = function(item, value) {
    if(item instanceof CatTreeItem) {
      self.setSubCbAndItem(item.getId(), value);

      var children = item.getChildren();
      if(children instanceof Array) {
        for(var i=0 ; i<children.length ; i++) {
          self.setSubCbAndItemAndDescendants(children[i], value);
        }
      }
    }
  }

  this.deep1 = function(item, state) {
    //console.info("deep1 "+item);
    if(item instanceof CatTreeItem) {
      var children = item.getChildren();
      if(children instanceof Array) {
        for(var i=0 ; i<children.length ; i++) {
          var child = children[i];
          if(child instanceof CatTreeItem) {
            if(child.getSelected()) {
              state &= child.getChecked();
            }
            state = self.deep1(child, state);
          }
        }
      }
    }
    return state;
  }

  // Setzt aktuelle Sub Cb/Item und alle Nachkommen und alle Vorfahren
  this.setSubCbAndItemAndDescendantsAndAncestor = function(id) {
    var item = catTree.findItem(id);
    //console.info(">>> "+item);
    if(item instanceof CatTreeItem) {
      var cb = self.findCb(ID_SUB_OFFSET + parseInt(item.getId()));
      if(cb != null) {
        //console.info(cb.checked);
        self.setSubCbAndItemAndDescendants(item, cb.checked ? 1 : 0);

        var parent1 = catTree.parentForItemAndDeep(item, 1);
        if(parent1 != null) {
          deep1State = self.deep1(parent1, 1);
          //console.info(deep1State);
          self.setSubCbAndItem(parent1.getId(), deep1State);
        }
      }
    }
  }

  // Setzt alle Cb wie ihre Item
  this.setCfgCbByItem = function(item) {
    if(item instanceof CatTreeItem) {
      self.setCfgCbAndItem(item.getId(), item.getSelected());
      //console.info(item);

      var children = item.getChildren();
      if(children instanceof Array) {
        for(var i=0 ; i<children.length ; i++) {
          self.setCfgCbByItem(children[i]);
        }
      }
    }
  }

  //-----------------------------------------------------------------------------

  this.catInMapDeep = function(item, catInMap) {
    if(item instanceof CatTreeItem) {
      if(item.getChecked()) {
        catInMap += item.getCatInMap();
      }

      var children = item.getChildren();
      if(children instanceof Array) {
        for(var i=0 ; i<children.length ; i++) {
          catInMap = self.catInMapDeep(children[i], catInMap);
        }
      }
//      console.info(catInMap + " - " + item.getNameLong());
    }
    return catInMap;
  }

  this.catInMap = function(item, catInMap) {
    var children = item.getChildren();
    if(children instanceof Array) {
      for(var i=0 ; i<children.length ; i++) {
        var child = children[i];
        if(child instanceof CatTreeItem) {
          //console.info(catInMap + " - " + child.getNameLong());
          catInMap = self.catInMapDeep(child, catInMap);
        }
      }
    }
    return catInMap;
  }

  this.catInMapShow = function() {
    if(catTreeItem instanceof CatTreeItem) {
      var children = catTreeItem.getChildren(); // roots children
      if(children instanceof Array) {
        for(var i=0 ; i<children.length ; i++) {
          var child = children[i];
          if(child instanceof CatTreeItem) {
            catInMap = self.catInMap(child, 0);
            //console.info(catInMap + " - " + child.getNameShort());
            if(catInMap > 0) {
              $("CNT"+ parseInt(child.getId())).update("&nbsp;[<b>" + catInMap + "</b>]");
            } else {
              $("CNT"+ parseInt(child.getId())).update("");
            }
          }
        }
      }
    }
  }

  this.catInMapClearItem = function(item) {
    if(item instanceof CatTreeItem) {
      item.setCatInMap(0);

      var children = item.getChildren();
      if(children instanceof Array) {
        for(var i=0 ; i<children.length ; i++) {
          var child = children[i];
          if(child instanceof CatTreeItem) {
            self.catInMapClearItem(child);
          }
        }
      }
    }
  }

  this.catInMapClear = function() {
    self.catInMapClearItem(catTreeItem);
    self.catInMapShow();
  }

  this.catInMapAddItem = function(item, idCat, cnt) {
    if(item instanceof CatTreeItem) {
      if(item.getIdCat() == idCat) {
        item.setCatInMap(item.getCatInMap() + parseInt(cnt));
      }
      var children = item.getChildren();
      if(children instanceof Array) {
        for(var i=0 ; i<children.length ; i++) {
          var child = children[i];
          if(child instanceof CatTreeItem) {
            self.catInMapAddItem(child, idCat, cnt);
          }
        }
      }
    }
  }

  this.catInMapAdd = function(idCat, cnt) {
    self.catInMapAddItem(catTreeItem, idCat, cnt);
  }

  //-----------------------------------------------------------------------------

  this.clearCfg = function(item) {
    $(trCfg1).update("");
    tdCfg1 = new Element("td", { "id": "tdCfg1"});
    $(trCfg1).appendChild(tdCfg1);

    $(tdCfg2).update("");
  }

  this.displayCfgCb = function(item) {
    var cntSelected;
    var selectedId = -1;
    var selectedState = -1;
    //console.info(item);
    if(item.getDeep() > 1) {
      var div = new Element("div", { });
      var nbr = new Element("nobr", { });

      var s = "";
      for(var i=0 ; i<item.getDeep()-2 ; i++) {
        s += "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
      }
      $(nbr).update(s);

      var cb = new Element("input", { "type": "checkbox", "id":  parseInt(item.getId()) });
      $(cb).checked = item.getSelected();
      $(cb).defaultChecked = item.getSelected();
      $(cb).observe('click', function(event) {
        self.setCfgCbAndItemAndDescendantsAndAncestor(item.getId());

        self.setSubCbAndItemAndDescendantsAndAncestor(item.getId());
        cntSelected = self.displaySubMain();
        self.setSubCbAndItemAndDescendantsAndAncestor(item.getId());

        selectedId = item.getId();
        selectedState = item.getSelected();

        if(cntSelected > maxSubCat) {
          catTree.copyOldSelectedToSelected(catTreeItem);

          $(cb).checked = item.getSelected();
          $(cb).defaultChecked = item.getSelected();

          self.setCfgCbByItem(catTreeItem);

          self.setSubCbAndItemAndDescendantsAndAncestor(item.getId());
          cntSelected = self.displaySubMain();
          self.setSubCbAndItemAndDescendantsAndAncestor(item.getId());

          selectedId = -1;
          selectedState = -1;

          alert("Sie können leider nicht mehr\nals " + maxSubCat + " Kategorien auswählen.");
        } else {
          catTree.copySelectedToOldSelected(catTreeItem);

          if(cntSelected == 0) {
            if(item.getDeep() == 1) {
              self.setSubCbAndItem(item.getId(), 0);
            } else {
              var parent1 = catTree.parentForItemAndDeep(item, 1);
              if(parent1 != null) {
                self.setSubCbAndItem(parent1.getId(), 0);
              }
            }
          }
        }

        catTree.updateResult(subSelection);
      });
      $(nbr).appendChild(cb);

      $(nbr).appendChild(document.createTextNode(item.getNameLong()));

      $(div).appendChild(nbr);

      $(tdCfg1).appendChild($(div));
    }
  }

  this.cfgClose = function() {
    self.clearCfg();
    cfgSelection = null;
  }

  this.displayCfgClose = function() {
    var div = new Element("div", { });

    var link = new Element("a", { });
    link.appendChild(document.createTextNode("schließen"));
    $(link).observe('click', function(event) {
      if(callbackClose) {
        callbackClose();
      }
      //self.clearCfg();
      //cfgSelection = null;
    });

    $(div).appendChild(link);
    $(div).style.cursor = "pointer";

    $(tdCfg2).appendChild($(div));
  }

  this.displayCfg = function(item, cnt) {
    if(item instanceof CatTreeItem) {
      self.displayCfgCb(item);
      cnt++;
      var children = item.getChildren();
      if(children instanceof Array) {
        for(var i=0 ; i<children.length ; i++) {
          var child = children[i];
          if(child instanceof CatTreeItem) {
            if(child.getDeep() <= 2 && cnt + child.getDescendants() > maxCfgCat) {
              $(tdCfg1).id = "";
              tdCfg1 = new Element("td", { "id": "tdCfg1", "valign": "top"});

              //var div = new Element("div", { }).update("&nbsp;");
              //$(tdCfg1).appendChild(div);

              $(trCfg1).appendChild(tdCfg1);
              cnt = 0;
            }
            cnt = self.displayCfg(child, cnt);
          }
        }
      }
    }
    return cnt;
  }

  this.displayCfgMain = function(id) {
    var localSelection = catTree.findItem(id);

    //self.clearCfg();
    if(cfgSelection == null || cfgSelection != localSelection) {
      cfgSelection = localSelection;
      var cnt = self.displayCfg(cfgSelection, 0);
      self.displayCfgClose();
    } else {
      cfgSelection = null;
    }
  }

  //-----------------------------------------------------------------------------

  this.displaySubCb = function(item) {
    //console.info(item);
    if(item.getDeep() > 0) {
      var div = new Element("div", { });
      var nbr = new Element("nobr", { });
      var imgId = "catPage" + item.getId();

      if(item.getDeep() > 1) {
        //var s = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
        var s = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src='/mallorca/web/img/Button-Weiss-1.gif' id='" + imgId + "' style='cursor: pointer;' width='35px' height='13px' alt='Übersicht anzeigen' title='Übersicht anzeigen' />";
        $(nbr).update(s);
      }

      var cb = new Element("input", { "type": "checkbox", "id":  (ID_SUB_OFFSET + parseInt(item.getId())) });
      $(cb).checked = item.getChecked();
      $(cb).defaultChecked = item.getChecked();
      $(cb).observe('click', function(event) {
        item.setChecked($(cb).checked ? 1 : 0);
        self.setSubCbAndItemAndDescendantsAndAncestor(item.getId());
        /*if(item.getNameShort() == "Golf") {
          self.catInMapClear();
        } else {
          if(item.getNameShort() == "Tennis") {
            self.catInMapAdd(29, 31);
          } else {
            self.catInMapShow();
          }
        }*/

        catTree.updateResult(subSelection);
        catTree.handleChecked();
      });
      $(nbr).appendChild(cb);

      if(item.getDeep() > 1) {
        $(nbr).appendChild(document.createTextNode(item.getNameShort()));
      } else {
        var link = new Element("a", { "alt": "aufklappen / zuklappen", "title": "aufklappen / zuklappen", "style": "text-decoration: none;" });
        $(link).appendChild(document.createTextNode(item.getNameShort()));

        var spanCnt = new Element("span", { "id": ("CNT" +  parseInt(item.getId())) });
        $(link).appendChild(spanCnt);

        $(link).observe('click', function(event) {
          if(callbackLink) {
            callbackLink();
          }
          self.displaySubDescendants(item.getId());
        });
        $(nbr).appendChild(link);

        $(div).style.cursor = "pointer";
        /*if(Prototype.Browser.Opera) {
          $(div).style.paddingBottom = "3px";
        } else {
          if(Prototype.Browser.IE) {
            $(div).style.paddingBottom = "0px";
          } else {
            $(div).style.paddingBottom = "-1px";
          }
        }*/
      }
      $(div).style.height = "17px";

      $(div).appendChild(nbr);

      $(divSub).appendChild($(div));

      if($(imgId)) {
        $(imgId).observe('click', function(event) {
           if(callbackCatPage) {
             var catIds = catTree.evalCheckedOrFlag(item, true, "");
             //alert(catIds);
             callbackCatPage(catIds);
          }
        });
      }
    }
  }

  this.displaySubMore = function(item) {
    var div = new Element("div", { });

    var s = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
    $(div).update(s);

    var link = new Element("a", { });
    link.appendChild(document.createTextNode("mehr..."));
    $(link).observe('click', function(event) {
      self.displayCfgMain(item.getId());
      if(callbackMore) {
        callbackMore();
      }
    });

    $(div).appendChild(link);
    $(div).style.cursor = "pointer";

    $(divSub).appendChild($(div));
  }

  this.displaySub = function(item, cntSelected) {
    if(item instanceof CatTreeItem) {
      var children = item.getChildren();
      if(children instanceof Array) {
        for(var i=0 ; i<children.length ; i++) {
          var child = children[i];
          if(child instanceof CatTreeItem) {
            if(child.getSelected()) {
              self.displaySubCb(child);
              cntSelected++;
            } else {
              cntSelected = self.displaySub(child, cntSelected);
            }
          }
        }
      }
    }
    return cntSelected;
  }

  this.displaySubMain = function() {
//      alert("displaySubMain");
    $(divSub).update("");
    cntSelected = 0;
    if(catTreeItem instanceof CatTreeItem) {
      var children = catTreeItem.getChildren();
      if(children instanceof Array) {
        for(var i=0 ; i<children.length ; i++) {
          var child = children[i];
          if(child instanceof CatTreeItem) {
            //child.setSelected(1);
            self.displaySubCb(child);
            if(child == subSelection) {
              cntSelected = self.displaySub(child, 0);
              self.displaySubMore(child);
            }
          }
        }
      }

      //self.catInMapShow();

      catTree.updateResult(subSelection);
      catTree.handleChecked();
    }
    return cntSelected;
  }

  this.displaySubDescendants = function(id) {
    var localSubSelection = catTree.findItem(id);

    if(subSelection == null || subSelection != localSubSelection) {
      subSelection = localSubSelection;
    } else {
      subSelection = null;
    }
    //self.clearCfg();
    self.displaySubMain();
  }

  //---------------------------------------------------------------------------

  this.initialize(pCatTree, pDivSub, pDocSub, pDocCfg, pDivCfg, pTrCfg1, pTdCfg1, pTrCfg2, pTdCfg2, pCallbackLink, pCallbackMore, pCallbackClose, pCallbackCatPage);
}

