/*
* miscStuff version .3.1
* Copyright 2000-2008 by Intellectual Reserve, Inc. All rights reserved.
* This notice may not be removed.
*/
/*
This file is for miscellaneous small scripts that don't warrant their own plugin file.  Additionally, to be included in this file the script should be:
- Relatively small (around 500 bytes)
- Have the potential to be used on the majority of pages
*/

$(function(){
	// inputHint version .5.1 - 10/01/08
	// script for having some instructional text INSIDE a text field until clicked on, when it will clear out to allow for text entry.  If the field ever returns to an empty state it will put the instructions back in.
	$.fn.inputHint = function(options) {
		// define the default settings, then update them with any settings the user has provided
		var settings = jQuery.extend({
			hintClass: "text-hint"
		}, options);
		
		return this.each(function(){
			if(!$(this).hasClass("text-hint-added")){ // this makes sure to not apply the code to the same fields again if called multiple times
				$(this).bind("focus.inputHint",function(){ // do this on focus of the field
					if(this.value == $(this).attr("title")){ // if the value and the title attribute are the same, then clear the value and remove the hintClass so the user can put in their own value
						$(this).val("").removeClass(settings.hintClass);
					}
				})
				.bind("blur.inputHint",function(){ // do this onblur of the field
					if(this.value == ""){ // if the value is empty, put back in the title and hintClass
						$(this).addClass(settings.hintClass).val($(this).attr("title"));
					}
				})
				.addClass("text-hint-added");
				
				if(this.value == "" || (this.value != "" && this.value == $(this).attr("title"))){ // if the value is empty, put in the title and hintClass.  This is for when the page is loaded or refreshed.
					$(this).addClass(settings.hintClass).val($(this).attr("title"));
				}
				if(!$(this).parents("form").hasClass("inputHintSubmitDone")){
					$(this).parents("form").bind("submit.inputHint",function(){
						$(this).find(".text-hint-added").each(function(){
							if(this.value == $(this).attr("title")) this.value = "";
						});
						$(this).addClass("inputHintSubmitDone");
					});
				}
			}
		});
	};
	
	addInputHints(); // this calls the function defined below
});
function removeInputHints(){
	$(".text-hint-added").each(function(){
		$(this).unbind(".inputHint").removeClass("text-hint-added").removeClass("text-hint");
		if(this.value == $(this).attr("title")) this.value = "";
	});
	$("form").unbind(".inputHint");
}
function addInputHints(){
	// this calls the inputHint with the most common set of form fields 
	$("input[type='text'][title],textarea[title]").inputHint();
}