
window.onload = function()
{
    if (window.winOnLoad) window.winOnLoad();
}


function winOnLoad()
{
  xAddEventListener(document, 'click',     onClick,     false);
}


if (! document.getElementById)
{
  document.getElementById = function() { return null; };
}


function setStyle(element, property, value)
{
  element = document.getElementById(element);
  if (element.style)
  {
    element.style[property] = value;
  }
}


function replaceString(oldS, newS, fullS) {
  // Replaces oldS with newS in the string fullS
  for (var i = 0; i < fullS.length; i++) {
    if (fullS.substring(i, i+oldS.length) == oldS) {
       fullS = fullS.substring(0, i) + newS + fullS.substring(i + oldS.length, fullS.length);
    }
 }
 return fullS;
}


function toggleDisplay(evt)
{
  var e      = new xEvent(evt);
  var linkId = e.target.id;
  var disp;
  var divId;
  var element_ref;
  var left;

  if (linkId.search(/aid_D\d+/) == -1) return;

  divId       = replaceString("aid_", "handle_", linkId);
  element_ref = document.getElementById(divId);

  //alert("linkId=" + linkId + ", divId=" + divId + ", PageX=" + e.pageX + ", PageY=" + e.pageY + ", OPageX=" + e.offsetX + ", OPageY=" + e.offsetY);

  if (element_ref)
  {
    disp = element_ref.style.display;
    element_ref.style.position = 'absolute';
    element_ref.style.left     = e.offsetX + 'px';
    element_ref.style.top      = e.offsetY + 'px';
    if (disp == 'none')
    {
      element_ref.style.display = '';
    }
    else
    {
      element_ref.style.display = 'none';
    }
  }
}


function getDebugNum()
{
  if (typeof(this.static_local) == 'undefined')
  {
    this.static_local=0;
  }
  this.static_local++;
  return(this.static_local);
}


function debug(line, subroutine, message)
{
  debug_link(line, subroutine, message);
  debug_postit(line, subroutine, message);
}


function debug_link(line, subroutine, message)
{
  this.curr_link    = 'D'            + getDebugNum();
  var div_handle    = 'handle_'      + this.curr_link;
  var aid           = 'aid_'         + this.curr_link;
  var ouput         = '';

  output  = '<span class="debug">';
  output += '<a id="' + aid + '" class="debug" href="javascript:void(0);">';
  output +=     this.curr_link;
  output += '</a>';
  output += '</span>';

  document.write(output);
}


function onClick(e)
{ 
  toggleDisplay(e);
}


function debug_postit(line, subroutine, message)
{
  var file_info     =  this.filename + ' (line:' + line + ')';
  var div_handle    = 'handle_'      + this.curr_link;
  var div_title     = 'title_'       + this.curr_link;
  var ouput         = '';
  var span_id       = 'span_'        + this.curr_link;
  var topZ          = getTopZ();

  output  = '<div id="' + div_handle + '" class="debug"';
  output += '     style="position:absolute; left:100px; top:100px; zindex:';
  output +=       topZ + '; border:1px solid #000000; display:none;">';
  output += '  <div id="' + div_title + '" class="debug">';
  output += '    <table id="' + this.curr_link + '" class="debug">';
  output += '      <tbody>';
  output += '        <tr>';
  output += '          <th class="debug">';
  output +=             '<a class="debugclose" href="javascript:void(0);" ';
  output +=                'title="Close Debug Info ' + this.curr_link + '" ';
  output +=                'onclick="javascript:document.getElementById(\'';
  output +=                         div_handle;
  output +=                         '\').style.display = \'none\';">X</a> ';
  output +=             '<span class="debug" id="' + span_id + '">';
  output +=                this.curr_link;
  output +=             '</span>';
  output += '            =&gt; File:';
  output += '          </th>';
  output += '          <td class="debug"> ' + file_info + ' </td>';
  output += '        </tr>';
  output += '        <tr>';
  output += '          <th class="debug"> Subroutine: </th>';
  output += '          <td class="debug"> ' + subroutine + ' </td>';
  output += '        </tr>';
  output += '        <tr>';
  output += '          <th class="debug"> Message: </th>';
  output += '          <td class="debug">' + message + '</td>';
  output += '        </tr>';
  output += '      </tbody>';
  output += '    </table>';
  output += '  </div>';
  output += '</div>';

  document.write(output);
}
