// require prototype

Ajax._debugQuery = 'debug_port=10000&start_debug=1&debug_start_session=1&debug_no_cache=1234567890&debug_host=127.0.0.1&debug_session_id=1234567890&send_sess_end=1';
Ajax._asyncQuery = 'async=1';

Ajax._isJsHttpRequest = function(url) {
	return url.indexOf('JsHttpRequest=');
};

Ajax.MyRequest = function(url, options) {
	//var url += (!url.indexOf('?') ? '?' : '&') + Ajax._debugQuery;
	this.options = Object.extend({method: 'get'/*, evalScripts: true, evalJS: 'force', evalJSON: 'force'*/}, options);
	if (Ajax._isJsHttpRequest(url)) { 
		Object.extend(this.options, {
			onSuccess: function(resp) {
				(options.onSuccess || Prototype.emptyFunction)(resp.transport);
			},
			onFailure: function(resp) {
				alert('Error code: ' + resp.transport.status + '\n\n' + resp.transport.responseText);
				(options.onFailure || Prototype.emptyFunction)(resp.transport);
			}
		});
	} else {
		url += (!url.indexOf('?') ? '?' : '&') + Ajax._asyncQuery;// + Math.round(Math.random() * 10000) + 1;
	}
	
	return new Ajax.Request(url, this.options);
};

Ajax.MyUpdater = function(element, url, options) {
	this.options = Object.clone(options);
	if (Ajax._isJsHttpRequest(url)) { 
		Object.extend(this.options, {
			onSuccess: function(resp) {
				$(element).update(resp.responseJS.html);
				(options.onSuccess || Prototype.emptyFunction)(resp.transport);
			},
			onFailure: function(resp) {
				(options.onFailure || Prototype.emptyFunction)(resp.transport);
			}
		});
	}	
	return new Ajax.MyRequest(url, this.options);
};

Element.addMethods('FORM', {
	getAction: function(element) {
		var action = $(element).readAttribute('action') || '';
	    if (action.blank()) { action = window.location.href; }
	    return action;
	}	
});

Ajax.MyForm = 
{
	register: function(form, options) {
		var form = $(form);
		if (!form) { return; }
		
		form.observe('submit', function(e) {
			Event.stop(e);
			//form.onsubmit = function(e) {
			this.request(form, options);
			return false;
		}.bind(this));
	},
	
	request: function(form, options) {
		(options.onBeforeCreate || Prototype.emptyFunction)();
	    form = $(form),
	    options = Object.clone(options || {});
	
	    var params = Object.clone(options.parameters), 
	    	action = form.readAttribute('action') || '';
	    if (action.blank()) { action = window.location.href; }
	    options.parameters = form.serialize(true);
	
	    if (params) {
	      if (Object.isString(params)) { params = params.toQueryParams(); }
	      Object.extend(options.parameters, params);
	    }
	    
	    if (form.hasAttribute('method') && !options.method) {
	      options.method = form.method;
	    }
		Ajax.MyRequest(action, options);
	}/*,

	onSuccess: function(resp, options) {
		if (!r.err) {
			AjaxForm.Messages.set(r.mess, AjaxForm.Messages.MT_WARNING)
		} else {
			AjaxForm.Messages.clear();
			AjaxForm.Messages.setType(AjaxForm.Messages.MT_ERROR);
			var e = r.err.split(";");
			for (var i = 0; i < e.length; i++) {
				AjaxForm.Messages.add(e[i]);
			}
		}
	}*/
};

// -------------------------------

Ajax.Process = 
{
	S_PROCESS: "подождите пожалуйста...",
	DIV_ID: 'ajax-process',

	init: function() {
		if (!$(this.DIV_ID)) {
			document.body.insert({top: new Element('div', {id: this.DIV_ID, style: 'display:none;'})});
		}
		this.hide();
	},

	show: function() {
		if (!this.process) {
			this.process = $(this.DIV_ID);
			if (!this.process) return;
		}

		var top = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop;
		this.process.style.top = top + "px";
		this.process.innerHTML = this.S_PROCESS;
		this.process.style.display = "block";
	},

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

Ajax.Responders.register({
	onCreate: function() {
		Ajax.Process.show();
	},

	onComplete: function(resp, json) {
		if(Ajax.activeRequestCount == 0){
			Ajax.Process.hide();
		}
		var all = arguments;
		try {
			//var messages = json.responseJSON['messages']; 
			var messages = resp.transport.responseJS['messages']; 
			if (messages) {
				Mbox.fillFromArray(messages);
			}
		} catch(e) { }
	}
});
