/**
 * AjaxController
 */

var MJL_Ajax_Handler_Request = Class.create(MJL_Ajax_Handler_History, {
	options:{}, requesting:0,
	
	inititialize:function() {

	},
	
	setOptions:function(options) {
		this.options = {
			url: !Object.isUndefined(options) && options.url ? options.url : null,
			responseId: !Object.isUndefined(options) && options.responseId ? options.responseId : '',
			method: !Object.isUndefined(options) && options.method ? options.method : 'get',
			insertion: !Object.isUndefined(options) && options.insertion ? options.insertion : null,
			parameters: !Object.isUndefined(options) && options.parameters && typeof(options.parameters) == 'object' ? Object.toQueryString(options.parameters) : Object.isUndefined(options) || !options.parameters ? '' : options.parameters.toQueryParams(),
			onSuccess: !Object.isUndefined(options) && options.onSuccess ? options.onSuccess : null,
			onFailure: !Object.isUndefined(options) && options.onFailure ? options.onFailure : null
		};
	},
	
	loading:function() {
		$('indicator').show();
		
		/*var loader = new YAHOO.util.Anim("mcp-loader", {
			width: {from: 0, to:100}
		}, 1.2);
		
		loader.animate();
		
		var instance = this;
		loader.onComplete.subscribe(function() {
			if(!instance.requesting) {
				instance.loading();
			} else {
				$('mcp-loader').setStyle({width:0});
			}
		});*/
	},
	
	request:function(url) {
		
		if((url.empty() || url == '#') && !options.url) {
			return;
		}
	
		var url	= !this.options.url ? url : this.options.url;

		this.initializeNavigationBar(url);
		
	},
	
	asyncRequest:function(url) {
		
		var callback =
		{
		  success: this.handleSuccess,
		  failure: this.handleFailure,
		  argument: this
		};
		
		this.requesting = 1;
		this.loading();
		
		YAHOO.util.Connect.asyncRequest(this.options.method, url, callback, this.options.parameters);
	},
	
	/*
	submitForm:function(form) {
		var action		= form.action;
		form.action		= this.options.url ? this.options.url : form.action;
		form.request(this.transport());
		form.action 	= action;
	},
	
	*/
	
	handleSuccess:function(transport) {
		
		var options = transport.argument.options;
		
		if(transport.responseText !== undefined){ 
			var responseId 	= options.responseId ? options.responseId : null;
  			var insertion	= options.insertion ? options.insertion : null;
  			var responder 	= new MJL_Ajax_Handler_Response(transport, options);

  			var response = responder.response();

  			if(options.onSuccess) {
  				$A(options.onSuccess).each(function(obj) {
  					eval(obj);
  				});
  			} 
  		
		}
		
		if(response) {
			$('indicator').hide();
			this.requesting = 0;
		}
		
	},
	
	handleFailure:function(transport) {
		$('indicator').hide();
		this.loading = 0;
	}

});


