// -*- coding: utf-8 -*-
// Copyright (C) 2009 Pythonheads, all rights reserved.

// Requires: jQuery-1.2.6

// Support library for ajax functions.
// This mainly makes html "plain" html into what we call "schrale" ajax.

var err_action_blank = 'The action attribute for the form is blank '
						+ 'and I don\'t know what url to submit to.\n'
						+ 'Please explicitly set the action value to the '
						+ 'url I need to submit to.';

function ajaxify(container) {
	alert('=== Ajaxify was removed, you should use the jquery ajaxalize extension ===');
}

function _ajaxalize(the_object) {	
	var that = this;
	
	// Handle replacing the current content after we execute a request
	this.request_callback = function(data) {		
		the_object.trigger('unload');
		the_object.html(data);
		that.attach_handlers(the_object);
		the_object.trigger('load');
	}

	// Handler for submitting forms
	this.form_submit_handler = function() {
		var data = jQuery(this).serialize();
		var method = jQuery(this).attr('method');
		var action = jQuery(this).attr('action');
		if(!action) { 
			alert(err_action_blank);		
		} else {
			if(method == 'post') {
				jQuery.post(action, data, that.request_callback);
			} else if(method == 'get') { 
				jQuery.get(action, data, that.request_callback);
			} else {
				alert('Unsupported method "' + method + '". Request aborted.');
			}
		}
		return false;
	}
	
	// Handler for clicking links
	this.link_click_handler = function() {		
		var href = jQuery(this).attr('href');
		if(href.charAt(0) != '#') {
			jQuery.get(href, null, that.request_callback);
		}
		return false;
	}

	// Attach all handlers
	this.attach_handlers = function(outer) {	
		outer.find('form').submit(this.form_submit_handler);
		outer.find('a[href][rel=inline]').click(this.link_click_handler);
	}

	this.attach_handlers(the_object)

}

// in this object this = a jQuery object matching the node
jQuery.fn.ajaxalize = function() {
	/* Ajaxalize elements in the specified container. Links with rel="inline"
	 * specified and forms will be posted (or getted) using ajax calls.
	 * 
	 * Events supported by this plugin:
	 *     load   - Called when the content of the container is (re)loaded.
	 *     unload - Called just before the content of the container 
	 *              is replaced.
	 *              
	 */ 
	// Do the initial attaching of handlers to all matched objects
	return this.each(function() { // Do for each matched object (may be one or multiple)
		new _ajaxalize(jQuery(this));
	});
}

function get_ajax_query_string() {
	// Parse a query string from the part in the url after the #
	var location = '' + window.location;
	var m = location.match(/#(.*)$/);	
	var query_string = (m) ? m[1] : m
	var data = {}
	if(query_string) {
		var tokens = query_string.split(/\&/);	
		for(var i in tokens) {		
			var v = tokens[i].split(/=/);
			data[v[0]] = v[1];
		}
	}
	return data;
}

/* DO NOT PUT OTHER JAVASCRIPT IN THIS FILE!!! */

