var NS3  = false;
var ver4 = false;
var IE4  = false;
var NS4  = false;
var MAC  = false;

function showMenu( a )    { return true; }
function hideMenu( a )    { return true; }
function setImage( a, b ) { return true; }
function mkattr( aname, avalue ) {
	var attribute = aname + '="' + avalue + '"';
	return( attribute );
}

function mkbutton( href, menuname, img, imgname, imgw, imgh, linkTitle) {
	if ( ver4 && ! ( MAC && IE4 ) ) {
		var output = '<a ';
		output    += mkattr("href",href)                              + ' ';
		output    += mkattr("onMouseOver","showMenu('"+menuname+"')") + ' ';
		output    += mkattr("onMouseOut","hideMenu('"+menuname+"')")  + ' ';
		output    += '>';
		output    += '<img ';
		output    += mkattr("name",imgname) + ' ';
		output    += mkattr("src",img)      + ' ';
		output    += mkattr("width",imgw)   + ' ';
		output    += mkattr("height",imgh)  + ' ';
		output    += mkattr("border",0)     + ' ';
		output    += '>' + linkTitle + '</a>';
		document.write( output );
	}
}

function mkbutton2( href, menuname, linkTitle) {
	if ( ver4 && ! ( MAC && IE4 ) ) {
		var output = '<a ';
		output    += mkattr("href",href)                              + ' ';
		output    += mkattr("onMouseOver","showMenu('"+menuname+"',1)") + ' ';
		output    += mkattr("onMouseOut","hideMenu('"+menuname+"')")  + ' ';
		output    += '>';
		output    += linkTitle + '</a>';
		document.write( output );
	}
}

function mkbutton3( href, menuname, linkTitle) {
	if ( ver4 && ! ( MAC && IE4 ) ) {
		var output = '<a ';
		output    += mkattr("href",href)                              + ' ';
		output    += mkattr("onMouseOver","showMenu('"+menuname+"',1,1)") + ' ';
		output    += mkattr("onMouseOut","hideMenu('"+menuname+"')")  + ' ';
		output    += '>';
		output    += linkTitle + '</a>';
		document.write( output );
	}
}


if ( ! versions_loaded ) {

    // Determine Browser Version
    var bV   = parseInt( navigator.appVersion );
    var NS4  = (document.layers)         ? true : false;
    var NS3  = (document.images)         ? true : false;
    var IE4  = ((document.all)&&(bV>=4)) ? true : false;
    var ver4 = (NS4 || IE4)              ? true : false;

    // Determine Browser Platform
    var MAC   = (navigator.userAgent.indexOf("Mac")      != -1) ? true : false;
    var Opera = (navigator.userAgent.indexOf("Opera")    != -1) ? true : false;
    var IE5   = (navigator.userAgent.indexOf("MSIE 5.0") != -1) ? true : false;

    var versions_loaded = true;
}

var cached_images = new Object();
var allmenus    = new Array(); // tracks all created menus for later printing.
allmenus.length = 0;
var timers     = new Object();

if ( ! Opera ) {
    new MenuItem( "", "" );
    new Menu( 'discard' );
    Menu.prototype.width             = 110;
    Menu.prototype.height            = 10;
    Menu.prototype.imagename         = null;
    Menu.prototype.borderstyle       = "none";
    Menu.prototype.bordercolor       = "#E1DDFF";
    Menu.prototype.bgcolor           = "#E1DDFF";
    Menu.prototype.bottomshadow      = "#E1DDFF";
    Menu.prototype.topshadow         = "#E1DDFF";
    Menu.prototype.leftOffset        = 100;
    Menu.prototype.topOffset         = 100;
    Menu.prototype.padding           = 0;
    Menu.prototype.delay             = 50;
    Menu.prototype.rollover          = false;
    Menu.prototype.leftimage         = null;
    Menu.prototype.leftimagewidth    = 50;
    Menu.prototype.leftimagecolor    = '';
    Menu.prototype.addMenuItem       = Menu_addMenuItem;
    Menu.prototype.addMenuSeparator  = Menu_addMenuSeparator;
    Menu.prototype.getHeight         = Menu_getHeight;
    Menu.prototype.print             = Menu_print;
}


function writeMenus(aNumber) {
    if ( ! ( MAC && IE4 ) ) {
        document.write( '<style type="text/css">.ver4hide{display:none;visibility:hidden;}</style>\n' );
    }
    
    Menu.prototype.leftOffset  = 0;
    Menu.prototype.topOffset   = 0;
    Menu.prototype.width       = 100;
    Menu.prototype.bgcolor     = "#E1DDFF";
    Menu.prototype.padding     = 10;
    Menu.prototype.borderstyle = "none";
    Menu.prototype.borderwidth = 1;
    
	var student_menu = new Menu('students','students_img');
	student_menu.addMenuItem("ITS services for current and prospective students","");
	student_menu.print();
	
	var facultystaff_menu = new Menu('facultystaff','facultystaff_img');
	facultystaff_menu.addMenuItem("ITS services for faculty and staff","");
	facultystaff_menu.print();
	
	var disabilities_menu = new Menu('disabilities','disabilities_img');
	disabilities_menu.addMenuItem("TTY, and special computing and phone services","");
	disabilities_menu.print();

	var affiliates_menu = new Menu('affiliates','affiliates_img');
	affiliates_menu.addMenuItem("Information for liaisons and network managers","");
	affiliates_menu.print();

	var support_menu = new Menu('support','support_img');
	support_menu.addMenuItem("Online help, service centers, alerts, downloads","");
	support_menu.print(1);

	var training_menu = new Menu('training','training_img');
	training_menu.addMenuItem("Phone and computing classes, online learning","");
	training_menu.print(1);
	
	var facilities_menu = new Menu('facilities','facilities_img');
	facilities_menu.addMenuItem("Labs, kiosks, classrooms, other resources","");
	facilities_menu.print(1);
	
	var about_menu = new Menu('about','about_img');
	about_menu.addMenuItem("Mission, projects, organization, jobs","");
	about_menu.print(1);
	
	var news_menu = new Menu('news','news_img');
	news_menu.addMenuItem("ITS Updates, Digit, other publications","");
	news_menu.print(1);
	
	var search_menu = new Menu('search','search_img');
	search_menu.addMenuItem("Search engine and site index","");
	search_menu.print(1);
}

function replaceString( string, replacement ) {
    var idx = string.indexOf( "%s" );
    var newstring = string;
    if ( idx >= 0 ) {
	newstring = string.substring( 0, idx ) + replacement + string.substring( idx + 2 );
    }
    return( newstring );
}

function getInsideWindowWidth() {
    var width = -1;
    if ( NS4 )
	width = window.innerWidth;
    else if ( IE4 )
	width = document.body.clientWidth;
    return( width );
}

function getInsideWindowHeight() {
    var height = -1;
    if ( NS4 )
	height = window.innerHeight;
    else if ( IE4 )
	height = document.body.clientHeight;
    return( height );
}

function getWindowLeft() {
    var x = -1;
    if ( NS4 )
	x = window.pageXOffset;
    else if ( IE4 )
	x = document.body.scrollLeft;
    return( x );
}

function getWindowTop() {
    var y = -1;
    if ( NS4 )
	y = window.pageYOffset;
    else if ( IE4 )
	y = document.body.scrollTop;
    return( y );
}

function getObject( objectName, container ) {
    var object = null;
    if ( typeof(objectName) != "string" ) {
	object = objectName;
    } else if ( ver4 ) {
	if ( ! container )
	    container = document;
	if ( IE4 ) {
	    if ( container.all[objectName] )
		object = container.all[objectName];
	} else if ( NS4 ) {
	    if ( container.anchors[objectName] )
		object = container.anchors[objectName];
	    else if ( container.forms[objectName] )
		object = container.forms[objectName];
	    else if ( container.images[objectName] )
		object = container.images[objectName];
	    else if ( container.layers[objectName] )
		object = container.layers[objectName];
	}
    }
    return( object );
}
		 
function findObjectContainer( objectName, container ) {
    var objectContainer = null;
    if ( NS4 ) {
	if ( ! container ) container = window.document;
	if ( getObject(objectName,container) ) {
	    objectContainer = container;
	} else {
	    var layers = getLayers( new RegExp(".*"), container );
	    for ( var i = 0; i < layers.length; i++ ) {
		objectContainer = findObjectContainer( objectName, layers[i].document );
		if ( objectContainer ) break;
	    }
	}
    }
    else if ( IE4 ) {
	objectContainer = getObject(objectName,document) ? document : null;
    }
    return( objectContainer );
}

function findObject( objectName ) {
    var object = null;
    if ( ver4 ) {
	var container = findObjectContainer( objectName );
	if ( container ) object = getObject( objectName, container );
    }
    return( object );
}

function getLayer( name ) {
    var obj = null;
    if ( IE4 ) {
	obj = document.all[name];
    } else if ( NS4 ) {
	obj = document.layers[name];
    }
    return( obj );
}

function getLayers( regex, doc ) {
    var layers = new Array();
    layers.length = 0;
    if ( ver4 ) {
	if ( ! doc ) doc = document;
	if ( NS4 ) {
	    for ( var i = 0; i < doc.layers.length; i++ ) {
		var obj = doc.layers[i];
		if ( ! regex || regex.exec(obj.id) ) {
		    layers[layers.length] = obj;
		}
	    }
	} else if ( IE4 ) {
	    var divColl = doc.all.tags("DIV");
	    for ( i = 0; i < divColl.length; i++ ) {
		if ( ! regex || regex.exec(divColl[i].id) ) {
		    layers[layers.length] = divColl[i];
		}
	    }
	}
    }
    return( layers );
}

function isHidden( obj ) {
    if ( IE4 ) {
	return( obj.style.visibility == "hidden" );
    } else if ( NS4 ) {
	return( obj.visibility == "hide" );
    } else {
	return( false );
    }
}

function hideLayer( obj ) {
    if ( IE4 )
	obj.style.visibility = "hidden";
    else if ( NS4 )
	obj.visibility = "hide";
}

function showLayer( obj ) {
    if ( IE4 ) {
	obj.style.visibility = "visible";
    } else if ( NS4 ) {
	obj.visibility = "show";
    }
}

function getObjTop( obj ) {
    if ( ver4 )
	return( IE4 ? obj.style.pixelTop  : obj.top );
    else
	return( 0 );
}

function getObjLeft( obj ) {
    if ( ver4 )
	return( IE4 ? obj.style.pixelLeft : obj.left );
    else
	return( 0 );
}

function getObjHeight( obj ) {
    if ( ver4 )
	return( IE4 ? obj.clientHeight : (obj.clip ? obj.clip.height : -1 ) );
    else
	return( 0 );
}

function getObjWidth( obj ) {
    if ( ver4 )
	return( IE4 ? obj.clientWidth  : (obj.clip ? obj.clip.width : -1 ) );
    else
	return( 0 );
}

function moveObjTo( obj, x, y, yesIfHidden ) {
    if ( ! isHidden(obj) || yesIfHidden ) {
	if ( IE4 ) {
	    if ( x >= 0 ) obj.style.pixelLeft = x;
	    if ( y >= 0 ) obj.style.pixelTop  = y;
	} else if ( NS4 ) {
	    if ( x < 0 ) x = getObjLeft( obj );
	    if ( y < 0 ) y = getObjTop( obj );
	    obj.moveTo( x, y );
	}
    }
    return( 1 );
}

function getImage( imgNm ) {
    return( findObject(imgNm) );
}

function getImageContainer( imgNm ) {
    return( findObjectContainer(imgNm) );
}

function getParentObject( obj ) {
    var parent = null;
    if ( IE4 ) {
	if ( obj.offsetParent )
	    parent = obj.offsetParent;
    } else if ( NS4 ) {
	if ( obj.parentLayer ) {
	    parent = obj.parentLayer || null;
	} else {
	    parent = findObjectContainer( obj );
	    if ( parent ) parent = parent.parentWindow;
	}
    }
    return( parent );
}

function findOffsetValue( obj, value ) {
    var offset = 0;
    if ( ver4 ) {
	if ( NS4 ) {
	    if ( value == "offsetLeft" )
		value = "pageX";
	    else if ( value == "offsetTop" )
		value = "pageY";
	}
	var parent = getParentObject( obj );
	while ( parent ) {
	    offset += ( parent[value] || 0 );
	    parent = getParentObject( parent );
	}
    }
    return( offset || 0 );
}

function getImageLeft( img ) {
    if ( typeof(img) == "string" ) img = getImage( img );
    var left   = 0;
    var offset = findOffsetValue( img, "offsetLeft" );
    if ( NS4 ) {
	left = img.x;
    } else if ( IE4 ) {
	left = img.offsetLeft;
    }
    return( left + offset );
}

function getImageTop( img ) {
    if ( typeof(img) == "string" ) img = getImage( img );
    var top    = 0;
    var offset = findOffsetValue( img, "offsetTop" );
    if ( NS4 ) {
	top = img.y;
    } else if ( IE4 ) {
	top = img.offsetTop;
    }
    return( top + offset );
}

function getImageHeight( img ) {
    if ( typeof(img) == "string" ) img = getImage( img );
    return( img.height );
}

function getImageWidth( img ) {
    if ( typeof(img) == "string" ) img = getImage( img );
    return( img.width );
}

function MenuItem( miname, mivalue ) {
    if ( ! Opera ) {
	this.name  = miname;
	this.value = mivalue;
	this.bold  = false;
	return( this );
    }
}

    
function getMenuImage( menuname ) {
    var imgname = null;
    if ( allmenus[menuname] )
	imgname = allmenus[menuname].imagename;
    return( imgname );
}

function Menu( mname, _imagename ) {
	if (ver4) {
    // Set up properties for this instance
    this.name                   = mname;
    this.imagename              = _imagename;
    Menu.prototype.items        = new Array();
    Menu.prototype.items.length = 0;
    // If the _width parameter isn't supplied, make an attempt
    // to determine the menu width based on the image's width.
    var imgWidth      = null;
    if ( _imagename ) {
	var img  = getImage( _imagename );
	if ( img ) {
	    imgWidth = getImageWidth( img );
	}
    }
    if ( imgWidth ) this.width = imgWidth;
    // If borderstyle is "image", make sure leftimage, leftimagewidth and
    // leftimagecolor are specified
    if ( this.borderstyle == "image" ) {
	if ( ! this.leftimagewidth || ! this.leftimage ) {
	    this.borderstyle = "solid";
	    this.leftimage = null;
	}
	else if ( ! this.leftimagecolor ) {
	    this.leftimagecolor = "#ffffff";
	}
    }
    // Add the menu to the list of menus if it's not the prototype
    if ( mname != "discard" ) {
	allmenus[allmenus.length++] = this;
	allmenus[mname]             = this;
    }
    return( this );
   	}
}

function Menu_addMenuItem( miname, mivalue ) {
    var newitem      = new MenuItem( miname, mivalue );
    newitem.index    = this.items.length;
    newitem.menuname = this.name;
    this.items[this.items.length++] = newitem;
    return( newitem );
}


function Menu_addMenuSeparator( hidden ) {
    var newitem = new MenuItem( "separator", "---" );
    newitem.hidden = hidden;
    this.items[this.items.length++] = newitem;
    return( newitem );
}

function Menu_print(aSwitch) {
    var menu = this;
    // write the header
    var output = "";
    if ( NS4 || ( IE4 && ! MAC ) ) {

	if ( navigator.appName == "Microsoft Internet Explorer" ) {
	    output += '<div id="' + menu.name + '" ';
	    output += 'onMouseOver="showMenu(\'' + menu.name + '\')" ';
	    output += 'onMouseOut="hideMenu(\'' + menu.name + '\')" ';
	    output += 'style="position:absolute;z-index:59600;visibility:hidden;';
	    output += 'width:' + menu.width + 'px;height:' + menu.height + ';"' ;
	    output += '>';
	}
	else {
	    output += '<layer name="' + menu.name + '" ';
	    output += 'onMouseOver="showMenu(\'' + menu.name + '\')" ';
	    output += 'onMouseOut="hideMenu(\'' + menu.name + '\')" ';
	    output += 'width="' + menu.width + '" height="' + menu.height + '" ';
	    output += 'visibility="hidden" zIndex="59000"';
	    output += '>';
	}

	    if (aSwitch) {
		    output += '<table border=0 cellspacing=0 cellpadding=0 width="94" ><tr><td width="10" align="left" valign="top" bgcolor="#FFFFFF"></td><td colspan="3" align="left" valign="top"><img src="images/alert_top2.gif" width="102" height="8"></td></tr><tr><td width="10"><img src="images/clear.gif" border=0 height=1 width=3></td><td width="1" bgcolor="#000000"><img src="images/clear.gif" border=0 height=1 width=1></td><td valign=TOP width="100" bgcolor="#E1DDFF"><center><img src="images/arrowup.gif" width="12" height="8" align="top" vspace="4"><br><table width="100" border="0" cellspacing="0" cellpadding="0" bgcolor="#E1DDFF"><tr valign="top"><td width="6">&nbsp;&nbsp;</td><td width="93">';	    
	    }
	    else {
		    output += '<table border=0 cellspacing=0 cellpadding=0 width="94" ><tr><td width="10" align="left" valign="top" bgcolor="#FFFFFF"></td><td colspan="3" align="left" valign="top"><img src="images/alert_top2.gif" width="102" height="8"></td></tr><tr><td width="10"><img src="images/clear.gif" border=0 height=1 width=3></td><td width="1" bgcolor="#000000"><img src="images/clear.gif" border=0 height=1 width=1></td><td valign=TOP width="100"><table width="100" border="0" cellspacing="0" cellpadding="0" bgcolor="#E1DDFF"><tr valign="top"><td width="6">&nbsp;&nbsp;</td><td width="73">';
		}
	
	// now add all the menu items
	for ( var j = 0; j < menu.items.length; j++ ) {
	    var mi = menu.items[j];
	    if ( mi.name == "separator" ) {
		if ( mi.hidden ) {
		    output += "<br>";
		}
		else {
		    output +=  '<hr class="eng" size="1">' ;
		}
	    }
	    else {
		output += makeMenuItem( mi, false );
	    }
	}
	
    if (aSwitch) {
    	output+='</td></tr></table></td><td width="1" bgcolor="#000000"><img src="images/clear.gif" border=0 height=1 width=1></td></tr><tr><td valign=TOP width="10">&nbsp;</td><td valign=TOP colspan="3"><img src="images/alert_bot2.gif" border=0 height=8 width=102></td></tr></table>';	    
    }
    else {
    	output+='</td><td width="21" align="right"><font face="Verdana, Arial, Helvetica, sans-serif" size="-1"><img src="pixel.gif" width="1" height="12"><br><img src="images/arrowright.gif" width="8" height="12" align="top" hspace="4"></font></td></tr></table></td><td width="1" bgcolor="#000000"><img src="images/clear.gif" border=0 height=1 width=1></td></tr><tr><td valign=TOP width="10">&nbsp;</td><td valign=TOP colspan="3"><img src="images/alert_bot2.gif" border=0 height=8 width=102></td></tr></table>';
	}

	if ( navigator.appName == "Microsoft Internet Explorer" ) {
	    output += '</div>\n';
	}
	else {
	    output += '</layer>\n';
	}
	document.write( output );
    }
}

function Menu_getHeight() {
    var height = 0;
    var i;
    for ( i = 0; i < this.items.length; i++ ) {
	height += getObjHeight( this.items[i].name );
    }
    return( height );
}

function makeMenuItem( mi ) {
    var output = "";
    output += '<font face="Arial, Helvetica, sans-serif" size="-1">';
    if ( mi.bold ) output += '<b>';
    /*output += '<a href="' + mi.value + '" class="englink"';
    output += ' id="' + mi.menuname + mi.index + 'link"';
    output += ' onMouseOver="highlightLink(\'' + mi.menuname + '\',' + mi.index + ',true)"';
    output += ' onMouseOut="highlightLink(\'' + mi.menuname + '\',' + mi.index + ',false)"';
    output += '>';*/
    output +=  mi.name + '<!--</a>//--><br>' ;
    if ( mi.bold ) output += '</b>';
    output += '</font>';
    return( output );
}

function printMenus() {
    var i = 0;
    for ( ; i < allmenus.length; i++ ) {
	allmenus[i].print();
    }
    return( i );
}

function showMenu( menuName , switcher, anotherSwitcher) {
    var imgName = getMenuImage( menuName );

    if ( NS4 || ( IE4 && ! MAC ) ) {
	// Move the menu to a location near the image
	var layer = getLayer( menuName );
	if ( imgName && isHidden(layer) ) {
	    var img = getImage( imgName );
	    var x   = getImageLeft(img)-130;
	    var y   = getImageTop(img)-33;
	    if (switcher) {
	    	x += 75;
	    	y += 33;
	    }
	    if (anotherSwitcher) {
	    	y -= 9;
	    }
	    moveObjTo( layer, x, y, true );
	    // Now, adjust the bottom of the menu with respect to the bottom
	    // of the window to make sure the entire menu is displayed.
	    // This is done after the initial move so the height of the
	    // object can be properly determined.
	    var lbottom = getObjTop( layer ) + getObjHeight( layer );
	    var wbottom = getInsideWindowHeight();
	    var wtop    = getWindowTop();
	    if ( lbottom > ( wbottom + wtop )) {
		y -= ( lbottom - ( wbottom + wtop ) );
		if ( y < wtop ) y = wtop;
		moveObjTo( layer, x, y, true );
		
	    }
	}
	// Display the menu
	showLayer( layer );
	clearTimeout( timers[menuName] );
    }
}

function hideMenu( menuName ){
    var cmd = 'hideMenu_delayed("' + menuName + '")';
    var delay = 50;
    if ( allmenus[menuName] )
	delay = allmenus[menuName].delay;
    if ( ver4 )
	timers[menuName] = setTimeout( cmd, delay );
    else
	eval( cmd );
}

function hideMenu_delayed( menuName ) {
    var imgName = getMenuImage( menuName );

    if ( NS4 || ( IE4 && ! MAC ) )
	hideLayer( getLayer(menuName) );
}
