/*************************************************************
 *                                                           *
 *   Created by Tamas Manhertz (PYLON)/Manhertz-Pylon Ltd.   *
 *            All rights reserved. 2007.                     *
 *                                                           *
 *************************************************************/
/*************************************************************
 *    #DEP: DOMComponent, singletons                         *
 *************************************************************/
/*************************************************************
 *  JSO Object:  LogHandler                                  *
 *        Type:  singleton                                   *
 *                                                           *
 * Description: Javascript log handler object                *
 *                                                           *
 *  EventHandler jso object must already be exist!           *
 *                                                           *
 *************************************************************/

jso_LogHandler = new pDOMComponent( "jso_LogHandler", "jso_LogHandler" );
jso_LogHandler.extend(
 {
   // ***************************************************
   //                  PROPERTIES
   // ***************************************************

           /**
            *  reference to DOM components in LogPanel
            *    - used for displaying and handling the log
            * @type: object (HTML object)
            */
       _oDOMPanel : null,
           /**
            *  CONSTANT HTML definition to create the Log Panel
            * @type: string (filename)
            */
       _bLoadHTML : true,
           /**
            *  Log text buffer, collect them as a simple string
            * @type: string
            */
       _sLogBuffer : "",
       _LOG_PANEL_HTML : "",
   // ***************************************************
   //                  PRIVATE  METHODS
   // ***************************************************
           /**
            * Component ID string.
            */
       _getIDString : function ()
        {
           return "Debugging JSO object.";
        },
           /**
            *   It tells whether the Log Panel HTML Layer is visible or not.
            */
       _isLayerVisible : function ()
        {
            return (( this._oDOMPanel ? this._oDOMPanel.style.visibility == "visible" : false ));
        },
           /**
            *   Initializes the HTML Log Panel.
            */
       _initializeLogPanel : function ()
        {
            syLog( "*** Initializing LogPanel HTML elements." );

              // Searching for the specific tags
            this._oDOMPanel = $(CONSTANTS.sLOG_PANEL_DIV_ID);
            if (!this._oDOMPanel)   return;
            this._oDOMPanel.style.visibility = "hidden";

              // setting up the eventhandlers ( Shift+F2 and buttons on Panel )
            this._setEventHandler( document.body, "onkeyup", this.onFormKeyUp );
            this._setEventHandler( document.getElementById("LOG_PANEL_CLEAR_BUTTON"), "onclick", this.clearBuffer );
            this._setEventHandler( document.getElementById("LOG_PANEL_SEND_BUTTON"), "onclick", this.sendBufferToServer );
            this._regComponentData( "_sLogBuffer" );
            this.addLine("*** LogPanel initialized." );

              // Logging informations
            this.addLine("Page and base JSO script are loaded.");
            this.addLine(" Page processing time (all number is milliseconds since midnight of 01/01/1970): ");
            this.addLine("  +------------------+------------------+------------------+------------------+");
            this.addLine("  |       Start time |        AfterHead |         End time |       Difference |");
            this.addLine("  +------------------+------------------+------------------+------------------+");
            this.addLine(sprintf("  | %16d | %16d | %16d | %16d |", _dUnixtime_Start, _dUnixtime_Afterhead, _dUnixtime_End, _dUnixtime_End-_dUnixtime_Start));
            this.addLine("  +------------------+------------------+------------------+------------------+");
            var _dNow = new Date();
            this.addLine("    Now: "+sprintf("%16d", _dNow));
            this.addLine("  Total: "+sprintf("%16d", (_dNow-_dUnixtime_Start)));
            this.addLine("**** Page is loaded and initialized ****");
        },
           /**
            *   Makes the HTML Log Panel visible or invisible.
            */
       _toggleLogArea : function ()
        {
           if (this._oDOMPanel) {
               if (this._oDOMPanel.style.visibility == "hidden")
                {
                   if (!(_oDOMObj = document.getElementById(CONSTANTS.sLOG_TEXTAREA_ID))) return false;
                         _oDOMObj.value = this._sLogBuffer;
                   this._oDOMPanel.style.visibility = "visible";
                }
               else
                   this._oDOMPanel.style.visibility = "hidden";
           }
        },
   // ***************************************************
   //                   PUBLIC METHODS
   // ***************************************************
           /**
            *   an onkeyup event handler for the BODY HTML element
            */
       onFormKeyUp : function ( oEvent )
        {
            if (oEvent.altKey)
             {
               switch(oEvent.keyCode)
                {
                    case 113:  //F2
                        this._toggleLogArea();
                        break;
                }
             }
           return true;
        },
           /**
            *   Initializes the LogHandler object:
            *       - creates the HTML Log Panel,
            *       - set BODY tag onkeyup event handler,
            *       - add some information log lines.
            */
       init : function ()
        {
            if ((!ComponentCommMgr._bPageLoaded) | (!document.body))
                  return setTimeout( function(){ jso_LogHandler.init(); }, 100 );

            if ($(CONSTANTS.sLOG_PANEL_DIV_ID))
             {
                syLog( "LogPanel is built into the HTML code, initializing." );
                this._initializeLogPanel();
             }
            else
             {
                syLog( "LogPanel will be downloaded from the server..." );
                this._call( "getLogPanel" );
             }
        },
           /**
            *   Processes the data from server (sent to client log buffer?)
            */
       processServerData : function ( sDName )
        {
            switch (sDName)
             {
                case "_LOG_PANEL_HTML":
                      if (!document.body)
                              return setTimeout( function(){ jso_LogHandler.processServerData(sDName); }, 100 );

                      this._unregComponentData( "_bLoadHTML" );
                      this._unregComponentData( "_LOG_PANEL_HTML" );

                        // creatig the new child for body that contains the Logpanel HTML
                      _oLayer = document.createElement("div");
                      _oLayer.innerHTML = this._LOG_PANEL_HTML;
                      document.body.appendChild(_oLayer);

                        // initializing the HTML code
                      this._initializeLogPanel();
                      break;
             }
        },
           /**
            *   Clears the log buffer.
            */
       clearBuffer : function ()
        {
           this._sLogBuffer = "";
           this.addLine("Log buffer was cleared.");
           return true;
        },
           /**
            *   Sends the log buffer to the server.
            */
       sendBufferToServer : function ()
        {
           this._sendData();
           return true;
        },
           /**
            *   Adds a line to log buffer (on the Log Panel too if it is visible)
            */
       addLine : function ( sTxt )
        {
           clock = new Date();
           _sLogLine = clock.Clock()+" "+sTxt;
           if (CONSTANTS.LOG_TEXT_DIRECTION == "up")
                this._sLogBuffer = _sLogLine.replace(/</g, "&lt;").replace(/>/g, "&gt;")+"\n" + this._sLogBuffer;
           else
                this._sLogBuffer += _sLogLine.replace(/</g, "&lt;").replace(/>/g, "&gt;")+"\n";
           if (this._isLayerVisible())
            {
               if (!(_oDOMObj = document.getElementById(CONSTANTS.sLOG_TEXTAREA_ID))) return false;
               _oDOMObj.value = this._sLogBuffer;
            }
        }
});
syLog("jso_LogHandler object is created.", CONSTANTS.LOG_TYPE_DEBUG_INFO);
   // ***************************************************
   //                Construct statements
   // ***************************************************
jso_LogHandler.init();
syLog("jso_LogHandler is initialized.");

