// FieldController.class.js (Fertile Form JS Framework) || Version: 0.04 || Last Updated: 2010-09-16 11:00 || Updated by: Hidde-Finne Peters || Created: 2010-08-23 by Hidde-Finne Peters
//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------

var FieldController = Class.extend({

	init: function (field_name, form_id) {
		this.fieldName = field_name;
		this.formId = form_id;
		this.field = null,
		this.supportElement = null;
		this.conditions = Array();
		this.used = false;
		
		$('document').ready($.proxy(this.documentReady, this));
	},
	
	//	------------------------------------------------------------------------------------------------------------
	//	--- documentReady ------------------------------------------------------------------------------------------	0.00
	//	------------------------------------------------------------------------------------------------------------
	//	Called after the document.ready event was triggered (when the page has finished loading and all physical objects are accesable) or if the document has already been loaded called immediatly.

	documentReady: function () {
		
		//	field
		
		if (this.formId) {
			this.field = $('#'+ this.formId +' input[name='+ this.fieldName +']');
			if (this.field.length == 0) {
				this.field = $('#'+ this.formId +' textarea[name='+ this.fieldName +']');
			}
		} else {
			this.field = $('input[name='+ this.fieldName +']');
		}
		if (this.field.length == 0) {
			this.field = null;
			alert('Field "'+ this.fieldName +'" not found!');
		}
		
		//	supportElement
		
		if (this.formId) {
			this.supportElement = $('#'+ this.formId +' #'+ this.fieldName +'_support');
		} else {
			this.supportElement = $('#'+ this.fieldName +'_support');	
		}
		if (this.supportElement.length == 0) {
			this.supportElement = null;	
		}
		
		//
		
		if (this.field) {
			$(this.field).bind('focusout', $.proxy(this.fieldFocusOutHandler, this));
		}
		
		for (var i = 0; i < this.conditions.length; i++) {
			this.conditions[i].setField(this.field);
		}
		
		//this.updateValid();
	},
	
	//	------------------------------------------------------------------------------------------------------------
	//	------------------------------------------------------------------------------------------ documentReady ---
	//	------------------------------------------------------------------------------------------------------------
	
	//	------------------------------------------------------------------------------------------------------------
	//	--- fieldFocusOutHandler -----------------------------------------------------------------------------------	0.00
	//	------------------------------------------------------------------------------------------------------------
	
	fieldFocusOutHandler: function () {
		this.setUsed(true);
	},
	
	//	------------------------------------------------------------------------------------------------------------
	//	----------------------------------------------------------------------------------- fieldFocusOutHandler ---
	//	------------------------------------------------------------------------------------------------------------
	
	//	------------------------------------------------------------------------------------------------------------
	//	--- addCondition -------------------------------------------------------------------------------------------	0.00
	//	------------------------------------------------------------------------------------------------------------
	
	addCondition: function (condition) {
		if (condition) {
			$(condition).bind('validChange', $.proxy(this.conditionValidChangeHandler, this));
			this.conditions.push(condition);
			if (this.field) {
				condition.setField(this.field);
			}
		}
	},
	
	//	------------------------------------------------------------------------------------------------------------
	//	------------------------------------------------------------------------------------------- addCondition ---
	//	------------------------------------------------------------------------------------------------------------
	
	//	------------------------------------------------------------------------------------------------------------
	//	--- removeCondition ----------------------------------------------------------------------------------------	0.00
	//	------------------------------------------------------------------------------------------------------------
	
	removeCondition: function (condition) {
		if (condition) {
			//	Replace with .indexOf() as soon as IE7 is ambigious (does not support it...)
			var index = -1;
			for (var i = 0; i < this.conditions.length; i++) {
				if (condition === this.conditions[i]) {
					index = i;
				}
			}

			if (index > -1) {
				$(condition).unbind('validChange', $.proxy(this.conditionValidChangeHandler, this));
				this.conditions.splice(index, 1);
				condition.setValid(null);	// Ensure the valid is reset so next change (to both true and false) will trigger a validChange event.
				this.updateValid();
			}
		}
	},
	
	//	------------------------------------------------------------------------------------------------------------
	//	---------------------------------------------------------------------------------------- removeCondition ---
	//	------------------------------------------------------------------------------------------------------------
	
	//	------------------------------------------------------------------------------------------------------------
	//	--- conditionValidChangeHandler ----------------------------------------------------------------------------	0.00
	//	------------------------------------------------------------------------------------------------------------
	
	conditionValidChangeHandler: function () {
		this.updateValid();
	},
	
	//	------------------------------------------------------------------------------------------------------------
	//	---------------------------------------------------------------------------- conditionValidChangeHandler ---
	//	------------------------------------------------------------------------------------------------------------

	//	------------------------------------------------------------------------------------------------------------
	//	--- valid --------------------------------------------------------------------------------------------------	0.00
	//	------------------------------------------------------------------------------------------------------------
	
	setValid: function (value) {
		if (value != this.valid) {
			this.valid = value;			
			this.updateLayout();
			$(this).trigger('validChange');
		}
	},
	
	//	------------------------------------------------------------------------------------------------------------
	//	-------------------------------------------------------------------------------------------------- valid ---
	//	------------------------------------------------------------------------------------------------------------

	//	------------------------------------------------------------------------------------------------------------
	//	--- used ---------------------------------------------------------------------------------------------------	0.00
	//	------------------------------------------------------------------------------------------------------------
	//	True after user has "used" the field (focus out)
	
	setUsed: function (value) {
		if (value != this.used) {
			this.used = value;
			this.updateLayout();
		}
	},
	
	//	------------------------------------------------------------------------------------------------------------
	//	--------------------------------------------------------------------------------------------------- used ---
	//	------------------------------------------------------------------------------------------------------------

	//	------------------------------------------------------------------------------------------------------------
	//	--- getFeedback --------------------------------------------------------------------------------------------	0.00
	//	------------------------------------------------------------------------------------------------------------
	
	getFeedback: function () {
		if (!this.valid) {
			//	Return first found invalid condition's feedback
			for (var i = 0; i < this.conditions.length; i++) {
				if (!this.conditions[i].getValid()) {
					return this.conditions[i].getFeedback();
				}
			}
		}
		return '';
	},
	
	//	------------------------------------------------------------------------------------------------------------
	//	-------------------------------------------------------------------------------------------- getFeedback ---
	//	------------------------------------------------------------------------------------------------------------

	//	------------------------------------------------------------------------------------------------------------
	//	--- update -------------------------------------------------------------------------------------------------	0.00
	//	------------------------------------------------------------------------------------------------------------
	//	Methods called to change properties dynamically
	
	//	Reanalyze situation and set property "this.valid" accordingly
	updateValid: function () {
		for (var i = 0; i < this.conditions.length; i++) {
			if (!this.conditions[i].getValid()) {
				this.setValid(false);
				return;
			}
		}
		this.setValid(true);
	},
	
	//	------------------------------------------------------------------------------------------------------------
	//	------------------------------------------------------------------------------------------------- update ---
	//	------------------------------------------------------------------------------------------------------------
	
	//	------------------------------------------------------------------------------------------------------------
	//	--- updateLayout -------------------------------------------------------------------------------------------	0.00
	//	------------------------------------------------------------------------------------------------------------

	updateLayout: function () {
		//	field
		if (this.field) {
			var className = this.field.attr('class');
			if (this.valid || !this.used) {
				//	invalid -> valid
				if (className.substr(className.length -8, 8) == '_invalid') {
					this.field.attr('class', className.substr(0, className.length - 8) +'_valid');
				}
			} else {
				//	valid -> invalid
				if (className.substr(className.length -6, 6) == '_valid') {
					this.field.attr('class', className.substr(0, className.length - 6) +'_invalid');
				}
			}
		}
		//	supportElement
		if (this.supportElement) {
			var className = this.supportElement.attr('class');
			if (this.valid || !this.used) {
				//	invalid -> valid
				if (className.substr(className.length -8, 8) == '_invalid') {
					this.supportElement.attr('class', className.substr(0, className.length - 8) +'_valid');
				}
			} else {
				//	valid -> invalid
				if (className.substr(className.length -6, 6) == '_valid') {
					this.supportElement.attr('class', className.substr(0, className.length - 6) +'_invalid');
				}
			}
		}
	}
	
	//	------------------------------------------------------------------------------------------------------------
	//	------------------------------------------------------------------------------------------- updateLayout ---
	//	------------------------------------------------------------------------------------------------------------
	
});
