/*
 * Input Example
 *
 * This plugin does several thing:
 *
 * 2010 Boz Boz
 * http://bozboz.co.uk/
 *
 * * .inputExample
 *
 * * Selected items have ...
 *    * the blurClass set to them initialy.
 *    * their value set to the value of the sourceAttribute attribute.
 *
 * * onFocus...
 *    * blurClass is removed, focusClass is added.
 *    * IF the value of the element is the same as its sourceAttribute value ...
 *       * the value is set to blank ("").
 *
 * * onBlur ...
 *    * IF the value of the element is the same as its sourceAttribute value or blank ("") ....
 *       * focusClass is removed, blurClass added.
 *       * the value is set to sourceAttribute value
 *
 * * .inputExample
 *
 * * Check the elements selected with validationSelector (REQUIRED!!!) 
 *    * if any are set to blurClass then cancle the submission.
 *
 * USAGE:
 * 
 *	$(document).ready(function() {
 *      // This example uses all paramaters, but they are set to the defaults.
 *		$('#enquiryForm input, #enquiryForm textarea').inputExample({
 *			sourceAttribute : "title", 
 *			focusClass : "focus",  
 *			blurClass: "blur"
 *		});
 *
 *		$('form').preventExampleSubmit({  
 *			selection : $('#enquiryForm input, #enquiryForm textarea'),
 *			blurClass: "blur",
 *			sourceAttribute : "title", 
 *			focusClass : "focus",  
 *			blurClass: "blur"
 *		});
 *	}); 
 *
 * Date: 2010-02-10 09:37:00 -0000
 * Revision: 1
 */

(function($){  

	$.fn.inputExample = function(options) {  
		
		var defaults = {  
			sourceAttribute : "title", 
			focusClass : "focus",  
			blurClass: "blur"
		};
		
		var options = $.extend(defaults, options);
				
		return this.each(function() {
	
			var obj = $(this);
			
			if(obj.val()=="" || obj.val()==obj.attr( options.sourceAttribute )){
				obj.val(obj.attr( options.sourceAttribute ));
				obj.addClass(options.blurClass);
			}else{
				obj.addClass(options.focusClass);
			}
		
			obj.blur(function()
			{
				if(obj.val()=="" || obj.val()==obj.attr( options.sourceAttribute )){
					obj.removeClass(options.focusClass);
					obj.addClass(options.blurClass);
					obj.val(obj.attr( options.sourceAttribute ));
				}
			});
			
			obj.focus(function()
			{
				obj.removeClass(options.blurClass);
				obj.addClass(options.focusClass);
				if(obj.val()=="" || obj.val()==obj.attr( options.sourceAttribute ) )
					obj.val("");
			});
		});
		
	};  
	
	// This prevents a form from submitting if it finds any thing that contains the class blurClass 
	$.fn.submitExampleControl = function(options) {  

		var defaults = {  
			allow : null,
			deny : null,
			blurClass: "blur"
		};

		var options = $.extend(defaults, options);

		//if(options.allow==null) throw "submitExample must be passed allow.";
		//if(options.deny==null) throw "submitExample must be passed deny.";

		return this.each(function() {

			var obj = $(this);

			obj.submit(function(e){
			
				//if($(options.deny).hasClass(options.blurClass))
//					return false;
					
					
				$(options.deny).each( function(i,e){
					if($(e).hasClass(options.blurClass))
						return false;
				});
					
				$(options.allow).each(function(i,e){
					if($(e).hasClass(options.blurClass))
						$(e).val("");
				})
			});
		});

	};  

})(jQuery);