var blnOpen = false;
var strOpenMenu;
var intTopNavTabNum;

function findPos(obj)
{
	var curleft = curtop = 0;
	if (obj.offsetParent)
	{
		do
		{
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		}
		while (obj = obj.offsetParent);
		return [curleft,curtop];
	}
}

function menuOpen(objTab)
{
	closeAll(objTab);
	var arrPos = findPos(objTab);
	var intCellNum = objTab.className;
	var objDD = document.getElementById('navMenu'+intCellNum);
	objDD.style.height = "";
	objDD.style.top = '134px';
	objDD.style.display='block';
	objDD.style.left = arrPos[0];
	objDD.style.top = arrPos[1] + objTab.offsetHeight;
	blnOpen = true;
	strOpenMenu = 'navMenu'+intCellNum;
}

function startMenuClose(objTab)
{
	blnOpen = false;
	var intCellNum = objTab.className;
	var objDD = document.getElementById('navMenu'+intCellNum);

	if(!blnOpen)
	{
		setTimeout("menuClose(document.getElementById('"+objTab.id+"'));", 500);
	}
}

function menuClose(objTab)
{
	var intCellNum = objTab.className;
	var objDD = document.getElementById('navMenu'+intCellNum);

	setTimeout("if(!blnOpen){recursiveClose('"+objDD.id+"');}", 500);
}

function menuIn(objMenu)
{
	blnOpen = true;
	strOpenMenu = objMenu.id;
	objMenu.style.height = "";
	objMenu.style.zIndex = "120";
}

function menuOut(objMenu)
{
	blnOpen = false;
	if(!blnOpen)
	{
		setTimeout("if(!blnOpen){recursiveClose('"+objMenu.id+"');}", 1000);
	}
	objMenu.style.zIndex = "119";
}

function closeAll(objTab)
{
	var intNumNav = document.getElementById('navlinks').cells.length;
	for(i=0; i<intNumNav; i++)
	{
		if(objTab.className != i+1)
		{
			var objMenu =	document.getElementById('navMenu'+(i+1));
			setTimeout("recursiveClose('"+objMenu.id+"');", 5);
		}
	}
}

function recursiveClose(strId)
{
	var objMenu =	document.getElementById(strId);
	var intMenuXPos = objMenu.style.height.replace("px", "");

	if(objMenu.style.height == "")
	{
		objMenu.style.height = objMenu.offsetHeight;
	}

	if(intMenuXPos > 1 && (!blnOpen || (strId != strOpenMenu)))
	{
		var intDecrement = 10;

		if(intDecrement < intMenuXPos)
		{
			intMenuXPos = intMenuXPos - intDecrement;
		}
		else
		{
			intMenuXPos = intMenuXPos - 1;
		}

		objMenu.style.height = intMenuXPos + "px";
		setTimeout("recursiveClose('"+strId+"')", 10);
	}
	else if(intMenuXPos == "" && intMenuXPos < 1 && (!blnOpen || (strId != strOpenMenu)))
	{
		setTimeout("recursiveClose('"+strId+"')", 10);
	}
	else
	{
		if(intMenuXPos > 1)
		{
		setTimeout("recursiveClose('"+strId+"')", 10);
		}
		else
		{
			if (intMenuXPos != "" && intMenuXPos.substring(0,1) != "0")
			{
				objMenu.style.display = "none";
			}
		}
	}
}

function navHover(cell,x,mblnReset)
{
	var objParentNode = cell.parentNode;
	//subtract one to account for the search box on the nav bar
	var intCellCount = objParentNode.cells.length;
	var objSubLinks = document.getElementById("nav-sub-links");
	var intLinkCount = objSubLinks.cells.length;
	blnOpen = true;
	strOpenMenu = cell.id;
	var intCellNum = strOpenMenu.substring(strOpenMenu.length-1, strOpenMenu.length);
	for(i=0; i<(intCellCount); i++)
	{
		var mDiv = objParentNode.cells[i].id;
		var mDivId = mDiv.substring(0,mDiv.length-1);
		if ((mDivId != "topnavlend") && (mDivId != "topnavdiv") && (mDivId != "topnavrend")) /*Don't mess with the dividers or ends...*/
		{
			if (mDiv != "topnavtd5" || gstrQuoteId == '')
			{
				objParentNode.cells[i].className = "UNAV NavBGUp";
			}
			else
			{
				objParentNode.cells[i].className = "ONAV NavBGUpOrng";
			}
		}
	}

	if(cell.id==document.getElementById(strDefaultSubNav).id)
	{
		cell.className = "UNAV NavBGDown";
	}
	else
	{
		cell.className = "UNAV NavBGHover";
	}

	if (!mblnReset) {
		if (x != 1)	{document.getElementById('topnavlend1').className = "NavEnds NavLeftEndG";}
		else {document.getElementById('topnavlend1').className = "NavEnds NavLeftEndW";}
	}

	while(objSubLinks.cells.length != 0)
	{
		objSubLinks.deleteCell(0);
	}
   switch(strOpenMenu) {
   	case "topnavtd1":
   		//*** Get Quote
   		intTopNavTabNum = 1;
			if (gblnReserved)
			{
				insertSublink('/moving-quotes/quoteview.asp','View My Bill of Lading',true,false);
				insertSublink('/moving-tools/docret/?id='+gstrQuoteId,'Document Retrieval',true,false);
				insertSublink('/moving-resources/movingtodolist.asp','U-Pack To-Do List',true,false);
				insertSublink('/moving-supplies/moving-boxes.asp','Buy Moving Boxes',true,false);
				insertSublink('/moving-resources/movingnotice/','Send a Moving Notice',false,false);
			}
			else if (gstrQuoteId != '')
			{
				insertSublink(gstrReserveLink,'Make a Reservation',true,false);
				insertSublink('/moving-quotes/quoteview.asp','View My Quote',true,false);
				insertSublink('/moving-quotes/resubmitquote.asp','Change My Quote',true,false);
				insertSublink('/moving-tools/space-estimators.asp','Space Estimators',true,false);
				insertSublink('/moving-quotes/moving-price.asp','Will My Price Change?',false,false);
			}
			else
			{
				insertSublink('/moving-quotes/quote.asp','Get a Quote',true,false);
				insertSublink('/moving-quotes/quoteview.asp','View My Quote',true,false);
				insertSublink('/moving-quotes/resubmitquote.asp','Change My Quote',true,false);
				insertSublink('/moving-tools/space-estimators.asp','Space Estimators',true,false);
				insertSublink('/moving-quotes/quoteidretrieval.asp','Forgot Reference Number?',true,false);
				insertSublink('/moving-quotes/moving-price.asp','Will My Price Change?',false,false);
			}
			break;

		case 'topnavtd2':

			//*** Moving Trailers
			intTopNavTabNum = 2;

			insertSublink( '/moving-services/trailer-specifications.asp', 'Trailer Size',            true, false );
			insertSublink( '/moving-services/trailer-parking.asp',        'Trailer Parking',         true, false );
			insertSublink( '/moving-services/trailer-storage.asp',        'Trailer Storage',         true, false );
			insertSublink( '/about/truck-rental.asp',                     'Compare to Truck Rental', true, false );
			insertSublink( '/about/full-service-movers.asp',              'Compare to Full Service', true, false );
			insertSublink( '/about/long-distance-moving.asp',             'Long Distance Moving',    true, false );
			insertSublink( '/moving-services/small-moves.asp',            'Ways to Save',           false, false );

			break;

   	case 'topnavtd3':
   		//*** Moving Containers
   		intTopNavTabNum = 3;
			insertSublink('/moving-services/moving-container-size.asp','ReloCube Size',true,false);
			insertSublink('/moving-services/relocube-parking.asp','ReloCube Parking',true,false);
			insertSublink('/moving-services/moving-container-storage.asp','ReloCube Storage',true,false);
			insertSublink('/about/compare-portable-storage.asp','Compare to Portable Storage',true,false);
			insertSublink('/moving-services/on-site-storage.asp','On-Site Storage',true,false);
			insertSublink('/about/self-move.asp','Self-Move Alternatives',true,false);
			insertSublink('/moving-services/small-moves.asp','Ways to Save',false,false);
			break;
   	case 'topnavtd4':
   		//***U-Pack Services
   		intTopNavTabNum = 4;
			insertSublink('/moving-services/moving-trailer.asp','Moving Trailer',true,false);
			insertSublink('/moving-services/moving-container.asp','Moving Containers',true,false);
			insertSublink('/packload/packload.asp','Pack and Load',true,false);
			insertSublink('/moving-services/moving-storage.asp','Moving &amp; Storage',true,false);
			insertSublink('/moving-services/door-to-door.asp','Door-to-door',true,false);
			insertSublink('/moving-supplies/moving-boxes.asp','Moving Boxes',true,false);
			insertSublink('/moving-services/special-moves.asp','Special Moves',true,false);
			insertSublink('/about/compare-movers.asp','Compare Movers',false,false);
			break;
   	case 'topnavtd5':
   		//*** Reserve Now
   		intTopNavTabNum = 5;
			if (gblnReserved)
			{
				insertSublink('/moving-quotes/quoteview.asp','View My Bill of Lading',true,false);
				insertSublink('/moving-tools/docret/?id='+gstrQuoteId,'Document Retrieval',true,false);
				insertSublink('/moving-resources/movingtodolist.asp','U-Pack To-Do List',true,false);
				insertSublink('/moving-supplies/moving-boxes.asp','Buy Moving Boxes',true,false);
				insertSublink('/moving-resources/movingnotice/','Send a Moving Notice',false,false);
			}
			else if (gstrQuoteId != '')
			{
				insertSublink(gstrReserveLink,'Make a Reservation',true,false);
				insertSublink('/moving-quotes/quoteview.asp','View My Quote',true,false);
				insertSublink('/moving-quotes/resubmitquote.asp','Change My Quote',true,false);
				insertSublink('/moving-tools/space-estimators.asp','Space Estimators',false,false);
			}
			else
			{
				insertSublink(gstrReserveLink,'Make a Reservation',true,false);
				insertSublink('/moving-quotes/quote.asp','Get a Quote',true,false);
				insertSublink('/moving-quotes/resubmitquote.asp','Change My Quote',true,false);
				insertSublink('/moving-quotes/quoteidretrieval.asp','Forgot My Reference Number?',true,false);
				insertSublink('/moving-resources/movingtodolist.asp','U-Pack To-Do Checklist',false,false);
			}
			break;
   	case 'topnavtd6':
   		//*** Moving Supplies
   		intTopNavTabNum = 6;
			insertSublink('/moving-supplies/boxbundles.asp','Moving Boxes',true,false);
			insertSublink('/moving-supplies/moving-accessories.asp','Moving Accessories',true,false);
			insertSublink('/moving-supplies/moving-packages.asp','Moving Packages',false,false);
			break;
   	case 'topnavtd7':
   		//*** Moving Resources
   		intTopNavTabNum = 7;
			insertSublink('/moving-resources/checklist.asp','Moving Checklist',true,false);
			insertSublink('/moving-resources/moving-forms.asp','Forms &amp; Documents',true,false);
			insertSublink('/moving-tools/default.asp','Moving Tools',true,false);
			insertSublink('/moving-resources/how-to-move.asp','How To Move',true,false);
			insertSublink('/moving-resources/coverage/','Service Center Lookup',true,false);
			insertSublink('/moving-resources/moving-advice.asp','Moving Advice',true,false);
			insertSublink('/moving-resources/moving-timeline.asp','Moving Timeline',true,false);
			insertSublink('/moving-resources/video.asp','Moving Videos',false,false);
			break;
      default:
//			alert(strOpenMenu);
         break;
	}
	document.getElementById("nav-bottom").onmouseover = subNavIn;
//	document.getElementById("nav-bottom").onmouseout = subNavOut;
}

function insertSublink(strText,strName,blnBorder,blnSmallFont)
{
	var objSubLinks = document.getElementById("nav-sub-links");
	var intLinkCount = objSubLinks.cells.length;
	var newCell = objSubLinks.insertCell(intLinkCount);
	var mOnClick = ' onclick="trackWebAnalyticEvent(92, \'sub nav ' + intTopNavTabNum + '-' + strName + '\');"';
	var mName = strName;
	var mTitle = strName;

	if (strName == "Make a Reservation") {
		mName = "<b>"+strName+"</b>";
	}

	if ((strPathInfo.indexOf(strText) >= 0) && (strName != "Service Center Lookup")) {
		newCell.innerHTML = '<span title="You are currently on '+strName+'...">'+mName+'</span>';
		newCell.className = "navCurPage";
	}
	else if ((strPathInfo.indexOf(strText) >= 0) && (strName == "Service Center Lookup")) {
		newCell.innerHTML = '<a href="'+strText + '" title="Return to Coverage Map..."' + mOnClick + '>'+mName+'</a>';
		newCell.className = "navCurPage";
	}
	else {
		if (gblnCallFocusQuoteResponse && ((strName == "Make a Reservation") || (strName == "Change My Quote") || (strName == "Space Estimators") || (strName == "Will My Price Change?")))
		{
			var mEventName = '\''+gstrEventPrefix+' SNAV '+intTopNavTabNum+'-'+strName+'\'';
			var mEvent = '\''+gstrWAEventName+'\','+mEventName;
			mOnClick = ' onclick="trackWebEvent('+mEvent+');' +
									   'trackWebAnalyticEvent('+mEvent+');"';
			if (gblnWAdiagsReviewer) {
				mOnClick += '" onmouseover="showWAdiags('+mEventName+');" onmouseout="hideWAdiags();"';
			}
		}
		newCell.innerHTML = '<a href="'+strText + '" title="'+mTitle+'"' + mOnClick + '>'+mName+'</a>';
		newCell.className += 'navDiffPage';
	}
	if (blnBorder) {
		var newPipeCell = objSubLinks.insertCell(intLinkCount+1);
		newPipeCell.innerHTML = '<span class="NavDiv">I</span>';
	}
	if (blnSmallFont) {
		newCell.className += ' navSmall';
		if (blnBorder) {
			newPipeCell.className += ' navSmall';
		}
	}
	else
	{
		newCell.className += ' navNorm';
	}
}

function resetNav(cell,x)
{
	blnOpen = false;
	setTimeout("resetNavMenu('"+cell.id+"',"+x+");", 20);
}
function resetNavMenu(strId,x)
{
	if(!blnOpen)
	{
		if (strDefaultSubNav == "topnavtd1")
		{
			document.getElementById('topnavlend1').className = "NavEnds NavLeftEndW";
		}
		else
		{
			document.getElementById('topnavlend1').className = "NavEnds NavLeftEndG";
		}
		navHover(document.getElementById(strDefaultSubNav),x,true);
	}

}

function subNavIn()
{
	blnOpen = true;
}
function subNavOut()
{
	blnOpen = false;
	resetNav(document.getElementById(strOpenMenu));
}

function toggleSearchButton()
{
	if(document.frmNavSearch.q.value.length > 0) {
		document.getElementById('searchbutton').className = 'UPHEADER UPSearchButton mag';
	}
}

function removeDefaultText()
{
	if(document.frmNavSearch.q.value == ' search') {
		document.frmNavSearch.q.value = '';
	}
}

function resetDefaultText()
{
	if (document.frmNavSearch.q.value == '') {
		document.frmNavSearch.q.value = ' search';
	}
}

function submitSearch()
{
	document.frmNavSearch.submit();
}