


/*
  Constant definitions
*/
PTTagDDMenu.DATAARRAY_PREFIX	= "DataAR";

PTTagDDMenu.gTagPTDDMenuTabSpacerWidth				= 4;
PTTagDDMenu.gTagPTDDMenuHorizontalTabHeight			= 12;
PTTagDDMenu.gTagPTDDMenuHorizontalTabWidth 			= 0;
PTTagDDMenu.gTagPTDDMenuMaxTabsPerRow				= 0;
PTTagDDMenu.gTagPTDDMenuHorizontalTabWidthDefault 	= 90; 

function PTTagDDMenu()
{
	return this;	
}

/*
  Cross browser helper function for getting the width of the browser window.
*/
PTTagDDMenu.getBrowserWindowWidth = function()
{
	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;
	}
}

PTTagDDMenu.menuclick = function(strVal)
{
	if (''==strVal)
		return;
	window.location = strVal;

	return false;
}

PTTagDDMenu.manmenuTabClick = function(e, strURL)
{
	PTTagDDMenu.menuclick(strURL)
}


// 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.
PTTagDDMenu.gPageReloadNeeded = false;



//
//
//
//
//

PTTagDDMenu.writePTDDMenuTabs = function(arPTDDTabArray)
{
	var ddTabArray		= arPTDDTabArray;
	if (PTTagDDMenu.gTagPTDDMenuHorizontalTabWidth == 0)
		PTTagDDMenu.gTagPTDDMenuHorizontalTabWidth = PTTagDDMenu.gTagPTDDMenuHorizontalTabWidthDefault;

	var nCellWidth		= PTTagDDMenu.gTagPTDDMenuHorizontalTabWidth;
	var nBrowserWinWidth = PTTagDDMenu.getBrowserWindowWidth();
	var nMaxWidth 		= nBrowserWinWidth;
	
	// Browser window was minimized during page load. Need to reload the page when window
	// is restored again.
	if ( nBrowserWinWidth == 0 ) {
	  gPageReloadNeeded = true;	
	}

	// Calculate how many tabs we can fit in one row
	//
	var nMaxTabs  = 0
	// Do we have a defined max tabs?
	if ( PTTagDDMenu.gTagPTDDMenuMaxTabsPerRow == 0 )
	{
		nMaxTabs = Math.floor( nBrowserWinWidth / (PTTagDDMenu.gTagPTDDMenuTabSpacerWidth + nCellWidth) );
	} else {
		nMaxTabs = PTTagDDMenu.gTagPTDDMenuMaxTabsPerRow	
	}
	
	PTTagDDMenu.tagWriteSpacerTable();
	PTTagDDMenu.tagWriteStartTable();
	for ( i = 0, nCurrRowTabs = 0; i < ddTabArray.length; i++ , nCurrRowTabs++)
	{
		if ( nCurrRowTabs == nMaxTabs ) 
		{			
			PTTagDDMenu.tagWriteEndOfTable();
			PTTagDDMenu.tagWriteSpacerTable();
			PTTagDDMenu.tagWriteStartTable();
			
			nCurrRowTabs = 0;
			
		} 
		
		PTTagDDMenu.tagWriteSpacerCell();
				
		var strToolTip		= PTTagDDMenu.getToolTipFromTabArray(ddTabArray, i);
		
		
		if ( PTTagDDMenu.isSimpleTabType(ddTabArray, i) ) 
		{
			var strTitle		= PTTagDDMenu.getTitleFromTabArray(ddTabArray, i);
			var strURL			= PTTagDDMenu.getTabDataFromTabArray(ddTabArray, i);
			PTTagDDMenu.writeSimpleTab(strTitle, strURL, strToolTip);		
		} 
		else 
		{
			var divID			= PTTagDDMenu.getTabDataFromTabArray(ddTabArray, i);
			PTTagDDMenu.writeMenuTabHolder(divID, strToolTip, PTTagDDMenu.gTagPTDDMenuHorizontalTabHeight, nCellWidth);	
			
		}
	}
	PTTagDDMenu.tagWriteEndOfTable();
	// Initialize the dropdown menus
	PTTagDDMenu.initPTDDMenus(arPTDDTabArray);
}




PTTagDDMenu.writeMenuTabHolder = function(strDivID, strToolTip, strTabHeight, strTabWidth)
{
	document.write("<td align='center' title='" + strToolTip + "' nowrap='nowrap' height='" 
				   + strTabHeight + "' width='" + strTabWidth + "' id='" + strDivID + "'></td>"); 
}

PTTagDDMenu.writeSimpleTab = function(strTitle, strURL, strToolTip)
{

	if (strTitle.length == strToolTip.length) {
		strToolTip = '';
	}	
	var strManTabOnClickHandler = "PTTagDDMenu.manmenuTabClick";
	
	document.write(	
		"<td class='navMidtabBtn' height='"+PTTagDDMenu.gTagPTDDMenuHorizontalTabHeight+"'>"+
			"<table border='0' cellspacing='0' cellpadding='0'><tr>"+				
			  "<td height='"+PTTagDDMenu.gTagPTDDMenuHorizontalTabHeight+"' align='center' nowrap onclick='"+strManTabOnClickHandler+"(event, \""+strURL+"\"); return false;'><table cellspacing=0 cellpadding=0 class='navMidtabBorder'><tr><td nowrap>"+
			    "<span class='navMidtabText' title='"+strToolTip+"'>"+
			      "<a href='#' onclick='"+strManTabOnClickHandler+"(event, \""+strURL+"\"); return false;'>"+
			        "<span style='line-height: 12px;'>&nbsp;"+strTitle+"&nbsp;</span>"+
				  "</a>"+
				"</span></td></tr></table>"+
			   "</td></tr></table>"+
		 "</td>");
		
}

/*
  Initialize the javascript menu objects
  
  
*/
PTTagDDMenu.initPTDDMenus = function(arDropDownTabArrayName, 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;
	
	// ddTagArray elements: 0 - image name, 1 - image width, 2 - title, 3 - link
	var ddTabArray		= arDropDownTabArrayName;
	
	if (PTTagDDMenu.gTagPTDDMenuHorizontalTabWidth == 0)
		PTTagDDMenu.gTagPTDDMenuHorizontalTabWidth = PTTagDDMenu.gTagPTDDMenuHorizontalTabWidthDefault;
	var trueWidth		= PTTagDDMenu.gTagPTDDMenuHorizontalTabWidth;

	// Iterate through the Dropdown tabs
	var i;
	for ( i = 0; i < ddTabArray.length; i++ )
	{
		if ( PTTagDDMenu.isMenuTabType(ddTabArray, i) )
		{
			var strTabTitle 	= PTTagDDMenu.getTitleFromTabArray(ddTabArray, i);
			var strDivID 		= PTTagDDMenu.getTabDataFromTabArray(ddTabArray, i);
			
			// !!!!!!!!!!!!! The tab HTML !!!!!!!!! Extract this
			var buttonText	= '<div style="width:' + trueWidth 
				+ 'px;line-height: 12px;" align="center"><table cellspacing=0 cellpadding=0 class="navMidtabBorder" width="' + trueWidth + '"><tr><td nowrap align="center" class="navMidtabText">'
				+ strTabTitle
				// the text way (uses webding)
				// + ' <span style="font-family:webdings;font-size:8px;vertical-align:middle;padding:0 2px;line-height: 12px;color:black;">&#054;</span></div></a>'; 
				
				// the image way
				//+ '<img height="11" src="' + g_SharedImgSvrImgsURL
				//+ 'icon_arrow_menu.gif" align="absmiddle" border="0" alt="'
				//+ PTTagDDMenu.gNavTabDownArrowImageAlt+'">' 
				+ '</td></tr></table></div></a>'; 
				
			// Get the DivID for the current menu
			var nMenuIndex		= i;
			// initializes and creates the dropdown menu from js array
			var menu = PTTagDDMenu.initMenu(strDivID, nMenuIndex);
			
			if (menu != 0)
			{		
				PTPMSelectMenu.init(menu, strDivID, buttonText);
			}
			else
			{
				// If the menu contains no entries
				// ?? Don't really get this yet
				var selectMenu = new PTPMSelectMenu();
				var menuButton = new PTPMMenuButton(buttonText,0);
				selectMenu.add(menuButton);
				PTDOMUtil.getElementById(strDivID).innerHTML = selectMenu.toHtml();
			}
		}
	}
	
	// How many tabs can we fit on one row?
	
}




/*
 Initializes a dropdown menu.
 Called from initDDandManTabsRows
*/
PTTagDDMenu.initMenu = function(strMenuID, index)
{
	var strMenuClickHandler = "PTTagDDMenu.menuclick";
	
	var menu = 0;
	//var menu = PTPMMenuContainer('' , strToolTip, '', '', '');
	
	// Get the javascript array with menu entries for the current menu
	array = getSafeVarWarn(PTTagDDMenu.DATAARRAY_PREFIX+strMenuID);
	var j;
	if (array.length > 0)
	{
		
		menu = new PTPMMenuContainer();
		// Add each entry to the PTPMMenuContainer
		for ( j = 0; j < array.length; j++ ) 
		{
			//alert(array[j][2]);
			menu.menuItems[j] = new PTPMSimpleMenuItem();
			// Set title
			menu.menuItems[j].text = array[j][2];
			// Set Image
			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];
			// Set action
			menu.menuItems[j].action = new PTPMJavaScriptAction();
			menu.menuItems[j].action.js = strMenuClickHandler+'(\''+array[j][3]+'\')';
		}
	}	
	return menu;
}

PTTagDDMenu.getTitleFromTabArray = function(ptddtabsarray, index)
{
	return ptddtabsarray[index][1];	
}

PTTagDDMenu.getToolTipFromTabArray = function(ptddtabsarray, index)
{
	var strToolTip = ptddtabsarray[index][3];
	if ( strToolTip == "null" ) strToolTip = "";
	return strToolTip;	
}

PTTagDDMenu.getTabDataFromTabArray = function(ptddtabsarray, index)
{
	return ptddtabsarray[index][2];	
}

PTTagDDMenu.isSimpleTabType = function(ptddtabsarray, index)
{
	return (ptddtabsarray[index][0] == "simpletab");
}

PTTagDDMenu.isMenuTabType = function(ptddtabsarray, index)
{
	return (ptddtabsarray[index][0] == "menutab");
}


// 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.
PTTagDDMenu.CheckForPageReload = function()
{
	if ( gPageReloadNeeded )
		window.location.reload(); 
}


PTTagDDMenu.tagWriteStartTable = function() 
{
	document.write("<table cellpadding=\"0\" cellspacing=\"0\" width=\"100%\" border=\"0\"><tr class=\"navMidtabBg\">");
}	
PTTagDDMenu.tagWriteEndOfTable = function() {
	document.write("<td width=\"100%\"></td></tr></table>");
}
PTTagDDMenu.tagWriteSpacerTable = function() 
{
	document.write(	"<table cellpadding=\"0\" cellspacing=\"0\" width=\"100%\" border=\"0\">"+
				   	"<tr class=\"banHeaderBg\">"+
					"<td colspan=\"1\" class=\"navMidtabSpacerBg\">"+
					"<img src=\""+strPathToImageServer+"portal/public/img/sp.gif\" alt=\"\" border=\"0\" height=\"4\" width=\"1\"></img>"+
					"</td></tr></table>"
				   );
}
PTTagDDMenu.tagWriteSpacerCell = function() 
{ 
	document.write(	"<td colspan=\"1\" class=\"navMidtabSpacerBg\">"+
					"<img src=\"" +strPathToImageServer+"portal/public/img/sp.gif\" alt=\"\" border=\"0\" height=\"1\" width=\"4\"/>"+
					"</td>"
				   ); 
}					