/**
 * @TODO Should have consistent loading message - possibly an AJAX Status class for this
 */
var GigaAjax =
{
	init: function(id, url, options) {
		if(options.length < 1)
		{
			options = {'recursive': false, 'preload': false};
		}

		selector = '#'+id;
		if(!options.preload)
		{
			//console.log('loading '+url);
			$(selector).trigger('beforeAjaxLoad', [url]);
			$(selector).html('Loading...');
			$(selector).load(URL+url, false, function(){
				if(options.recursive)
				{
					GigaAjax._ajaxifyChildren(this, options);
				}
				$(this).trigger('afterAjaxLoad');
			});
		}
		else
		{
			if(options.recursive)
				GigaAjax._ajaxifyChildren($(selector), options);
		}
	},

	_ajaxifyChildren: function(parentElement, options)
	{
		//console.log('ajaixying children of: ');
		//console.log(parentElement);
		$('#'+$(parentElement).attr('id')+' a').ajaxify({
			target: '#'+$(parentElement).attr('id'),
			onSuccess: function(options, data) {
				$(parentElement).trigger("afterAjaxLoad", [data]);
				GigaAjax._ajaxifyChildren(parentElement, options);
			}
		});	

		// handle forms as well
		$('#'+$(parentElement).attr('id')+' form').each(function(){
			
			GigaAjaxForm.init($(this).attr('id'), {
				'formResponse': 'followRedirect',
				'datatype': 'text',
				'beforeSubmit': false,
				'responseArea': '#'+$(parentElement).attr('id'),
				'parentElement': $(this),
				'parentOptions': options,
				'onSuccess': function(options){
					GigaAjax._ajaxifyChildren(options.responseArea, options.parentOptions);
				}
			});
		});
			
	}
};

var GigaAjaxForm = 
{
	init: function(id, options)
	{
		//console.log('ajaxifying form'+id);
		selector = '#' + id;
		if(typeof(options) == 'undefined' || options.length < 1)
		{
			options = {
				'formResponse': 'followRedirect',
				'onSuccess': false,
				'datatype': 'text',
				'beforeSubmit':false,
				'responseArea': selector
			};
		}
		if(options.formResponse == 'inlineFlash' || options.formResponse == 'dialog')
		{
			$(selector).attr('action', $(selector).attr('action')+'?flashOnly');
		}
		
		$(selector).ajaxForm({
			dataType: options.datatype,
			beforeSubmit: function(opt,jqForm){
				//console.log(jqForm);
				this.domForm = jqForm;
				GigaAjaxForm._beforeSubmit($(selector), options);
			},
			success: function(response, statusText) {
				GigaAjaxForm._afterSubmit(this.domForm, options, response, statusText);
			}
		});		
	},
	_beforeSubmit: function(element, options)
	{
		if($.isFunction(options.beforeSubmit)==true)
		{
			options.beforeSubmit.call();
		}
		$('#'+$(element).attr('id')+' div.error-message').fadeIn().remove();
    	$('#'+$(element).attr('id')+' .submit input').attr('disabled', true);
    	$('#'+$(element).attr('id')).trigger('beforeAjaxSubmit');

    	// note: progress handling should be done a better customizable way
    	//$('#".$options['id']." .form_progress').show();		
	},
	_afterSubmit: function(element, options, response, status)
	{
		//console.log('submitted');
		$('#'+$(element).attr('id')).trigger("afterAjaxSubmit", [response, status]);
		$('#'+$(element).attr('id') + ' .submit input').attr('disabled', false);
		
		// see note aboven beforeSubmit() 
    	//$(selector + ' .form_progress').hide();
		//console.log(options);
		if(options.formResponse == 'inlineFlash')
		{
			$(options.responseArea).hide().html(response).fadeIn();
		}
		if(options.formResponse == 'dialog')
		{
			gAlert(response);
		}
		if(options.formResponse == 'followRedirect')
		{
			//console.log('filling div');
			//console.log($(options.responseArea));
			$(options.responseArea).html(response).fadeIn();
		} 
		//console.log(options.onSuccess);
		if($.isFunction(options.onSuccess)==true)
		{
			options.onSuccess.call(this,options,element); // this might not be right - hit and trial fix
		}	
	}

};
