// JavaScript Document
function setBackgroundImage (id, imageURL) {
  if (document.layers)
    document[id].background.src = imageURL == 'none' ? null : imageURL;
  else if (document.all)
    document.all[id].style.backgroundImage = imageURL == 'none' ? 'none' : 'url(' + imageURL + ')';
  else if (document.getElementById)
    document.getElementById(id).style.backgroundImage = imageURL == 'none' ? 'none' : 'url(' + imageURL + ')';
}

function addEvent(elm, evType, fn, useCapture){
	if(elm.addEventListener){
		elm.addEventListener(evType, fn, useCapture);
		return true;
	}else if (elm.attachEvent){
		var r = elm.attachEvent('on' + evType, fn);
		return r;
	}else{
		elm['on' + evType] = fn;
	}
}

function getEvent()
{
	if(window.event) {
		return this.window.event;
	}else{
		return getEvent.caller.arguments[0];
	}
}
/*/////////////
	Set value of input fields which need labelling.
	the toLabel key relates to <key>field in the input id
	value is used as the placeholder for safari
	other browsers use a background image referenced <key>_inputbg.gif
*/////////////
var toLabel = new Object();
toLabel['q'] = 'search';
	
///functions nfor labelling

function toggleInputBG()
{
	var oEvent = getEvent();
	var oTarget = (oEvent.target || oEvent.srcElement);
	//alert(oTarget.id);
	if(oTarget.id){
		if(oEvent.type=="blur" && oTarget.value==''){
			for ( keyVar in toLabel ) {
				if( oTarget.id.indexOf(keyVar)==0){
					//found key to image
					setBackgroundImage (oTarget.name, 'images/layout/' + toLabel[keyVar] + '_inputbg.gif');
				}
			}
		
		}else if(oEvent.type=="focus"){
			//alert(oTarget.id);
			setBackgroundImage (oTarget.name, 'none');
		}
	}
}

function setInputs()
{
	// Label all the input text fields with background image
	for ( keyVar in toLabel ) {
		var tt_input = document.getElementById(keyVar);
		if(tt_input){
			addEvent(tt_input, 'focus', toggleInputBG, false);
			addEvent(tt_input, 'blur', toggleInputBG, false);
			
			// Next line special for safari < 3 
			// commented out as latest safari supports background image in input
			//tt_input.setAttribute("placeholder", toLabel[keyVar]);
			
			if(tt_input.value == '') {
				setBackgroundImage (keyVar, 'images/layout/' + toLabel[keyVar] + '_inputbg.gif');
				tt_input.blur();
			}else{
				setBackgroundImage (keyVar, 'none');
			}
			// take them out of the field for now events applied..
		}
	}
}
addEvent(window, 'load', setInputs, false);