/* Description: This file has the code to construct the first and seconday level navigation. 
*/
function TopMenu(){}

TopMenu.prototype.menuArray=null;
TopMenu.prototype.enable=TMenable;
TopMenu.prototype.top=113;
if (window.location.href.indexOf("/inst") != -1){
TopMenu.prototype.width=749;
}
else{
TopMenu.prototype.width=750;
}
TopMenu.prototype.primaryMenuHeight=28;
TopMenu.prototype.primaryLeftMargin=0;
TopMenu.prototype.secondaryNavPresent=true;
TopMenu.prototype.secLeftMargin=5;
TopMenu.prototype.secRightMargin=5;
TopMenu.prototype.imageRoot='/img/common/';
TopMenu.prototype.pMStr='';
TopMenu.prototype.primaryIndexOn= -1;
TopMenu.prototype.primaryBgColor='#ffcc00';
TopMenu.prototype.secondaryBgColor='#ffff99';
TopMenu.prototype.secondaryIndexOn=-1;
TopMenu.prototype.delay=1200;
TopMenu.prototype.setMenuArray=TMsetMenuArray;
TopMenu.prototype._buildPMenu=TMbuildPMenu;
TopMenu.prototype._drawPMenu=TMdrawPMenu;
TopMenu.prototype._drawSMenus=TMdrawSMenus;
TopMenu.prototype._buildSMenu=TMbuildSMenu;
TopMenu.prototype._loadPMenuImgs=TMloadPMenuImgs;
TopMenu.prototype._calSMenuLPos=TMcalSMenuLPos;
TopMenu.prototype.build=TMbuildMenu;
TopMenu.prototype.posSMenus=TMposSMenus;
TopMenu.prototype.hilite=TMhilite;
TopMenu.prototype.OnClick=TMOnClick;


var timer=null;

function TMsetMenuArray(menu){
	this.menuArray=menu;
}
function TMenable(pIndex,sIndex){
	this.primaryIndexOn=pIndex;
	this.secondaryIndexOn=sIndex;
}

function TMloadPMenuImgs(){
	for(i=0;i<this.menuArray.length;i++)
		eval('TM_OVER_'+i+'=newImage("'+this.imageRoot+this.menuArray[i][0][1]+'");');
}
function TMbuildMenu(){
	eval("TMimgRt='"+this.imageRoot+"';");
	eval("TMdefPItem="+this.primaryIndexOn+";");
	eval("TMsecNavPrsnt="+this.secondaryNavPresent+";");
	_topMenu=this.menuArray;

	this._loadPMenuImgs();
	this._buildPMenu();
	this._drawPMenu();
	if(this.secondaryNavPresent) this._drawSMenus();
	if (!(navigator.userAgent.toLowerCase().indexOf("mac")>-1 && navigator.userAgent.toLowerCase().indexOf("msie")>-1)) {
		tm.posSMenus();
		tm.hilite();
	}
}
function TMOnClick(i){
window.status=this.menuArray[i][0][3];
TMactivate(i); 
return;
}
function TMbuildPMenu(){
	var i;
	var nCols=0;
	var s='<table width="'+this.width+'" border=0 cellpadding=0 cellspacing=0>';
	s+='<tr>';
	if(this.primaryLeftMargin>0){
		s+='<td bgcolor="'+this.primaryBgColor+'" width="'+this.primaryLeftMargin+'"><img src="/img/common/s.gif" width="'+this.primaryLeftMargin+'" height=1></td>';
		nCols=nCols+1;
	}
	var tot_pimg_w=0;
	var bNeedRSpc=false;

	for(i=0;i<this.menuArray.length;i++){
		s+='<td bgcolor="'+this.primaryBgColor+'"><a href="#"';
		s+=' onclick= "tm.OnClick('+i+'); return true;"';
		s+=' onmouseover= "window.status=\''+this.menuArray[i][0][3]+'\'; return true;"';
		s+=' onmouseout= "window.status=\' \'; return true;">';
		s+='<img name="primaryTabImg_'+i+'" src="'+this.imageRoot+this.menuArray[i][0][0]+'" width='+this.menuArray[i][0][4]+' height="'+this.primaryMenuHeight+'"';
		s+=' border=0 alt="'+this.menuArray[i][0][2]+'">';
		s+='</A></td>';
		tot_pimg_w+=this.menuArray[i][0][4];
	}
/*
	for(i=0;i<this.menuArray.length;i++){
		s+='<td><a href="'+(this.menuArray[i][0][2]==""?'#':this.menuArray[i][0][2])+'" ';
		s+=' onmouseover= "window.status=\''+this.menuArray[i][0][3]+'\'; TMactivate('+i+'); return true;"';
		s+=' onmouseout= "window.status=\' \'; TMdeactivateAndSnapBackWithDelay('+i+','+this.delay+'); return true;">';
		s+='<img name="primaryTabImg_'+i+'" src="'+this.imageRoot+this.menuArray[i][0][0]+'" width='+this.menuArray[i][0][4]+' height="'+this.primaryMenuHeight+'"';
		s+=' border=0 alt="'+this.menuArray[i][0][2]+'">';
		s+='</A></td>';
		tot_pimg_w+=this.menuArray[i][0][4];
	}
*/
	nCols+=i;
	if((tot_pimg_w+this.primaryLeftMargin)<this.width) bNeedRSpc=true;

	if(bNeedRSpc){
	s+='<td bgcolor="'+this.primaryBgColor+'" align="left" width="'+(this.width-(tot_pimg_w+this.primaryLeftMargin))+'">';//primary nav background color
	s+='<img src="/img/common/s.gif" width=1 height=1></td>';
	}
	s+='</tr></table>';
	if(this.secondaryNavPresent){
		s+='<table width="'+this.width+'" border=0 cellpadding=0 cellspacing=0>';
		s+='<tr>';
		s+='<td width="'+(tot_pimg_w+this.primaryLeftMargin)+'" height="21" colspan="'+nCols+'" align="left" bgcolor="'+this.secondaryBgColor+'">';//secondary nav background color
		s+='<img src="/img/common/s.gif" width=100 height=21></td>';
		if(bNeedRSpc){
			s+='<td bgcolor="'+this.secondaryBgColor+'" align="left">';//secondary nav background color
			s+='<img src="/img/common/s.gif" width=1 height=1></TD>';
		}
		s+='</tr>';
		s+='<tr>';
        if (window.location.href.indexOf("/inst") != -1){
		s+='<td width="'+(tot_pimg_w+this.primaryLeftMargin)+'" height="1" colspan="'+nCols+'" align="left" bgcolor="#C5C5C5">';
		s+='<img src="/img/common/s.gif" width=1 height=1></td>';
		if(bNeedRSpc){
			s+='<td bgcolor="#C5C5C5" align="left">';
			s+='<img src="/img/common/s.gif" width=1 height=1></TD>';
		    }
        }
        else{
		s+='<td width="'+(tot_pimg_w+this.primaryLeftMargin)+'" height="2" colspan="'+nCols+'" align="left" bgcolor="#666666">';
		s+='<img src="/img/common/s.gif" width=1 height=2></td>';
		if(bNeedRSpc){
			s+='<td bgcolor="#666666" align="left">';
			s+='<img src="/img/common/s.gif" width=1 height=2></TD>';
		    }
        }        
		s+='</tr>';
	}
	s+='</table>';
	this.pMStr=s;
}
function TMbuildSMenu(pIndex){
	var i;
	var s='';
	var sMItm=this.menuArray[pIndex][1];
	var sMLen=sMItm.length;
	var initWidth="";
	if(navigator.family=='ie4' && navigator.version<5)
		initWidth=" style=\"width:1px;\"";

	if(navigator.family=='nn4'){
//		s+='<layer id="secondaryMenu_'+pIndex+'" '+' onmouseover="TMactivate('+pIndex+'); return true;" ';
//		s+='onmouseout="TMdeactivateAndSnapBackWithDelay('+pIndex+','+this.delay+'); return true;">';
		s+='<layer id="secondaryMenu_'+pIndex+'">';
	}else{
//		s+='<div id="secondaryMenu_'+pIndex+'" class="secnav" nowrap'+' onmouseover="TMactivate('+pIndex+'); return true;" ';
//		s+='onmouseout="TMdeactivateAndSnapBackWithDelay('+pIndex+','+this.delay+'); return true;">';
		s+='<div id="secondaryMenu_'+pIndex+'" class="secnav" nowrap'+initWidth+'>';
	}
	s+='<table height=21 border="0" cellspacing="0" cellpadding="0">';
	s+='<tr><td valign="middle" nowrap bgcolor="'+this.secondaryBgColor+'">';//div background color
	for(i=1;i<sMLen;i++){
		var subnavClass=((i==this.secondaryIndexOn+1)&&(pIndex==this.primaryIndexOn))? "secnavhilite" : "secnav";
		s+='<a class="'+subnavClass+'" href="'+sMItm[i][1]+'" target="_top">'+sMItm[i][0]+'</a>';
		if(i<sMLen-1) s+='<span class="secnavsp">&nbsp;&nbsp;:&nbsp;&nbsp;</span>';
	}
	s+='</td></tr></table>';
	if(navigator.family=='nn4') s+='</layer>';
	else s+='</div>';

	return s;
}

function TMdrawSMenus(){
	var i,j;
	var left=0;

	for(i=0;i<this.menuArray.length;i++){
		left=0;
		document.write(this._buildSMenu(i));
		var obj=getDivObj("secondaryMenu_"+ i);
		var sObj=new xbStyle(obj);
		sObj.setLeft(left)
		sObj.setTop(this.top); 
		if(navigator.family=='nn4') sObj.setVisibility("hidden");
	}
}
function TMposSMenus(){
	if(this.secondaryNavPresent){
		var i=0;
		for(i=0;i<this.menuArray.length;i++){
			var obj = getDivObj("secondaryMenu_"+ i);
			var sObj = new xbStyle(obj);
			if(navigator.family=='ie4' && navigator.version<5)
				sObj.setLeft(this._calSMenuLPos(i, obj.offsetWidth));
			else
				sObj.setLeft(this._calSMenuLPos(i, sObj.getWidth()));
		}
	}
}
function TMhilite(){
	TMactivate(this.primaryIndexOn);
}
function TMcalSMenuLPos(pIndex, sMWidth){
	var i;
	var tbL=0;
	var tbCtr=0;
	var tbW=this.menuArray[pIndex][0][4];
	var lPos=0;
	var rPos=0;

	tbW=((tbW%2)==0)?tbW:(tbW-1);
	sMWidth=((sMWidth%2)==0)?sMWidth:(sMWidth-1);

	for(i=0;i<pIndex;i++){
	 tbL=eval(tbL+_topMenu[i][0][4]);
	}
	tbCtr=eval(tbL+(tbW/2)); 
	if(tbL<(this.width/2)){
	//Align it to left
		lPos=eval(tbCtr-(sMWidth/2));
		lPos=(lPos<this.secLeftMargin)?this.secLeftMargin:lPos;
	}else{
	//Align it to right
		rPos=eval(tbCtr+(sMWidth/2));
		rPos=(rPos>(this.width-this.secRightMargin))?(this.width-this.secRightMargin):rPos;
		lPos=eval(rPos-sMWidth);
	}
	return lPos;
}

function TMdrawPMenu(){
	document.write(this.pMStr);
}

var activate_parent_called = -1;

function TMactivate(pIndex){
	if(pIndex!=-1){
//		if(timer) window.clearTimeout(timer);
		if(activate_parent_called != pIndex) TMdeactivate(activate_parent_called);
		document["primaryTabImg_"+pIndex].src=eval("TM_OVER_"+pIndex+".src");
		//TMimgRt+_topMenu[pIndex][0][1];
		if(TMsecNavPrsnt){
			var sObj=new xbStyle(getDivObj("secondaryMenu_"+pIndex));
			sObj.setVisibility("visible");
		}
		activate_parent_called=pIndex;
	}
}
function TMdeactivate(pIndex){
	if(pIndex!=-1){
		document["primaryTabImg_"+pIndex].src=TMimgRt+_topMenu[pIndex][0][0];
		if(TMsecNavPrsnt){
			var styleObj=new xbStyle(getDivObj("secondaryMenu_"+pIndex));
			styleObj.setVisibility("hidden");
		}
	}
}

function TMdeactivateAndSnapBack(pIndex){
	if(pIndex!=TMdefPItem){
		TMdeactivate(pIndex);
		TMactivate(TMdefPItem);
	}
}
function TMdeactivateAndSnapBackWithDelay(pIndex,delay){
	timer=setTimeout("TMdeactivateAndSnapBack("+pIndex+")",delay);
}

var tm;

function alignTopMenu(){
	// to be called on onload of the body tag only for IE on Mac
	if (navigator.userAgent.toLowerCase().indexOf("mac")>-1 && navigator.userAgent.toLowerCase().indexOf("msie")>-1) {
		tm.posSMenus();
		tm.hilite();
	}
}
