/* ****************************************************************************
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 cTabbedPane(){
  var self = this;
  
  this.cObject               = cObject;
  this.cObject();
  
  this.current               = null;
  this.tabs                  = {};
  this.stack                 = [];
  this.addModule             = _addModule;
  this.activate              = _activate;
  this.clear                 = _clear;
  
  this.onCloseModuleListener = _onCloseModuleListener;
  
  function _clear(){
    for(var iTabs = self.cells.length - 1;iTabs >= 0; iMenus--){
      self.deleteCell(iTabs);
    }
    self.tabs    = {};
  }
  
  function _addModule(oModule){
    if(self.tabs[oModule.name] == undefined || self.tabs[oModule.name] == null){
      debugMessage("cTabbedPane: Adding module "  + oModule.name);
      var oTab = self.insertCell(self.cells.length);
      oTab.className      = "pestana_label_on";
      oTab.innerText      = oModule.title;
      oTab.module         = oModule;
      oModule.addListener('onclose', self.onCloseModuleListener); 
      oModule.tab         = oTab;
      oTab.attachEvent('onclick', _tabclick);
      var oTab1 = self.insertCell(self.cells.length);
      oTab1.className = "pestana_left";
      self.tabs[oModule.name] = oTab;
      self.stack.push(oTab);
      oTab.stackIndex = self.stack.length - 1;  
    }else{
      var oTab = self.tabs[oModule.name];
    }
    self.activate(oTab);
  }  
  
  function _tabclick(){
    var oTab = window.event.srcElement;
    if(oTab.active) return;
    self.activate(oTab);
  }
  
  function _activate(oTab){
    if(self.current != null){
      self.current.className = "pestana_label";
      self.current.module.hide();
      self.current.active = false;
    }
    self.current = oTab;
    self.current.className = "pestana_label_on";
    self.current.module.show();
    self.current.active = true;
    var oEvent = new Object();
    oEvent.name = 'ontabactivate';
    oEvent.object = oTab;
    self.dispatch(oEvent);    
  }
  
  function _onCloseModuleListener(oEvent){
    var oModule = oEvent.object;
    if(oModule ==  undefined || oModule == null){return;}
    var oTab = self.tabs[oModule.name];
    if(oTab != undefined && oTab != null){
      oTab.style.display = 'none'; 
      if(self.current == oTab){
        self.current = null;
      }
      self.tabs[oModule.name] = null;
    }    
  }
}

