/************************************************************************************** 
*	HOW TO USE kwHint
*	Programmed by: Chris London
*	
*	KWHINT allows for you to have a hint in input fields.  
*	Simply add an attribute hint="________________"
*	and fill in the quotes with what you want the hint to be.  
*	It uses the class kwHinted and kwUnhinted for the styling of the elements
***************************************************************************************/

$(document).ready(function() {
	$('input[hint]').each(kwHint);
	$('input[hint]').focus(kwUnhint);
	$('input[hint]').blur(kwHint);
	$('form').submit(kwSubmit);
});


// Clears all input values that has a value the same as it's hint
function kwSubmit(e) {
	$(this).children().each(function() {
		if ($(this).attr('hint') == $(this).val()) {
			$(this).val('');
		}
    });
}

function kwHint(e) {
	if ($(this).val() == "" || $(this).val() == $(this).attr('hint')) {
		if ($(this).attr('type') == 'password') {
			$clone = $(this).clone();
			$clone.attr('type', 'text');
			$clone.attr('password', 'true');
			$clone.val($(this).attr('hint'));
			$clone.removeClass("kwUnhinted");		
			$clone.addClass("kwHinted");
			$clone.focus(kwUnhint);
			$clone.insertAfter($(this));
			$(this).remove();			
		} else {
			$(this).val($(this).attr('hint'));
			$(this).removeClass("kwUnhinted");		
			$(this).addClass("kwHinted");
		}
	}
}

function kwUnhint(e) {
	if ($(this).val() == $(this).attr('hint')) {
		if ($(this).attr('password') == 'true') {
			$clone = $(this).clone();
			$clone.attr('type', 'password');
			$clone.val("");
			$clone.removeClass("hintHinted");		
			$clone.addClass("hintUnhinted");
			$clone.blur(kwHint);
			$clone.insertAfter($(this));
			$clone.focus();
			$(this).remove();			
		} else {
			$(this).val("");
			$(this).removeClass("hintHinted");		
			$(this).addClass("hintUnhinted");
		}		
	}
}
