/*!
 * TextBox Default Text Plugin
 * Version 1.0.0
 * Dependencies:
 * - requires jQuery
 *
 * Copyright © 2011 David Young & Cape Fear Webmasters, Inc.
 * http://www.cfwebmasters.com/
 * http://dcyoung.com/
 * Released under the GNU GPL Version 2 license.
 * 
 *************** Usage ****************************************
 *
 * jQuery('#MyTextField').cfwiTextBoxDefault('My Default Text')
 *
 **************************************************************
 */
if(typeof(jQuery)!=='undefined'&&window.jQuery){
	(function($){
		jQuery.fn.cfwiTextBoxDefault=function(t){
			var m_default=(typeof(t)==='string'&&(t.replace(/\s/g,'').length > 0 && t!=='undefined'))?t:null;
			var m_checkval=function(v){
				return (v.toLowerCase() !== m_default.toLowerCase() && v.replace(/\s/g,'').length > 0);
			}
			return this.each(function(){
				if(this.type==='text'){
					var $e=jQuery(this),$p=$e.parent();
					var m_prevent=function(e){
						ri=true;
						if(!m_checkval($e.val())){
							if(e.preventDefault){e.preventDefault();}
							if(e.returnValue){e.returnValue=false;}
							ri=false;
						}
						return ri;
					};
					$e
						.val(function(index,value){ return (m_checkval(value))?value:m_default; })
						.focus(function(){ var $this=$(this); $this.addClass('focus'); (!m_checkval($this.val()))?$this.val(''):$this.val(); })
						.blur(function(){ var $this=$(this); (m_checkval($this.val()))?$this.val():$this.val(m_default).removeClass('focus'); });
					if(m_checkval($e.val())){$e.addClass('focus');}	
					if($p.siblings('.SearchButton')){
						$p.siblings('.SearchButton')
							.click(function(e){
								return m_prevent(e);
							});
					}
				}
			});
		}
	})(jQuery);
}
