// Global nav variables
var ohprimary = document.getElementById('ohprimary');
var liTags = document.getElementById('ohprimary').getElementsByTagName('li');
var ohsubnavs = document.getElementById('ohsubnavs').getElementsByTagName('div');

// Functions to reset, manipulate nav elements
function resetsubnavs(){
	for(x=0;x<ohsubnavs.length;x++){
		ohsubnavs[x].style.display = 'none';
	}
}
function resetlitags(){
	for(i=0;i<liTags.length;i++){
		if(liTags[i].getAttribute('temp')){
			liTags[i].className = ' ';
		}
	}
}
function resetnav(){
	var testforactive = false;
	resetsubnavs();
	for(i=0;i<liTags.length;i++){
		if(liTags[i].getAttribute('temp')){
			liTags[i].className = ' ';
		} else if(liTags[i].className=='active'){
			var thisId = liTags[i].getAttribute('id');
			var thisArray = thisId.split('-');
			var thisSubnav = 'subnav-'+thisArray[1];
			document.getElementById(thisSubnav).style.display = 'block';
			testforactive = true;
		}
	}
	if(!testforactive){
		document.getElementById('subnav-home').style.display = 'block';
	}
}

// Attach class names, attributes, behaviors
for(i=0;i<liTags.length;i++){
	liTags[i].getElementsByTagName('a')[0].onmouseover = function(){
		resetsubnavs();
		var thisId = this.parentNode.getAttribute('id');
		var thisArray = thisId.split('-');
		var thisSubnav = 'subnav-'+thisArray[1];
		document.getElementById(thisSubnav).style.display = 'block';
		resetlitags();
		if(this.parentNode.className!='active'){
			this.parentNode.className = 'active';
			this.parentNode.setAttribute('temp',true);
		}
	}
}

// Attach mouseover behaviors to surrounding elements
// (this is a cheat, but it works)
// (these are now in _function.js and run onload)
//document.getElementById('ohlogo').onmouseover = resetnav;
//document.getElementById('ohbody').onmouseover = resetnav;


// "More..." dropdown:
function appendMoreMenu(thissubnav){
	var subnavitems = thissubnav.getElementsByTagName('li');
	var moreitems = new Array();
	for(x=0;x<subnavitems.length;x++){
		if(subnavitems[x].className=='more'){
			moreitems.push(subnavitems[x]);
		}
	}
	var morediv = document.createElement('span');
	morediv.className = 'moreDropDown';
	morediv.innerHTML = '<span class="text">More&nbsp;&nbsp;<img src="http:\/\/www.daytondailynews.com\/custom\/nospider\/impl\/images\/arrow_down.gif" height="9" width="9" alt="" \/><\/span>';
	var moredropdown = document.createElement('ul');
	for(y=0;y<moreitems.length;y++){
		moredropdown.appendChild(moreitems[y]);
	}
	moredropdown.style.display = 'none';
	morediv.appendChild(moredropdown);
	morediv.onmouseover = function(){
		moredropdown.style.display = 'block';
	}
	morediv.onmouseout = function(){
		moredropdown.style.display = 'none';
	}
	if(moreitems.length > 0){
		thissubnav.appendChild(morediv);
	}
}

/* We collect subnavs into a static list
   because using the dynamic list from
   getElementsByTagName to append children
   sends JavaScript into an infinite loop
*/
var collectTemp = [];
for(i=0;i<ohsubnavs.length;i++){
	collectTemp[collectTemp.length] = ohsubnavs[i];
}
for(i=0;i<collectTemp.length;i++){
	// Run function on each subnav
	appendMoreMenu(collectTemp[i]);
}
collectTemp = null;

