/* Copyright (c) 2006-2007 Ido Kanner under the Modified MIT license
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this 
 * software (the "Software"), to deal in the Software without restriction, including 
 * without limitation the rights to use, copy, modify, merge,  publish, distribute,
 * sub-license, and/or sell copies of the Software, and to permit persons to whom the 
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all 
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 
 * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT 
 * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
 * DEALINGS IN THE SOFTWARE.
 */

/* 
 * This log code, is inspired by FPC LCL TStringList (It's base on Borland Delphi idea) 
 * The idea was also been inspired by Google's XSTL library - ajaxslt
 */

// Should we allow ourselfs to store the log ?
var debug_ToLog = false;

// Our Log object 
function Log() {};

// This array stores our entire log
Log.strings = new Array;

// add the log lines into strings 
Log.add = function (text)
{
  if (debug_ToLog)
  {
    var line = text.replace(/&/g, '&amp;').
                    replace(/</g, '&lt;').
                    replace(/>/g, '&gt;').
                    replace(/\"/g, '&quot;').
                    replace(/\r\n/, '<br/>\r\n').
                    replace(/\n/, '<br/>\n').
                    replace(/\s/, '&nbsp;')
                    ;
    this.strings.push (line);
  } // if (debug_ToLog)
} // Log.lines.Add = function (s)

// allow you to add text in specific location in the list
Log.addIndexed = function (index, text)
{
  if (debug_ToLog)
  {
    if (! typeof index == "number" || isNaN(index)) return ;
    var line = replace(/&/g, '&amp;').
               replace(/</g, '&lt;').
               replace(/>/g, '&gt;').
               replace(/\"/g, '&quot;').
               replace(/\r\n/, '<br/>\r\n').
               replace(/\n/, '<br/>\n').
               replace(/\s/, '&nbsp;')
               ;

    if (index <= 0)
      this.strings.unshift (line);
    else if (index < this.strings.length)
      this.strings.splice(parseInt(index, 10), 0, line);
    else
      this.strings.push(line);
  } // if (debug_ToLog)
} // Log.replace = function (index, text)

// this function places the content as a one string.
// the function does not use the function toString, in order to display line numbers.
Log.printLog = function (LineNumber)
{
  if (debug_ToLog)
  {
    var result = "";
    if (this.strings.length != 0)
    {
      if (LineNumber == true)
        result += "<ol>\n";
      for (var i=0; i< this.strings.length; i++)
      {
        if (LineNumber == true)
          result += "<li>";
     
        result += this.strings[i];
        if (LineNumber == true)
          result += "</li>\n";
      } // for (var i=0; i< this.strings.length; i++)
      if (LineNumber == true)
        result +="</ul>\n";
    } // if (this.strings.length != 0)

    return result;
  } // if (debug_ToLog)
  else
  {
    return ""
  } //else
} // Log.printLog = function (LineNumber = true)

// this function places the content of the array as one string.
// the function is the same as printLog, only the end of the a line also contain <br/>.
Log.printLogXHTML = function (LineNumber)
{
  if (debug_ToLog)
  {
    var result = "";
    if (this.strings.length != 0)
    {
      if (LineNumber == true)
        result += "<ol>\n";
      for (var i=0; i< this.strings.length; i++)
      {
        if (LineNumber == true)
          result += "<li>";
     
        result += this.strings[i].replace(/\n/g, "<br/>\n");
        if (LineNumber == true)
          result += "</li>\n";
      } // for (var i=0; i< this.strings.length; i++)
      if (LineNumber == true)
        result +="</ul>\n";
    } // if (this.strings.length != 0)

    return result;
  } // if (debug_ToLog)
  else
  {
    return ""
  } // else
} // Log.printLogXHTML = function (LineNumber = true)

// Clear the log string
Log.clear = function()
{
  this.strings = [];
} // Log.lines.clear = function()

//Should we scroll down to the last written entry ?
Log.displayLastLine = false;

//Create or return the log div
Log.div = function ()
{
  var width  = arguments[0] ? arguments[0] : 450;
  var height = arguments[1] ? arguments[1] : 250;

  if (isNaN (width) || isNaN (height))
    throw ("Wrong width and/or height value(s)");

  var element = document.getElementById("logLines");
  if (! element)
  {
    element                = document.createElement('div');
    element.id             = "logLines";
    element.className      = "logLines";
    element.style.width    = parseInt(width, 10) + "px";
    element.style.height   = parseInt(height, 10) + "px";
    
    document.getElementById("content").appendChild(element);
  } // if (! element)

  this.element = element;
} // debug.prototype.div = function ()

// place the log inside the div. 
Log.displayLog = function()
{
  if (debug_ToLog)
  {
    if (! this.element)
      this.div();

    this.element.innerHTML = '\n<div style="width: 100%; '      + 
                                         'text-align: center; ' + 
                                         'font-weight: bold; '  + 
                                         'font-size: 110%; '    + 
                                         '">Debug</div>\n'      + 
                             this.printLogXHTML(true);
    this.element.scrollTop = this.displayLastLine ? this.element.scrollHeight : 0;
  } // if (debug_ToLog)
} // debug.prototype.displayLog = function()

