/**
 * Ajax-MVC 
 * Handling Ajax Requests and misc. functions
 * 
 * @author   Jürgen Cito <citostyle@gmx.at>
 * @version  0.1
 * 
 */

if(!AM) var AM = {};

AM.extend = function(destination, source) 
{
  if(destination != null)
    for (var property in source)
      destination[property] = source[property];
  return destination;  
};

AM.Env = {
   /**
    *  Browser is Firefox
    */   
	ff : /firefox/i.test(navigator.userAgent),
   /**
    *  Browser is Internet Explorer
    */
	ie : /msie/i.test(navigator.userAgent),
   /**
    *  Browser uses Gecko Engine
    */   
	gecko : /gecko/i.test(navigator.userAgent),
   /**
    *  Browser supports ActiveX
    */   
	ax : typeof(ActiveXObject) == 'function'
};

AM.Manager = function(url, options)
{
     this.url = url;
     this.options = { method : 'GET', async : true, postdata : null };
     this.setOptions(options);
};

AM.Manager.prototype = {

  setOptions : function(options)
  {
    AM.extend(this.options, options || {});
  },

  send : function(params)
  {
     var referer = this;
     AM.xhr.open(this.options.method, this.url, this.options.async);
     
     AM.xhr.onreadystatechange = function() { AM.Manager.InternalResponseHandler.call(referer); }; 
     
     if (this.options.method == "POST")
        AM.xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");        
        
     AM.xhr.setRequestHeader("Connection", "Close");
     
     /**
      * Telling the server in our http request header, 
      * that this request is a XmlHttpRequest (more or less Ajax)
      * 
      * Inspired (we could also say stolen) by Prototype =)
      *
      */
     AM.xhr.setRequestHeader("X-Requested-With", "XmlHttpRequest");
     
     AM.xhr.send(this.options.postdata);
  }
};

AM.Manager.InternalResponseHandler = function ()
{
 if(typeof this.options['onReadyStateChange'] == "function")
   this.options['onReadyStateChange'].call(AM.xhr);
 
 if (AM.xhr.readyState == 4)  
   if (AM.xhr.status == 200)
   {
      if(typeof this.options['onSuccess'] == "function")
        this.options['onSuccess'](AM.xhr);
   }
};

AM.extend(AM,
{   
   xhr : null,

   setOptions : function(options) { this.options = options; },
   
   Request : function(url, options)
   {
      if(this.xhr == null) this.xhr = new XHRFactory();
      m = new AM.Manager(url, options);
      m.send();
   }
});

AM.Ajax = {
   _frontController : 'index.php',
   
   setFrontControllerPath : function(path)
   {
      this._frontController = path;   
   },
   
   Open : function(actionStr, options)
   {
      AM.Request(this._frontController + '?as='+actionStr, options);
   }
};


AM.Misc = {
   id : {   
      _prefix : '',
      complex : function()   
      {
         return (new Date().getTime() + Math.Random().toString().replace('.', ''));
      },
      
      /**
       * Generates an unique id within the dom structure
       * @return {String} 
       */
      dom : function(prefix)
      {
         prefix = prefix || this._prefix;
         var lfdNr = 1;
         while($get(prefix + lfdNr) !== null)
            lfdNr++;
         return prefix + lfdNr;
      }
   },
   
   /**
    * Generates random (integer) numbers between min and maximum
    * @param {Integer} min
    * @param {Integer} max
    * @type {Integer}
    * @return Random number between min and max
    *
    */
   random : function(min, max)
   { 
      return Math.floor(Math.random()*(max-min)+min); 
   }
}

