//	PLACEHOLDER PLUGIN v1.1
//
// SELF EXECUTING FUNCTION
(function($) {
	var phClass 		= "placeholder", // PLACEHOLDER
	    pluginName 		= "html5Forms",
	    fieldsToCheck	= "input[type='text'], input[type='email'], input[type='search'], input[type='tel'], input[type='url'], input[type='password'], textarea",
	    methods 		= {
		init: function(o,callback) {
			return this.each(function() {
				var $this= $(this),
				    data = $this.data(pluginName);
				if(!data) {
					var fieldTypes	= $this.find(fieldsToCheck), // <input /> and <textarea> that use "placeholder"
					    settings 	= {
						placeholderClass: phClass,
						alertText: "test this mutha"
					    };
					if(o) {
						$.extend(settings,o);
					};
					
					$.each(fieldTypes,function(i,f) { // The current form <input /> or <textarea>
						var ph		= $(f).attr("placeholder"), // Value of "placeholder" attribute
						    parent 	= $(f).parents("form:first");
						if(ph == $(f).val() || $.trim($(f).val()) == "") {
			
							// Add placeholder text into the "value" attribute and add class for styling
							$(f).val(ph).addClass(phClass);
						};
			
						// On focus, remove the styling class and clear the "value" IF it = "placeholder" text
						$(f).bind("focus",function(){
							methods.fieldFocus.call($(this),ph);
						});
			
						// On blur, add ".placeholder" class and value if the field is empty (besides spaces)
						$(f).bind("blur",function(){
							methods.fieldBlur.call($(this),ph);
						});
			
						// On submit, if "value" = "placeholder", remove that value before it submits
						$(f).bind("submit",function(){
							methods.clearValue.call($(this),ph);
						});
					});
					
					var instanceObj = {
						settings: settings
					};
					$this.data(pluginName,instanceObj);
				};
				if(typeof callback != "undefined") {
					callback();
				};
			});
		},
		fieldFocus: function(ph) {
			return this.each(function() {
				var $this = $(this);
				$this.removeClass(phClass);
				methods.clearValue.call($this,ph);
			});
		},
		fieldBlur: function(ph) {
			return this.each(function() {
				var $this = $(this);
				if($.trim($this.val()) == "") {
					$this.addClass(phClass).val(ph);
				};
			});
		},
		clearValue: function(ph) {
			return this.each(function() {
				var $this = $(this);
				if ($this.val() == ph) {
					$this.val("");
				};
			});
		}
	};
		
	// EXTEND JQUERY
	$.fn.html5Forms = function(m) {
		var fakeInput= document.createElement("input"), // Create fake <input /> to see if "placeholder" attribute is native to browser
		    isNative = !!("placeholder" in fakeInput); // Test fake <input /> for "placeholder"

		// If "placeholder" is NOT native to the browser, fake it with "value"				
		if(!isNative) {
			// IF THE METHOD PASSED EXISTS...
			if(methods[m]) {
				
				// RETURN THE METHOD AND ANY ATTACHED ARGUMENTS
				return methods[m].apply(this,Array.prototype.slice.call(arguments,1));
			
			// IF NOTHING IS PASSED OR NO METHOD IS PASSED BUT AN OBJECT IS PASSED...
			} else if (!m || typeof m == "object") {
				
				// RUN THE init METHOD BY DEFAULT AND PASS ANY ATTACHED ARGUMENTS
				return methods.init.apply(this,arguments);
				
			// IF WHAT IS PASSED DOESNT APPLY...
			} else {
				
				// SHOW AN ERROR
				$.error("Invalid method passed");			
			};
		};
	}
})(jQuery);

