/* 	Copyright 2008 Maximum Processing Inc
	This script provides logging capabilities

	Dependencies:
	  - Utilities.js

	Change Log
 	Created 11/19/2008 Nathan Townsend
*/






// log entry types, the value is used as a class when displayed
LogType = new function()
{
    this.Error = "error";
    this.Info = "info";
    this.Warning =  "warning";

}


// Provides logging capabilities
Log = new function()
{

  /* Public Properties */
  
  this.LoggingEnabled = false;
 
  // RethrowErrors should always be set to true 
  this.RethrowErrors = true;
	
	this.AlertErrorsWhenDisabled = false;
  
  
  
  
  // private
  var _loggingDiv = null;
  
  var _logTable = null;
    
  var _entryID = 0;
  
  var _logID = 0;


  /* Public Methods */
  return{
    // Add an entry to the log [default Type = LogType.Info]
    Add: function(Source, Message, Type)
    {
		  //alert("Source: " + Source + "\n\nMessage: " + Message + "\n\nType: " + Type);
		
      if(Type == null)
        Type = LogType.Info;

      if(this.LoggingEnabled)
      {
          var msg = "";

          if(typeof Message == "object")
          {
             if(Message.EntryID == null)
               Message.EntryID = GetEntryID();

             msg = Message.description + " [Entry = " + Message.EntryID + "]";

						 if(Message.fileName)
						   msg += " [File = " + Message.fileName + "]";

						 if(Message.lineNumber)
						   msg += " [line = " + Message.lineNumber + "]";

          } else { msg = Message; }

          var entry = new LogEntry(Source, msg, Type);

          _logTable.append(FormatEntry(entry));
      }

			// send the error to stringray
			if(Type == LogType.Error)
			{
			  //var rList = encodeURIComponent(ReqList.GetList());
				var rList = ReqList.GetList();
			  var data = "ReqList=" +  rList;
				var vrmName = $("#middle").attr("VRMName");
				var url = "Logging.max?action=log&Source=" + Source + "&Message=" + Message + "&VRMName=" + vrmName;
				var d = new Date().getTime();
				//$("#bottom").text(url + "  " + d);
				
			  Communication.CustomRequest(url, function(response){ }, data);
			}

      if(this.RethrowErrors && (Type == LogType.Error))
        throw Message;
    },
    
    
    Initialize: function(DisplayDiv)
    {
        _loggingDiv = $(DisplayDiv);

        _logTable = $("<table class='log'><tr><th>id</th><th>Time</th><th>Source</th><th>Message</th></tr></table>");

        _loggingDiv.append(_logTable);
        
        this.LoggingEnabled = true;
      
        this.RethrowErrors = true;

        this.Add("Log.Initialize", "Initialized", LogType.Info);
    }
    
  }
  

  /* Private Properties */
  var _log = null; 
  
   
  
  function GetEntryID(){
    _entryID = _entryID + 1;
    
    return _entryID;
  }
  
  
  // Get the log html
  function FormatEntry(entry){
    
    _logID++;
      
    var le = $("<tr id='LogEntry" + _logID + "' />");
    
    le.append("<td class='LogEntryId'>" + _logID + "</td>");
    
    le.append("<td class='LogEntryTime'>" + entry.Time + "&nbsp;" + "</td>");
    
    le.append("<td class='LogEntrySource'>" + entry.Source + "&nbsp;" + "</td>");
    
    le.append("<td class='LogEntryMessage " + entry.Type + "'>" + entry.Message + "&nbsp;"  + "</td>");
    
    return le;
  }
  

 
  /* Private Methods */
  function LogEntry(Source, Message, Type)
  {
    this.Source = Source;
    
    this.Message = Message;
    
    this.Time = Utilities.GetFormattedTime();
    
    this.Type = Type;
    
    
  }
  
  
  

}
