var dbug = new DBugger();

function DBugger() {
   this.active = false;
   this.que = new Array();
   this.limit = 10;
   this.add = function (str,level) {
   	  if (! this.active) return;
      this.que.unshift(DBQO(str,level));
      this.write();
   }
   this.write = function () {
   	  if (! this.active) return;
      var div = document.getElementById('wexdebug');
      if(!div) { alert('wexdebug is not defined'); }
      else {
         var showing = this.que.length<this.limit ? this.que.length : this.limit;
         div.innerHTML = '<div style="color:#ffffff;background-color:#ff0000;width:100%;">'+
                            '<center>WEX DEBUGGER (showing '+
                               '<input type="text" size="2" maxlength="2" value="'+this.limit+'" style="font-size:8pt;" onblur="dbug.setLimit(this.value);"> of '+this.que.length+')'+
                            '</center>'+
                         '</div>';
         div.style.border = '1px solid #000';
         div.style.backgroundColor = '#ffffff';
         div.style.display = 'block';
         div.style.fontFamily = 'courier new';
         div.style.fontSize = '8pt';
         div.style.width = '400px';
         div.style.height = '200px';
         for(var i=0; i < this.que.length; ++i) {
            if(i<this.limit) {
               div.innerHTML += '<div style="color:'+this.que[i].color+';"><span style="background-color:#999999;color:#ffffff;width:20px;text-align:right;">'+i+'</span><b>'+this.que[i].level+':</b> '+this.que[i].src+'</div><hr style="font-size:1pt;">';
            } else {
               break;
            }
         }
      }
   }
   this.setLimit = function (n) {
      this.limit = Math.abs(n);
      this.write();
   }
}

function DBQO (str, level) {
   if (! this.active) return;
   switch(level) {
      case 1: return new Object({'src':str,'level':'Important','color':'#0000ff'});
      case 2: return new Object({'src':str,'level':'Warning','color':'#ff9900'});
      case 3: return new Object({'src':str,'level':'Error','color':'#ff0000'});
      default: return new Object({'src':str,'level':'Comment','color':'#009900'});
   }
}
