ODIL_Initializer.add_selector_with_action(".ajax_loader", function(element) {
	new AJAXLoader().initialize_on_element(element);
});

var AJAXLoader = Class.create(ODIL_Base, {
	namespace: function($super) {
		return $super() + 'ajax_loader';
	},
	
	element: null,
	
	defaults: {
		url: null,
		duration: 0.3,
		ajax_loader_loading_indicator: 'ajax_loader_loading_indicator'
	},
	
	initialize_on_element: function(element) {
		this.element = element;
		this.load_properties(element);
		
		var that = this;
		element.observe('click', function() {
			that.activate();
		});
		
		this.element.ajax_loader = this;
	},
	
	activate: function() {
		this.load_json_from_url(this.properties.get('url'));
	},
	
	load_json_from_url: function(url) {
		var that = this;
		
		new Ajax.Request(url, {
			method: 'get',
			onCreate: function() {
				new Effect.Opacity('ajax_loader_loading_indicator', {
					from: 0,
					to: 0.95,
					duration: that.properties.get('duration'),
					beforeStart: function () {
						$('ajax_loader_loading_indicator').setOpacity(0);
						$('ajax_loader_loading_indicator').show();
					}
				});
			},
			onSuccess: function(transport) {
				var response = $H(transport.responseText.gsub("\n", "").gsub("\r", "").evalJSON());
				
				response.each(function(pair) {
					if(pair.key == 'action') {
						new PeriodicalExecuter(function(pe) {
							pe.stop();
							pair.value();
						}, that.properties.get('duration'));
					}
					else {
						$$(pair.key).each(function(match) {
							if(pair.value.animate) {
								new Effect.Opacity(match, {
									to: 0,
									duration: that.properties.get('duration'),
									afterFinish: function() {
										if(pair.value.attribute)
											match[pair.value.attribute] = pair.value.value;
											if(pair.value.attribute == 'innerHTML') {
												ODIL_Initializer.run_selectors_on_element(match);
											}
											
										if(pair.value.attributes) {
											pair.value.attribute_pairs.each(function (attribute_pair) {
												alert(attribute_pair.key);
											})
										}
										
										new Effect.Opacity(match, {
											to: 1,
											duration: that.properties.get('duration')
										});
									}
								});
							}
							else {
								if(pair.value.attribute) {
									new PeriodicalExecuter(function(pe) {
										pe.stop();
										match[pair.value.attribute] = pair.value.value;
									}, that.properties.get('duration'));
								}
								
								if(pair.value.attributes) {
									$H(pair.value.attributes).each(function (attribute) {
										match[attribute.key] = attribute.value;
										if(attribute.key == 'innerHTML') {
											ODIL_Initializer.run_selectors_on_element(match);
										}
									})
								}
							}
						});
					}
				});
			},
			onComplete: function(transport) {
				new Effect.Opacity('ajax_loader_loading_indicator', {
					to: 0,
					duration: that.properties.get('duration'),
					afterFinish: function() {
						$('ajax_loader_loading_indicator').hide();
					}
				});
			}
		});
	}
});
