if(typeof(Prototype) == "undefined")
	throw "Validation requires Prototype to be loaded.";

/* This validation class is a javascript tunnel 
   based upon the Rock Validation class and is 
   only intended to pass through validation 
   messages created by the Rock class to the 
   user in a more controlable way with effects 
   and stuff like that, It does not validate 
   fields on its own. Required Rock Validation 
   version: v1.0 or higher */
   
var Validation = Class.create();
	Validation.prototype = {
	
	initialize : function (form, errors, settings) {
		this.form 	= form;
		this.errors = errors;
		
		this.setSettings(settings);
		
		if (this.errors)
			this.raiseErrors();
	},
	
	raiseErrors : function () {
		var keys = Array();
		for (var key in this.errors) keys.push(key);
		
		Form.getElements(this.form).each(function(formElement) {
			this.settings.callback({ element : formElement, message : this.errors[formElement.name], settings : this.settings, status : in_array(formElement.name, keys) ? true : false });
		}.bind(this));
	},
	
	setSettings : function (settings) {
		this.settings = {
			hasErrorClass 	: 'validation-failed',
			hasNoErrorClass : 'validation-passed',
			callback		: this.balloon
		}
			
		Object.extend(this.settings, settings || {});
	}
}
/* ---END--- */



/* this is a custom function! not mandatory in this validation class, but default available */
Validation.prototype.balloon = function (node) {

	if(typeof(Builder) == "undefined")
	throw "Validation requires script.aculo.us (and) Builder to be loaded.";
	
	/* remove error class and add noError class */
	node.element.removeClassName(node.settings.hasErrorClass);
	node.element.addClassName(node.settings.hasNoErrorClass);
	
	/* stop observing the element */
	node.element.stopObserving();
	
	/* if we dont have error for this element we stop here */
	if (!node.status) return;
		
	/* we do have error, so do the same as above but in reverse */
	node.element.removeClassName(node.settings.hasNoErrorClass);
	node.element.addClassName(node.settings.hasErrorClass);

	/* start observing the element for mouseover, create balloon */
	Event.observe(node.element, 'mouseover', function (e) {
		if (!$('validation_balloon'))
			var balloon = Builder.node('div', { id: 'validation_balloon' });
				balloon.setStyle({'position' : 'absolute', 'top' : e.pageY + 10 + 'px', 'left' : e.pageX + 10 + 'px'});
				balloon.update(node.message);
			
			new Insertion.After(node.element, balloon);
	});

	/* stop observing, and destroy the balloon */
	Event.observe(node.element, 'mouseout', function (e) {
		$('validation_balloon').remove()
	});
}