﻿/**
* jQuery plugin to add hint-texts to input fields.
* @author: Lars Huring, (c) 2008 Vinter.
* @version: 0.3
* Usage:
* 
* $(document).ready(function() {
* 
*	$("#formId").formHints({
*		css_class: "preview-value"			// Class to add to inputs
*		elements: "input"					// Elements/class/id to look for in form
*		on_submit: function()				// Function to execute on form submit. Default only clears the inputs.
*	});
*	
* });
*
**/

jQuery.fn.formHints = function(settings) {
    settings = jQuery.extend({
        css_class: "preview-value",
        elements: "input,textarea",
        on_submit: function() {
            jQuery("." + settings.css_class).each(function(i, item) {
                item.value = "";
            });
        }
    }, settings);

    var elem = jQuery(settings.elements, this);
    

    elem.each(function(i, item) {
    
        if (item.title == "" || (item.value != "" && item.value != item.title))
            return;

        jQuery(item).attr("value", item.title);

        jQuery(item)
		    .addClass(settings.css_class)
		    .bind("click, focus", function(e) {

		        if ($(this).hasClass(settings.css_class)) {
		            $(this).removeClass(settings.css_class);
		            this.value = "";
		        }
		    });

        jQuery(item).bind("blur", function(e) {
            
            if (item.value == "") {
                jQuery(this).addClass(settings.css_class);
                item.value = item.title;
            }

        });

    });

    // We need to remove the values on submit
    // override this function using on_submit settings-object
    this.bind("submit", settings.on_submit);

}
