﻿// This is the extended version

// Author: Qasim Alyas
// Date: 13 Jan 2009
// Plugin name: Text placeholder
// Version: 0.1
// Dependencies: jQuery 1.2.6 + available from http://jquery.com


// ----------------------------------
// Instructions
// ----------------------------------


// 1. Add an attribute to the required field i.e. 
//		<input type="text" id="textfield" title="This text will show as default" />
// 2. Call the plugin like so: $('form').placeholder();
// 3. Done!


// ----------------------------------
// Note
// ----------------------------------
/*
   This plugin will only work on title attribute itself. You can't set an option to override this when you call the plugin.
   If someone wants to make a contribution to fix that then you're more than welcome to do so and I'll add it to the plugin and give
   credit where it's due.
   This is my very first plugin so i'm sure this could've been done better with less code, feel free to add comments on improvements 
   and i'll make the adjustments.
*/

// ----------------------------------
// Features to add
// ----------------------------------

// 1. Stop the results from submitting if default value is present
// 2. Clientside error handling. This one will be a while so i'll release when I'm happy with the performance.


$.fn.placeholder = function(){									// plugin setup
	
	return this.each(function(){

		var fields = $(this).find(":text, textarea")			// all the input fields
			.addClass("text");									// add default class
		
		$(fields).each(function(){ 								// cycle thorugh each field
		var formplaceHolder = $(this).attr("title");			// store the attribute value in a variable
			$(this).val(formplaceHolder)						// sets the default value to match title attribute
				.focus(function(){								// on focus
					if(this.value == formplaceHolder) {			// if the value matches attribute name
						$(this).val("");						// then remove it
					};
				})
				.blur(function(){								// on blur
					if (this.value == "") {						// if the value is empty
						$(this).val(formplaceHolder);			// then add the attrinbute name as default value
					};
				});
		});

	});
};