var mySlider;
var menuGrow;
var state = 0;
var menuTO;
var oldOver;
var subNav;
var navOpacity;
var curPage = "";
var clickState = 0;

//The subnavigation for Energy Effeciency
var url1 = new Array('reduce-energy-use.php','financial-returns.php','energy-efficiency-energy-review.php','performance-contracting.php','getting-started.php');
var anchor1 = new Array('Reduce Energy Use','Financial Returns','Energy Review','Performance Contracting','Getting Started');

//The subnavigation for the renewable energy sectionb
var url2 = new Array('energy-options.php','evaluation-process.php','renewable-energy-partners.php','next-step.php');
var anchor2 = new Array('Energy Options','Energy Audit','Partners','Next Step');

//The subnavigation for Utility Incentives
var url3 = new Array('incentives-overview.php','demand-response-management.php','integrated-monitoring-and-control.php','get-started.php');
var anchor3 = new Array('Incentives Overview','Demand Response Management','Integrated Monitoring and Control','Get Started');

//Subnav for Monitoring
var url4 = new Array('monitoring-overview.php','performance-monitoring.php','load-management.php','ppa-billing.php','public-awareness.php');
var anchor4 = new Array('Monitoring &amp; Control','Performance Monitoring','Load Management','PPA Billing','Public Awareness');

//Subnav for LEED
var url5 = new Array('leed-overview.php','leed-certification.php','leed-audit.php');
var anchor5 = new Array('Overview','Certification','LEED Audit');


function refineLinks( section )
{
	
	var subNavString = "<ul class = 'nav topNav'>\n";
	urlArr = "url"+section;
	anchorArr = "anchor"+section;
		
	for ( i = 0 ; i < eval(urlArr).length ; i++ )
	{
		
		currentLink = "";
		
		if (curPage == eval(urlArr)[i])
		{
		  currentLink = " class = 'active' ";
		}
		
		subNavString += "<li"+currentLink+"><a href = '" + eval(urlArr)[i] + "'>" + eval(anchorArr)[i] + "</a>\n";
	}
	
	subNavString += "</ul>\n";
	return subNavString;
	
}

function init(){
	startList();
	menuGrow = new Fx.Styles('menu', {duration: 300});
	menuShrink = new Fx.Styles('menu', {duration: 400});
	$('menu').addEvent('mouseleave', menuOut);
	$('but1').addEvent('mouseenter', function(){hover('1');});
	$('but2').addEvent('mouseenter', function(){hover('2');});
	$('but3').addEvent('mouseenter', function(){hover('3');});
	$('but4').addEvent('mouseenter', function(){hover('4');});	
	$('but5').addEvent('mouseenter', function(){hover('5');});	
	$('menu').addEvent('mouseenter', function(){clearTimeout(menuTO)});	
	subNav = document.getElementById('subNav');
	
	var myHREF = location.href;
	myHREF = myHREF.split('.com/')
	curPage = myHREF[1];	
	//checkStatus();
}

function checkStatus()
{
	
	for ( i = 1; i <= 5 ; i++ )
	{
	
		console.log(i);
		curArray = eval( "url"+ i );
		console.log("Array at position " + i + " is " + curArray);
		
		
		
		for ( j = 0 ; j < ( curArray.length ) ; j++ )
		{
			if ( curArray[j] == curPage )
			{
				//console.log(i);
				hover(i);
			}	
		}
		
		
	}
	
}

function initActions()
{
	$('action_01').addEvents({
		'mouseenter': function(){ actions('on','01') },
		'mouseleave': function(){ actions('','01') }
	});
	$('action_03').addEvents({
		'mouseenter': function(){ actions('on','03') },
		'mouseleave': function(){ actions('','03') }
	});
	$('action_04').addEvents({
		'mouseenter': function(){ actions('on','04') },
		'mouseleave': function(){ actions('','04') }
	});
}

/*
nextAnchor()

This function checks to see what the current page URL is, then checks that 
URL against the strings of URLs for the primary navigation, and if there is 
a page following it in it's section it links to it.

*/

function nextAnchor(){

	var myHREF = location.href;
	myHREF = myHREF.split('.com/')
	curPage = myHREF[1];	

	for ( i = 1; i <= 5 ; i++ )
	{
		curArray = eval( "url"+ i );
		
		for ( j = 0 ; j < ( curArray.length-1 ) ; j++ )
		{
			myCurUrl = curArray[j];
		  	
			if ( curArray[j] == curPage )
			{
				anchorString = "<div style = \"float:right; margin-right: 30px; margin-bottom: 20px;\">&raquo;<a href =\""+curArray[j+1]+"\">Continue</a></div>";
			  	document.write(anchorString);	
			}	
		}
	}

	
}

function clickHold()
{
	clickState = 1;
}

function menuOn()
{
	menuShrink.stop();
	menuGrow.start({
		
		'margin-top':[35,0],
		'height':[35,70]
	});
	
	state = 1;
}

function actions( status, target )
{
  $('action_'+target).setProperty('src', 'images/action'+status+'_'+target+'.jpg');
}


function menuOut() {
	if (clickState == 0)
	{
		menuTO = setTimeout("menuOutConfirmed()", 400);
	}
}

function menuOutConfirmed(){
	
	if (state == 1)
	{
		menuGrow.stop();
		menuShrink.start({
			'margin-top':[0,35],
			'height':[70,35]
		});
		state = 0;
	}
	
	$('but1').setProperty('src', 'images/topnav_01.jpg');
	$('but2').setProperty('src', 'images/topnav_02.jpg');
	$('but3').setProperty('src', 'images/topnav_03.jpg');
	$('but4').setProperty('src', 'images/topnav_04.jpg');
	$('but5').setProperty('src', 'images/topnav_05.jpg');	
	
}

function hover( slot ){

	clickState = 0;

	//if the menu is not already open, open it
	if (state == 0)
	{
		menuOn();
	}
	
	//highlight the current mouseOver button
	$('but'+slot).setProperty('src', 'images/topnavon_0'+slot+'.jpg');
	
	//update the subnavigation
	//navOpacity.start(0,1)
	subNav.innerHTML = refineLinks(slot);
	
	//Tests to see what the last mousedOver link was, and if it is different it goes back to original state
	if (oldOver != null && oldOver != slot)
	{
		$('but'+oldOver).setProperty('src', 'images/topnav_0'+oldOver+'.jpg');
	}
		
	oldOver = slot;
}

startList = function() {
if (document.all&&document.getElementById) {
navRoot = document.getElementById("botnav");
for (i=0; i<navRoot.childNodes.length; i++) {
node = navRoot.childNodes[i];
if (node.nodeName=="LI") {
node.onmouseover=function() {
this.className+=" over";
  }
  node.onmouseout=function() {
  this.className=this.className.replace(" over", "");
   }
   }
  }
 }
}