function DebugTools(active) {
  active=(active==null)?true:active;
  var firebug=(window.console==null)?false:true;
  var debugWin,pre;
  var history=new Array();
  this.printObj = function(obj,recursive,title) {
    if(!active)return;
    recursive=recursive||false;
    title=(title!=null)?title+' : ':'';
    title+=typeof(obj);
    this.group(title)
    for (var i in obj) {
      try {
        var o = obj[i];
        if(typeof(o) == 'object' && recursive){this.printObj(o,recursive,i);}
        else{this.printLn(i+' : '+o);}
      } catch (e) {
        this.printLn(i+' : '+e);
      }
    }
    this.groupEnd();
  }
  this.printLn = function(line) {
    if(!active)return;
    if(line==null)return;
    if(firebug){
      window.console.log(line.toString());
    }else{
      if (debugWin==null||debugWin.closed)openWin();
      line=line.toString().replace(/\r/g,'').replace(/\n/g,'\r\n');
      var div=debugWin.document.createElement('div');
      div.style.borderTop='1px solid #666666';
      div.style.padding='3px';
      div.appendChild(debugWin.document.createTextNode(line));
      pre.appendChild(div);
      debugWin.focus();
    }
  }
  this.clear = function() {
    if(!active)return;
    if(!firebug){
      if (debugWin==null||debugWin.closed)return;
      var nodes = pre.childNodes;
      for (i = 0; i < nodes.length; i++) {
        pre.removeChild(nodes[i]);
        i--;
      }
    }
  }
  function openWin(){
    var name='DebugConsole';
    debugWin=window.open('',name,'width=480,height=360,menubar=0,toolbar=0,status=0,location=0,resizable=1,scrollbars=1');
    var html='<html><head><title>Debug Console</title></head>';
    html+='<body style="margin:0px;font-size:11px;"><pre></pre></body></html>';
    debugWin.document.write(html);
    debugWin.document.close();
    pre=debugWin.document.body.firstChild;
  }
  this.group=function(title){
    if(!active)return;
    if(firebug){
      window.console.group(title);
    }else{
      this.printLn(title);
      var div=debugWin.document.createElement('div');
      div.style.borderLeft='1px solid #666666';
      div.style.marginLeft='25px';
      pre.appendChild(div);
      history.push(pre)
      pre=div;
    }
  }
  this.groupEnd=function(){
    if(!active)return;
    if(firebug){
      window.console.groupEnd();
    }else{
      pre=history.pop();
      pre.appendChild(debugWin.document.createElement('br'));
    }
  }
}