
/*
  Constant definitions
*/
var LISTVIEWOBJ_PREFIX	= "ListVW";
var DATAARRAY_PREFIX	= "DataAR";

var gTabSpacerWidth			= 4;
var gTotalTabsWidth			= gTabSpacerWidth;

/*
  Browser detection
*/
var gIsNS = document.layers;
var gIsIE = document.all;
var gIsGecko = document.getElementByID && !document.all;

/*
  Cross browser helper function for getting a DOM element.
*/
function getElement(elementName)
{
	if (document.getElementById)
	{
		return document.getElementById(elementName);
	}
	else if (document.all)
	{
		return eval("document.all."+elementName);
	}
	else if (document.layers)
	{
		return eval("document.layers."+elementName); 
	}
	return null;
}

/*
  Cross browser helper function for getting the width of the browser window.
*/
function getBrowserWindowWidth()
{
	if (document.body && typeof(document.body.clientWidth) == 'number')
	{
		// Gecko 1.0 (Netscape 7) and Internet Explorer 5+
		return document.body.clientWidth;
	}
	else
	{
		// Navigator 4.x, Netscape 6.x, CompuServe 7 and Opera
		return window.innerWidth - 16;
	}
}

function menuclick(strVal)
{
	if (''==strVal)
		return;
	templateURLRedirect(strVal);

	return false;
}

function initMenu(strMenuID, index)
{
	var menu = 0;
	//alert(strToolTip);
	//var menu = PTPMMenuContainer('' , strToolTip, '', '', '');
	array = getSafeVarWarn(DATAARRAY_PREFIX+strMenuID);
	var j;
	if (array.length > 0)
	{
		menu = new PTPMMenuContainer();
		for ( j = 0; j < array.length; j++ ) 
		{
			//alert(array[j][2]);
			menu.menuItems[j] = new PTPMSimpleMenuItem();
			menu.menuItems[j].text = array[j][2];
			menu.menuItems[j].image	= new PTPMImage();
			menu.menuItems[j].image.imgSrc = array[j][0];
			menu.menuItems[j].image.imgHeight = '20';
			menu.menuItems[j].image.imgWidth = array[j][1];
			menu.menuItems[j].action = new PTPMJavaScriptAction();
			menu.menuItems[j].action.js = 'menuclick(\''+array[j][3]+'\')';
		}
	}	
	return menu;
}

function initDDandManTabsRows(arDropDownTabArrayName, arMandTabArrayName, defaultStyleSheet)
{
	// This may or may not be necessary for you to set,
	// depending upon how you implement the menus
	// relative to the portal page viewing the menus
	PTPMMenu.defaultCssFile = defaultStyleSheet;
	
	var ddTabArray		= arDropDownTabArrayName;
	var mandTabArray	= arMandTabArrayName;
	
	if (gHorizontalTabWidth == 0)
		gHorizontalTabWidth = gHorizontalTabWidthDefault;
	var trueWidth		= gHorizontalTabWidth;

	// Dropdown tabs
	var i;
	for ( i = 0; i < ddTabArray.length; i++ )
	{
		var buttonText	= '<div style="width:' + trueWidth 
			+ 'px;line-height: 15px;" align="center">'
			+ ddTabArray[i][0] 
			// the text way (uses webding)
			// + ' <span style="font-family:webdings;font-size:8px;vertical-align:middle;padding:0 2px;line-height: 15px;color:black;">&#054;</span></div></a>'; 
			
			// the image way
			+ '<img src="' + g_SharedImgSvrImgsURL
			+ 'icon_arrow_menu.gif" align="absmiddle" border="0" alt="'
			+ gNavTabDownArrowImageAlt+'">' 
			+ '</div>'; 
			
		var divID		= ddTabArray[i][1];
		var nMenuIndex		= i;
		var menu = initMenu(divID, nMenuIndex);

		if (menu != 0)
		{		
			PTPMSelectMenu.init(menu, divID, buttonText);
		}
		/*
		else
		{
			var selectMenu = new PTPMSelectMenu();
			var menuButton = new PTPMMenuButton(buttonText,0);
			selectMenu.add(menuButton);
			PTDOMUtil.getElementById(divID).innerHTML = selectMenu.toHtml();
		}*/
	}
	
	// How many tabs can we fit on one row?
	
	if ( null != mandTabArray )
	{
		writeMandatoryTabs(mandTabArray, gTotalTabsWidth);
	}
	writeEndOfTable();
}

// Flag to reload the page if the browser was minimized during page load. This would
// cause the menu tab calculations to be off since browser width would be 0 pixels.
var gPageReloadNeeded = false;

function writePortalMenuDivs(arDropDownTabArrayName)
{
	var ddTabArray		= arDropDownTabArrayName;
	if (gHorizontalTabWidth == 0)
		gHorizontalTabWidth = gHorizontalTabWidthDefault;
	var trueWidth		= gHorizontalTabWidth;
	var nBrowserWinWidth = getBrowserWindowWidth();
	// Browser window was minimized during page load. Need to reload the page when window
	// is restored again.
	if ( nBrowserWinWidth == 0 ) {
	  gPageReloadNeeded = true;	
	}

	writeSpacerTable();
	writeStartTable();
	for ( i = 0; i < ddTabArray.length; i++ )
	{
		var cellLength = trueWidth; // DAP note this calc is bad: (ddTabArray[i][0].length * 8) + 24/*for the arrow*/;		
		if ( gTotalTabsWidth + gTabSpacerWidth + cellLength > nBrowserWinWidth ) 
		{			
			writeEndOfTable();
			writeSpacerTable();
			writeStartTable();
			gTotalTabsWidth = gTabSpacerWidth + cellLength;
		} else {
			gTotalTabsWidth += gTabSpacerWidth + cellLength;	
		}
		var divID		= ddTabArray[i][1];
		var strToolTip		= ddTabArray[i][2];
		if ( strToolTip == "null" ) strToolTip = "";
		
		writeSpacerCell();
		document.write("<td align='center' title='" + strToolTip + "' nowrap='nowrap' height='" 
			+ gHorizontalTabHeight + "' width='" + trueWidth + "' id='" + divID + "'></td>"); 
	}
}

// Called when browser window is restored. Actually it is called whenever
// the window receives focus but the gPageReloadNeeded flag will always be false
// except in the case when we need to reload.
function CheckForPageReload()
{
	if ( gPageReloadNeeded )
		window.location.reload(); 
}

