/*
Script: Ajaxtor.js
        Contains the <Ajaxtor> class with extensions.

Credits:
        Loosely based on the version from prototype.js <http://prototype.conio.net>

License:
        MIT-style license.
*/

var Ajaxtor = Ajax.extend(
{
   options:
   {
      isShowLoader: true,
      onShowLoader: null,
      onHideLoader: null,
      onHistory: null
   },

   initialize: function(options)
   {
      this.setOptions(options);
      this.parent();
      this.url = window.location.href;
      this.options.evalResponse = true;
      this.options.evalScripts = true;
      this.currentHash = null;
      this.historyInterval = null;
      this.addEvent('onComplete', this.hideLoader);
   },

   call: function(func)
   {
      var params = "", i = 1, args = arguments;
      if (arguments.length == 2 && typeof(arguments[1]) == 'object')
      {
         args = arguments[1];
         i = 0;
      }
      params += "ajaxfunc=" + encodeURIComponent(func);
      for (; i<args.length; i++)
        params += "&ajaxargs[]=" + encodeURIComponent(this.encodeObj(args[i]));
      this.showLoader();
      return this.request(params);
   },

   encodeObj: function(param)
   {
      if (typeof(param) == 'object')
      {
         var obj = "<ajaxarray>";
         for (i in param)
         {
             if (i == 'constructor' || param[i] && typeof(param[i]) == 'function') continue;
             obj += "<k>" + i + "</k><v>" + this.encodeObj(param[i]) + "</v>";
         }
         obj += "</ajaxarray>";
         return obj;
      }
      return param;
   },

   submit: function(func, form, target)
   {
      form = $(form);
      var old_target = form.target;
      var old_action = form.action;
      var old_method = form.method;
      var old_enctype = form.encoding;
      form.action = this.url.replace('#', '') + (this.url.contains('?') ? '&' : '?') + "ajaxfunc=" + encodeURIComponent(func) + "&ajaxsubmit=1";
      form.method = 'post';
      form.target = target;
      form.encoding = 'multipart/form-data';
      form.submit();
      form.target = old_target;
      form.action = old_action;
      form.method = old_method;
      form.encoding = old_enctype;
   },

   showLoader: function()
   {
      if (this.options.isShowLoader)
      {
         if (document.body) document.body.style.cursor = 'wait';
         if (typeof(this.options.onShowLoader) == 'function') this.options.onShowLoader();
      }
   },

   hideLoader: function()
   {
      if (this.options.isShowLoader)
      {
         if (document.body) document.body.style.cursor = 'default';
         if (typeof(this.options.onHideLoader) == 'function') this.options.onHideLoader();
      }
   },

   startHistory: function()
   {
      this.currentHash = window.location.hash;
      if (window.ie)
      {
      	 var el = new Element('iframe', {'styles': {'display': 'none'}, 'id': 'ajax_historyFrame'});
         el.inject(document.body, 'top');
         var iframe = $('ajax_historyFrame').contentWindow.document;
         iframe.open();
         iframe.close();
         iframe.location.hash = this.currentHash;
         if (!this.currentHash) this.currentHash = '#';
      }
   	  this.historyInterval = setInterval(this.history, 100);
   },

   addHistory: function(hash)
   {
   	  if (window.ie)
   	  {
   	     var iframe = $('ajax_historyFrame').contentWindow.document;
         iframe.open();
         iframe.close();
         iframe.location.hash = hash;
   	  }
   	  window.location.hash = hash;
   },

   stopHistory: function()
   {
   	  clearInterval(this.historyInterval);
   	  this.currentHash = null;
   	  this.historyInterval = null;
   },

   history: function()
   {
      var hash;
      if (window.ie) hash = $('ajax_historyFrame').contentWindow.document.location.hash;
      else hash = window.location.hash;
      if (this.currentHash != hash)
      {
         this.currentHash = hash;
         if (window.ie) window.location.hash = hash;
         if (typeof(this.onHistory) == 'function') this.onHistory(this.currentHash.substr(1));
      }
   }

});