/*
 * 	jQuery Inner Label PLugin 1.0 - jQuery plugin
 *	written by Jamie Thompson	
 *
 *	Copyright (c) 2009 The SaleGroup 
 *	http://thesalegroup.co.uk
 *
 *	Built on jQuery
 *	http://jquery.com
 *
 *	Example Usage
 *	======================================
 *	<label for="username">Enter your username</label>
 *	<input type="text" name="username" id="username" />
 * 
 *	$('#username').innerLabel();
 *  
 */
(function($) {
		
$.fn.innerLabel = function(options){
  
	// default configuration properties
	var defaults = {			
		appliedClass: 	'jIL_applied',
		labelledClass: 	'jIL_labelled',
		focusClass: 	'jIL_focused',
		passwordClass: 	'jIL_password'
	}; 
	
	var s = $.extend(defaults, options);  
	
	switchInputType =  function($el,toType) {
		if (!$.browser.msie) {
			$el.get(0).setAttribute("type", toType); 
		}
	}
		
	return this.each(function() {  
		
		$this = $(this);
		
		if (!$this.hasClass(s.appliedClass)) {
			
			$label = $('label[for*='+$this.attr('name')+']');
			$this
				.data('label', $label.text())
				.addClass(s.labelledClass)
				.addClass(s.appliedClass)
			$label.remove(); 
			
			$this
				.val($this.data('label'))
				.addClass(s.appliedClass)
				
			.focus(function(){
				$(this).addClass(s.focusClass);
				
				if ($(this).hasClass(s.labelledClass)) {
					
					if ($(this).hasClass(s.passwordClass)) {
						switchInputType($this,'password');
					}
					$(this).val('').addClass(s.labelledClass);
				}
			})
			
			.blur(function(){
				$(this).removeClass(s.focusClass);
				if (!$(this).val()) {
					if ($(this).hasClass(s.passwordClass)) {
						switchInputType($this,'text');
					}
					$(this).val($(this).data('label')).addClass(s.labelledClass);
				} else {
					$(this).removeClass(s.labelledClass);
				}
			})
			
			.keyup(function(){
				if ($(this).val()) {
					$(this).removeClass(s.labelledClass);
				}
			});
			
			if($this.attr('type')=='password') {
				$this.addClass(s.passwordClass);
				switchInputType($this,'text');
			}
			
		}
	});
};

})(jQuery);
