/***************************************************
 * fieldLabel plugin for jQuery 
 * http://www.hayesdata.com
 * 
 * Copyright 2010, Chris Hayes 
 * 
 * Licensed under the terms of: 
 * 
 * Don't sue me.
 *  
 * Use this however you'd like but I (Chris Hayes) take no responsibility for anything created with this plugin.
 *   
 * You may use this for open source and/or commercial software development.
 *  
 * Date: October 19, 2010
 **************************************************/

(function( $ ){
	$.fn.fieldLabel = function(config){
		
		//CONFIG OPTIONS FOR THE PLUGIN
		if(config){
		}else{
			config = {
				className: 'fieldLabel'					
			};		
		};
		
		//LOOP OVER EACH ITEM PASSED TO THE FUNCTION
		this.each(function(){
			var label = $(this).attr('label');
			var value = $(this).val();
			
			$(this).attr('labelClass',config.className);
			
			//ON INIT IF THE FIELD HAS NO VALUE ATTACH A LABEL
			if(value == '' || value == label){			
				var value = $(this).val();
				$(this).val(label);
				$(this).addClass($(this).attr('labelClass'));
			}else{
				var value = $(this).val();
				$(this).val(value);
				$(this).removeClass($(this).attr('labelClass'));
			};
			
			//ON SELECTION OF THE FIELD, IF IT HAS A VALUE SHOW IT'S ACTUAL VALUE INSTEAD OF THE LABEL
			$(this).focus(function(){		
				var value = $(this).val();
				if(value == label){
					$(this).val('');
					$(this).removeClass($(this).attr('labelClass'));
				};			
			});
			
			//ON LEAVING FIELD IF THE FIELD HAS NO VALUE SHOW THE LABEL
			$(this).blur(function(){	
				var value = $(this).val();
				if(value == ''){
					$(this).val(label);	
					$(this).addClass($(this).attr('labelClass'));
				};
			});
		});
				
		return null;
	};
})( jQuery );








