//window.addEvent('domready', function() {
window.addEvent('load', function() {

	var currentPageID=document.body.id;
	var navList = $$('.topnav');
	var navLinksList = $$('.topnav').getElement('a');
	var subnavList = $$('.topnav ul a');
	//var navKnubCenter = 56; //half nav background image width
	var navKnubCenter = 80; //half nav background image width
	//var intBackgroundLeft = -70; //old
	var intBackgroundLeft = -114;
	if (currentPageID != 'home'){
		var currentMenuItem = $$('.'+currentPageID)[0];
		intBackgroundLeft = (currentMenuItem.getCoordinates('navWrapper').left - (navKnubCenter - Math.ceil(currentMenuItem.getCoordinates().width/2)) - 15);
	}
	var intBackgroundTop = 1;
	var navTimerId = new Array();
	var navResetDelayTime = 1000;
	var subOpenID = -1;
	var subOpenedID = -1;
	var knobLeftPos = 0;
	var navOffset = 15;
	
	subnavList.each(function(link, i) {
		link.addEvent('mouseover', function(e) {
		
			resetNavTimer();

		});
		
		link.addEvent('mouseout', function(e) {
			resetNavTimer();
			var temp = (function(){ resetNavPos(); }).delay(navResetDelayTime);
			navTimerId.push(temp);
		});
	});
	
	function closeSub() {
		if (navList[subOpenID].getElement('ul'))  navList[subOpenedID].getElement('ul').setStyle('top', '-2000px');
	}
	
	
	function openCloseSub() {
	
		if (subOpenedID == subOpenID) {
			
			subOpenedID = -1;
			
		} else {
			if (navList[subOpenID].getElement('ul')) navList[subOpenID].getElement('ul').setStyle('top', '145px');
			subOpenedID = subOpenID;
		}
		
		
	};
	
	function resetNavPos() {
	
		closeSub();

		menuFx.start('backgroundPosition', intBackgroundLeft+" "+intBackgroundTop);
			navLinksList.each(function(highlighted, j) {
				highlighted.setStyle('color', '#000');
			
			});
			
		if (currentPageID != 'home'){
			currentMenuItem.setStyle('color', '#fff');
		}
	};
	
	function resetNavTimer() {		
		if (navTimerId.length > 0) {
			for (i=0; i<navTimerId.length; i++) {
				$clear(navTimerId[i]);
			};
			navTimerId.empty();
		};
	}
	
	var menuFx = new Fx.Tween('nav', {duration: 'short', link: 'cancel',
	
        onComplete: function(){
        	openCloseSub();
		}
	
	});
	
	
	navLinksList.each(function(link, i) {
	
		knobLeftPos = link.getCoordinates('navWrapper').left - (navKnubCenter - Math.ceil(link.getCoordinates().width/2)) + navOffset - 1;
		
		if (navList[i].getElement('ul')) navList[i].getElement('ul').setStyle('left', knobLeftPos);
	
		link.addEvent('mouseover', function(e) {
		
			resetNavTimer();
			
			if(i != subOpenedID) {
			
				if (subOpenedID != -1) {
					closeSub();
				};

				navLinksList.each(function(highlighted, j) {
					highlighted.setStyle('color', '#000');
				
				});
				this.setStyle('color', '#fff');
				
				
				if (currentPageID != 'home'){
					currentMenuItem.setStyle('color', '#fff');
				}
				
				subOpenID = i;
				knobLeftPos = this.getCoordinates('navWrapper').left - (navKnubCenter - Math.ceil(this.getCoordinates().width/2)) - navOffset;
				menuFx.start('backgroundPosition',knobLeftPos+" "+intBackgroundTop);
			
			}
		});
		
		

		link.addEvent('mouseout', function(e) {
			resetNavTimer();
			knobLeftPos = this.getCoordinates('navWrapper').left - (navKnubCenter - Math.ceil(this.getCoordinates().width/2));
			var temp = (function(){ resetNavPos(); }).delay(navResetDelayTime);
			//menuFx.start('backgroundPosition',knobLeftPos+" "+intBackgroundTop);
			navTimerId.push(temp);
		});
				
	});
	
	
	//int
	
	//window.addEvent('load', function() {
	
		if (currentPageID != 'home'){
			intBackgroundLeft = (currentMenuItem.getCoordinates('navWrapper').left - (navKnubCenter - Math.ceil(currentMenuItem.getCoordinates().width/2)) - navOffset);
		}
		menuFx.set('backgroundPosition', intBackgroundLeft+" "+intBackgroundTop);
	//});
	
});
