/*
* jQuery emptyonclick plugin
*
* Created by Andreas Creten (andreas@madewithlove.be) on 2008-06-06.
* Modified by Andreas Creten (andreas@madewithlove.be) and Jonas Van Schoote (jonas@madewithlove.be)
* Copyright (c) 2008-2011 madewithlove. All rights reserved.
*
* Version: 1.3.3
*
* Changelog :
* Version 1.3.3 (3 Apr 2011)
*  - Fix for browsers that do not support placeholder: the defaultValue is now set if the input is empty
*
* Version 1.3.2 (29 Mar 2011)
*  - Fix for undifend options object if no options were set on init
*
* Version 1.3.1 (16 Mar 2011)
*  - Fix for undefined options.changeClass throwing a warning, now this option needs to be defined to assign a class
*
* Version 1.3 (15 Dec 2010)
*  - Now uses the HTML5 placeholder attribute to define the default content
*  - Fallback to HTML5 system if available
*  - Adds a user-defined class (if defined) when not displaying the default value and removes it again if
*  - Changed the method used to select the containing form from parent() to closest()
*
* Version 1.2 (17 Jun 2008)
*  - Empty the fields onsubmit when they are not changed
*
* Version 1.1 (11 Jun 2008)
*  - Fixed a bug when working with an empty field (no default value)
*
* Version 1.0 (06 Jun 2008):
*  - Original version
*/

jQuery.fn.emptyonclick = function(options) {
    return this.each(function(){
        $.emptyonclick(this, options);
    });
};

$.emptyonclick = function(element, options) {
	options = options || {};
	
    var defaultValue = jQuery(element).attr('placeholder');
    
	if(defaultValue != jQuery(element).val() && jQuery(element).val().length && undefined !== options.changeClass) {
		jQuery(element).addClass(options.changeClass);
	}
	
	// If the browser does not support the placeholder attribute and the val is therefore empty:
	// fill it with the default value (from the placeholder attribute)
	if(!supports_input_placeholder() && jQuery(element).val().length == 0) {
		jQuery(element).val(defaultValue);
	}

    // Bind event handlers to the element
    jQuery(element)
    // On Focus: Store the default value if it's not set, empty the field
    .bind("focus", function(e) {
		if(defaultValue == jQuery(this).val()) {
            if(!supports_input_placeholder()) {
				jQuery(this).val('');
			}
        }
		if(undefined !== options.changeClass) {
			jQuery(this).addClass(options.changeClass);
		}
    })
    // On Blur: if the field is empty, reset the default value
    .bind("blur", function(e) {
        if(!jQuery(this).val()) {
            if(!supports_input_placeholder()) {
				jQuery(this).val(defaultValue);
			}
			if(options.changeClass) {
				jQuery(this).removeClass(options.changeClass);
			}
        }
    });

    // Search for the form which has the element
    jQuery(element).closest('form')
    // If the form gets resetted, set the default value back
    .bind('reset', function(e) {
		if(!supports_input_placeholder()) {
			jQuery(element).val(defaultValue);
		}
        if(options.changeClass) {
			jQuery(this).removeClass(options.changeClass);
		}
    }) 
    // If the form gets submitted empty, remove the default values
    .bind('submit', function(e) {
        if(!supports_input_placeholder() && jQuery(element).val() == defaultValue)
            jQuery(element).val('');
    });
};

var supports_placeholder = false;
var supports_placeholder_checked = false;
function supports_input_placeholder() {
	if(!supports_placeholder_checked) {
		var i = document.createElement('input'); 
		supports_placeholder = 'placeholder' in i;
		supports_placeholder_checked = true;
	}
	return supports_placeholder;
}
