/*
	*************************
	**** BEGIN Menu/Submenu
	*************************
*/

/*
	*************************
	**** CONFIGURABLES
	*************************
*/
var _iSubnavHangTime = 400;// ms to keep subnav open before closing unless mouse interaction keeps it open
var _iSubnavTxIn = 200;// ms to transition in open subnav
var _iSubnavInitLeft = 120;// starting left pos of submenu (px); should correspond with CSS
var _iSubnavAnimDist = 30;// pixels to move submenu during transition in
var _iFPS = 50;// frames per second to run FX


/*
	*************************
	**** no need to change anything below
	*************************
*/
var _iSubnavEndLeft = _iSubnavInitLeft + _iSubnavAnimDist;
var _elCurMenuOvr = null;
var _iTimeoutId = null;
var _fxCur = null;
var _aMenuData = [];
var elDbg;

// initialize menu items, submenu items and related data/info
function initMenu(){
//elDbg = document.getElementById("dbg");// remove for deployment
try {
	// determine subnav top positions by inspecting main menu items
  var iTop, sSubnavId, elSubnavItem, oData, oFX;
  var elParent = document.getElementById("navContent");
  var aAnchors = elParent.getElementsByTagName("a");

  for(var i=0, iLen=aAnchors.length; i<iLen; i++) {
  	if(aAnchors[i].className.indexOf("mainMenuItem") != -1) {
        sSubnavId = aAnchors[i].getAttribute("rel") || null;
		
        if(sSubnavId) {
        	elSubnavItem = document.getElementById(sSubnavId) || null;
			
			if(elSubnavItem) {
				var aImages, aImageF1, aImageF2
				//main menu current image - f1
				aImages=aAnchors[i].getElementsByTagName("img");
				aImageF1=aImages[0].src;
				
				//main menu current image - f1
				aImageF2=aImageF1.replace('-f1','-f2');
				
             	//iTop = getRealPos(aAnchors[i], "Top");
				oFX = null;

				//elSubnavItem.style.top = iTop + "px";
				oFX = new Fx.Morph(elSubnavItem, {fps:_iFPS, duration:_iSubnavTxIn});
				
				// update global data
				oData = {id:aAnchors[i].id, elA:aAnchors[i], subId:sSubnavId, elSub:elSubnavItem, top:iTop, fx:oFX, f1:aImageF1, f2:aImageF2};
				_aMenuData.push(oData);
				_aMenuData[aAnchors[i].id] = oData;// make associative as well
				_aMenuData[sSubnavId] = oData;// make associative as well
           }
        }
     }
  }
}catch(e) { alert("ERROR initializing menu (Ha ha!):\n" + e.message); }
}

function menuItemOvr(elMenuItem){
	// kill any possible timeouts
	if(_iTimeoutId) window.clearTimeout(_iTimeoutId);

	//	if the same as the curr item, do nothing;   
  if(elMenuItem == _elCurMenuOvr) return;       


  // must be a new subnav to show, hide 'em all before showing it
  resetAllSubnav();

  // update globals
  _elCurMenuOvr = elMenuItem;

  //change main menu image
	aImages=_elCurMenuOvr.getElementsByTagName("img");
	aImageID = aImages[0].id;
	//alert(document.getElementById(aImageID).src);
	document.getElementById(aImageID).src = _aMenuData[_elCurMenuOvr.id].f2;
	

  elMenuItem.className = "mainMenuItemOvr";

	// open up subnav of new menu item
  _aMenuData[_elCurMenuOvr.id].fx.start({
  					"opacity": [0, 1]
             });	
}

function menuItemOut(elMenuItem){
	//alert("elMenuItem = " + elMenuItem.id);
	_iTimeoutId = window.setTimeout("resetBTN('" + elMenuItem.id + "');killSubnav('" + _aMenuData[elMenuItem.id].subId + "');_elCurMenuOvr=null;window.clearTimeout(_iTimeoutId);", _iSubnavHangTime);
}

function resetBTN(elMenuItem){
	curItem = document.getElementById(elMenuItem);
	
	//change main menu image
	aImages=curItem.getElementsByTagName("img");
	aImageID = aImages[0].id;
	document.getElementById(aImageID).src = _aMenuData[curItem.id].f1;
}

function subnavOvr(elSub){
	// kill any possible timeouts
	if(_iTimeoutId) window.clearTimeout(_iTimeoutId);       
}

function subnavOut(elSubnav){
	_iTimeoutId = window.setTimeout("killSubnav('" + elSubnav.id + "');_elCurMenuOvr=null;window.clearTimeout(_iTimeoutId);", _iSubnavHangTime);
}

// reset all subnavs to their 'out' state
function resetAllSubnav() {
	for(var i=0, iLen=_aMenuData.length; i<iLen; i++) {
  	killSubnav(_aMenuData[i].subId);
  }
}

function killSubnav(sIdSubnav){
	parentItem = _aMenuData[sIdSubnav].elA;
	//alert(parentItem);
	
	aImages=parentItem.getElementsByTagName("img");
	aImageID = aImages[0].id;
	document.getElementById(aImageID).src = _aMenuData[parentItem.id].f1;	
	
	
	
	_aMenuData[sIdSubnav].elA.className = "mainMenuItem";
  _aMenuData[sIdSubnav].fx.cancel();
  _aMenuData[sIdSubnav].fx.set({
    			"opacity": 0
             });
}

/*
	*************************
	**** END Menu/Submenu
	*************************
*/




