//_____________________________________________________________________________
// Module : lib.tree.js
//
// GESTION DE MENUS ARBORESCENTS
//
// version  : 2.0
// author   : David Crumiere
//            d.crumiere@quadralmedia.com
// last modified by D. CRUMIERE - 2004-06
//
//_____________________________________________________________________________


var BUILD_TREE_EXPANDED = false;

var LEVEL1_SPACER       = usr_theme+'/images/tree/spacer.gif';
var LEVEL1_IMG_COLLAPSED= usr_theme+'/images/tree/lev1_collapsed.gif';
var LEVEL1_IMG_EXPANDED = usr_theme+'/images/tree/lev1_expanded.gif';

var LEVEL2_SPACER       = usr_theme+'/images/tree/spacer.gif';
var LEVEL2_IMG_COLLAPSED= usr_theme+'/images/tree/lev2_collapsed.gif';
var LEVEL2_IMG_EXPANDED = usr_theme+'/images/tree/lev2_expanded.gif';

var LEVEL3_SPACER       = usr_theme+'/images/tree/spacer.gif';
var LEVEL3_IMG_COLLAPSED= usr_theme+'/images/tree/lev3_collapsed.gif';
var LEVEL3_IMG_EXPANDED = usr_theme+'/images/tree/lev3_expanded.gif';

var oActive = null;

function Tree(_layer, _EEMtable, _ipm, _oParent) {
  this.oParent          = _oParent;
  this.ipm              = _ipm;
  this.oDiv             = null;

  this.brothersEEM      = new Array();  // Liste de oEEM frres
  this.brothersList     = new Array();
  this.childrenList     = new Array();  // Liste de obj Tree fils

  this.buildBrothersEEM = Tree_buildBrothersEEM;
  this.buildTree         = Tree_buildTree;

  this.expandNodes      = Tree_expandNodes;
  this.expandOneNode    = Tree_expandOneNode;
  this.expandAll        = Tree_expandAll;
  this.expandNodeById   = Tree_expandNodeById;
  this.collapseNode     = Tree_collapseNode;
  this.collapseAll      = Tree_collapseAll;

  this.buildTree(_layer, _EEMtable);

  return this;
}


function Tree_buildBrothersEEM(_EEMtable) {
  if (_EEMtable!=null)
  for (var i=0; i<_EEMtable.length; i++)
    if (_EEMtable[i].ipm==this.ipm)
      this.brothersEEM[this.brothersEEM.length] = _EEMtable[i];
}


function Tree_buildTree(_layer, _EEMtable) {

  this.buildBrothersEEM(_EEMtable);

  var oDiv              = document.createElement("DIV");
  oDiv.id               = "Div_tree_"+this.ipm;

  this.oDiv             = oDiv;

  if (this.oParent==null) {   // On accroche le Div a la racine !
    var oTarget         = document.getElementById(_layer);
    if (oTarget==null) oTarget = document.body;
    oTarget.appendChild(oDiv);

    oDiv.isExpanded = true;
  }
  else {                    // On accroche le Div a son oTD parent !
    for (var i=0;i<this.oParent.brothersList.length;i++) {
      if (this.oParent.brothersList[i].iem==this.ipm) {
        this.oParent.brothersList[i].appendChild(oDiv);
        break;
      }
    }

    oDiv.isExpanded = BUILD_TREE_EXPANDED;
  }
    oDiv.style.display= (oDiv.isExpanded) ? "block" : "none"; // permet la construction du menu a l'etat collapse

  var oTable            = document.createElement('table');
  oTable.className      = (this.oParent==null) ? LEVEL1_BORDER_CSS : "table2";
  oDiv.appendChild(oTable);

  var oTBody            = document.createElement('tbody');
  oTable.appendChild(oTBody);


  var isLevel1          = (this.oParent==null);
  var isLevel3          = (!isLevel1 && this.oParent.oParent!=null);
  for (var i=0; i<this.brothersEEM.length && isLevel3; i++)
    isLevel3 = !EEM_hasChild(_EEMtable,this.brothersEEM[i].iem);


  for (var i=0; i<this.brothersEEM.length; i++) {
    var oEEM            = this.brothersEEM[i];
    var oTR             = document.createElement('tr');
    oTBody.appendChild(oTR);

    var oTD1            = document.createElement('td');
    oTR.appendChild(oTD1);

    oTD1.oCurrent       = this;
    oTD1.iem            = oEEM.iem;
    oTD1.id             = "TD_tree_"+oEEM.iem;

    var hasChild        = EEM_hasChild(_EEMtable,oEEM.iem);

    /* PREMIER NIVEAU */
    if (isLevel1) {
      oTR.level         = 1;
      oTD1.level        = 1;
      oTD1.className    = LEVEL1_BGCOLOR_CSS;

      var oSpc          = document.createElement("IMG");
      oTD1.appendChild(oSpc);
      oSpc.src          = LEVEL1_SPACER;
      oSpc.className    = "level1_spacer";

      var oA            = document.createElement("A");
      oTD1.appendChild(oA);
      var oImg          = document.createElement("IMG");
      oA.appendChild(oImg);
      oImg.src          = LEVEL1_IMG_COLLAPSED;
      oImg.className    = "level1_image";

      var oSpc          = document.createElement("IMG");
      oTD1.appendChild(oSpc);
      oSpc.src          = LEVEL1_SPACER;
      oSpc.className    = "level1_spacer";

      var oText         = document.createElement("A");
      oTD1.appendChild(oText);
      oText.className   = LEVEL1_BGCOLOR_CSS;

      var oTD = oTD1;
    }
    else {
      if (!isLevel3) {
      /* NIVEAUX INTERMEDIARES */
        oTR.level       = 2;
        oTD1.level      = 2;
        oTD1.className  = "level2"+(i%2);
        oTD1.style.width= 20;

        var oSpc        = document.createElement("IMG");
        oTD1.appendChild(oSpc);
        oSpc.src        = LEVEL2_SPACER;
        oSpc.className  = "level2_spacer";


        var oTD2        = document.createElement('td');
        oTR.appendChild(oTD2);
        oTD2.oCurrent   = this;
        oTD2.iem        = oEEM.iem; // HYPER IMPORTANT : permet de raccrocher les fils
        oTD2.id         = "TD_tree_"+oEEM.iem;

        oTD2.level      = 2;
        oTD2.className  = "level2"+(i%2);

        var oFrm        = document.createElement("Div");
        var oLft        = document.createElement("Div");
        var oRgt        = document.createElement("Div");
        oTD2.appendChild(oFrm);
        oFrm.appendChild(oLft);
        oFrm.appendChild(oRgt);
        oFrm.style.position = "relative";
        oLft.style.position = "relative";
        oRgt.style.position = "absolute";
        oRgt.style.top  = 2;
        oRgt.style.left = 135;
        oRgt.style.width= 15;

        var oText       = document.createElement("A");
        oLft.appendChild(oText);
        oText.className = "level2"+(i%2);

        var oA          = document.createElement("A");
        oRgt.appendChild(oA);
        var oImg        = document.createElement("IMG");
        oA.appendChild(oImg);
        oImg.src        = (hasChild) ? LEVEL2_IMG_COLLAPSED : LEVEL2_SPACER;
        oImg.className  = "level2_image";

        var oTD = oTD2;
      }
      else {
      /* DERNIER NIVEAU */
        oTR.level       = 3;
        oTD1.level      = 3;
        oTD1.className  = "level3";

        var oA          = document.createElement("A");
        oTD1.appendChild(oA);
        var oImg        = document.createElement("IMG");
        oA.appendChild(oImg);
        oImg.src        = LEVEL3_IMG_COLLAPSED;
        oImg.className  = "level3_image";

        var oSpc          = document.createElement("IMG");
        oTD1.appendChild(oSpc);
        oSpc.src          = LEVEL3_SPACER;
        oSpc.className    = "level1_spacer";

        var oText       = document.createElement("A");
        oTD1.appendChild(oText);
        oText.className = "level3";

        var oTD = oTD1;
      }
    }

    this.brothersList[this.brothersList.length] = oTD;

    // Ecriture du libelle
    oTD.oEEM          = oEEM;
    oTD.oText         = oText;
    oText.innerHTML   = oEEM.lib;
    oText.href        = "#menu";
    oA.href           = "#menu";

    // Ajout des actions
    if (hasChild) {                          //  possede fils
      oChild = new Tree(_layer, _EEMtable, oEEM.iem, this);
      oTD.oChild      = oChild;
      this.childrenList[this.childrenList.length] = oChild;
      oTD.oImg          = oImg;

      // derniere image est cliquable : img = [+] ou [-]
      addEvent(oImg,  'click', Node_Tree_onClick, false);
      addEvent(oText, 'click', Node_Tree_onClick, false);
    }
    else {
      oTD.oChild        = null;
      oTD.oImg          = oImg;

      addEvent(oText, 'click', TD_Tree_onClick, false);
    }

    addEvent(oImg,  'mouseout',  Node_Tree_onMouseOut,  false);
    addEvent(oText, 'mouseout',  Node_Tree_onMouseOut,  false);
    addEvent(oImg,  'mouseover', Node_Tree_onMouseOver, false);
    addEvent(oText, 'mouseover', Node_Tree_onMouseOver, false);

  }
}

function TD_Tree_onClick(obj) {
  var oText             = getObjet(this,obj);
  var oTD               = oText;
  while (oTD.tagName!="TD") oTD = oTD.parentNode;

  Tree_Highlight(oActive, false);
  oActive = oTD;
  Tree_Highlight(oActive, true);
  if (oTD.tagName=="TD") launchApplication(oTD.oEEM);
}

//_____________________________________________________________________________


function Node_Tree_onMouseOver(obj) {
  var oTD               = getObjet(this,obj);
  while (oTD.tagName!="TR") oTD = oTD.parentNode;
  oTD = oTD.firstChild;

  Tree_Highlight(oTD, true);
}

function Node_Tree_onMouseOut(obj) {
  var oTD               = getObjet(this,obj);
  while (oTD.tagName!="TR") oTD = oTD.parentNode;
  oTD = oTD.firstChild;

  Tree_Highlight(oTD, false);
}
function Tree_Highlight(oTD, isHL) {
  if (oTD!=null)
  if (isHL) {
    if (oTD.level==3) {
      oTD.oImg.src = LEVEL3_IMG_EXPANDED;
      oTD.lastChild.className = "level3_active";
    }
  }
  else {
    if (oTD!=oActive)
      if (oTD.level==3) {
        oTD.oImg.src = LEVEL3_IMG_COLLAPSED;
        oTD.lastChild.className = "level3";
      }
  }
}


function Node_Tree_onClick(obj) {
  var oTD               = getObjet(this,obj);
  while (oTD.tagName!="TR") oTD = oTD.parentNode;
  oTD = (oTD.level==2) ? oTD.lastChild : oTD.firstChild;

  if (oTD.oEEM.url!="") launchApplication(oTD.oEEM);

//  (oTD.oChild.oDiv.isExpanded) ? oTD.oChild.collapseNode() : oTD.oChild.expandOneNode();
  (oTD.oChild.oDiv.isExpanded) ? oTD.oChild.collapseAll() : oTD.oChild.expandOneNode();
//  Possiblite de modifier facilement les comportements ... ex ci-dessous
//  (oTD.oChild.oDiv.isExpanded) ? oTD.oChild.collapseNode() : oTD.oChild.expandNodes();
//  (oTD.oChild.oDiv.isExpanded) ? oTD.oChild.collapseAll() : oTD.oChild.expandNodes();
//  (oTD.oChild.oDiv.isExpanded) ? oTD.oChild.collapseAll() : oTD.oChild.expandAll();
//  ...
}

function Tree_expandNodes() {
  this.oDiv.isExpanded=true;
  this.oDiv.style.display="block";

  var oTD = this.oDiv.parentNode;
  if (oTD.level==2) {
    oTD.oImg.src = LEVEL2_IMG_EXPANDED;
    oTD.oText.className = oTD.className.substring(0,7)+'_active';
  }
}

function Tree_expandOneNode() {
  var brothersList = this.oParent.childrenList;
  for (var i=0; i<brothersList.length ;i++) {
    if(brothersList[i]!=this)   // fermer le freres deja ouverts
      brothersList[i].collapseNode();
    else                        // ouvrir celui qui vient d'etre clique !
      brothersList[i].expandNodes();
  }
}


function Tree_collapseNode() {
  this.oDiv.isExpanded=false;
  this.oDiv.style.display="none";

  var oTD = this.oDiv.parentNode;
  if (oTD.level==2) {
    oTD.oImg.src = LEVEL2_IMG_COLLAPSED;
    oTD.oText.className = oTD.className.substring(0,7);
  }
}

function Tree_expandAll() {
  this.expandNodes();

  for (var i=0; i<this.childrenList.length ;i++)
    this.childrenList[i].expandAll();
}

function Tree_collapseAll() {
  if (this.oParent!=null)
    this.collapseNode();

  for (var i=0; i<this.childrenList.length ;i++)
    this.childrenList[i].collapseAll();
}

function Tree_expandNodeById(nodeId) {
    oTD = document.getElementById("TD_tree_"+nodeId);

  if (oTD!=null) {
    if (oTD.oChild==null) {
      Tree_Highlight(oActive, false);
      oActive = oTD;
      Tree_Highlight(oActive, true);

//      launchApplication(oTD.oEEM);
    }
    else
      oTD.oChild.expandOneNode();

    var oCurrent = oTD.oCurrent;
    while (oCurrent.oParent!=null) {
      oCurrent.expandOneNode();
      oCurrent=oCurrent.oParent;
    }
  }

}

