/*
Copyright (c) 2006, NAKAMURA Satoru
All rights reserved.

Redistribution and use in source and binary forms, with or without 
modification, are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice,
      this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright notice,
      this list of conditions and the following disclaimer in the documentation
      and/or other materials provided with the distribution.
    * Neither the name of the NAKAMURA Satoru nor the names of its contributors
      may be used to endorse or promote products derived from this
      software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**
 * Console.js
 * 
 * @website http://clonedoppelganger.net/
 * @version 0.0.1
 * 
 * Example: Console.print(foo);       // print toString value.
 *          Console.print(foo, true); // print analyzed object.
 */
var Console = {

  id: "____CONSOLE_AREA____",

  cue: [],

  breakTag: "<br />",

  enabled: true,

  loadTime: 50,

  waitTime: 10,

  // Style
  minWidth: 300,
  minHeight: 180,
  maxWidth: 800,
  maxHeight: 500,

  print: function(value, parse) {
    if (!this.enabled) return;
    parse = (parse === true) ? true : false;
    this.cue[this.cue.length] = {"value":value, "parse":parse};
    this.printing();
  },

  printing: function() {
    if (!document.body) {
      setTimeout("Console.printing()", this.loadTime);
    } else {
      setTimeout("Console.out()", this.waitTime);
    }
  },

  out: function() {
    var cue = this.cue;
    if (cue.length == 0) {
      return;
    } else if (cue.length == 1) {
      this.cue = [];
    } else {
      this.cue = cue.slice(1);
    }
    var value = cue[0]["value"];
    var parse = cue[0]["parse"];
    var id = this.id;
    var area = document.getElementById(id);
    if (!area) {
      var body = document.getElementsByTagName("body").item(0);
      var div = document.createElement("div");
      div.setAttribute("id", id);
      div.style.position = "absolute";
      div.style.top = "0";
      div.style.right = "0";
      div.style.width = this.minWidth + "px";
      div.style.backgroundColor = "#EBEBEB";
      div.style.zIndex = "100";
      var buttonStyle = ' style="font-family:Arial; font-size:11px; border:1px solid #bbb;'
        + 'border-top:none; border-left:none; width:32px; background-color:#F9F9F9;" ';
      div.innerHTML = [
        '<form style="margin:0px;padding:0px;">',
        '<input type="button" value="&lt;" onclick="Console.move(\'left\')"', buttonStyle, '>',
        '<input type="button" value="^" onclick="Console.move(\'top\')"', buttonStyle, '>',
        '<input type="button" value="_" onclick="Console.move(\'bottom\')"', buttonStyle, '>',
        '<input type="button" value="&gt;" onclick="Console.move(\'right\')"', buttonStyle, '>',
        '<input type="button" value=")(" onclick="Console.min()"', buttonStyle, '>',
        '<input type="button" value="()" onclick="Console.max()"', buttonStyle, '>',
        '<input type="button" value="Clear" onclick="Console.clear()"', buttonStyle, '>',
        '<input type="button" value="X" onclick="Console.close()"', buttonStyle, '>',
        '</form>',
        '<div style="height:', this.minHeight, 'px; font-size:12px; ',
        'font-family:Consolas,Courier New; color:#ddd; background-color:#000; ',
        'border:1px solid #ddd; padding:2px; overflow:auto;"></div>'
      ].join("");
      body.appendChild(div);
      area = div;
    }
    var innerDiv = area.getElementsByTagName("div").item(0);
    if (parse) {
      innerDiv.innerHTML += this.parse(value);
    } else {
      innerDiv.innerHTML += this.escape(value) + this.breakTag;
    }
    innerDiv.scrollTop = innerDiv.scrollHeight - innerDiv.offsetHeight + 2;
    if (cue.length > 0) {
      setTimeout("Console.out()", this.waitTime);
    }
  },

  parsed: null,
  parse: function(value) {
    this.parsed = "";
    this.parsing(value, 0);
    return this.parsed;
  },
  parsing: function(obj, level) {
    if (level > 6) return;
    var indent = "";
    for (var i = 0; i < level; i++) indent += "&nbsp;&nbsp;";
    for (var k in obj) {
      if (typeof obj[k] == "object") {
        this.parsed += indent + "[" + this.escape(k)+ "] --------" + this.breakTag;
        this.parsing(obj[k], level + 1);
      } else {
        this.parsed += indent + "[" + this.escape(k)
          + "] => "+ this.escape(obj[k]) + this.breakTag;
      }
    }
  },

  clear: function() {
    if (!this.enabled) return;
    var area = document.getElementById(this.id);
    var innerDiv = area.getElementsByTagName("div").item(0);
    innerDiv.innerHTML = "";
  },

  move: function(pos) {
    if (!this.enabled) return;
    var area = document.getElementById(this.id);
    area.style.display = "none";
    switch (pos) {
      case "top":
        area.style.bottom = "";
        area.style.top = "0";
        break;
      case "right":
        area.style.left = "";
        area.style.right = "0";
        break;
      case "bottom":
        area.style.top = "";
        area.style.bottom = "0";
        break;
      case "left":
        area.style.right = "";
        area.style.left = "0";
        break;
      default:
        break;
    }
    area.style.display = "block";
  },

  min: function() {
    if (!this.enabled) return;
    var area = document.getElementById(this.id);
    var innerDiv = area.getElementsByTagName("div").item(0);
    area.style.width = this.minWidth + "px";
    innerDiv.style.height =  this.minHeight + "px";
  },

  max: function() {
    if (!this.enabled) return;
    var area = document.getElementById(this.id);
    var innerDiv = area.getElementsByTagName("div").item(0);
    area.style.width = this.maxWidth + "px";
    innerDiv.style.height = this.maxHeight + "px";
  },

  close: function() {
    if (!this.enabled) return;
    document.getElementsByTagName("body").item(0).removeChild(
      document.getElementById(this.id)
    );
  },

  escape: function(value) {
    var t = document.createElement("div");
    t.appendChild(document.createTextNode(value));
    return t.innerHTML;
  }

}
