// Adds a console global object to browsers which have none (ie6)
// the console checks for the value of the hsp.log variable, if logging is turned off (see .htaccess) console.log() does nothing
// Otherwise a overlay logging window is added to the page, where logging statements are displayed
(function () {

  var $,lib,loglevel,stage;
  $ = jQuery;
  lib = hsp;
  stage = lib.stage || 'prod';
  if (!lib.log) {
    stage = 'prod';
  }

  // create a console object even before document.ready and cache all calls to log()
  if (typeof (console) === 'undefined') {
    console = {
      log : function (text) {
        if (stage === 'prod') {
        // do nothing on production system
        }
        else  {
          console.cache.push(text);
        }
      }
    }
    console.cache = [];
    // set a marker, that we know that this is our console object
    console.log.fflog = true;
  }

  $(document).ready(function () {
    if (console.log.fflog) {
      // configure logging here
      // a reasonable way to do this is to
      if (stage !== 'prod') {
        $('body').append(
          '<div id="logging-div">'+
          '<div id="logging-header" style="border-bottom: 1px solid black;padding: 1px 2px;">'+
          'Logging Window, click to close or toggle with \'L\''+
          '</div>'+
          '</div>'
          );
        $('#logging-div').css({
          display: 'block',
          position: 'absolute',
          zIndex: 5000,
          top: '20px',
          right: '20px',
          backgroundColor: '#eeeeee',
          height: '200px',
          overflow: 'auto',
          width: '400px',
          border: '1px solid black',
          opacity: '0.7'
        }).click(function () {
          $(this).fadeOut();
        });
      }



      var cache = console.cache;
      console.log = function (x) {
        // if logging is not turned on, the concole object just eats the console.log() calls
        if (stage === 'prod') return;
        $('#logging-div').append(x+'<br/>');
      }
      while(cache.length > 0) {
        var txt = cache.shift()
        console.log(txt);
      }
      

      if (stage !== 'prod') {
        $(document).keyup(function(e){
          // set the L key
          if (e.keyCode === 76)
          {
            var display = $('#logging-div').css('display');
            if (display === 'block') {
              $('#logging-div').fadeOut(200);
            } else {
              $('#logging-div').fadeIn(200);
            }
          }
        });
      }
    }
  });
}());

