function hideloader(divId){
      var slideshow_effect = new fx.Opacity(divId, {duration: 1000});
      slideshow_effect.toggle();
}

var SmartyAjax = {
  update: function(update_id, url, method, params, callback) {
    var myAjax = new Ajax.Updater(
      update_id,
      url,
      {
        method: method,
        parameters: params,
        onComplete: callback
      });
  },

  call: function(url, method, params, callback, params_func) {
	var showPopup, changeCursor;
	if (arguments.length >= 6)
		showPopup = arguments[5];
	if (arguments.length >= 7)
		changeCursor = arguments[6];
		
    if (params_func) {
      if (params.length != 0) params += "&";
      params += $H(params_func()).toQueryString();
    }
      var div_id = 'ajax_hover_menu';

	if (showPopup){
      // show div menu
      if (document.getElementById(div_id) == null){
      	// show default alertbox
		div = document.getElementById(div_id+"_default");
		div_id = div_id+"_default";
		div.style.top = ((document.documentElement.clientHeight - 60) + document.documentElement.scrollTop) + 'px';      
      } else {
      	// show alertbox
      	div = document.getElementById(div_id);
      }    	
	} 
	      var myAjax = new Ajax.Request(
	      url,
	      {
	        method: method,
	        parameters: params,
	        onComplete: function (request)
				{
					if(showPopup) {
						setTimeout("hideloader('"+div_id+"');",1000);
					}
					if(changeCursor)
						resetCursor();
					
					callback(request);
				}
	      });
	if (showPopup){
      //fadeIn message
      var slideshow_effect = new fx.Opacity(div_id, {duration: 500, transition: fx.expoIn});
      slideshow_effect.custom(0.4,1);	  
      // hide div alert with timeout      
  	  setTimeout("hideloader('"+div_id+"');",1000); 
    }
    
    // change cursor during AJAX request
    if(changeCursor){
   		setCursor();
    }
  },
  
  submit: function(form, params, callback) {
  	var myAjax = new Ajax.Request(
  		form.action,
  		{
  			method: form.method,
  			parameters: Form.serialize(form.id),
        onComplete: callback || this.onSubmit
  		});
  },

  onSubmit: function(originalRequest) {
    var results = originalRequest.responseText.split(";");

    if (results[0] == "true") {
      SmartyAjax.Messages.set(results[1], SmartyAjax.Messages.MT_WARNING)
    } else {
      SmartyAjax.Messages.clear();
      SmartyAjax.Messages.setType(SmartyAjax.Messages.MT_ERROR);
      for (var i = 1; i < results.length; i++) {
        SmartyAjax.Messages.add(results[i]);
      }
    }
  }
}

SmartyAjax.GlobalHandlers = {
	onCreate: function() {
		SmartyAjax.Process.show();
	},

	onComplete: function() {
		if(Ajax.activeRequestCount == 0){
  		SmartyAjax.Process.hide();
		}
	}
};

Ajax.Responders.register(SmartyAjax.GlobalHandlers);

SmartyAjax.Messages = {
  MT_WARNING: 0,
  MT_ERROR: 1,

  S_MT_WARNING: "Please note:",
  S_MT_ERROR: "Please fix following errors:",

  initialize: function() {
    this.messages = $("messages");
    this.messagesTitle = $("messages-title");
  },

  clear: function() {
    if (!this.messagesList) {
      this.messagesList = $("messages-list");
    	if (!this.messagesList) return;
    }
  	this.messagesList.innerHTML = "";
  	this.messagesList.style.display = "none";
  },

  add: function(message) {
    if (!this.messagesList) {
      this.messagesList = $("messages-list");
    	if (!this.messagesList) return;
    }
  	var messageLI = document.createElement("LI");
  	messageLI.innerHTML = message;
  	this.messagesList.appendChild(messageLI);
  	this.messagesList.style.display = "block";
  	Element.scrollTo("messages");
  },

  set: function(message, type) {
    this.clear();
    this.setType(type);
    this.add(message);
  },

  setType: function(type) {
    if (!this.messages) this.messages = $("messages");
  	if (!this.messagesTitle) this.messagesTitle = $("messages-title");
  	switch (type) {
  	  case this.MT_ERROR:
  	    if (this.messages) this.messages.className = "data-error";
  	    if (this.messagesTitle) this.messagesTitle = this.S_MT_ERROR;
  	    break;
  	  case this.MT_WARNING:
  	  default:
  	    if (this.messages) this.messages.className = "data-warning";
  	    if (this.messagesTitle) this.messagesTitle = this.S_MT_WARNING;
  	}
  }
}

SmartyAjax.Process = {
  S_PROCESS: "processing request...",

  show: function() {
    if (!this.process) {
      this.process = $("ajax-process");
      if (!this.process) return;
    }
    var top = window.pageYOffset ? window.pageYOffset : document.documentElement ? document.documentElement.scrollTop : document.body.scrollTop;
    this.process.style.top = top + "px";
    this.process.innerHTML = this.S_PROCESS;
    this.process.style.display = "inline";
  },

  hide: function() {
    if (!this.process) {
      this.process = $("ajax-process");
      if (!this.process) return;
    }
    this.process.style.display = "none";
  }
}

function setCursor()
{
	document.body.style.cursor = 'wait';
	document.body.className = 'ajaxcursor';
}

function resetCursor()
{
	document.body.className = '';
	document.body.style.cursor = 'auto';
}
