(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;
		
		if(jQuery.fn.validate){
			
			// now get a handle for each form
			form = $(this).parents('form');
			
			//setup default actions for validate plugin
			$.validator.setDefaults({
				submitHandler: function(form) {
					//on submit make sure that any inputs that have the same value as their label then blank the value
					
					//pass form id to local var
					var formId = jQuery(form).attr('id');
					

					//iterate through submit elements
					$('#'+formId+' input[type=text],select,textarea,radio,checkbox').each(function(){
						
						//get current_input 
						var current_input = $(this);
						//get label for current_input 
						var current_label = $('label[for=' + current_input.attr('id') + ']');
						//check that the element is either a text or textarea
						if(current_input.attr('type') == 'text' || current_input.attr('cols')){
							//if label and input the same then blank the value
							if(current_label.text() == current_input.val()){	
								current_input.val('');
							}
						}
					});

					//submit form
					form.submit();
				}
			});
			
			if(opts.validatorHightLight){
				$.validator.setDefaults({
					highlight: function(element, errorClass) {
							
							
							$(element).animate({
								opacity: 0.9
							}, 400, function () {							
								$(element).css('color',opts.validatorhightLightOptions.highlightColor); // background for invalid data
								$(element).css('background-color',opts.validatorhightLightOptions.highlightBackgroundColor); // background for invalid data
								$(element).css('border-color',opts.validatorhightLightOptions.highlightBorderColor); // border for invalid data
							});
					}
				});
			}else{
				$.validator.setDefaults({
					highlight: function(element, errorClass) {		
							$(element).animate({
								opacity: 0.9
							}, 400, function () {							
								$(element).css('color',$(element).data('color')); // color for valid based on default css
								if(opts.bgColour != null){
									$(element).css('background-color',opts.bgColour); // background for valid data based on opts.bgColour
								}else{
									$(element).css('background-color',$(element).data('background-color')); // background for valid data based on default css								
								}
								$(element).css('border-color',$(element).data('border-color')); // border for valid data based on default css
							});
					}
				});
			}
			
			if(opts.validatorUnHightLight){
				$.validator.setDefaults({
					unhighlight: function(element, errorClass) {
						$(element).animate({
							opacity: 1.0
						}, 400, function () {							
							$(element).css('color',opts.validatorUnHightLightOptions.unHighlightColor); // color for valid data
							$(element).css('background-color',opts.validatorUnHightLightOptions.unHighlightBackgroundColor); // background for invalid data
							$(element).css('border-color',opts.validatorUnHightLightOptions.unHighlightBorderColor); // border for invalid data
						});
					}
				});
				
			}else{
			
				$.validator.setDefaults({
					unhighlight: function(element, errorClass) {
						$(element).animate({
							opacity: 1.0
						}, 400, function () {							
							$(element).css('color',$(element).data('color')); // color for valid based on default css
							if(opts.bgColour != null){
								$(element).css('background-color',opts.bgColour); // background for valid data based on opts.bgColour
							}else{
								$(element).css('background-color',$(element).data('background-color')); // background for valid data based on default css								
							}
							$(element).css('border-color',$(element).data('border-color')); // border for valid data based on default css
						});
					}
				});
			}
			
			$.validator.addMethod("notLabelText",
					
				function(value, element) {
					// compate elements value with its label 
					// excluding the error label generated by the validation script
					// usin jQuery selector filter element[attribute!=value]
					var result = ($(element).val() == $('label[for=' + $(element).attr('id') + '][generated!=true]').text()) ? false : true;
					return result;
				}, "Please enter a value.");
		}
		
		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');
			
			current_input.data('border-color', current_input.css('border-color'));
			current_input.data('color', current_input.css('color'));
			current_input.data('background-color', current_input.css('background-color'));
			
			// only run if ,div.input does not have class 'no-effect'
			if($(this).hasClass('no-effect')==false){
				
				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: 1});
							}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. Plese 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. Plese use the following XHTML markup : '+markup);
					}
				}

				if(opts.wrapType == 'inner'){
					$(this).wrapInner('<div class="text-bg"></div>');
				}else if(opts.wrapType == 'outer'){
					$(this).wrap('<div class="text-bg"></div>');
				}

			}			
		});
	
		function fnFocus(event){
			
			//get event elements
			var focused = $(event.target);
			var label = $(focused).prev('label');
			
			//set the optional bg colour
			if(opts.animate == true){
				$(focused).css('background-color', opts.bgColourFocus);
				$(focused).animate({opacity: opts.opacityFocusIn});
			}
			
			//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 == 'none'){
					jQuery.each(jQuery.browser, function(i) {
						//IE7 & less
						if($.browser.msie && parseInt($.browser.version) <= 7){
							is_IE7 = true;
					  	}
					  	else{
							is_IE7 = false;
						}
					});
					
					//only modern browsers
					if(is_IE7 == false){
						$(focused).css('background', 'inherit');
					}else{
						$(focused).css('background', 'none');
					}
					
				}else{
					$(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: opts.opacityFocusOutEmpty},{easing: opts.easingType},{duration:opts.speedIn});
				}
			}else{
				if(opts.animate == true){
					$(focused).animate({opacity: opts.opacityFocusOutFilled},{easing: opts.easingType},{duration:opts.speedIn});
				}
			}
		return false;
		}
	}
  
	$.fn.inputfocus.defaults = {
		bgColourFocus: null,
		bgColour: null,
		animate: false,
		easingType: 'swing',
		opacityFocusIn:0.8,
		opacityFocusOutEmpty:1,
		opacityFocusOutFilled:1,
		speedIn: 300,
		wrapType: 'outer',
		validatorHightLight:true,
		validatorhightLightOptions:{
			highlightColor:'#000',
			highlightBackgroundColor:'#FBE3E4',
			highlightBorderColor:'#FBC2C4'		
		},
		validatorUnHightLight:true,
		validatorUnHightLightOptions:{			
			unHighlightColor:'#000',
			unHighlightBackgroundColor:'#E6EFC2',
			unHighlightBorderColor:'#C6D880'
		}
	}
})(jQuery);
