/*
  Knowmax Magma Platform Logging

    Provides support for logging events form Knowmax Magma
    Platform Applications. Relies on Prototype JavaScript library.

  version 1.1.0 - june 13 2007
    - Added method updateUrl
    - Set REQUIRED_PROTOTYPE to '1.5.0' (was '1.5.1').
    - Added function 'buildversion'
    - Changed function 'version' to include 'buildversion'
  version 1.0 - april 11 2007
    - initial version

  (c) 2007 Knowmax
*/

var KMMPLogging = {
  majorversion: function() {
    return 1;
  },

  minorversion: function() {
    return 1;
  },

  buildversion: function() {
    return 0;
  },

  version: function() {
    return this.majorversion() + "." + this.minorversion() + "." + this.buildversion();
  },

  // Internally called to set default values.
  _construct: function() {
    this._url = null;
    this._initialized = false;
    this._enabled = true;
  },  	
	
	REQUIRED_PROTOTYPE: '1.5.0',
	
  initialize: function(url) {
    if (!this._initialized) {
      function convertVersionString(versionString){
        var r = versionString.split('.');
        return parseInt(r[0])*100000 + parseInt(r[1])*1000 + parseInt(r[2]);
      }

      if ((typeof Prototype=='undefined') ||
          (typeof Element == 'undefined') ||
          (typeof Element.Methods=='undefined') ||
          (convertVersionString(Prototype.Version) < convertVersionString(KMMPLogging.REQUIRED_PROTOTYPE))) {
        throw("KMMPLogging requires the Prototype JavaScript framework >= " + KMMPLogging.REQUIRED_PROTOTYPE);       	
      } else if ((url == null) || (url == "")) {      	
      	throw("KMMPLogging requires value for url");
      } else {
        this._url = url;
        this._initialized = true;
        return true;
      }
    } else {
      return true;
    }
  },

  // Checks whether this instance is initialized.
  initialized: function() {
    return this._initialized;
  },

  disableLogging: function() {
  	this._enabled = false;
  },

  // Update url, instance must be initialized.
  // Url must be defined and not empty.
  updateUrl: function(url)
  {
    var urlupdated = false;
    if (this._initialized) {
      if (typeof url != 'undefined') {
        if (url != '') {
          this._url = url;
          urlupdated = true;
        }
      }
    }
    return urlupdated;
  },

  // eventhash should contain an hash instance created with $H. Prefix key
  // values with "k_", prefix parameter values with "p_". sidvalue is optional.
  logEvent: function(eventhash, sidvalue)
	{							
		if ((this._enabled) && (this._initialized) && (eventhash != null)) {					
		  if (sidvalue == null) {
			  sidvalue = "";
		 	}			
		 	try {
		 	  var list = new Hash({ sid: sidvalue,
		 		  kmmagmaplatformrequesttype: "ajaxlogevent" });												
  	 	  list = list.merge(eventhash);  	 	
				
		    new Ajax.Request(this._url,
		    {
			    method: 'get',
			  	parameters: list,
			  	onSuccess: function(transport) {				  		
					  var json = transport.responseText.evalJSON(true);								
						if (!json.Logging.Enabled) {							
							KMMPLogging.disableLogging();
						}						
			  	},
			  	  onFailure: function() {			  	  	
			  	  	KMMPLogging.disableLogging();
				  }
				});			
		  } catch(err) {
		    KMMPLogging.disableLogging();	
		  }
		}
	}  	
}

KMMPLogging._construct();
