/**
 * Client-side validation
 *
 * @copyright  BliXem Internet Services
 *
 * @package    Core
 * @subpackage Controller
 * @filesource
 */

// Make sure the JQuery library is loaded and that the required form serialize
// function is present
if (typeof jQuery == 'undefined') {
	throw('Partialviews require the JQuery JavaScript framework');
}

/**
 * This variable contains the validation ruleset
 */
var ValidationRuleset = null;

/**
 * Extend the JQuery object with validation functions
 */
jQuery.fn.extend({

	/**
	 * This function should be called to prepare for partial-views
	 */
	applyPartial: function(element)
	{
		$(element).find('a.partial, form.partial').each(function() {
			$(this).bindPartial();
		});
	},


	/**
	 * Add partial-view behaviour to an element
	 */
	bindPartial: function()
	{
		// Get the partial reference
		var partialRef = this.getPartialRef();
		if (partialRef == undefined || partialRef == '') {
			return;
		}

		// Determine if this partial is a form and depending on that value determine
		// the link to use and the event to bind. For forms we use the action as the
		// link and we bind the onsubmit event; for anchor tags we use the href
		// value as the link and we bind the onclick event
		var isForm = this.is('form');
		var partialLink = isForm ? this.attr('action') : this.attr('href');
		var bindEvent = isForm ? 'submit' : 'click';

		// Ensure that the partial link is valid
		if (partialLink != undefined && partialLink != '') {
			// Apply the partial update when the form is submitted
			this.bind(bindEvent, function() { return $(this).partial(partialRef, partialLink, isForm); });
		}
	},


	/**
	 * Get the ID of the element the partial element references
	 */
	getPartialRef: function()
	{
		var classes = this.attr('class').split(' ');
		var classesLength = classes.length;

		// Loop through all class names and check if one of those items is a
		// partial reference
		for (var i = 0; i < classesLength; ++i) {

			// If the class starts with 'pvref_', the rest of that class equals the ID
			// of partial reference
			if ($.trim(classes[i]).toLowerCase().substr(0, 6) == 'pvref_') {
		  	return classes[i].substr(6);
			}
		}

		return this.attr('id');
	},


	/**
	 * Apply partial-updating
	 */
	partial: function(partialRef, partialLink, isForm)
	{
		// Update the contents of the element with that returned by the partial
		// link, for forms we also send the form's elements as parameters which will
		// automatically result in a POST
		var partialParams = isForm ? this.serializeArray() : null;
		$('#' + partialRef).load(partialLink, partialParams, function() {
			// Re-check the loaded source for partials
			$(document).applyPartial('#' + partialRef);
		});

		// Return false to prevent the form from being submitted and the anchor
		// element's href to be loaded
		return false;
	}
});


// Initialize the validation when the document is ready loading
$(document).ready(function() {

	$(document).applyPartial('body');
});