//
// MENU PROGRAM
//
// Table of contents
// 1. Structure of the particular menu 
// 2. Other Global Variables
// 3. Routines for writing the menus on pages
// 4. Routings for handling mouse events 
// 5. Other non-trivial routines
//
// Note: The file menustyle.css is essential for the appearence of the menu.
// 
//
// 1. Structure of menu
// 
// This is the only part of the program that should be modified.   Be careful when editing this and // be sure to make a backup.  Any mistake in punctuation can cause the menu not to appear.  
//
var menuArray = new Array()
	menuArray[0] = new Array (["SPAR Website"],["SPAR Homepage","http://www.baruch.cuny.edu/grants/index.html"],["Pre-application","preapp.htm"],["Proposal Preparation","propprep.htm"],["Post-award Admin.","postaward.htm"], ["RF Employee","HumanResources.htm"],["Documents and Forms","publications.html"],["Site Map","SPARSiteMap.htm"]);
	menuArray[1] = new Array (["General Human Resources"],["Human Resources Page","HumanResources.htm"],["Employee Forms","RFEmployeeForms.htm"],["Employee Benefits","RFEmployeeBenefits.htm"],["Payroll Problems","RFEmployeePayrollProblem.htm"]);
	menuArray[2] = new Array (["New Hires"],["New Hire Page","RFNew_Employees.htm"],["Background Check","RFBackgroundCheck.htm"],["Pre-tax Benefits","RFFTPreTaxBenefits.htm"],["Benefits Orientations","benefits_orientation.htm"],["Policy 507E","RFPolicy507E.htm"]);
 	menuArray[3] = new Array (["Rehires"],["Rehire Page","SPARRFRehire.htm"]);
	menuArray[4] = new Array (["Foreign Students"],["Foreign Student Emps.","Foreign_Student_Employees.htm"],["F-1/J-1 Information","F1J1StudEmployees.htm"],["H-1B1 Processing","RFH1B1Processing.htm"]);
 	
// 2. Other Global variables
//

// variables pertaining to timing:


// Delay before menu closes when leaving it
var leaveDelay=350;
// To count time left for menu to close
var leaveTimer=0;
// Delay before item closes because you on a new item
var closeDelay=350;
// To count menu time left of an item to close
var closeTimer=0;
// Delay before menu opens or closes so action is not too fast for user
var openDelay = 350;
var openTimer = 0;


// other variables:


// Holds name of previous div so that it can be closed
// 	Note: div names must be converted to addresses by the program in order to open and close	
//
var prevDiv="none"
// Holds the name of a div about to open
var nextDiv="none";
//

// holds addresses of parent buttons when necessary.
var buttonArray = new Array();
var stackPointer = 0;

// String to hold the menu being created 
var menuString="";			
// This makes sure that the menu closes if the user leaves the page.  This can be commented out if // this action is not desired.		
window.onbeforeunload = Leave;

//									//
// 3. Routines to write the menu
//
// Note that the target page must have all the information documented in file "target.htm"

// menuBuild controls the writing of the menu.  The parameter x is not used but is reserved
// in case it is needed for program modifications.

function menuBuild(x) 
{
	menuAccumulate('<link rel=\"stylesheet\" href=\"menustyle.css\" context=\"text/css\" media=\"screen\"> <form>');
	menuAccumulate('<script  src=\"myjs.js\"></script>');
	// note: you still have to have link in target header to script file.
	for (i=0;i<menuArray.length; i++) writeMainEntry(i);	// writes main buttons
	menuAccumulate('</form>');
	writeByBrowser(menuString);
}


//  writeMainEntry writes each of the major items and calls functions to write the minor items
//  The parameter i takes in the number of the item, which will be used to form the div Id in
//  parent items. 

function writeMainEntry(i)
{
	menuAccumulate('<input type=\"button\" class=\"btn1\" value=\"');
	menuAccumulate(menuArray[i][0][0]); 
	menuAccumulate('"');
	if (menuArray[i][0].length==2)	// address is provided
	{
		menuAccumulate(' onmouseover=\"javascript:mouseOverNonParent(this,\'div'+i+'\');\" ');
		menuAccumulate(' onmouseout=\"mouseOutNonParent(this)\" ');
		menuAccumulate('onClick=\"window.location=');
		menuAccumulate('\'');
		menuAccumulate(menuArray[i][0][1])
		menuAccumulate('\'\"');
		menuAccumulate('>');
		menuAccumulate('<br>');
	
	}
	else
	{

		// put function call for open closer on page
		menuAccumulate(' onmouseover=\"javascript:mouseOverParent(this,\'div'+i+'\');\" ');
		menuAccumulate(' onmouseout=\"javascript:mouseOutParent(this,\'div'+i+'\');\" ');
		menuAccumulate('>');
		menuAccumulate('<br>');
		divWriter(i);
	}
}

// divWriter writes the divs which are the segments that open and close
//

function divWriter(i)
{
	menuAccumulate('<div id=\"div'+i+'\" style="display:none; margin-bottom:0; margin-top:0;\">');
	for (j=1;j<menuArray[i].length;j++) 
	{
		writeSubEntry(i,j); 
	}	
	menuAccumulate('</div>');
}

// writeSubEntry writes the buttons for the minor entries
//


function writeSubEntry(i,j){
	// i is the address of main item
	// j is the address of the subordinate item

	menuAccumulate('<input type=\"button\" class=\"btn2\" value=\"');
	menuAccumulate(menuArray[i][j][0]); 
	menuAccumulate('\"');
	menuAccumulate('onclick=\"window.location=');
	menuAccumulate('\'');
	menuAccumulate(menuArray[i][j][1])
	menuAccumulate('\'\"');
	menuAccumulate(' onmouseover=\"javascript:mouseOverChild(this);\" ');
	menuAccumulate(' onmouseout="mouseOutChild(this)" ');
	menuAccumulate('>');
	menuAccumulate('<br>');
}

// menuAccumulate accumulates the text needed to write the menu
//

function menuAccumulate(menuText) 
{
	menuString+=menuText;
}

// writeByBrowser detects the browser and writes the menu to the page
//


function writeByBrowser(str)
{
	var layer='theMenu';
	if (navigator.userAgent.indexOf("Opera")!=-1
	&& document.getElementById) type="OP";
	if (document.all) type="IE";
	if (document.layers)type="NN";
	if (!document.all && document.getElementById) type="MO";
	// do the writing
	// internet explorer
	if (type=="IE") {
		//alert("Internet Explorer");
		document.all[layer].innerHTML= str;
	}
	// netscape navigator
	if (type=="NN")
	{
		//alert("Netscape Navigator");
		document.layers[layer].document.open();
		document.layers[layer].document.write(str);
		document.layers[layer].document.close();
	}
	// mozilla or opera	
	if (type=="MO" || type=="OP") 
	{
		//alert("Mozilla or Opera");
		document.getElementById(layer).innerHTML=str;
	}
}

//
//	4. Routines to handle mouse movements
//



function mouseOverParent(buttonId,divId)
{
	//alert('mop');
	// previous requests to open and close must be removed
	clearTimers();
	// color must change
	//alert('206');
	popAllButtonsExcept(buttonId);	
	pushButton(buttonId);
	buttonId.className='btn3';
	// after the close delay this any previous div closes
	nextDiv=divId;
	// after the open delay this parents div opens
	// you have to make sure that it closes before it opens
	closeTimer=setTimeout("Close()",closeDelay);
	openTimer=setTimeout("Open()",openDelay);
}




function mouseOutParent(buttonId,divId)
{
	// we cannot assume the div is closing as we may be going to a child.
	// we can request that the menu be closed under the assumption we might be leaving the menu
	leaveTimer=setTimeout("Leave()",leaveDelay);	
}


function mouseOverNonParent(buttonId,divId)
{	// previous requests to open and close must be removed
	clearTimers();
	// color must change
	//alert('233');
	popAllButtonsExcept(buttonId);
	buttonId.className='btn5';
	//buttonId.style.textDecoration='underline';
		// after the close delay any previous div closes
	//alert("monp");
	closeTimer=setTimeout("Close()",closeDelay);
}
	

function mouseOutNonParent(buttonId,divId)
{
	buttonId.className='btn1';
	//buttonId.style.textDecoration='none';
	leaveTimer=setTimeout("Leave()",leaveDelay);
}



function mouseOverChild(object)
{
	clearTimers();	
	object.className='btn4';
}

function mouseOutChild(object)
{
		// change color back
	object.className='btn2';
	leaveTimer=setTimeout("Leave()",leaveDelay)
	// if the user goes to another item in the menu, the timeout must be cancelled.	
}


// clear Timers is used when a new area is entered as previous requests are discarded
function clearTimers(code)
{
	clearTimeout(leaveTimer);
	clearTimeout(openTimer);
	clearTimeout(closeTimer);
}


function Open()
{
		
	// this function can only be called from mouseoverparent
	// only this function changes the appearence of the parent buttons to btn3
	var currId = document.getElementById(nextDiv);
	currId.style.display="block";
	prevDiv=nextDiv;
	//alert('pause');
}


function Close()
// only this function can change the appearence of parent buttons back to btn1
{
	//alert("close");
	if (prevDiv!="none")
	{
		var currId = document.getElementById(prevDiv);
		currId.style.display="none";
		prevDiv="none";
		
		//changeButtons();
	}
	//alert('299');
	//popAllButtonsExcept("none");
}


function Leave()
{
	//alert("leave");
	//changeButtons();
	popAllButtonsExcept("none");
	Close();

}


function pushButton(button)
{
	buttonArray[stackPointer]= button;
	stackPointer++;
	//alert('push '+stackPointer);
}

function popAllButtonsExcept(except)
{
	if (stackPointer>0)
	for(i=stackPointer-1; i>=0; i--)
		if (buttonArray[stackPointer-1]!=except)
		buttonArray[stackPointer-1].className='btn1';
	stackPointer=0;
}









