// Placeholder script
(function() {
	
	// Test for native implementation
	if ('placeholder' in document.createElement('input'))
	{
		return;
	}
	
	// On DOM ready
	$(function() {
	
	    if (window.placeholder_beforeunload !== false)
	    {
	        // When people navigate away from the current page
	        $(window).bind('beforeunload', function(e) {
	        
	            // Clear the placeholder values
                $(':input.placeholder').val('');
            });
        }
        
        if (window.placeholder_beforeunload !== false)
	    {
            // On forms submitions do the same thing
            $('form').submit(function() {
                $(':input.placeholder').val('');
            });
        }

		// Loop through inputs with the placeholder attribute (valid HTML5)
		$('input[placeholder]').each(init_placeholder);	

	});
	
	// Inits placeholders
	function init_placeholder() {
		
	    // Empty value
	    if ($(this).val() == '')
		{
			if ($(this).attr('type') == 'password')
			{
				$(this).replaceWith(toTextField(this));
			}
			else
			{
				$(this)
					.addClass('placeholder')
					.val($(this).attr('placeholder'))
					.focus(onfocus)
					.blur(onblur);
			}
		}
		else
		{
		    if ($(this).attr('type') == 'password')
			{
		        $(this)
		            .addClass('is_password')
		            .focus(onfocus)
		            .blur(onblur);
		    }
		    else
		    {
		        $(this)
					.focus(onfocus)
					.blur(onblur);
		    }
		}

	}
	
	// Changes a password field to a text field
	function toTextField(input) {
	
		return $('<input type="text" />')
			.attr('placeholder', $(input).attr('placeholder'))
			.attr('style', $(input).attr('style') || '')
			.attr('class', $(input).attr('class') || '')
			.attr('name', $(input).attr('name') || '')
			.attr('id', $(input).attr('id') || '')
			.addClass('placeholder')
			.addClass('is_password')
			.val($(input).attr('placeholder'))
			.focus(onfocus)
			.blur(onblur)
			.get(0);
	}
	
	// Changes a text field to a password field
	function toPassField(input) {
	
		return $('<input type="password" />')
			.attr('placeholder', $(input).attr('placeholder'))
			.attr('style', $(input).attr('style') || '')
			.attr('class', $(input).attr('class') || '')
			.attr('name', $(input).attr('name') || '')
			.attr('id', $(input).attr('id') || '')
			.removeClass('placeholder')
			.removeClass('is_password')
			.focus(onfocus)
			.blur(onblur)
			.get(0);
	}

	// OnFocus callback
	function onfocus() {
		
		if ($(this).hasClass('placeholder'))
		{
			if ($(this).hasClass('is_password'))
			{
				var rand = Math.random().toString().replace('.', '');
				$(this).addClass(rand);
				$(this).replaceWith(toPassField(this));
				
				// Without setTimeout IE does not give focus
				setTimeout(function() {
					$('.' + rand)
						.focus()
						.removeClass(rand);
				}, 1);
			}
			else
			{
				$(this)
					.removeClass('placeholder')
					.val('');
			}
		}
	};
	
	// OnBlur callback
	function onblur() {
		
		if ($(this).val() == '')
		{
			if ($(this).attr('type') == 'password')
			{
				$(this).replaceWith(toTextField(this));
			}
			else
			{
				$(this)
					.addClass('placeholder')
					.val($(this).attr('placeholder'));
			}
		}
	};
	
	// jQuery fn
	jQuery.fn.placeholder = function() {
	
	    // Test for native implementation
	    if ('placeholder' in document.createElement('input'))
	    {
		    return;
	    }
	
        return this.each(init_placeholder);
    };

	
})(); // End Placeholder script

