/**
 * Copyright (c) 2009
 * TechEmpower, Inc.
 * All Rights Reserved.
 *
 * log.js
 *
 * Development history:
 *   Jan 16, 2009 - jdavis - Created
 */
 
/**
 * Log
 *
 * A simple logging object that passes messages to either a custom 
 * logDebugMessage function defined on your window or, if not available, 
 * will pop up alert boxes. Defining a logDebugMessage function would 
 * allow things such as a visual container on the page that displays 
 * the messages.
 * 
 * A GlobalLog variable will be intialized and available after this script
 * is included.
 */
function Log()
{
  // Allows toggling of alerts, in case they are not desired
  this.allowAlert = false;
  this.containerID = 'DebugWindow';
  this.logID = 'DebugLog';
  this.currentMessage = '';
  this.messageHistory = '';
  
  // Logs a message either by passing it off to the window or 
  // popping up an alert box.
  this.debug = function(message)
  {
    this.messageHistory = this.currentMessage + '<br />' + this.messageHistory;
    this.currentMessage = message;
    
    if (!this.updateVisualElements() && this.allowAlert)
      alert(message);
  }
  
  this.clearHistory = function()
  {
    this.messageHistory = '';
    this.updateVisualElements();
  }
  
  this.updateVisualElements = function()
  {
    var element = document.getElementById(this.logID);

    if (element == undefined)
      return false;
    
    element.innerHTML = '<span class="current">' + this.currentMessage + '</span><br />' + this.messageHistory;
  }
  
  this.generateVisualElements = function(id, containerID)
  {
    if (id == undefined)
      id = 'DebugLog';
    if (containerID == undefined)
      containerID = 'DebugWindow';
    
    this.containerID = containerID;
    this.logID = id;
    var html = '<div id="' + containerID + '" class="debugWindow"><div><div id="' + id + '" class="scrollpane"></div><a class="debugButton" href="javascript:void(0);" onclick="GlobalLog.toggle(this)">Close Debug Window</a></div></div>';
    
    document.write(html);
  }
  
  this.toggle = function(source)
  {
    var element = document.getElementById(this.logID);
    if (element == undefined)
      return;

    var text;

    if (element.style.display == "none")
    {
      element.style.display = "";
      text = "Close Debug Window";
    }
    else
    {
      element.style.display = "none";
      text = "Open Debug Window";
    }

    if (source != undefined)
      source.innerHTML = text;
  }
}

/* Initialize the globalLog variable so it will be available in all 
 * subsequent scripts.
 */
var GlobalLog = new Log();
