$.DF.AJAX = {
	'json': function(options) {
		options.success	= $.isFunction(options.success)?options.success:function() {};
		options.error	= $.isFunction(options.error)?options.error:function() {};
		
		$.ajax({
			type:		options.type || 'GET',
			data:		options.data,
			dataType:	'json',
			url:		options.url,
			
			complete:	options.complete || null,
			
			error:		function (XMLHttpRequest, textStatus, errorThrown) {
				options.error(textStatus);
			},
			
			success:	function(data, textStatus) {
				/*if(!data.data) {
					options.error("Response is empty");
				} else*/
				if(data.result == 'Error') {
					options.error(data.error);
				} else
				//if(data.result == 'Ok') {
					options.success(data.data);
				//}
			}
		});
	},
	
	'html': function(element, options) {
		options.success	= $.isFunction(options.success)?options.success:function() {};
		options.error	= $.isFunction(options.error)?options.error:function() {};
		
		if(element) $(element).block({ message: null });
		
		$.ajax({
			type:		options.type || 'GET',
			data:		options.data,
			dataType:	'html',
			url:		options.url,
			
			complete:	options.complete || null,
			
			error:		function (XMLHttpRequest, textStatus, errorThrown) {
				options.error(textStatus);
			},
			
			success:	function(data, textStatus) {
				if(element) {
					$(element).unblock();
					$(element).html(data);
				}
				
				options.success(data);
			}
		});
	},
	
	'submit': function(form, data, options) {
		options = options || {};
		
		var data = $.extend(data || {}, $.DF.AJAX.getFormData(form, options.onlyUpdated));
		
		$(form).block({ message: null });
		$(form).trigger('ajax.submit');
		
		$.DF.AJAX.json({
			type:		$(form).attr('method'),
			url:		$(form).attr('action'),
			data:		data,
			
			success:	function(data) {
				$(form).unblock();
				if($.isFunction(options.success)) options.success(data);
			},
			
			error: function(message) {
				$(form).unblock();
				if($.isFunction(options.error)) options.error(message);
			}
		});
	},
	
	'getGlobalScopeData': function(data) {
		return $.extend({
			'site_id':	$('select#site_id').val(),
			'lang_id':	$('input#lang_id:hidden').val()
		}, data || {});
	},
	
	'getFormData': function(form, onlyUpdated) {
		var data = {};
		var types = [
		    'input:hidden',
			'input:text:enabled',
			'input:password:enabled',
			
			'input:checkbox:checked:enabled',
			'input:radio:checked:enabled',
			
			'textarea:enabled',
			'select:enabled'
		].join(',');
		
		$(form).find(types).each(function() {
			if(	(onlyUpdated == true && !$(this).is(':hidden') && this.defaultValue != $(this).val()) || 
				(onlyUpdated == true && $(this).is(':hidden')) || 																		   
				(onlyUpdated != true)) data[this.name] = $(this).val();
		});
		
		return data;
	}
};
