
var AjaxRequest = { };


AjaxRequest = Class.create();

AjaxRequest.updateLinkOptions = $H();
AjaxRequest.updateLink = function(containerId)
{
	if($(containerId))
	{
		$(containerId).select('a.ajaxLink', '.ajaxLinks a').each(function(link)
		{
			var options;

			if(link.readAttribute('ajaxOptions'))
			{
				eval('optionsFunction = (function(){'+link.readAttribute('ajaxOptions')+'})');

				options = optionsFunction();
			}
			else
			{
				options = (function(){return {}});
			}

 			options = Object.extend({
 				link: link
 			}, options);


			link.observe('click', function()
			{			  
				new AjaxRequest(link.readAttribute('ajax'), options);
			});

			link.writeAttribute('ajax', link.readAttribute('href'));
			link.writeAttribute('href', 'javascript:void(0);');
		});
	}
}

AjaxRequest.submitForm = function(formId, url, options)
{
  
	form = $(formId);

	options = Object.extend({ }, options);

	form.select('input.boolean[type="checkbox"]', 'input.boolean[type="radio"]').each(function(checkbox)
	{
		if(!checkbox.checked)
		{
			checkbox.value = 0;
			checkbox.checked=true;
			setTimeout(function()
			{
				checkbox.checked=false;
				checkbox.value = 1;
			}, 10);
		}
	});

	var params;
	params = form.serialize(true);

	if(form.identify())
	{
		params.current_form_identifier = form.identify();
	}


	options.parameters = params;
	new AjaxRequest(form.readAttribute('action'), options);
}

AjaxRequest.currentFormUpdated;
AjaxRequest.formsUpdated = $H({});
AjaxRequest.updateForm = function(containerId)
{
	if($(containerId))
	{
		AjaxRequest.currentFormUpdated = null;

		$(containerId).select('form.ajaxForm').each(function(form)
		{
			var options;

			if(form.readAttribute('ajaxOptions'))
			{
				eval('optionsFunction = (function(){'+form.readAttribute('ajaxOptions')+'})');

				options = optionsFunction();
			}
			else
			{
				options = (function(){return {}});
			}


			options = Object.extend({
				form: form
			}, options);

			AjaxRequest.formsUpdated.set(form.identify(), false);

			form.onsubmit = function(){ return false; };

			form.observe('submit', function()
			{
				AjaxRequest.submitForm(form.identify(), form.readAttribute('action'), options);
			});

			form.getElements().each(function(formElem)
			{
				formElem.observe('change', function()
				{
					AjaxRequest.formsUpdated.set(form.identify(), true);

					AjaxRequest.currentFormUpdated = form;

					if(!$(form.identify()+'__form_updated'))
					{
						Insertion.Bottom(form, '<input type="hidden" name="_form_updated" value="1" id="'+form.identify()+'__form_updated" />');
					}
				});
			});
		});
	}
	
}

AjaxRequest.isBusy = false;
AjaxRequest.current = null;

AjaxRequest.prototype = { 
	transaction:null,
	options:null,

	initialize: function(url, options) 
	{
		this.setOptions(options);

		if(typeof(this.options.onBeforeStartTransaction)=='function')
		{
			this.options.onBeforeStartTransaction(this);
		}

		if(!AjaxRequest.isBusy && this.options.active)
		{
			AjaxRequest.isBusy = true;

			divLoading = Builder.node('div', {id:'ajax_request_loading_box', style:'position:fixed; top:0px; left:0px;  z-index:999999; background-color:#ffffff; width:100%; height:100%; opacity:0.8;'});
			spanLoading = Builder.node('span', {id:'ajax_request_loading_span', style:'position:relative; top:20px; text-align:center; padding:20px 80px; background:#ffffff url(js/ajax-loader.gif) no-repeat center center; opacity:0.8;'}, ' ');

			divLoading.appendChild(spanLoading)

			document.body.appendChild(divLoading);

			this.transaction = new Ajax.Request(url, this.options);

			AjaxRequest.current = this.transaction;
		}

		if(typeof(this.options.onAfterStartTransaction)=='function')
		{
			this.options.onAfterStartTransaction(this);
		}
  	},

	setOptions: function(options)
	{
		this.options = Object.extend({
			active: true,
			onBeforeStartTransaction: function(instance){},
			onAfterStartTransaction: function(instance){},
			onFinish: function(instance){},
			onSuccess: this.onSuccess,
			onComplete: this.onComplete
		}, options || { });
	},

	onSuccess: function(transaction)
	{
		if($('ajax_request_loading_box') && $('ajax_request_loading_box').parentNode)
		{
			$('ajax_request_loading_box').remove();
		}

		AjaxRequest.isBusy = false;


		new AjaxCallback(transaction, 'onSuccess');
	},


	onComplete: function(transaction)
	{
		if(typeof(transaction.request.options.onFinish)=='function')
		{
			transaction.request.options.onFinish(transaction);
		}

		if($('ajax_request_loading_box') && $('ajax_request_loading_box').parentNode)
		{
			$('ajax_request_loading_box').remove();
		}

		AjaxRequest.isBusy = false;
	}





}
