// Expects an element or id to put the log messages in
// TODO Put in real log level handling
function Log(el_or_id){
  this.debugDiv = $(el_or_id);
}

// Log level constants
Log.INFO = 3;
Log.WARN = 2;
Log.ERROR = 1;

Log.prototype.setLogLevel = function(logLevel){
  this.level = logLevel;
}

Log.prototype.show = function(){
  this.debugDiv.style.display = 'block';
  //toggleDiv(this.debugDiv.id);
}

Log.prototype.hide = function(){
  this.debugDiv.style.display = 'none';
}

Log.prototype.info = function(message) {
  if(this.level >= Log.INFO && this.debugDiv){
    this.prepareDiv();
    var p = WU.addChildElement(this.debugDiv, 'p');
    WU.addText(p, message);
  }
}

Log.prototype.error = function(message) {
  if(this.level >= Log.ERROR && this.debugDiv){
    this.prepareDiv();
    var p = WU.addChildElement(this.debugDiv, 'p');
    var s = WU.addChildElement(p, 'span', {className:'warn'});
    s.className = 'warn';
    WU.addText(s, message);
  }
}

Log.prototype.error = function(message) {
  if(this.level >= Log.ERROR && this.debugDiv){
    this.prepareDiv();
    var p = WU.addChildElement(this.debugDiv, 'p');
    var s = WU.addChildElement(p, 'span', {className:'error'});
    s.className = 'error';
    WU.addText(s, message);
  }
}

// Makes sure that we have a label and the clear link
Log.prototype.prepareDiv = function(){
  if (Element.empty(this.debugDiv)) {
    var p = document.createElement('p');
    
    var b = document.createElement('b');
    var label = document.createTextNode('Debug Messages: ');
    b.appendChild(label);
    p.appendChild(b);
    
    var a = document.createElement('a');
    a.href = 'javascript: void(0)';
    a.onclick = this.clearLog.bindAsEventListener(this);
    var txt = document.createTextNode('Clear');
    a.appendChild(txt);
    p.appendChild(a);
    
    this.debugDiv.appendChild(p);
  }
}

Log.prototype.clearLog = function(){
  if(this.debugDiv){
    Element.removeAllChildren(this.debugDiv);
  }
}
