/*!
 * Textfield Plugin
 * Version 1.0.1
 *
 * Copyright © 2009,2010 David Young & Cape Fear Webmasters, Inc.
 * http://dcyoung.com/
 * Released under the GNU GPL Version 2 license.
 * 
 */
;if(typeof(jQuery)!=='undefined'&&window.jQuery){
	(function($){
		jQuery.fn.TextField=function(settings){
			var Config = {
				Css:{
					Wrapper:'hint-wrapper',
					Arrow:'hint-arrow',
					Text:'hint-text',
					OnFocus:'has-focus',
					OnBlur:null,
					OnKeyPress:null,
					TextChanged:'text-changed'
				},
				Fade:{
					In:true,
					Out:true,
					Delay:300
				},
				Callback:{
					OnFadeInComplete:function(){return true;},
					OnFadeOutComplete:function(){return true;}	
				},
				Autosize:true,
				UseOuterWidth:true,
				UseOuterHeight:true
			}
			var TrimGlobal=function(val){return val.replace(/\s/g,'');}
			$.extend(true,Config,settings);
			return this.each(function(){
				var $this=$(this),textchanged=false;
				var TextChanged=function($txt,$e){
					var $d=$txt.prev('div').find('span.'+Config.Css.Text).css({'position':'relative'});
					textchanged=(TrimGlobal($txt.val()).length > 0);
					if(textchanged){
						$txt.addClass(Config.Css.TextChanged)
							.addClass(Config.Css.OnFocus);
						if($e.type==='keydown'&&(typeof Config.Css.OnKeyPress==='string')){
							$txt.addClass(Config.Css.OnKeyPress);	
						}else if($e.type==='keyup'&&(typeof Config.Css.OnKeyPress==='string')){
							$txt.removeClass(Config.Css.OnKeyPress);	
						}
						Config.Fade.Out?$d.fadeOut(Config.Fade.Delay,Config.Callback.OnFadeOutComplete):$d.hide();
					}else{
						$txt.removeClass(Config.Css.TextChanged)
							.removeClass(Config.Css.OnFocus)
							.addClass(function(){return typeof Config.Css.OnBlur==='string'?Config.Css.OnBlur:''});
						Config.Fade.In?$d.fadeIn(Config.Fade.Delay,Config.Callback.OnFadeInComplete):$d.show();
					}
				}
				var $div=$('<div class="'+Config.Css.Wrapper+'"></div>');
				var $arrow=$('<span class="'+Config.Css.Arrow+'"></span>');
				var $text=$('<span class="'+Config.Css.Text+'">'+$this.attr("title")+'</span>');
				if(Config.Autosize){
					$div.css({
						'width':(Config.UseOuterWidth?$this.outerWidth():$this.width()),
						'height':(Config.UseOuterHeight?$this.outerHeight():$this.height()),
						'overflow':'hidden'
					})
				}
				$div
					.html(function(){return $arrow;})
					.append($text);
				$this
					.css({'z-index':99,'position':'relative'})
					.before($div)
					.focus(function(e){$this.addClass(Config.Css.OnFocus);TextChanged($this,e)})
					.keydown(function(e){TextChanged($this,e)})
					.keyup(function(e){TextChanged($this,e)})
					.blur(function(e){TextChanged($this,e)})
			})
		}
	})(jQuery);	
}
