(function($) {   	
	$.fn.inputfocus = function(options) {
		
		var opts = $.extend({}, $.fn.inputfocus.defaults, options);
		var markup = '\r\n\r\n<div class="input">\r\n\t<label for="input">label</label>\r\n\t<input name="name" type="text" id="id" />\r\n</div>';
		
		global_current_label = null;
		
		return this.each(function() {
			
			//get a handle on the elements
			var current_input = $(this).children("input[type=text],select,textarea,radio,checkbox");
			
			//get label for current_input 
			var current_label = current_input.siblings('label:first');

			if($(current_label).length && $(current_input).length){
				//get a handle on the elements
				$(current_label).css('display', 'none'); //remove the current label from view
				//cache label text
				var current_label_text = $(current_label).text();
				//populate each text field form element with the contents of the corresponding label <label>
				//check if we're using a text field or a textarea
				if($(current_input).attr('type') == 'text' || $(current_input).attr('cols')){
					//apply label text to input value
					if(!$(current_input).val() || $(current_input).val() == $(current_label).text()){
						if(opts.animate == true){
							$(current_input).val($(current_label).text()).animate({opacity: 0.8});
						}else{
							$(current_input).val($(current_label).text());
						}
					}
					$(current_input).focus(fnFocus);
					$(current_input).blur(fnBlur);
				}
			}else{
				if(!$(this).children('label').length){
					alert('oops the input "'+$(this).children('input').attr('id')+'" does now have a label. Please use the following XHTML markup : '+markup);
				}else if(!$(this).children('input').length){
					alert('oops the label "'+$(this).children('label').text()+'" does now have an input. Please use the following XHTML markup : '+markup);
				}
			}
		});
	
		function fnFocus(event){
			
			//get event elements
			var focused = $(event.target);
			var label = $(focused).prev('label');
			
			//set the optional bg colour
			if(opts.animate == true){
				if(opts.bgColourFocus != null){
					$(focused).css('background-color', opts.bgColourFocus);
				}
				$(focused).animate({opacity: 1});
			}
			
			//if the current value is the same as the label then blank
			if($(focused).val() == $(label).text()){
				$(focused).val('');
			}
		return false;
		}
		
		function fnBlur(event){			
		
			//get event elements
			var focused = $(event.target);
			var label = $(focused).prev('label');
			
			//set the optional bg colour
			if(opts.animate == true){
				if(opts.bgColour != null){
					$(focused).css('background-color', opts.bgColour);
				}
			}
			
			//if the user has not typed something apply label text to input value
			if(!$(focused).val()){
				$(focused).val($(label).text());
				if(opts.animate == true){
					$(focused).animate({opacity: 0.8});
				}
			}else{
				if(opts.animate == true){
					$(focused).animate({opacity: 1});
				}
			}
		return false;
		}
	}
	 
  
	$.fn.inputfocus.defaults = {
		bgColourFocus: '#CCFF99',
		bgColour: '#FFFFFF',		
		animate: false
	}
	
})(jQuery);
