/**
* Graph
*/
function clsGraph() {
  this.aNodes = new Object;
  this.aEdges = new Object;
  this.aArcs = new Object;
  this.aAdjList = new Object;
  this.aAdjMatrix = new Object;
  this.bDirected = false;
  this.bCycle = false;
  this.bNegWeight = false;
  this.iInf = 10000;
}

clsGraph.prototype.setNode = function(_iNode) {
  if (_iNode != null) {
    var id = _iNode;
  } else {
    var id = this.aNodes.length;
  }
  this.aNodes[id] = id;
  return id;
}

clsGraph.prototype.setEdge = function(_iEnd1, _iEnd2, _iWeight) {
  var id = this.aEdges.length;
  var iWeight = (_iWeight == null)?1:_iWeight;
  this.aEdges[id] = new Array(_iEnd1, _iEnd2, iWeight);
  if (this.aAdjList[_iEnd1] == null) {
    this.aAdjList[_iEnd1] = new Object;
  }
  this.aAdjList[_iEnd1][_iEnd2] = iWeight;
  if (this.aAdjList[_iEnd2] == null) {
    this.aAdjList[_iEnd2] = new Object;
  }
  this.aAdjList[_iEnd2][_iEnd1] = iWeight;
  return id;
}

clsGraph.prototype.setArc = function(_iTail, _iHead, _iWeight) {
  var id = this.aArcs.length;
  var iWeight = (_iWeight == null)?1:_iWeight;
  this.aArcs[id] = new Array(_iTail, _iHead, iWeight);
  if (this.aAdjList[_iTail] == null) {
    this.aAdjList[_iTail] = new Object;
  }
  this.aAdjList[_iTail][_iHead] = iWeight;
  return id;
}

/* clsGraph.prototype.toString = function() {
  return this.aAdjList;
} */

clsGraph.prototype.weight = function(_u, _v) {
  if (this.aAdjList[_u] && this.aAdjList[_u][_v]) {
    return this.aAdjList[_u][_v];
  } else {
    return this.iInf;
  }
}

/* Dijkstra's algorithm */
clsGraph.prototype.dijkstra = function(_s, _t) {
  var d = new Object;
  var done = new Object;
  var prev = new Object;
  var V = 0;
  for (var v in this.aNodes) {
    d[v] = this.iInf;
    done[v] = false;
    V++;
  }
  d[_s] = 0;
  var u, v;
  var min;
  for (var i = 0; i < V; i++) {
    min = this.iInf;
    u = -1;
    for (var v in this.aNodes) {
      if (!done[v]) {
        if (d[v] < min) {
          min = d[v];
          u = v;
        }
      }
    }
    done[u] = true;
    if (_t != null && u == _t) {
      break;
    }
    for (v in this.aAdjList[u]) {
      if (d[u] + this.aAdjList[u][v] < d[v]) {
        d[v] = d[u] + this.aAdjList[u][v];
        prev[v] = u;
      }
    }
  }
  /*
  if (_t != null) {
    var aPath = new array();
    u = _t;
    while (prev[u] != null) {
      aPath.unshift(u);
      u = prev[u];
    }
  }
  */
  return d;
}

/**
* Tree
*/
function clsTree() {
  clsGraph.apply(this);
  this.aLevels = new Object;
}
clsTree.prototype = new clsGraph();

clsTree.prototype.setChild = function(_iNode, _iParent, _iWeight) {
  var v = this.setNode(_iNode);
  if (_iParent != null) var e = this.setArc(_iParent, v, (_iWeight == null)?1:_iWeight);
  this.aLevels = this.dijkstra(0);
  return v;
}

clsTree.prototype.getChildren = function(_v) {
  return this.aAdjList[_v];
}

clsTree.prototype.getType = function(_v) {
  if (_v == 0) {
    return "Root";
  } else if (this.aAdjList[_v] == null) {
    return "Leaf";
  } else {
    return "Inner";
  }
}

clsTree.prototype.getLevel = function(_v) {
  return this.aLevels[_v];
}

clsTree.prototype.getLevels = function() {
  return this.aLevels;
}

/**
* TreeBox
*/
if (!TreeBoxCache) var TreeBoxCache = new Object();
function clsTreeBox(_sId, _sPath) {
  this.sId = _sId;
  this.sPath = _sPath;
  
  this.container = document.getElementById(this.sId);
  var treeContainerId = this.sId + "_tree";
  this.treeContainer = document.createElement("div");
  this.treeContainer.id = treeContainerId;
  this.treeContainer.className = "tree";
  this.statusContainer = document.createElement("div");
  this.statusContainer.id = this.sId + "_tree_status";
  this.statusContainer.className = "tree_status";
  this.loadingIndicator = document.createElement("img");
  this.loadingIndicator.src = "http://static.dh.hu/img/default/e.gif";
  this.loadingIndicator.className = "icon loading";
  this.loadingIndicator.style.position = "relative";
  this.container.appendChild(this.treeContainer);
  this.container.appendChild(this.statusContainer);
  this.container.appendChild(this.loadingIndicator);
  
  this.objTree = new clsTree();
  this.levelWidth = 130;
  this.objSlider = new Slider(treeContainerId, this.levelWidth, "left");
  Event.observe(this.treeContainer, "click", callback(this, this.handleEvent));
  
  this.setChild(0);
  this.epath = encodeURIComponent(this.sPath);
  if (TreeBoxCache && TreeBoxCache[this.epath] && TreeBoxCache[this.epath]["0"]) {
    this.loadJSON(TreeBoxCache[this.epath]["0"]);
  } else {
    new asyncRequest(this.sPath + "?node[]=0", callback(this, this.loadXHR));
  }
  
  // Form reset segítése.
  var form = this.container;
  while (form.nodeName != "FORM") form = form.parentNode;
  if (!form.tree) form.tree = new Array();
  form.tree.push(this);
  
  Event.observe(form, "reset", callback(this, function() {
    window.setTimeout(callback(this, this.checkAll), 0);
  }));
}
clsTreeBox.prototype.handleEvent = function(e) {
  Event.stop(e);
  var objNode = Event.delegate(e);
  if (objNode.nodeName == "INPUT") this.check(objNode);
}
clsTreeBox.prototype.check = function(objNode) {
  var iNode = objNode.parentNode.id.substring(objNode.parentNode.id.indexOf("_node_") + 6);
  this.updateStatus(this.getLeaves2(0));
  if (objNode.checked && !this.objTree.getChildren(iNode)) {
    var iLevel = this.objTree.getLevel(iNode);
    this.loadingIndicator.style.left = iLevel * this.levelWidth + "px";
    this.loadingIndicator.style.display = "";
    if (TreeBoxCache && TreeBoxCache[this.epath] && TreeBoxCache[this.epath][iNode]) {
      this.loadJSON(TreeBoxCache[this.epath][iNode]);
    } else {
      new asyncRequest(this.sPath + "?node[]=" + iNode, callback(this, this.loadXHR));
    }
  } else {
    this.showNodes(iNode);
  }
}
clsTreeBox.prototype.checkAll = function() {
  for (var i in this.objTree.aNodes) {
    if (i != "0" && this.objTree.aNodes.hasOwnProperty(i)) {
      this.check(document.getElementById(this.sId + "_node_" + this.objTree.aNodes[i]).firstChild);
    }
  }
}
clsTreeBox.prototype.showNodes = function(_iNode) {
  var bChecked = (_iNode==0?true:document.getElementById(this.sId + "_node_" + _iNode).firstChild.checked);
  var aChildren = this.objTree.getChildren(_iNode);
  var iLevel = this.objTree.getLevel(_iNode) + 1;
  var objLevel = document.getElementById(this.sId + "_level_" + iLevel);
  /* 
  if (bChecked && aChildren && objLevel.style.display == "none") {
    objLevel.style.display = "";
  } 
  */
  for (var i in aChildren) {
    if (aChildren[i] == 1) {
      var objNode = document.getElementById(this.sId + "_node_" + i);
      if (bChecked) {
        objNode.style.display = "";
      } else {
        this.hideNodes(i);
        objNode.style.display = "none";
        objNode.firstChild.checked = false;
      }
    }
  }
  
  // Színezés.
  if (objLevel) {
    var j = 0;
    for (var i = 0; i < objLevel.childNodes.length; i++) {
      if (objLevel.childNodes[i].style.display != "none") {
        objLevel.childNodes[i].className = (j++ % 2?"node even":"node");
      }
    }
  }
  // Csúsztatás.
  if (!bChecked && this.hideLevel(iLevel) && iLevel > 3) this.objSlider.slideTo(iLevel - 3);
  if (aChildren && iLevel >= 3) this.objSlider.slideTo(iLevel - 2);
}
clsTreeBox.prototype.hideNodes = function(_iNode) {
  var aChildren = this.objTree.getChildren(_iNode);
  for (var i in aChildren) {
    if (aChildren[i] == 1) {
      this.hideNodes(i);
      var objNode = document.getElementById(this.sId + "_node_" + i);
      objNode.style.display = "none";
      objNode.firstChild.checked = false;
    }
  }
  var iLevel = this.objTree.getLevel(_iNode) + 1;
  this.hideLevel(iLevel)
}
clsTreeBox.prototype.hideLevel = function(_iLevel) {
  var objLevel = document.getElementById(this.sId + "_level_" + _iLevel);
  if (objLevel) {
    var bHasChildren = false;
    for (var i = 1; i < objLevel.childNodes.length; i++) {
      if (objLevel.childNodes[i].style.display != "none") bHasChildren = true;
    }
    if (!bHasChildren) {
      //objLevel.style.display = "none";
      // A mindent kijelölő box.
      //objLevel.childNodes[0].firstChild.checked = false;
      return true;
    }
  }
  return false;
}
clsTreeBox.prototype.setChild = function(_iNode, _iParent, _sName, _sValue, _sLabel) {
  var iNode = this.objTree.setChild(_iNode, _iParent);
  if (_iParent != null) {
    var iLevel = this.objTree.getLevel(iNode);
    var sLevelId = this.sId + "_level_" + iLevel;
    var objLevel = document.getElementById(sLevelId);
    if (!objLevel) {
      objLevel = document.createElement("div");
      objLevel.id = sLevelId;
      objLevel.className = "level";
      objLevel.style.left = (iLevel - 1) * this.levelWidth + "px";
      // A mindet kijelölő box.
      /*
      var objNode = this.getNode("all", "level_" + _sId + "_all", "", "Mind");
      objLevel.appendChild(objNode); 
      */
      //if (iLevel != 1) objLevel.style.display = "none";
      this.treeContainer.appendChild(objLevel);
    }
    var objNode = document.createElement("div");
    objNode.id = this.sId + "_node_" + iNode;
    //objNode.className = "node";
    var objInput = document.createElement("input");
    objInput.id = this.sId + "_" + _sName + "";
    objInput.name = _sName + "";
    objInput.type = "checkbox";
    var regexp = new RegExp(iNode + "=t");
    objInput.checked = (document.location.search.match(regexp) ? true : false);
    objNode.appendChild(objInput);
    objInput.value = _sValue + "";
    var objLabel = document.createElement("label");
    objLabel.htmlFor = this.sId + "_" + _sName + "";
    objLabel.innerHTML = _sLabel + "";
    objNode.appendChild(objLabel);
    //if (iLevel != 1) objNode.style.display = "none";
    objInput.tree = this;
    
    // Rendezés címke szerint.
    var bInserted = false;
    /*
    // A mindent kijelölő boxot ne rendezze.
    //for (var i = 1; i < objLevel.childNodes.length; i++) {
    for (var i = 0; i < objLevel.childNodes.length; i++) {
      if (objNode.lastChild.innerHTML.toLowerCase() < objLevel.childNodes[i].lastChild.innerHTML.toLowerCase()) {
        objLevel.insertBefore(objNode, objLevel.childNodes[i]);
        bInserted = true;
        break;
      }
    }
    */
    if (!bInserted) objLevel.appendChild(objNode);
    this.objSlider.addSlide(iNode);
    
    if (document.location.search.match(regexp)) this.check(objInput);
  }
  return iNode;
}
clsTreeBox.prototype.loadXHR = function(xhr) {
  this.loadJSON(JSON.parse(xhr.responseText));
}
clsTreeBox.prototype.loadJSON = function(aChilds) {
  if (aChilds.length) {
    var show = new Array();
    for (var i = 0; i < aChilds.length; i++) {
      this.setChild(aChilds[i][0], aChilds[i][1], aChilds[i][0], "t", aChilds[i][2]);
      var contains = false;
      for (var j = 0; j < show.length; j++) {
        if (show[j] == aChilds[i][1]) {
          contains = true;
          break;
        }
      }
      if (!contains) show.push(aChilds[i][1]);
    }
    while (show.length) this.showNodes(show.shift());
  }
  this.loadingIndicator.style.display = "none";
}
clsTreeBox.prototype.getLeaves = function(_iNode) {
  var aLeaves = new Array();
  var bLeaf = true;
  var aChildren = this.objTree.getChildren(_iNode)
  for (var i in aChildren) {
    if (aChildren[i] == 1) {
      if (document.getElementById(this.sId + "_node_" + i).firstChild.checked) {
        bLeaf = false;
        aLeaves = aLeaves.concat(this.getLeaves(i));
      }
    }
  }
  if (bLeaf) aLeaves.push(_iNode);
  return aLeaves;
}
clsTreeBox.prototype.getLeaves2 = function(_iNode) {
  var aLeaves = new Array();
  var bLeaf = true;
  var aChildren = this.objTree.getChildren(_iNode)
  var l = this.objTree.getLevel(_iNode);
  for (var i in aChildren) {
    if (aChildren[i] == 1) {
      if (document.getElementById(this.sId + "_node_" + i).firstChild.checked) {
        bLeaf = false;
        if (l < 2) aLeaves = aLeaves.concat(this.getLeaves2(i));
      }
    }
  }
  if (bLeaf || (l == 2)) aLeaves.push(_iNode);
  return aLeaves;
}
clsTreeBox.prototype.updateStatus = function(aLeaves) {
  this.statusContainer.innerHTML = "";
  if (aLeaves[0] != 0) {
    var levels = new Object();
    for (var i = 0; i < aLeaves.length; i++) {
      levels[this.objTree.getLevel(aLeaves[i])] = true;
      var leaf = document.getElementById(this.sId + "_node_" + aLeaves[i]);
      var hidden = document.createElement("input");
      hidden.type = "hidden";
      hidden.name = leaf.parentNode.id + "[" + leaf.firstChild.name + "]";
      hidden.value = leaf.firstChild.value;
      this.statusContainer.appendChild(hidden);
    }
    var l = 0;
    for (var i in levels) {
      if (levels.hasOwnProperty(i)) l++;
    }
    if (l) {
      var hidden = document.createElement("input");
      hidden.type = "hidden";
      hidden.name = this.sId + "_levels";
      hidden.value = l;
      this.statusContainer.appendChild(hidden);
    }
  }
}
/* // A mindent kijelölő metódus.
clsTreeBox.prototype.handleAllClick = function(_objEvent) {
  Event.stop(_objEvent);
  var obj = Event.delegate(_objEvent);
  var aNodes = obj.parentNode.parentNode.childNodes;
  for (var i = 1; i < aNodes.length; i++) {
    if (aNodes[i].style.display != "none" && aNodes[i].firstChild.checked != obj.checked) {
      aNodes[i].firstChild.checked = obj.checked;
      //this.handleNode(aNodes[i].firstChild);
    }
    var iNode = aNodes[i].id.substring(aNodes[i].id.indexOf("_node_") + 6);
    if (aNodes[i].firstChild.checked && !this.objTree.getChildren(iNode)) {
      this.aLoading.push(iNode);
    } else {
      this.showNodes(iNode);
    }
  }
  if (this.aLoading.length != 0) new asyncRequest(this.sPath + "?node[]=" + this.aLoading.join("&node[]="), callback(this, this.loadXHR));
  this.updateStatus();
} */
/* clsTreeBox.prototype.handleLevelClick = function(_objEvent) {
  Event.stop(_objEvent);
  var obj = Event.delegate(_objEvent);
  var sId = obj.id;
  if (sId.indexOf("_level_") == -1) {
    sId = obj.parentNode.id;
  }
  var iLevel = parseInt(sId.substring(sId.indexOf("_level_") + 7)) - 1;
  this.objSlider.slideTo(iLevel);
} */
/* clsTreeBox.prototype.updateStatus2 = function() {
  var aLevels = this.objTree.getLevels();
  var aObjLevels = new Array();
  for (var i in aLevels) {
    var iLevel = aLevels[i];
    if (iLevel != 0) {
      if (aObjLevels[iLevel] == null) {
        aObjLevels[iLevel] = new Array();
      }
      var objNode = document.getElementById(this.sId + "_node_" + i);
      if (objNode.firstChild.checked) {
        var objLi = document.createElement("li");
        objLi.appendChild(document.createTextNode(objNode.lastChild.innerHTML));
        aObjLevels[iLevel][aObjLevels[iLevel].length] = objLi;
      }
    }
  }
  var objDl = document.createElement("dl");
  for (var i = 1; i < aObjLevels.length; i++) {
    if (aObjLevels[i].length != 0) {
      var objDt = document.createElement("dt");
      objDt.appendChild(document.createTextNode("Level " + i));
      objDl.appendChild(objDt);
      var objDd = document.createElement("dd");
      for (var j = 0; j < aObjLevels[i].length; j++) {
        objDd.appendChild(aObjLevels[i][j]);
      }
      objDl.appendChild(objDd);
    }
  }
  document.getElementById(this.sId).lastChild.innerHTML = "";
  document.getElementById(this.sId).lastChild.appendChild(objDl);
} */

