/**
	jQuery placeholder plugin.
	
	Makes browsers that does not support HTML5 placeholder attribute, preform as a modern browser.
	
	Created by:
		Lasse Soberg
		06.04.2011
		
**/
(function($) {
	$.place_holder_support = ('placeholder' in document.createElement('input'));
	$.fn.placeholder = function(command) {
		if(command) {
			switch(command) {
				case 'clear':
					this.each(function() {
						var el = $(this)
						if(el.data('isEmpty') || el.val() == el.attr('placeholder')) {
							el.val('');
						}
					});
				break;
			}
			return this;
		}
		native_support = $.place_holder_support;

		if(!native_support) {
			this.each(function() {
				if(!$(this).data('gotPlaceholder')) {
					$(this).focus(function() {
						var el = $(this);
						if(el.data('isEmpty')) {
							el.val('').removeClass('placeholder');
						}
					}).blur(function() {
						var el = $(this);
						if(el.data('isEmpty') || !el.val().length) {
							el.val(el.attr('placeholder')).addClass('placeholder');
						}
					}).keyup(function() {
						var el = $(this);
						el.data('isEmpty', (el.val().length == 0));
					}).data('gotPlaceholder', true);

					if(!$(this).val().length || $(this).val() == $(this).attr('placeholder')) {
						$(this).val($(this).attr('placeholder')).addClass('placeholder').data('isEmpty', true);
					}
				}
			});
		}
		return this;
	}

	$(function(){
		$('[placeholder]').placeholder();
	});
})(jQuery);
