/*!
 * HTML5 Placeholder jQuery Plugin v1.5
 * @link http://github.com/mathiasbynens/Placeholder-jQuery-Plugin
 * @author Mathias Bynens <http://mathiasbynens.be/>
 * Almost entirely rewritten by Dan Blaisdell
 */
;
(function($) {

	// if browser supports placeholder, return
	if ('placeholder' in document.createElement('input')) {
		$.fn.placeholder = function() {
			return this;
		};
		return;
	}

	function onFocus() {
		$(this).hide().next().eq(0).show().focus();
	}

	function setPlaceholder(elem) {
		var $replacement,
		$input = $(this);
		if ($input.val() === '' || $input.val() === $input.attr('placeholder')) {
			if (!$input.data('placeholder-textinput')) {
				var attributes = ['style', 'size', 'maxlength', 'accesskey', 'tabindex', 'rows', 'cols'];
				var type = $input.attr('type');
				if(type == 'password')
					type = 'text';

				$replacement = jQuery('<' + $input[0].tagName.toLowerCase() + ' type="' + type + '">');

				$(attributes).each(function(){
					var attr = this;
					try{
						var value = $input.attr(attr);
						if(typeof value != 'undefined')
							$replacement.attr(attr, value);
					} catch (e){
//						console.log("Unable to set " + this + ":\n" + e);
					}
				});

				$replacement
					.addClass($input[0].className + ' placeholder')
					.val($input.attr('placeholder'))
					.bind('focus.placeholder', onFocus);

				$input.data('placeholder-textinput', $replacement).before($replacement);
			}
			$input.hide().prev().show();
		}
	}

	$.fn.placeholder = function() {
		return this
			.filter(':input[placeholder]:not(.placeholder)')
			.filter(function(){
				return (!$(this).data('placeholder-textinput'));
			})
			.bind('blur.placeholder' , setPlaceholder)
			.trigger('blur.placeholder').end();
	}

})(jQuery);
