
var InputDefaultText={
	init:function InputDefaultText_init(input) {
		// Call using setTimeout to let things be rendered first.
		setTimeout(function(){
			InputDefaultText.initInput(input);
		},0);
	},
	initFields:function InputDefaultText_initFields(parent,names) {
		if (typeof parent != 'object') {
			parent=document.getElementById(parent);
		}
		if (!parent) return;
		var inputs=parent.getElementsByTagName('input');
		if (!inputs || inputs.length<1) return;
		var init=[];
		var len=inputs.length;
		if (typeof names == 'string') {
			// A single name
			for(var i=0;i<len;i++) {
				if (inputs[i].name==names) {
					init.push(inputs[i]);
				}
			}
		} else {
			// An array of names
			for(var i=0;i<len;i++) {
				if (this.arrayContains(names,inputs[i].name)) {
					init.push(inputs[i]);
				}
			}
		}
		len=init.length;
		if (len>0) {
			// Call using setTimeout to let things be rendered first.
			setTimeout(function(){
				for(var i=0;i<len;i++) {
					InputDefaultText.initInput(init[i]);
				}
			},0);
		}
	},
	arrayContains:function InputDefaultText_arrayContains(arr,val) {
		if (typeof arr.indexOf != 'undefined') {
			return (arr.indexOf(val)>=0);
		}
		var len=arr.length;
		for(var i=0;i<len;i++) {
			if (arr[i]===val) return true;
		}
		return false;
	},
	initInput:function InputDefaultText_initInput(input) {
		var originalValue=input.getAttribute('value');
		if (typeof originalValue != 'string') return;
		var lbl=document.createElement('div');
		lbl.className='input-default-text';
		lbl.innerHTML=originalValue;
		lbl.style.whiteSpace='nowrap';
		lbl.style.display='inline-block';
		// IE7 doesn't support inline-block quite as easily, hence this hack.
		if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)) {
			var iever=new Number(RegExp.$1);
			if (iever<8) {
				// Set zoom to trigger hasLayout
				lbl.style.zoom='1';
				// hasLayout plus display=inline causes inline-block behaviour
				lbl.style.display='inline';
			}
		}
		this.copyStyles(lbl,input);
		lbl.style.marginRight='-'+input.offsetWidth+'px';
		lbl.style.marginBottom='-'+input.offsetHeight+'px';
		input.parentNode.insertBefore(lbl,input);
		if (input.value==originalValue || input.value=='') {
			input.style.background='transparent';
			input.value='';
			// This is to make it be blank after the user uses the back button
			input.setAttribute('value','');
		} else {
			input.style.backgroundColor=lbl.style.backgroundColor;
		}
		lbl.onclick=function(e) {
			input.focus();
		};
		input.onfocus=function(e) {
			this.style.backgroundColor=lbl.style.backgroundColor;
			this.select();
		};
		input.onblur=function(e) {
			if (this.value=='') {
				this.style.background='transparent';
			}
		};
	},
	copyStyles:function InputDefaultText_copyStyles(to,from) {
		var re=/^(background|border|padding|margin|font|text|width$)/;
		if (typeof from.currentStyle != 'undefined') {
			// IE or other browser supporting currentStyle
			for(var k in from.currentStyle) {
				if (re.test(k)) {
					to.style[k]=from.currentStyle[k];
				}
			}
			to.style.height=from.clientHeight+'px';
		} else if (typeof window.getComputedStyle != 'undefined') {
			// A browser that supports getComputedStyle
			var st=window.getComputedStyle(from,null);
			for(var i=0;i<st.length;i++) {
				var k=st.item(i);
				if (k=='height' || re.test(k)) {
					to.style.setProperty(k,st.getPropertyValue(k),'');
				}
			}
		}
	}
};

