

// add a classname to tell css that js is available and page is loading
var rootEl = document.getElementsByTagName("html")[0];
Element.addClassName(rootEl, "jsEnabled");

// global onload
function global_onload() {

	Element.addClassName(rootEl, "jsLoaded");

	var lessThanIE7 = oldBrowser();

// adds a class="focus" for Internet Explorer to forms which receive focus	
	addFocusClass(lessThanIE7);

// adds a class="hover" for Internet Explorer to list items
	var targetEls = '#primary-nav li';
	addLiHover(lessThanIE7,targetEls);
	
	var firstChildEls = '#primary-nav #models-li';
	addFirstChildLiHover(lessThanIE7,firstChildEls);
}

// fix background image flickering in IE
 try {
  document.execCommand("BackgroundImageCache", false, true);
} catch(err) {}

String.prototype.normalize = function(){
	return this.replace(/\s+/g, " ");
};

// ie6 browser detection
function oldBrowser() {
	var rslt = navigator.appVersion.match(/MSIE (\d+\.\d+)/, '');
	var lessThanIE7 = (rslt != null && Number(rslt[1]) >= 5.5 && Number(rslt[1]) < 7);
	return lessThanIE7;
}

// add a hover state to list items for ie6
function addLiHover(lessThanIE7,targetEls) {

	if(lessThanIE7 == true) {
		var li = $$(targetEls);
		li.each(function(node){
			Event.observe(node,'mouseover',function(){
				Element.addClassName(node, "hover");
			});
			Event.observe(node,'mouseout',function(){
				Element.removeClassName(node, "hover");
			});		
		});
	}
}

// add a individual hover state for the models nav dropdown - controls the appearance of the menu background
function addFirstChildLiHover(lessThanIE7,targetEls) {

	if(lessThanIE7 == true) {
		var li = $$(targetEls);
		li.each(function(node){
			Event.observe(node,'mouseover',function(){
				Element.addClassName(node, "first-child-hover");
			});
			Event.observe(node,'mouseout',function(){
				Element.removeClassName(node, "first-child-hover");
			});		
		});
	}
}

// adds a class="focus" for Internet Explorer to forms which receive focus	
function addFocusClass(lessThanIE7) {

	var textInputs = $$('input[type=text]','input[type=password]');
	
	// add and remove text input values onFocus
	textInputs.each(function(node){
	
		Element.addClassName(node, "text");	
	
		/* Save the current value */
		if (node.value != '') {
			
			node.defaultText = node.value;
			
			Event.observe(node,'focus',function(){		
				if(node.value == node.defaultText) {node.value = ''}
			});
			Event.observe(node,'blur',function(){
				if(node.value == '' && node.defaultText) {node.value = node.defaultText}
			});					
		}		 
	});
	
/*	if (document.all) {
		textInputs.each(function(node){
			Event.observe(node,'focus',function(){
				Element.addClassName(node, "focus");
			});
			Event.observe(node,'blur',function(){
				Element.removeClassName(node, "focus");
			});					
		});
	}*/
}

Event.observe(window, 'load', global_onload);
// clear event handlers on unload - prevents memory leaks
Event.observe(window, 'unload', Event.unloadCache, false);
