(function() {

  var mconsole = document.mconsole = {

  };

  mconsole.log =
  function mconsole_log(msg) {

    var x = document.createElement('tr');
    var y = document.createElement('td');
    y.appendChild(document.createTextNode(xb.jsdate2sql(new Date(), true)));
    x.appendChild(y);
    y = document.createElement('td');
    y.appendChild(document.createTextNode(msg));
    x.appendChild(y);

    if (!document.mconsole.tbody) {
        document.mconsole.tbody = $('#console_table').get(0);
    }

    document.mconsole.tbody.appendChild(x);

    document.mconsole.content.scrollTop = document.mconsole.content.scrollHeight;
  };

  mconsole.show =
  function mconsole_show(visible) {
    this.console.style.display = (visible ? 'inline' : 'none');
  }

  mconsole.clear =
  function mconsole_clear() {

    var header = this.tbody.firstChild;

    while (header.nextSibling) {
      this.tbody.removeChild(header.nextSibling);
    }
  }


  // 2008-10-02
  mconsole.init =
  function mconsole_init()
    {
      this.console = $('#console').get(0);
      this.tbody = $('#console_table').get(0);
      this.content = $('#console_content').get(0);

      xb.cleanDom(this.tbody);

      $('#console').Resizable
      (
       {
           minWidth: 200,
           minHeight: 60,
           maxWidth: 1000,
           maxHeight: 500,
           dragHandle: '#console_handle',
           handlers: {
             se: '#console_resize'
           },
           onResize : function(size, position) {
               var x1 = parseInt($('#console_bottom').get(0).clientHeight);
               var x2 = parseInt($('#console_handle').get(0).clientHeight);
             //$('#windowBottom, #windowBottomContent').css('height', size.height-33 + 'px');
             var consoleContent = $('#console_content').css('width', size.width + 'px');
             consoleContent.css('height', size.height - x1 - x2 + 'px');
           }
       }
       );

      dd("MConsole initialized");

      //document.mconsole.show(true);
    }

})();

document.mready.push({
    callback: function() { document.mconsole.init() },
    runlevel: 5,
    description: 'mconsole init'});


dd = document.mconsole.log;
