/**
 * -- jForm jQuery Form plugin --
 * This plugin is meant to automize the creation form validation, tracking,
 * and defaults.
 * 
 * @author Zachary Quintana
 */
(function() {
	//Parent Class for Forms and containing elements
	function jForm(jObj, settings) {
		this.jObj	= jObj; //jQuery object containing Form
		this.settings	= settings;
		this.formElements	= new Array();
		
		var href = document.location.href;
		href = href.split('.');
		href = href.pop();
		href = href.substring(href.indexOf('/',0), href.length);
		href = (href.length == 1) ? href + "index" : href;
		if (href.charAt(href.length - 1) == "/") href = href.substring(0,href.lastIndexOf('/'));
		this.uri = href;
		
		this.initialize();
	}
	
	jForm.prototype = {
		initialize: function() {
			var $this = this;
			
			if (this.settings.trackAsPage) this.initTracking();
			if (this.settings.validate) this.jObj.bind('submit', function() { return $this.validate(); });
			this.initDefaults();
		},
		
		initDefaults: function () {
			var $this	= this;
			
			$(":input", this.jObj).each(function() {
				var type	= $(this).attr('type').toLowerCase();
				
				if (type == 'text' || type == 'textarea') {
					$this.formElements.push(new jFormTxtInput($(this)));
				}
				
			});
		},
		
		initTracking : function() {
			var $this 	= this;
			
			$(":input", this.jObj).each(function() {
				var page	= ($this.settings.addFormId) ? 
						'/vpv' + $this.uri + '/form:' + $this.jObj.attr('id') + '/' + $(this).attr('name') : '/vpv' + $this.uri + '/' + $(this).attr('name');
				var type	= $(this).attr('type').toLowerCase();
				
				if (type == 'text' 
					|| type == 'checkbox' 
					|| type == 'textarea' 
					|| type == 'radio'
					|| type == 'select') {
					$(this).focusin(function() {
						_gaq.push([ '_trackPageview', page]);
						if ($this.settings.debug) alert(page);
					});
				}
			});
		},
		
		validate: function() {
			var valid = true;
			$('span.jform-msg').remove();
			this.resetValidation();
			
			for (var i in this.formElements) {
				if (!this.formElements[i].validate()) valid = false;
			}
			
			return valid;
		},
		
		resetValidation: function() {
			for (var i in this.formElements) {
				this.formElements[i].resetValidation();
			}
			
			return this;
		}
	}
	
	//Class for input elements
	function jFormTxtInput(jObj) {
		this.jObj	= jObj;
		this.defaultValue	= this.jObj.val(); 
		this.classes	= this.jObj.attr('class').split(' ');
		
		this.initialize();
	}
	
	jFormTxtInput.prototype = {
		initialize: function() {
			var $this = this;
			
			this.jObj.focusin(function(e) {
				if ($this.jObj.val() == $this.defaultValue) {
					$this.jObj.val('');
				}
			});
			
			this.jObj.focusout(function(e) {
				if ($this.jObj.val().length < 1) {
					$this.jObj.val(this.defaultValue);
				}
			});
		},
		
		validate: function() {
			var valid = true;
			
			if (!this.classes || this.classes.length < 1) return false;
			
			for (var i in this.classes) {
				switch (this.classes[i]) {
					case "not-empty":
						if (!this.notEmpty()) valid = false;
						break;
				}
				
				if (!valid) {
					break;
				}
			}
			
			return valid;
		},
		
		notEmpty: function() {
			if (this.jObj.val().length < 1) {
				this.outputFail('This field is required');
			}
			return (this.jObj.val().length > 0) ? true : false;
		},
		
		outputFail: function(msg) {
			this.jObj.after('<span class="jform-msg validation-failed">' + msg + '</span>');
			this.jObj.addClass('jinput-required');
		},
		
		resetValidation: function() {
			this.jObj.removeClass('jinput-required');
			return this;
		}
	}
	
	
	
	//jQuery Form function to initialize a jQ object as a jForm
	$.jForm = {
		Forms 	: new Array(),
		defaults: {
			trackAsPage : false,
			debug	: false,
			addFormId	: false
		},
		
		init	: function (jObj, params) {
			var settings = $.extend($.jForm.settings, params || {});

   			$.jForm.Forms.push(new jForm(jObj, settings));
		}
	}
	
	$.fn.jForm = function(params) {
		return this.each(function() {
			$.jForm.init($(this), params);
		});
	}
})(jQuery);
