/* ****************************************************************************
Copyright 2007,
Todos los derechos Reservados
CM.COM E.U.
cmhenao@epm.met.co

El uso sin solicitud espresa a la compañia de esta libreria es una clara
violación a la propiedad intelectual y los derechos de autor.
Prohibida su distribucion y/o uso sin el permiso explicito de cm.com
**************************************************************************** */

/* ****************************************************************************
Autor: Jhon Alejandro Ramirez Velez
       Desarrollador
       Progress - WebSpeed
       jhalrave@epm.net.co
**************************************************************************** */
function cTreeView(){

  var self       = this;

  this.cObject                 = cObject;
  this.cObject();

  this.name           = 'cTreeView';
  this.display        = _display;
  this.itemselect     = _itemselect;
  this.itemover       = _itemover;
  this.itemout        = _itemout;

  this.icons          = {};
  this.icons['plus']  = 'baseline/images/plus.gif';
  this.icons['minus'] = 'baseline/images/minus.gif';
  this.icons['dot']   = 'baseline/images/dot.gif';
  this.label          = 'nombre';
  this.sub_label      = 'nombre_est';
  this.subs           = 'establecimientos';
  this.createSub      = _createSub;

  function _display(aArray){
    for(;self.children.length > 0;){
      self.children(self.children.length - 1).removeNode(true);
    }
    for(var iLength = 0; iLength < aArray.length; iLength++){
      var aItem         =  aArray[iLength];
      var oItem         = document.createElement('DIV');
      //var oIcon         = document.createElement('IMG');
      //oIcon.width       = 10;
      //oIcon.height      = 10;
      //oIcon.src         = self.icons['plus'];
      
      //oItem.appendChild(oIcon);
      var oLabel        = document.createElement('SPAN');
      oLabel.className  = "label_tree_with_child";
      oLabel.innerText  = aItem[self.label];
      oLabel.object     = aItem;
      oLabel.onclick    = self.itemselect;
      oLabel.onmouseover  = self.itemover;
      oLabel.onmouseout   = self.itemout;
      oLabel.expand       = _expand;
      oLabel.collapse     = _collapse; 
      oLabel.tree         = self;
      //oLabel.icon       = oIcon;
      oItem.appendChild(oLabel);
      oItem.itemType      = "ROOT";
      self.appendChild(oItem);
      if(aItem[self.subs] != undefined){
        self.createSub(aItem[self.subs], oItem);    
      }
    }
  }
  
  function _createSub(aArray, oParent, sType){
    if (sType == undefined || sType == null){
      var sType = "CHILD-" + oParent.itemType;
    }
    var oSubs = document.createElement('DIV');    
    oSubs.className       = "tree_subs";
    oSubs.style.display   = "none";
    for(var iLength = 0; iLength < aArray.length; iLength++){
      var aItem           = aArray[iLength];
      var oItem           = document.createElement('DIV');
//       var oIcon           = document.createElement('IMG');
//       oIcon.width         = 10;
//       oIcon.height        = 10;
//       oIcon.src           = self.icons['dot'];
//       oIcon.className     = "label_tree";
//      oItem.appendChild(oIcon);
      var oLabel          = document.createElement('SPAN');
      oLabel.className    = "label_tree";
      oLabel.innerText    = aItem[self.sub_label];
      oLabel.object       = aItem;
//      oLabel.icon         = oIcon;
      oLabel.onclick      = self.itemselect;
      oLabel.onmouseover  = self.itemover;
      oLabel.onmouseout   = self.itemout;
      oLabel.expand       = _expand;
      oLabel.collapse     = _collapse; 
      oLabel.tree         = self;
      oItem.itemType      = sType; 
      oItem.appendChild(oLabel);
      oSubs.appendChild(oItem);
    }
    if(oParent.children(1) == null)
      oParent.appendChild(oSubs);
    else
      oParent.children(1).replaceNode(oSubs);  
  }
  
  function _itemselect(){
    oLabel = window.event.srcElement;
    var oEvent = new Object();
    oEvent.name = 'onitemselect';
    oEvent.object = oLabel.object;
    oEvent.widget = oLabel;
    self.dispatch(oEvent);
  }
  
  function _itemover(){
    oLabel = window.event.srcElement;
    oLabel.className = oLabel.className.replace("_on","");
    oLabel.className = oLabel.className + "_on"; 
  }
  
  function _itemout(){
    oLabel = window.event.srcElement;
    oLabel.className = oLabel.className.replace("_on","");
  }
  
  function _collapse(){
    if(oLabel.parentElement.children.length >= 2){
      oLabel.parentElement.children(1).style.display = "none";
    }
    oLabel.className = oLabel.className.replace("_on","");
    oLabel.className = oLabel.className.replace("_sel","");
  }
  
  function _expand(){
    if(oLabel.parentElement.children.length >= 2){
      oLabel.parentElement.children(1).style.display = "";
    }
    oLabel.className = oLabel.className.replace("_on","");
    oLabel.className = oLabel.className.replace("_sel","");
    oLabel.className = oLabel.className + "_sel"; 
  }
  
}

