var Logger = Class.create({
  MAX_DEBUG_LINES: 500,
  DEBUG_SWITCH_STRING: [68,69,66,85,71,49],
  qs: String(window.location).toQueryParamsToLowerKeys(),
  
  initialize: function(name) {
    this.name=name;
    this.toString = function() {
      return this.name;
    };
    this.debug=false;
    this.key_press_array=[];
    this.log_to_console=true;
    this.msg_num=0;
    if(!$('debug_area')){
      $(document.body).insert(new Element('div',{id:'debug_area'}).hide());
    }
    this.log('logger initialized');
  },
  
  log: function(txt) {
    this.msg_num++
    if($('debug_area')) {
			if($('debug_area').select('p').size() > this.MAX_DEBUG_LINES) {
				$('debug_area').firstDescendant().remove();
			}
      txt = ">> " + this.msg_num + ". " + this.name + ": " + txt;
      $('debug_area').insert(new Element('p',{id: this.name+"_msg"+this.msg_num}).update(txt));
      $(this.name+"_msg"+this.msg_num).scrollIntoView(false);
    }
    try{
      if(this.log_to_console) {
        window.console.log(txt);
      }
    }
    catch(e) {
      // catch something here?...      
    }
  },
  
  look_for_debug: function(key) {
    this.key_press_array.push(key);
    if(key==32) {
      this.key_press_array=[];
    }
    if(this.key_press_array.length >= this.DEBUG_SWITCH_STRING.length) {
      if(this.key_press_array.join() == this.DEBUG_SWITCH_STRING.join()) {
        this.toggle_debug();
      }
      else {
        this.key_press_array=[];
      }
    }
  },

  toggle_debug: function() {
    if(this.debug) {
      $('debug_area').hide();
      $(document.body).removeClassName('debug');
      this.debug=false;
    }
    else {
      $('debug_area').show();     
      $(document.body).addClassName('debug');
      this.debug=true;
    }
    this.log('you have toggled debug mode: ' + this.debug);
  }
});
