// I-Frame menuing system (c)2003 - Port Lawrence Title & Turst Co.
// Wholly owned subsidiary of First American Title Insurance Co.
// Any use of this script without permission is prohibited

// Script written by: Joseph Coutcher

checkTimer = window.setInterval("checkLoaded()", 200);

function checkLoaded()
{
	if((top.mnu_sys_loaded) && (top.mnu_finalized))
	{
		window.clearInterval(checkTimer);
		
		for(i = 0; i < totalTabs; i++)
		{
			myObj = parent.topFrame.document.getElementById("MnuItem" + i);
			myObj.style.visibility = "visible";
		}
	}
}

// USER-DEFINED VARIABLES
var menu_border_color = "#A5D1FF";
var menu_background_color = "#0D3B8C";
var menu_highlight_color = "#FFB400";

// Declare our page-global variables
var totalTabs  = 0;              // variable for how many tabs we have on the page
var timerID    = null;           // variable for the timer's id value
var temp_index = 0;              // temporary index value for menuing system
var focus_array = new Array(3);  // array so that menu knows what has focus

// Set focus array all to zero's
focus_array[0] = 0;
focus_array[1] = 0;
focus_array[2] = 0;

// menuInfo Class - holds information about the menu tabs
function menuInfo()
{
	this.img_name = "";              // name of the image file
	this.img_on = null;              // handle to the (on) state of the image
	this.img_off = null;             // handle to the (off) state of the image
	this.x = 0;                      // x location of button
	this.y = 0;                      // y locaiton of button
	this.w = 0;                      // width of button
	this.zindex = 0;                 // initial z-index value
	this.menuID = 0;                 // menu ID for the naming convention of the buttons ("MnuImg" + id)
	this.site_href = "";             // URL (if menuID == -1, which stands for "don't use a menu for this button")
	this.menu_pointer = null;        // pointer to this tab's menu
}

// menuDefinition Class - holds information about the actual menu
function menuDefinition()
{
	this.menu_id = "";                // menu id (what the object's name is)
	this.size_x = 0;                  // menu width
	this.size_y = 0;                  // menu item height
	this.menu_entries = new Array();  // creates array of menu entries
}

// menuItemDefinition Class - holds information about the menu items
function menuItemDefinition()
{
	this.caption = "";            // caption to display in menu
	this.is_local_site = false;   // determines if the site is local
	this.sub_menu = new Array();  // pointer to sub-menu
	this.site_url = "#";          // URL that this should link to
	this.overload_link = 0
}

var menuInfo_Array = new Array();

// ***************************************************************************************************************************************
// *** START OF MENUBAR HANDLING ROUTINES *** START OF MENUBAR HANDLING ROUTINES *** START OF MENUBAR HANDLING ROUTINES ******************
// ***************************************************************************************************************************************

// *********************************************************************
// *** function getImageURLFromURL - find image objects URL in array ***
// *********************************************************************
function getImageURLFromURL(onoff, img_src)
{
	var i;

	// Search the array, and if it finds the image, return the opposite URL for that image
	for(i = 0; i < totalTabs; i++)
	{
		if(onoff == 1) // Should I do the search using img_on as the name to search for?
		{
			if(img_src.indexOf(menuInfo_Array[i].img_on) != -1)
			{
				temp_index = i;
				return menuInfo_Array[i].img_off;
			}
		}
		else // Well, guess we should use img_off then...
		{
			if(img_src.indexOf(menuInfo_Array[i].img_off) != -1)
			{
				temp_index = i;
				return menuInfo_Array[i].img_on;
			}
		}
	}
}

// *********************************************************************
// *** function tabMouseOut - when a menu tab is highlighted         ***
// *********************************************************************
function tabMouseOver()
{
	this.src = getImageURLFromURL(0, this.src);  // highlight tab
	menu_Tab_Selected(temp_index);               // move tabs order

	if(top.mnu_sys_loaded)
	{
		if(menuInfo_Array[temp_index].menuID != -1)
			{
			focus_array[0] = 1;
			focus_array[1] = 0;
			focus_array[2] = 0;
			closeMenu();
			showFirstMenu();
		}
		else
		{
			focus_array[0] = 0;
			focus_array[1] = 0;
			focus_array[2] = 0;
			closeMenu();
		}
	}
}



// *********************************************************************
// *** function tabMouseOut - when a menu tab loses "focus"          ***
// *********************************************************************
function tabMouseOut()
{
	if(top.mnu_sys_loaded)
	{
		this.src = getImageURLFromURL(1, this.src);  // unhighlight tab
		menu_Tab_Selected(0);                        // reset tabs order
	
		if(menuInfo_Array[temp_index].menuID != -1)
		{
			focus_array[0] = 0;
			startTimer();
		}
	}
}

// *********************************************************************
// *** function add_menu_Tab - adds a menu tab to the menuing system ***
// ***                         and stores it in a structured array.  ***
// ***                         Plus, it pre-loads the images.        ***
// *********************************************************************
	function add_Menu_Tab(fileName, x, y, w, h, zindex, menuID, site_href)
	{
		// create a temporary variable to hold new <img> tag
		var temp_item = new Image();

		// define this just in case we need to set a tab as a <a href="">
		var temp_href;

		// temporary menu item structure
		var newItem = new menuInfo;

		// Create a new hidden image ("on" state)
		newItem.img_on  = fileName + "_on.gif";

		// Create a new hidden image ("off" state)
		newItem.img_off = fileName + ".gif";

		// set up the entry for the new menu tab
		newItem.img_name  = fileName + ".gif";
			if(totalTabs == 0) newItem.x = "0px";
			else               newItem.x = ((menuInfo_Array[totalTabs - 1].w.replace(/px/, "") - 8) + (menuInfo_Array[totalTabs - 1].x.replace(/px/, "") - 10)) + "px";
		newItem.y         = y.replace(/px/, "");
		newItem.w         = w.replace(/px/, "");
		newItem.zindex    = zindex;
		newItem.menuID    = menuID;
		newItem.site_href = site_href;

		// set up the new menu item
		temp_item.id               = "MnuItem" + totalTabs;
		temp_item.name             = "MnuItem" + totalTabs;
		temp_item.src              = fileName + ".gif";
		temp_item.border           = 0;
		temp_item.style.position   = "absolute";
		temp_item.style.left       = newItem.x;
		temp_item.style.top        = y;
		temp_item.style.width      = w;
		temp_item.style.height     = h;
		temp_item.style.zindex     = zindex;
		temp_item.style.visibility = "hidden";

		// set onmouseover and onmouseout events for the <img> tag
		temp_item.onmouseover = tabMouseOver;
		temp_item.onmouseout  = tabMouseOut;

		// Add the new element to the menuInfo_Array
		menuInfo_Array[totalTabs] = newItem;

		// if this has no menu, then make the tab an href tag
		if(menuID == -1)
		{
			// create a temporary variable to hold new <a href> tag
			temp_href = document.createElement('a');

			// assign url to <a href>
			temp_href.href = site_href;

			temp_href.target = '_top';

			// add image to the inside of this tag
			temp_href.appendChild(temp_item);
			
			// add entire <a href=""><img></a> package to the document
			document.getElementsByTagName('body')[0].appendChild(temp_href);
		}
		else // add <img> tag to the document
			document.getElementsByTagName('body')[0].appendChild(temp_item);

		totalTabs++;
		menu_Tab_Selected(0);
	}

// ******************************************************************************
// *** function menu_Tab_Selected - changes z-index of tab images in the menu ***
// ***                              and highlights the menu tab               ***
// ******************************************************************************
	function menu_Tab_Selected(tabNum)
	{
		var j;     // counter variable

		// time to re-arrange the z-index of the tabs
		for(j = 0; j < totalTabs; j++)
		{
			if(j < tabNum)		document.getElementById("MnuItem" + j).style.zIndex = 30 + j;
			else if(j == tabNum)	document.getElementById("MnuItem" + j).style.zIndex = 30 + totalTabs + 2;
			else			document.getElementById("MnuItem" + j).style.zIndex = 20 - j;
		}
	}

// ***************************************************************************************************************************************
// ***** END OF MENUBAR HANDLING ROUTINES ***** END OF MENUBAR HANDLING ROUTINES ***** END OF MENUBAR HANDLING ROUTINES ******************
// ***************************************************************************************************************************************








// ***************************************************************************************************************************************
// ********* START OF FIRST MENU ROUTINES ********* START OF FIRST MENU ROUTINES ********* START OF FIRST MENU ROUTINES ******************
// ***************************************************************************************************************************************

// ******************************************************************************
// *** function showFirstMenu - moves the menu into position and displays it  ***
// ******************************************************************************
	function showFirstMenu()
	{
		var tempMenuFrame = parent.mainFrame.document.getElementById('menuFrame');
		focus_array[0] = 0; focus_array[0] = 0;
		top.mainFrame.menuFrame.document.body.onmouseout = hideFirstMenu;
		top.mainFrame.menuFrame.document.body.innerHTML = generate_Menu_Content(temp_index, 0);
		tempMenuFrame.style.left = menuInfo_Array[temp_index].x;


		if(top.isIE)
		{
			tempMenuFrame.style.height = parent.mainFrame.menuFrame.menuTable.clientHeight;
			tempMenuFrame.style.width = parent.mainFrame.menuFrame.menuTable.clientWidth;

			if ((parseInt(parent.mainFrame.menuFrame.menuTable.clientWidth) + parseInt(menuInfo_Array[temp_index].x.replace('px', ''))) > top.mainFrame.document.body.clientWidth)
			    tempMenuFrame.style.left = top.mainFrame.document.body.clientWidth - parent.mainFrame.menuFrame.menuTable.clientWidth
		}
		else
		{
			tempMenuFrame.style.height = top.frames['mainFrame'].menuFrame.document.getElementsByTagName('TABLE').item(0).offsetHeight;
			tempMenuFrame.style.width  = top.frames['mainFrame'].menuFrame.document.getElementsByTagName('TABLE').item(0).offsetWidth;

			if ((parseInt(top.frames['mainFrame'].menuFrame.document.getElementsByTagName('TABLE').item(0).offsetWidth) + parseInt(menuInfo_Array[temp_index].x.replace('px', ''))) > top.mainFrame.document.body.clientWidth)
			    tempMenuFrame.style.left = parseInt(top.mainFrame.document.body.clientWidth) - parseInt(top.frames['mainFrame'].menuFrame.document.getElementsByTagName('TABLE').item(0).offsetWidth)
		}

		tempMenuFrame.style.visibility = "visible";
	}

	function showSecondMenu(menuItemSel, itemID)
	{
		var top = 0, rowlength, myList;
		var tempMenuFrame = parent.mainFrame.document.getElementById('insetFrame');
		var tempMenuFrame2 = parent.mainFrame.document.getElementById('menuFrame');
		parent.mainFrame.insetFrame.document.body.onmouseout = hideSecondMenu;
		parent.mainFrame.insetFrame.document.body.innerHTML = generate_Menu_Content(temp_index, 1, itemID);

		if(top.isIE)
		{
			tempMenuFrame.style.left = (tempMenuFrame2.style.left.replace(/px/, "") - 0) + tempMenuFrame2.clientWidth + 3;
			myList = top.frames['mainFrame'].menuFrame.document.getElementsByTagName('TR');
		}
		else
		{
			tempMenuFrame.style.left = (tempMenuFrame2.style.left.replace(/px/, "") - 0) + tempMenuFrame2.offsetWidth + 3;
			myList = window.top.frames['mainFrame'].menuFrame.document.getElementsByTagName('TR');
		}

		rowlength = myList.length;

		for(i = 0; i < rowlength; i++)
		{
			if(myList.item(i) == menuItemSel)
			{
				break;
			}
			else
			{
				if(top.isIE)
				{
					top = top + ((myList.item(i).clientHeight - 0) + 2);
				}
				else
				{
					top = top + ((myList.item(i).offsetHeight - 0) + 2);
				}
			}
		}
		tempMenuFrame.style.top = (top + 10) + "px";

		if(top.isIE)
		{
			tempMenuFrame.style.height = parent.mainFrame.insetFrame.menuTable.clientHeight;
			tempMenuFrame.style.width = parent.mainFrame.insetFrame.menuTable.clientWidth;

            if ((parseInt(tempMenuFrame.style.left.replace('px', '')) + parseInt(tempMenuFrame.style.width.replace('px', ''))) > top.mainFrame.document.body.clientWidth)
                tempMenuFrame.style.left = (tempMenuFrame2.style.left.replace(/px/, "") - 0) - (parseInt(tempMenuFrame.style.width.replace('px', '')) + 3);
		}
		else
		{
			tempMenuFrame.style.height = window.top.frames['mainFrame'].insetFrame.document.getElementsByTagName('TABLE').item(0).offsetHeight;
			tempMenuFrame.style.width  = window.top.frames['mainFrame'].insetFrame.document.getElementsByTagName('TABLE').item(0).offsetWidth;

            if ((parseInt(tempMenuFrame.style.left.replace('px', '')) + parseInt(tempMenuFrame.style.width.replace('px', ''))) > window.top.frames['mainFrame'].document.body.clientWidth)
                tempMenuFrame.style.left = (tempMenuFrame2.style.left.replace(/px/, "") - 0) - (parseInt(tempMenuFrame.style.width.replace('px', '')) + 3);
		}

		tempMenuFrame.style.visibility = "visible";
	}

	function hideFirstMenu()
	{
		top.topFrame.focus_array[1] = 0;
		top.topFrame.startTimer();
	}

	function hideSecondMenu()
	{
		top.topFrame.focus_array[2] = 0;
		top.topFrame.startTimer();
	}

// *********************************************************************************
// *** function closeMenu - check focus of the objects, and close them if needed ***
// *********************************************************************************
	function closeMenu()
	{
		var tempVar = null;
		
		if(timerID)
			window.clearTimeout(timerID);

		// is the first menu selected, or the second menu unselected?
		if((focus_array[0] == 1) || focus_array[2] == 0)
		{
			// hide second menu
			tempVar = parent.mainFrame.document.getElementById('insetFrame');
			tempVar.style.visibility = "hidden";
		}

		// have all 3 menus lost focus?
		if((focus_array[0] == 0) && (focus_array[1] == 0) && (focus_array[2] != 2))
		{
			// hide first menu
			tempVar = parent.mainFrame.document.getElementById('menuFrame');
			tempVar.style.visibility = "hidden";

			// hide second menu
			tempVar = parent.mainFrame.document.getElementById('insetFrame');
			tempVar.style.visibility = "hidden";
		}

		timerID = null;
	}

// *****************************************************************************
// *** function startTimer - when tabMouseOut is called, this is called to   ***
// ***                       start the timer before it checks what's still   ***
// ***                       in focus                                        ***
// *****************************************************************************
	function startTimer()
	{
		if(!timerID)
			timerID = window.setTimeout("closeMenu()", 500);
	}

	function highlight(dest_frame, menuItemSel, id)
	{
		var i, c;
		var tableRows, rowlength;

		tableRows = dest_frame.document.getElementsByTagName('TABLE').item(0).getElementsByTagName('TR');
		rowlength = dest_frame.document.getElementsByTagName('TABLE').item(0).getElementsByTagName('TR').length;

		for(c = 0; c < rowlength; c++)
			if(tableRows.item(c) == menuItemSel)
				break;

		for(i = 0; i < rowlength; i++)
			dest_frame.document.getElementsByTagName('DIV').item(i).style.background = menu_background_color;

		dest_frame.document.getElementsByTagName('DIV').item(c).style.background = menu_highlight_color;
	}





	// declare some more global variables
	var temp_menu_ID = 0, temp_sub_menu_ID = 0, temp_menu_Item_ID = 0;

	function addMenuItem(caption, url, local_site, overload_link)
	{
		temp_sub_menu_ID = 0;

		temp_menu_Item_ID = menuInfo_Array[temp_menu_ID].menu_pointer.menu_entries.length;

		menuInfo_Array[temp_menu_ID].menu_pointer.menu_entries[temp_menu_Item_ID]               = new menuItemDefinition;
		menuInfo_Array[temp_menu_ID].menu_pointer.menu_entries[temp_menu_Item_ID].caption       = caption;
		menuInfo_Array[temp_menu_ID].menu_pointer.menu_entries[temp_menu_Item_ID].is_local_site = local_site;
		menuInfo_Array[temp_menu_ID].menu_pointer.menu_entries[temp_menu_Item_ID].site_url      = url;
		menuInfo_Array[temp_menu_ID].menu_pointer.menu_entries[temp_menu_Item_ID].overload_link = overload_link;
	}
	
	function addSubMenuItem(caption, url, local_site, overload_link)
	{
		temp_sub_menu_ID = menuInfo_Array[temp_menu_ID].menu_pointer.menu_entries[temp_menu_Item_ID].sub_menu.length;

		menuInfo_Array[temp_menu_ID].menu_pointer.menu_entries[temp_menu_Item_ID].sub_menu[temp_sub_menu_ID]               = new menuItemDefinition;
		menuInfo_Array[temp_menu_ID].menu_pointer.menu_entries[temp_menu_Item_ID].sub_menu[temp_sub_menu_ID].caption       = caption;
		menuInfo_Array[temp_menu_ID].menu_pointer.menu_entries[temp_menu_Item_ID].sub_menu[temp_sub_menu_ID].is_local_site = local_site;
		menuInfo_Array[temp_menu_ID].menu_pointer.menu_entries[temp_menu_Item_ID].sub_menu[temp_sub_menu_ID].site_url      = url;
		menuInfo_Array[temp_menu_ID].menu_pointer.menu_entries[temp_menu_Item_ID].sub_menu[temp_sub_menu_ID].overload_link = overload_link;
	}

	function createMenu(x, y, menuID)
	{
		temp_menu_ID = menuID;
		temp_sub_menu_ID = 0;
		temp_menu_Item_ID = 0;
		menuInfo_Array[temp_menu_ID].menu_pointer = new menuDefinition;
		menuInfo_Array[temp_menu_ID].menu_pointer.menu_id = "menu" + temp_menu_ID;
	}

	function generate_Menu_Content(indexVal, is_Sub, itemSelected)
	{
		var i = 0, focus_index = 4;
		var generated_String = "";
		var menuEntries_Ptr = null;
		var dest_frame = null;
		
		switch(is_Sub)
		{
			case 0:
				menuEntries_Ptr = menuInfo_Array[indexVal].menu_pointer.menu_entries;
				dest_frame = "window.top.frames['mainFrame'].menuFrame";
				focus_index = 1;
				break;

			case 1:
				menuEntries_Ptr = menuInfo_Array[indexVal].menu_pointer.menu_entries[itemSelected].sub_menu;
				dest_frame = "window.top.frames['mainFrame'].insetFrame";
				focus_index = 2;
				break;

			default:
				alert("NOT A VALID VALUE FOR is_Sub!\nMenu may not work correctly.");
				generated_String = "NULL_MENU";
				return;
		}
		
		totalEntries = menuEntries_Ptr.length;

		generated_String += "<table id='menuTable' onmouseover='top.topFrame.focus_array[" + focus_index + "] = " + (focus_index) + "' border='0' cellpadding='1' cellspacing='2' class='mainMenu' style='left: 0px; top: 0px; cursor: hand; background-color: " + menu_border_color + "'>\n";
		for(i = 0; i < totalEntries; i++)
		{
			if(focus_index == 1)
				generated_String += "   <tr id='MenuItem" + i + "' onmouseover=\"top.topFrame.focus_array[2] = 0; top.topFrame.focus_array[1] = 1; top.topFrame.closeMenu(); top.topFrame.highlight(" + dest_frame + ", this, " + i + ");";
			else
				generated_String += "   <tr id='MenuItem" + i + "' onmouseover=\"top.topFrame.highlight(" + dest_frame + ", this, " + i + ");";

			if(menuEntries_Ptr[i].sub_menu.length > 0)
				generated_String += " top.topFrame.focus_array[2] = 1; MenuItem" + i + "Arrow.src = 'images/arrow_on.gif'; top.topFrame.showSecondMenu(this, " + i + ")\" onmouseout=\"MenuItem" + i + "Arrow.src = 'images/arrow.gif'";

			generated_String += "\">\n";

			generated_String += "      <td nowrap style=\"background-color: " + menu_background_color + "\">\n"
			if(menuEntries_Ptr[i].site_url != "")
			{
				generated_String += "         <a href='" + menuEntries_Ptr[i].site_url + "'"

				if(menuEntries_Ptr[i].overload_link == 0)
					generated_String += " target='targetWebFrame' onclick='top.topFrame.went_to_link_count()'>\n"
				else
					generated_String += " target='_top'>\n"
			}

			generated_String += "            <div onmouseover=\"this.style.color='#000000' \" onmouseout=\"this.style.color='#FFFFFF' \" style='width: 100%; cursor: hand' id='MenuItemDiv" + i + "'>\n";
			generated_String += "               " + menuEntries_Ptr[i].caption + "\n";
			generated_String += "            </div>\n"
			if(menuEntries_Ptr[i].site_url != "")
				generated_String += "         </a>\n";

			generated_String += "      </td>\n";

			if((menuEntries_Ptr[i].sub_menu.length > 0) && (!is_Sub))
			{
				generated_String += "      <td>\n"
				generated_String += "         <img id=\"MenuItem" + i + "Arrow\" src='images/arrow.gif'>\n"
				generated_String += "      </td>\n";
			}

			generated_String += "   </tr>\n";
		}
		generated_String += "</table>\n";
		return generated_String;
	}

	var total_link_hit_count = 0;

	function went_to_link_count()
	{
		total_link_hit_count++;

		if(total_link_hit_count == 5)
			window.open("beneficial.html", "plt_beneficial_frame", "left=" + ((screen.availwidth / 2) - 75) + ",top=" + ((screen.availheight / 2) - 75) + ",width=150,height=150,scrollbars=0,resizable=0");
	}
	
	function finalize_menu()
	{
		top.mnu_finalized = 1;
	}

// ***************************************************************************************************************************************
// *********** END OF FIRST MENU ROUTINES *********** END OF FIRST MENU ROUTINES *********** END OF FIRST MENU ROUTINES ******************
// ***************************************************************************************************************************************