////////////////////////////////////////////////////////////////////////////////
///////list generally refers to a list item and the ul below it/////////////////
///////both must be altered to open and color change the li and sublist/////////
////////////////////////////////////////////////////////////////////////////////

//before page is loaded collapse the menu
DomLoaded.load(rollup);
//addDOMLoadEvent(rollup);

//after page is loaded fix safari's rendering problems
addOnload(stupidSafariHeightFix);


//fix for safari's resizing retardation. checks the height of the mainwrap and then sets main to that height plus 1. doesn't work to set it to same height exactly.
function stupidSafariHeightFix() {
	if (/KHTML|WebKit/i.test(navigator.userAgent))
		document.getElementById('main').style.height = document.getElementById('mainwrap').offsetHeight + 1 + 'px';
}


//onload collapse menu function
function rollup() {
	var objMenu = document.getElementById('menu');

	var objList = objMenu.getElementsByTagName('ul');
	var ulLength = objList.length

	// Hide each of the nested unordered list
	for (var i=0; i<ulLength; i++)
		objList[i].className = 'hidden';
		
	//compare the id of the spans to the cookies to see which should be open
	var objSpan = objMenu.getElementsByTagName('span');
	var spanLength = objSpan.length;
	for (var i=0; i<spanLength; i++) {
		objSpan[i].id = "id"+i;  //add an id to the span
		addEvent(objSpan[i], "click", rolldown); //add click event to span
		
		if (readCookie(objSpan[i].id)) { //if the cookie exists, then open the list
			openLevelID = readCookie(objSpan[i].id);
			
			document.getElementById(openLevelID).className = 'down';
			document.getElementById(openLevelID).parentNode.getElementsByTagName('ul')[0].className = 'visible';
		}
	}
	
	//check each of the list links and see which page we're on
	if (gup('id')) {
		/*
		var queryMake = gup('make');
		var queryModel = gup('model');
		var queryView = gup('view');
		
		var linkString = "http://" + location.hostname + location.pathname + "?make=" + queryMake + "&model=" + queryModel + "&view=" + queryView;
		*/
		//this will be the actual thing we'll use instead of the the above code
		var linkString = "http://" + location.hostname + location.pathname + "?id=" + gup('id');
		
		var objLink = objMenu.getElementsByTagName('a');
		var linkLength = objLink.length;
		for (var i=0; i<linkLength; i++) {
			if (objLink[i].href==linkString)
					objLink[i].className = 'current';
		}
		//alert(gup('truck'));
	}
}


//close all the open li below the closed li and set the span tag to class up
//fired through the rolldown function onclick
function closeNestedLi(listItem) {
	var objList = listItem.parentNode.parentNode.getElementsByTagName('ul');
	var ulLength = objList.length

	// Hide each of the nested unordered list and change the span tag classes to up
	for (var i=0; i<ulLength; i++) {
		objList[i].className = 'hidden';
		spanTag = objList[i].parentNode.getElementsByTagName('span')[0];
		spanTag.className = 'up';
		eraseCookie(spanTag.id); //erase the cookies so that only the open one is set
	}
}


// menu rollout function
function rolldown() {
	if (this.parentNode.getElementsByTagName('ul')[0].className == 'hidden') {
		//if there is another list open on the current menu, close it and all the ones below on different levels
		closeNestedLi(this);

		//set the clicked list to the open cookie
		createCookie(this.id,this.id);
		
		//open the list
		this.className = 'down';
		this.parentNode.getElementsByTagName('ul')[0].className = 'visible';
	}
	else {
		//close all the open li below the closed li
		closeNestedLi(this);
		
		//close the list
		
		this.className = 'up';
		this.parentNode.getElementsByTagName('ul')[0].className = 'hidden';
	}
}


////////////////////////////////////////////////////////////////////////////////////////////
///////////////function to set the menu open level for the featured truck link
function setMenuCookies(id1, id2){
	for (var i=0; i<=25; i++) {
		if (readCookie('id'+i))
			eraseCookie('id'+i);
	}
	createCookie('id0','id0');
	createCookie('id'+id1,'id'+id1);
	createCookie('id'+id2,'id'+id2);
}


//////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////not really part of the menu, but i didn't feel like including another file

function featuredisplay(feature) {
	document.getElementById('feature').innerHTML = feature;	
}
function featurehide() {
	document.getElementById('feature').innerHTML = '';	
}