/** 
 * Navigation Script
 * 
 * Included from nav/display db template
 * 
 * @project mysun.co.uk
 * @version 1
 * @lastmodified 2009-22-09 01:15 PM CST
 * @lastmodifiedby bmorgan
 * @author bmorgan
 *
 **/
 
 function sunav() {
 	// Create the constructor
 }
 
 sunav.prototype = {
 	
 	// Initially selected element
 	initEl: '',
 	
 	// Currently selected menu ID
 	targID: '',
 	
 	// Menu holder
 	targDiv: '',
 	
 	// Initialize the sucker
 	init: function() {
 		this.targDiv = YAHOO.util.Dom.get('SubnavTargetDiv');
 		var subNavs = this.targDiv.getElementsByTagName("li");
 		YAHOO.util.Event.addListener(this.targDiv, "mouseover", this.delTimer, this, true);
 		var navs = YAHOO.util.Dom.getElementsByClassName("level1-li");
 		YAHOO.util.Event.addListener(navs, "mouseover", this.rollin, this, true);
 		var navCont = YAHOO.util.Dom.getElementsByClassName("NavBar-Container")[0];
 		YAHOO.util.Event.addListener(navCont, "mouseout", this.rollout, this, true);
 		var tR = YAHOO.util.Dom.getRegion(this.targDiv);
 		this.targL = tR.left;
 		this.targR = tR.right;
 		this.initEl = YAHOO.util.Dom.getElementsByClassName('NavBar-MainRow-Selected')[0];
 		if(this.initEl != '') {
 			this.targID = this.initEl.id.split("_")[2];
 			this.chgSub(this.initEl);
 		}
 	},
 	
 	// Do this when a main link is rolled over
 	rollin: function(e) {
 		this.delTimer();
 		var targ = YAHOO.util.Event.getTarget(e);
 		while(targ.tagName.toUpperCase() != "LI"){
 			targ = targ.parentNode;
 		}
 		var chkID = targ.id.split("_")[2];
 		this.targID = chkID;
 		this.chgSub(targ);
 	},
 	
 	// Write the new nav to the subnav holder thingy
 	chgSub: function(roll) {
 		var menuCont = YAHOO.util.Dom.get('hidden_subnav_' + this.targID).innerHTML;
 		this.targDiv.innerHTML = '<ul>'+menuCont+'<div class="spreader"></div></ul>';
 		var selectedMainTabs = YAHOO.util.Dom.getElementsByClassName('NavBar-MainRow-Selected');
		YAHOO.util.Dom.replaceClass(selectedMainTabs,'NavBar-MainRow-Selected','NavBar-MainRow-Unselected');
		YAHOO.util.Dom.replaceClass(roll, 'NavBar-MainRow-Unselected', 'NavBar-MainRow-Selected');
		
		// Bit to move the nav to the place it belongs
		var navUL = this.targDiv.getElementsByTagName("ul")[0];
		var ulR = YAHOO.util.Dom.getRegion(navUL);
		var ulWM = (ulR.right-ulR.left)/2;
		var rollR = YAHOO.util.Dom.getRegion(roll);
		var rollM = rollR.right-((rollR.right-rollR.left)/2);
		var ulLeft = (rollM-ulWM < this.targL)?0:rollM-ulWM-this.targL;
		if(rollM+ulWM > this.targR) {
			ulLeft = ulLeft-(rollM+ulWM-this.targR);
		}
		YAHOO.util.Dom.setStyle(navUL, "left", ulLeft+"px");
 	},
 	
 	// Revert on rollout 
 	rollout: function(e){
 		this.killMenuTimer = YAHOO.lang.later(1000,this,this.killMenu);
 	},
 	
 	// Cancel the menu killer
 	delTimer: function(){
 		if(this.killMenuTimer){
 			this.killMenuTimer.cancel();
 		}
 	},
 	
 	// Function to reset the menu to where it started
 	killMenu: function() {
 		this.targID = this.initEl.id.split("_")[2];
 		this.chgSub(this.initEl);
 	}
 }
 
YAHOO.util.Event.onDOMReady(function() {
	var sn = new sunav();
	sn.init();
});