
/***************  templates/main/libs/jquery/jquery.placeholder.js  ***************/
/*
	jQuery plugin placeholder by Jakob Givoni
	
	PURPOSE:
	- Immitates attribute placeholder as it works in Chrome, Opera etc.
	- Helps the user easily identify input fields and what to type in them.
	- Visual hint: Prefills your input or textarea field with the value of your title attribute (the tooltip becomes the initial value)
	- Removes the hint on focus
	- Reenters the hint on blur of field is left empty
	- Styling: Adds a class when the field contains the placeholder text. Use it to i.e. style the text dimmed, lightening the color
	- Hint not submitted: Removes the name attribute while the hint is displayed so that the hint won't be submitted if case of form submission
	
	USAGE:
	jQuery('input').placeholder(options); // options is an optional object
	- optional options: 
		- activeClass: the class name to add when the field is showing the hint (ie when the field is empty and not focused)

*/

;(function($){
    jQuery.fn.placeholder = function(options) {
    
    	var settings = jQuery.extend({
    		activeClass: 'placeholder-active'
    	}, options);
    	
    	this.each(function(){
    		if (!$(this).attr('placeholder') || 'placeholder' in document.createElement('input'))
    		// There is no placeholder attribute or this behavior is already natively supported (tentative)
    		{
    			return;
    		}
    		$(this).data('orig_name', jQuery(this).attr('name'));
            
    		// Copying the placeholder text to input value and remove the name attribute to avoid submitting
    		jQuery(this).val(jQuery(this).attr('placeholder')).addClass(settings.activeClass).removeAttr('name');
    		
    		// On focus, remove the placeholder text and add the name attribute
    		jQuery(this).focus(function(){
    			if (jQuery(this).val() == jQuery(this).attr('placeholder')) {
    				jQuery(this).val('').removeClass(settings.activeClass).attr('name', $(this).data('orig_name'));
    			}
    		});
    		
    		// On blur, add default text if field is empty
    		jQuery(this).blur(function(){
    			if (jQuery(this).val() == '') {
    				jQuery(this).val(jQuery(this).attr('placeholder')).addClass(settings.activeClass).removeAttr('name');
    			}
    		});
		});
		
    	return this;
    }
})(jQuery);
