
var lastInput = null;
function initPage() {

	if (Prototype.Browser.IE) {
		fixIE6BackgroundFlicker();
	}
	
	var textInputs = $$('input[type="text"], textarea');
	textInputs.each(function(theInput) {
		Element.addClassName(theInput, 'textInput');
		Event.observe(theInput, 'focus', inputFocused);
		Event.observe(theInput, 'blur', inputBlurred);
		if (isIE6) {
			// Needed because giving the inputs a filter means the 'onfocus' event never fires! Nice one, Microsoft!
			Event.observe(theInput, 'click', inputFocused);
			Event.observe(document, 'click', unfocusLastInput);
		}
	});
	
}

function inputFocused(eventData) {
	var theInput = Event.element(eventData);
	Element.addClassName(theInput, 'focused');
	if (isIE6) {
		if (lastInput != null && lastInput != theInput) {
			Element.removeClassName(lastInput, 'focused'); 
		}
		lastInput = theInput;
		Event.stop(eventData);	// so the document onclick doesn't fire and unfocus the input we've just focused!
	}  
}

function inputBlurred(eventData) {
	var theInput = Event.element(eventData);
	Element.removeClassName(theInput, 'focused'); 
}

function unfocusLastInput() {
	if (isIE6 && lastInput != null) {
		Element.removeClassName(lastInput, 'focused'); 
		lastInput = null;
	}
}

/*
	Fix the background-flicker-when-scrolling bug in IE6.
	Thanks to http://www.mister-pixel.com/
*/
function fixIE6BackgroundFlicker() {
	try {
		document.execCommand("BackgroundImageCache", false, true);
	} catch(err) {}
}

Event.observe(document, 'dom:loaded', initPage);
