(function ($)
{
	$.extend($, { placeholder: {
		browser_supported: function ()
		{
			return this._supported !== undefined ?
					this._supported :
					(this._supported = !!('placeholder' in $('<input type="text">')[0]));
		},
		shim: function (opts)
		{
			var config = {
				color: '#AAA',
				cls: 'placeholder',
				lr_padding: 4
			};
			$.extend(config, opts);
			if (!this.browser_supported()) $('input[placeholder]')._placeholder_shim(config);
		}
	}
	});

	$.extend($.fn, {
		_placeholder_shim: function (config)
		{
			function calcPositionCss(target)
			{
				var op = $(target).offsetParent().offset();
				var ot = $(target).offset();
				//console.log(op,ot);
				return {
					top: ot.top - op.top + ($(target).outerHeight() - $(target).height()) / 2 + $(target).height() * .07,
					left: ot.left - op.left + config.lr_padding,
					width: $(target).width() - config.lr_padding
				};
			}
			return this.each(function ()
			{
				if ($(this).data('placeholder'))
					return true;

				var ol = $('<label />')
					.text($(this).attr('placeholder'))
					.addClass(config.cls)
					.css({
						position: 'absolute',
						display: 'block',
						float: 'none',
						overflow: 'hidden',
						whiteSpace: 'nowrap',
						textAlign: 'left',
						color: config.color,
						cursor: 'text',
						fontSize: parseInt($(this).height() * .85)
					})
					.css(calcPositionCss(this))
					.data('target', $(this))
					.click(function ()
					{
						$(this).data('target').focus()
					})
					.insertAfter(this);
				$(this)
					.data('placeholder', ol)
					.focus(function ()
					{
						ol.hide();
					}).blur(function ()
					{
						ol[$(this).val().length ? 'hide' : 'show']();
					}).triggerHandler('blur');

					$("input").keydown(function ()
					{
						setTimeout(refresh, 100);
					});
					$("form").submit(function ()
					{
						setTimeout(refresh, 100);
					});
				$(window).resize(refresh);

				function refresh()
				{
					var $target = ol.data('target')
					ol.css(calcPositionCss($target))
				}

			});
		}
	});

})(jQuery);
