/**
 * KW Assurantiën
 * 
 * @file		kwa.js
 * @author		Orestis Molopodis
 * @updated		23th of June 2011
 *
 */

window.KWA = (function($) {
	
	Function.prototype.bind = function(scope) {
		var method = this;
		return function() {
			return method.apply(scope, arguments);
		};
	};
	
	var KWA = {
		
		init: function() {
			this.contact = new this.Form('#contact-form');
			this.columns = new this.EqualizedNodes('.col');
		}
		
	};
	
	KWA.Form = function(id) {
		this.form = $(id);
		this.init();
	};
	
	KWA.Form.prototype = {
		
		init: function() {
			this.form.submit(this.submit.bind(this));
			
			this.fields = $('input[type=text], input[type=radio], input[type=checkbox], select, textarea', this.form);
			this.fields.focus(this.onFocus.bind(this));
			
			this.form.find('.group label').click(this.onFocus.bind(this));
			
			this.replaceSubmit();
		},
		
		replaceSubmit: function() {
			var submit = $('input[type=submit]', this.form);
			
			submit.addClass('replaced');
			
			var button = $(this.defaults.btnTemplate);
			
			$('a', button).click(this.submit.bind(this));
			$('span', button).text(submit.val());
			
			button.insertAfter(submit);
		},
		
		onFocus: function(e) {
			var field = $(e.target).closest('.field');
			field.removeClass('error');
		},
		
		submit: function(e) {
			e.preventDefault();
			
			var valid = true;
			
			var i, l = this.fields.length,
				node, field;
			
			for (i = 0; i < l; i++) {
				node = this.fields[i];
				field = $(node).closest('.field');
				if (this.isRequired(node) && !this.hasValue(node)) {
					valid = false;
					field.addClass('error');
				} else {
					field.removeClass('error');
				}
			}
			
			if (valid) {
				this.form[0].submit();
			}
		},
		
		isRequired: function(node) {
			return node.hasAttribute('required');
		},

		hasValue: function(node) {
			if(/check|radio/i.test(node.type)) {
				return this.checkedOne(node);
			}

			if(/select/i.test(node.type)) {
				return this.selectedOne(node);
			}
			
			if(node.hasAttribute('min')) {
				return (node.value >= node.getAttribute('min'))? true : false;
			}
			
			if(node.hasAttribute('max')) {
				return (node.value <= node.getAttribute('max'))? true : false;
			}

			return node.value? true : false;
		},

		checkedOne: function(node) {
			var form = node.form;
			var name = node.name;
			var checked = $(form).find('input[name="' + name + '"]:checked');
			return checked.length? true : false;
		},

		selectedOne: function(node) {
			var value = node[node.selectedIndex].value;
			return (value === '-1')? false : true;
		},
		
		defaults: {
			btnTemplate: '<p class="actions"><a class="submit button" href="#" title=""><span></span></a></p>'
		}
		
	};
	
	KWA.EqualizedNodes = function(selector) {
		this.nodes = $(selector);
		this.init();
	};
	
	KWA.EqualizedNodes.prototype = {
		
		init: function() {
			this.populateHeights();
			this.setHeights();
			this.positionButtons();
		},
		
		populateHeights: function() {
			this.heights = [];
			
			var i, l = this.nodes.length,
				node;
			
			for (i = 0; i < l; i++) {
				node = $(this.nodes[i]);
				this.heights.push(node.height());
			}
		},
		
		setHeights: function() {
			var highest = this.getHighest();
			this.nodes.height(highest);
		},
		
		getHighest: function() {
			return Math.max.apply(Math, this.heights);
		},
		
		positionButtons: function() {
			$('.actions', this.nodes).addClass('positioned');
		},
		
		defaults: {
		}
		
	};
	
	$(function() {
		
        KWA.init();
		
    });
	
    return KWA;
	
}(jQuery));
