(function() {
	// Do nothing if the browser already supports placeholders.
	if ("placeholder" in document.createElement("input")) return;
	
	// If not, check for inputs with placeholders on DOMContentLoaded. Anything added dynamically will be left out.
	var hasRun = false;
	function placeholderInit() {
		if (hasRun) return;
		hasRun = true;
		var inputs = document.getElementsByTagName("input");
		for (var i = 0; i < inputs.length; i++)
			(function(inp) {
				if (!inp.getAttribute("placeholder")) return;
				// Wrap the input with a relatively positioned inline-block.
				// Add the placeholder text as an absolutely positioned element within that inline-block,
				// with its line-height set to the height of the inline-block. This seems to get consistent
				// positioning across browsers.
				var wrapper = document.createElement("span");
				wrapper.style.display = "inline-block";
				wrapper.style.position = "relative";
				var overlay = wrapper.appendChild(document.createElement("span"));
				inp.parentNode.insertBefore(wrapper, inp);
				wrapper.appendChild(inp);
				overlay.className = "placeholder";
				overlay.style.position = "absolute";
				overlay.style.color = "#a9a9a9";
				overlay.style.paddingLeft = "3px";
				overlay.style.fontSize = inp.style.fontSize;
				overlay.style.lineHeight = wrapper.offsetHeight + "px";
				overlay.appendChild(document.createTextNode(inp.getAttribute("placeholder")));
				// A click on the overlay counts as a click on the input
				overlay.style.cursor = "text";
				overlay.onmousedown = function() { setTimeout(function() { inp.focus(); }, 0) };
				// Show/hide the placeholder text on blur/focus as needed
				inp.onfocus = function() { overlay.style.display = "none"; }
				inp.onblur = function() { if (this.value == "") overlay.style.display = ""; }
			})(inputs[i]);
	}
	if (document.addEventListener) document.addEventListener("DOMContentLoaded", placeholderInit, false);
	/*@cc_on
		document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
		var script = document.getElementById("__ie_onload");
		script.onreadystatechange = function() {
			if (this.readyState == "complete") {
				placeholderInit();
			}
		};
	@*/
	var old_onload = window.onload;
	window.onload = function() { if (old_onload) old_onload(); placeholderInit(); }
})();

