function cDebugger(bState){
  this.state = bState;
  this.print = _print;
  this.printObject = _printObject;
  this.printArray = _printArray;
  this.startGroup = _startGroup;
  this.stopGroup = _stopGroup;
  this.show = _show;
  this.hide = _hide;
  this.textArea = null;
  this.button = null;
  this.opened = false;
  this.indent = 0;
  this.indentstep = 10;
  this.currentGroup = new Array();
  
  if(this.state){
    oDiv = document.createElement("DIV");
    oButton = document.createElement('<INPUT type="button" Value="Depurador">');
    oDiv.appendChild(oButton);
    oButton.attachEvent("onclick", DebuggerClick);
    oTextArea = document.createElement("DIV");
    oTextArea.style.display = "none";
    oTextArea.className = "debugTextArea";
    oDiv.appendChild(oTextArea);
    document.body.insertAdjacentElement("afterBegin", oDiv);
    this.textArea = oTextArea;
    oButton.debug = this;
    this.button = oButton;
    this.currentGroup.push(oTextArea);
  }
  function _print(sMsg){
    if(!this.state){
      return;
    }
    if(sMsg instanceof Array){
      this.printArray(sMsg);
      return;
    }
    if(sMsg instanceof Object){
      this.printObject(sMsg);
      return;
    }
    var oDiv = document.createElement("DIV");
    var oGroup = this.currentGroup[this.currentGroup.length - 1];
    oGroup.appendChild(oDiv);
    oDiv.innerText += sMsg;
    oDiv.className = "DebugText";
  }

  function _startGroup(sGroupName){
    if(!this.state){
      return;
    }
    oGroup = document.createElement("DIV");
    oContainer = this.currentGroup[this.currentGroup.length - 1];
    oContainer.appendChild(oGroup);
    oGroupLabel = document.createElement("SPAN");
    oGroup.appendChild(oGroupLabel);
    oGroupLabel.innerText = " + " + sGroupName;
    oGroupLabel.label = sGroupName;
    oGroupLabel.className = "groupLabelDebug";
    oGroupContent = document.createElement("DIV");
    oGroup.appendChild(oGroupContent);
    oGroupLabel.content = oGroupContent;
    oGroupLabel.attachEvent("onclick", GroupClick);
    oGroupLabel.opened = false;
    oGroupLabel.show = _showGroup;
    oGroupLabel.hide = _hideGroup;
    oGroupContent.style.display = "none";
    oGroupContent.className = "groupDebug";
    this.currentGroup.push(oGroupContent);    
  }
  
  function _stopGroup(sGroupName){
    if(!this.state){
      return;
    }
    this.currentGroup.pop();    
  }
  
  function _printArray(oArray){
    if(!this.state){
      return;
    }
//    if (oArray.length > 0)      
//      this.startGroup("Array");
    for(var i = 0; i < oArray.length; i++){
      this.startGroup("Elemento " + i);
      this.print(oArray[i]);
      this.stopGroup();
    }
//    if (oArray.length > 0)    
//      this.stopGroup();
  }

  function _printObject(oObj){
    if(!this.state){
      return;
    }
//    this.startGroup("Objeto");
    for(var i in oObj){
      if(oObj[i] instanceof Function){continue;}
      if(oObj[i] instanceof Array){
        if (oObj[i].length == 0){
          this.print(" * " + i + " [ Array (" + oObj[i].length + ") ]");
          continue;
        }   
        this.startGroup(i + " [ Array (" + oObj[i].length + ") ]");
        this.printArray(oObj[i]);
        this.stopGroup();
        continue;
      }
      if(oObj[i] instanceof Object){
        this.startGroup(i + " [ Objeto ]");
        this.printObject(oObj[i]);
        this.stopGroup();
        continue;
      }
      this.print(" * " + i + " : " + oObj[i]);
    }
//    this.stopGroup();
  }

  function _showGroup(){
    this.content.style.display = "";
    this.opened = true;
    this.innerText = " - " + this.label;
  }

  function _hideGroup(){
    this.content.style.display = "none";
    this.opened = false;
    this.innerText = " + " + this.label;
  }

  function _show(){
    if(!this.state){
      return;
    }
    this.textArea.style.display = "";
    this.opened = true;
  }
  function _hide(){
    if(!this.state){
      return;
    }
    this.textArea.style.display = "none";
    this.opened = false;
  }
}

function DebuggerClick(){
  oButton = window.event.srcElement;
  if(oButton.debug.opened){
    oButton.debug.hide();
  }else{
    oButton.debug.show();
  }
}

function GroupClick(){
  oGroup = window.event.srcElement;
  if(oGroup.opened){
    oGroup.hide();
  }else{
    oGroup.show();
  }
}


