/**
 * FAKE LABELS PLUGIN
 * 
 * Creates fake labels that hide on blur event. 
 * Only type=text inputs are accepted, as well as textareas.
 * Uses custom attribute "placeholder" for the fake label string.
 * 
 * ****************************CHANGELOG****************************
 * 
 * 1.2 (20/05/2010) => Changed "prompt" attribute to "placeholder" attribute for html5 compatibility. Added support for password inputs. 
 * 
 * @author Jose Manuel (josemanuel@digio.es)
 * @extends jQuery
 * @version 1.2
 */


jQuery.extend({
	fake_label: function(params){
		$('input[type=text][placeholder], input[type=password][placeholder], textarea[placeholder]').fake_label(params);
	}
});

$.fn.fake_label = function(options){
	
	$.each(this, function(ip, elp){;
		var form_el = $(elp);
		var fake_label = '';
		var active_class = 'fake_label';
		
		$.each(options, function(i, el){
			if(i == 'active_class'){
				active_class = el;
			}
		});
		
		if(form_el.attr('placeholder'))
		{
			//init object
			if(form_el.val() == form_el.attr('placeholder') || form_el.val().length == 0)
			{
				//type text inputs and textareas 
				if(form_el.attr('type') == 'text' || form_el[0].tagName.toLowerCase() == 'textarea')
				{
					form_el.addClass(active_class);
					form_el.val(form_el.attr('placeholder'));
					//bind events
					form_el
						.unbind()
						.focus(function(){
							if(form_el.val() == form_el.attr('placeholder')){
								form_el.val('');
								form_el.removeClass(active_class);	
							}
						})
						.blur(function(){
							if(form_el.val().length == 0)
							{
								form_el.val(form_el.attr('placeholder'));
								form_el.addClass(active_class);
							}
						});
						
				}
				//password type inputs
				else
				{
					form_el.hide();
					new_form_el = $('<input>')
						.attr({
							type: 'text'
						}).val(form_el.attr('placeholder'))
						.insertAfter(form_el)
						.focus(function(){
							new_form_el.hide();
							form_el.show().focus();
						})
						.addClass(active_class);
					form_el
						.val('')
						.removeAttr('placeholder')
						.blur(function(){
							if(form_el.val().length == 0)
							{
								form_el.hide();
								new_form_el.show();
							}
						});
				}
			}
			
			
			//capture submit event of element parent form
			form_el.parents('form').submit(function(){
				if(form_el.val() == form_el.attr('placeholder')){
					form_el.val('');
				}
			});
				
		}
	});
}
	
$(function(){
	if($.fake_label)
	{
		$.fake_label({
			active_class: 'fake_label'
		});	
	}
});
