/* **********************************************************************
 * MODULE	: dyn_menus.js
 * AUTHOR	: Brian P. King
 * CREATED	: May 3, 1999
 
* COMPATIBILITY: Requires JavaScript 1.1.
 * RCS		: $Id: dyn_menus.js,v 1.16 1999/08/23 16:42:39 bking Exp $
 * DESCRIPTION	: Implements dynamic pulldown menus.
 * COPYRIGHT    : Copyright (c) 1999 Symantec Corporation.
 * DISTRIBUTION : Freely redistributable.  Use at your own risk.                              
 * **********************************************************************/

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;
}

/* **********************************************************************
 * Create a MenuItem object.
 * **********************************************************************/

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

/* **********************************************************************
 * Create the Menu object.
 * **********************************************************************/
    
var allmenus    = new Array(); 
// tracks all created menus for later printing.
allmenus.length = 0;
    
function Menu( mname, _imagename ) {

    // Set up properties for this instance

    this.name         = mname;
    this.imagename    = _imagename;
    this.items        = new Array();
    this.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 && 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_createMenuItem( miname, mivalue ) {
    var newitem;
    newitem          = new MenuItem( miname, mivalue );
    newitem.index    = this.items.length;
    newitem.menuname = this.name;
    this.items[this.items.length++] = newitem;
    return( newitem );
}

function Menu_addMenuItem( miname, mivalue ) {
    var newitem;
    if (
	! this.maxitems
	|| this.spilltype == "columns"
	|| this.items.length < this.maxitems
	|| mivalue == "---submenu"
	) {
	newitem = this.createMenuItem( miname, mivalue );
    }
    else {
	if ( ! this.childmenu ) {
	    var cname = this.name + "_child";
	    var cimg  = this.name + "_child_img";
	    var cm = new Menu( cname, cimg );
	    this.childmenu = cm;
	    this.addSubMenu( cm, "/images/more.gif" );
	}
	newitem = this.childmenu.addMenuItem( miname, mivalue );
    }
    return( newitem );
}

function Menu_addMenuItemHeader( miname, mivalue ) {
    var newitem = this.addMenuItem( miname, mivalue );
    if ( newitem ) {
	newitem.bold = true;
    }
    return( newitem );
}

function Menu_addMenuSeparator( hidden ) {
    var newitem = this.addMenuItem( "separator", "---" );
    if ( newitem ) {
	newitem.hidden = hidden;
    }
    return( newitem );
}

function Menu_addSubMenu( menu, mimage ) {
    var newitem    = this.addMenuItem( menu.name, "---submenu" );
    if ( newitem ) {
	newitem.image       = mimage;
	newitem.menu        = menu;
	newitem.menu.parent = this;
    }
    return( newitem );
}

function Menu_setLocation() {
    var img = getImage( this.imagename );
    var x, y;
    if ( this.parent ) {
	var parent_layer = getLayer( this.parent.name );
	x = getObjLeft(parent_layer) + getObjWidth(parent_layer) + 1;
	y = getObjTop(parent_layer)  + getImageTop( img ) + this.topOffset + 1;
    }
    else {
	x = getImageLeft( img ) + this.leftOffset;
	y = getImageTop( img ) + getImageHeight( img ) + this.topOffset + 1;
    }
    moveObjTo( getLayer(this.name), x, y, true );
    if ( this.childmenu ) {
	this.childmenu.setLocation();
    }
}

function Menu_show() {

    if ( NS4 || ( IE4 && ! MAC ) ) {
	if ( this.parent ) this.parent.show();
	var menuName = this.name;
	if ( this.imagename )
	    setImage( this.imagename, "active" );

	// Move the menu to a location near the image

	var layer = getLayer( menuName );
	if ( this.imagename && isHidden(layer) ) {
	    this.setLocation();

    // Now, adjust the bottom of the menu with respect to the bottom
    // of the window to make sure the entire menu is displayed.

	    var y = getObjTop( layer );
	    var x = getObjLeft( layer );

    // adjust the top

	    var lbottom = y + getObjHeight( layer );
	    var wbottom = getInsideWindowHeight();
	    var wtop    = getWindowTop();
	    if ( lbottom > ( wbottom + wtop )) {
		y -= ( lbottom - ( wbottom + wtop ) );
	    }
	    if ( y < wtop )
		y = wtop;

    // adjust the left edge

	    var lright  = getObjLeft( layer ) + getObjWidth( layer );
	    var wright  = getInsideWindowWidth();
	    var wleft   = getWindowLeft();
	    if ( lright > ( wright + wleft ) ) {
		x -= lright - ( wright + wleft );
	    }
	    if ( x < wleft )
		x = wleft;

    // move the object to the new location

	    moveObjTo( layer, x, y, true );
	}

   // Display the menu
	showLayer( layer );
	clearTimeout( this.timeout );
    }
}

function Menu_hide( now ) {
    if ( NS4 || ( IE4 && ! MAC ) ) {
	if ( now ) {
	    if ( this.imagename )
		setImage( this.imagename, "inactive" );
	    hideLayer( getLayer(this.name) );
	}
	else {
	    var cmd      = 'allmenus["' + this.name + '"].hide(true)';
	    var delay    = this.delay;
	    this.timeout = setTimeout( cmd, delay );
	    if ( this.parent ) {
		this.parent.hide();
	    }
	}
    }
}

function Menu_print() {
    var menu = this;

    // write the header
    var output = "";
    if ( NS4 || ( IE4 && ! MAC ) ) {

   // Make sure we haven't already been printed

	if ( getLayer(this.name) ) return;
	if ( this.childmenu ) {
	    this.childmenu.print();
	}
	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 += '>\n';
	}
	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 += '>\n';
	}

	if ( menu.borderstyle == 'shadow' ) {
	    output += '<table border="0" cellpadding="0" cellspacing="0"';
	    output += ' width="' + menu.width + '" bgcolor="' + menu.bgcolor + '">\n';
	    output += '<tr height="1">\n';
	    output += '<td bgcolor="' + menu.topshadow + '" width="2" height="1">';
	    output += '<img src="/images/bg/spacer.gif" width="1" height="1"></td>\n';
	    output += '<td bgcolor="' + menu.topshadow + '" width="' + ( menu.width - 7 ) + '" height="1">';
	    output += '<img src="/images/bg/spacer.gif" width="1" height="1"></td>\n';
	    output += '<td bgcolor="' + menu.topshadow + '" width="5" height="1">';
	    output += '<img src="/images/bg/spacer.gif" width="1" height="1"></td>\n';
	    output += '</tr>\n';
	    output += '<tr height="' + ( menu.height - 10 ) + '">\n';
	    output += '<td bgcolor="' + menu.topshadow + '" width="4" height="' + ( menu.height - 10 ) + '">';
	    output += '<img src="/images/bg/spacer.gif" width="1" height="1"></td>\n';
	    output += '<td width="' + ( menu.width - 2 ) + '" height="' + ( menu.height - 10 ) + '" bgcolor="' + menu.bgcolor + '">';
	    output += '<table border="0" cellpadding="' + menu.padding + '" cellspacing="1"';
	    output += ' width="' + ( menu.width - 7 ) + '" height="' + ( menu.height - 10 ) + '">\n';
	    output += '<tr>\n';
	    output += '<td valign="top" >\n';
	} else if ( menu.borderstyle == 'image' ) {
	    output += '<table border="0" cellpadding="0" cellspacing="0"';
	    output += ' width="' + menu.width + '" bgcolor="' + menu.bgcolor + '">\n';
	    output += '<tr height="1">\n';
	    output += '<td bgcolor="' + menu.topshadow + '" width="1" height="1">';
	    output += '<img src="/images/bg/spacer.gif" width="1" height="1"></td>\n';
	    output += '<td bgcolor="' + menu.topshadow + '" width="' + menu.leftimagewidth + '" height="1">';
	    output += '<img src="/images/bg/spacer.gif" width="1" height="1"></td>\n';
	    output += '<td bgcolor="' + menu.topshadow + '" width="' + ( menu.width - 7 ) + '" height="1">';
	    output += '<img src="/images/bg/spacer.gif" width="1" height="1"></td>\n';
	    output += '<td bgcolor="' + menu.topshadow + '" width="5" height="1">';
	    output += '<img src="/images/bg/spacer.gif" width="1" height="1"></td>\n';
	    output += '</tr>\n';
	    output += '<tr height="' + ( menu.height - 10 ) + '">\n';
	    output += '<td bgcolor="' + menu.topshadow + '" width="4" height="' + ( menu.height - 10 ) + '">';
	    output += '<img src="/images/bg/spacer.gif" width="1" height="1"></td>\n';
	    output += '<td bgcolor="' + menu.leftimagecolor + '" background="' + menu.leftimage + '" width="' + menu.leftimagewidth + '" height="1">';
	    output += '<img src="/images/bg/spacer.gif" width="' + menu.leftimagewidth + '" height="1"></td>\n';
	    output += '<td width="' + ( menu.width - 2 ) + '" height="' + ( menu.height - 10 ) + '" bgcolor="' + menu.bgcolor + '">';
	    output += '<table border="0" cellpadding="' + menu.padding + '" cellspacing="1"';
	    output += ' width="' + ( menu.width - 7 ) + '" height="' + ( menu.height - 10 ) + '">\n';
	    output += '<tr>\n';
	    output += '<td valign="top" >\n';
	} else if ( menu.borderstyle == "solid" ) {
	    output += '<table border="0" cellpadding="0" cellspacing="0"';
	    output += ' width="' + menu.width + '" bgcolor="' + menu.bgcolor + '">\n';
	    output += '<tr height="1">\n';
	    output += '<td bgcolor="' + menu.bordercolor + '" height="' + menu.borderwidth + '" colspan="3">';
	    output += '<img src="/images/bg/spacer.gif" width="1" height="1"></td>\n';
	    output += '</tr>\n';
	    output += '<td bgcolor="' + menu.bordercolor + '" width="' + menu.borderwidth + '">';
	    output += '<img src="/images/bg/spacer.gif" width="1" height="1"></td>\n';
	    output += '<td valign="top" bgcolor="' + menu.bgcolor + '">\n';
	    output += '<table border="0" cellspacing="0" cellpadding="' + menu.padding + '">';
	    output += '<tr><td>';
	} else {
	    output += '<table border="0" cellpadding="' + menu.padding + '" cellspacing="0"';
	    output += ' width="' + menu.width + '" bgcolor="' + menu.bgcolor + '">';
	    output += '<tr><td valign="top">\n';
	}
	
    // now add all the menu items
	var j, c;
	for ( j = 0, c = 0; j < menu.items.length; j++, c++ ) {
	    if ( c > this.maxitems && this.spilltype == "columns" ) {
		output += '</td>\n<td valign="top">';
		c = 0;
		if ( menu.items[0].bold ) {
		    output += '&nbsp;<br>';
		    c++;
		}
	    }
	    var mi = menu.items[j];
	    if ( mi.name == "separator" ) {
		if ( mi.hidden ) {
		    output += "<br>";
		}
		else {
		    output +=  '<hr class="thin" size="1">' ;
		}
	    }
	    else {
		output += this.makeMenuItem( mi, false );
	    }
	    output += '\n';
	}
	
	if ( menu.borderstyle == 'shadow' ) {

     // and add in the footer
	    output += '</tr>\n';
	    output += '</table></td>\n';
	    output += '</td>\n';
	    output += '<td bgcolor="' + menu.bottomshadow + '" width="5" height="' + ( menu.height - 10 ) + '">';
	    output += '<img src="/images/bg/spacer.gif" width="1" height="1"></td>\n';
	    output += '</tr>\n';
	    output += '<tr height="3">\n';
	    output += '<td bgcolor="' + menu.topshadow + '" width="2" height="3">';
	    output += '<img src="/images/bg/spacer.gif" width="1" height="1"></td>\n';
	    output += '<td bgcolor="' + menu.bottomshadow + '" width="' + ( menu.width - 7 ) + '" height="3">';
	    output += '<img src="/images/bg/spacer.gif" width="1" height="1"></td>\n';
	    output += '<td bgcolor="' + menu.bottomshadow + '" width="3" height="3">' ;
	    output += '<img src="/images/bg/spacer.gif" width="1" height="1"></td>\n';
	    output += '</tr></table>\n';
	} else if ( menu.borderstyle == 'image' ) {

    // and add in the footer
	    output += '</tr>\n';
	    output += '</table></td>\n';
	    output += '</td>\n';
	    output += '<td bgcolor="' + menu.bottomshadow + '" width="5" height="' + ( menu.height - 10 ) + '">';
	    output += '<img src="/images/bg/spacer.gif" width="1" height="1"></td>\n';
	    output += '</tr>\n';
	    output += '<tr height="3">\n';
	    output += '<td bgcolor="' + menu.topshadow + '" width="2" height="3">';
	    output += '<img src="/images/bg/spacer.gif" width="1" height="1"></td>\n';
	    output += '<td bgcolor="' + menu.bottomshadow + '" width="' + menu.leftimagewidth + '" height="1">';
	    output += '<img src="/images/bg/spacer.gif" width="1" height="1"></td>\n';
	    output += '<td bgcolor="' + menu.bottomshadow + '" width="' + ( menu.width - 7 ) + '" height="3">';
	    output += '<img src="/images/bg/spacer.gif" width="1" height="1"></td>\n';
	    output += '<td bgcolor="' + menu.bottomshadow + '" width="3" height="3">' ;
	    output += '<img src="/images/bg/spacer.gif" width="1" height="1"></td>\n';
	    output += '</tr></table>\n';
	} else if ( menu.borderstyle == 'solid' ) {
	    output += '</td></tr></table></td>';
	    output += '<td bgcolor="' + menu.bordercolor + '" width="' + menu.borderwidth + '">';
	    output += '<img src="/images/bg/spacer.gif" width="1" height="1"></td></tr>\n';
	    output += '<tr height="1">';
	    output += '<td bgcolor="' + menu.bordercolor + '" height="' + menu.borderwidth + '" colspan="3">';
	    output += '<img src="/images/bg/spacer.gif" width="1" height="1"></td>\n';
	    output += '</tr></table>\n';
	} else {
	    output += '</td></tr></table>\n';
	}

	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 Menu_makeMenuItem( mi ) {
    var output = "";
    output += '<div class="AVIPdiv">';
    if ( mi.bold ) output += '<b>';
    if ( mi.value == "---submenu" ) {
	output += '<a href="#" onClick="return false"';
	output += ' onMouseOver="showMenu(' + "'" + mi.menu.name + "'" + ')"';
	output += ' onMouseOut="hideMenu('  + "'" + mi.menu.name + "'" + ')"';
	output += '>';
	output += '<img name="' + mi.menu.imagename + '" src="' + mi.image + '" border="0"></a><br>';
    }
    else {
	var label = mi.name;
	if ( this.nobreaks ) {
	    var t = "";
	    var start, end;
	    for ( start = 0, end = 0; (end = label.indexOf(" ",start)) != -1; ) {
		t += label.substring(start,end) + "&nbsp;";
		start = end + 1;
	    }
	    t += label.substring( start );
	    label = t;
	}
	output += '<a href="' + mi.value + '" class="AVIPlink"';
	output += ' id="' + mi.menuname + mi.index + 'link"';
	if ( IE4 ) {
		output += ' onMouseOver="highlightLink(\'' + mi.menuname + '\',' + mi.index + ',true)"';
		output += ' onMouseOut="highlightLink(\'' + mi.menuname + '\',' + mi.index + ',false)"';
	}
	output += '>';
	output +=  label + '</a><br>' ;
    }
    if ( mi.bold ) output += '</b>';
    output += '</div>';
    return( output );
}


/* **********************************************************************
 * Initialize the Menu object prototype.
 * **********************************************************************/

if ( ! Opera ) {
    new MenuItem( "", "" );
    new Menu( 'discard' );

    // Properties
    Menu.prototype.maxitems          = 0;
    Menu.prototype.childmenu         = null;
    Menu.prototype.width             = 160;
    Menu.prototype.height            = 8;
    Menu.prototype.imagename         = null;
    Menu.prototype.borderstyle       = "shadow";
    Menu.prototype.bordercolor       = "#000000";
    Menu.prototype.bgcolor           = "#FFFFF0";
    Menu.prototype.bottomshadow      = "#666666";
    Menu.prototype.topshadow         = "#cccccc";
    Menu.prototype.leftOffset        = 10;
    Menu.prototype.topOffset         = 1;
    Menu.prototype.padding           = 0;
    Menu.prototype.delay             = 50;
    Menu.prototype.rollover          = false;
    Menu.prototype.leftimage         = null;
    Menu.prototype.leftimagewidth    = 0;
    Menu.prototype.leftimagecolor    = '';
    Menu.prototype.spilltype         = "child";
    Menu.prototype.nobreaks          = 0;

    // Methods
    Menu.prototype.createMenuItem    = Menu_createMenuItem;
    Menu.prototype.addMenuItem       = Menu_addMenuItem;
    Menu.prototype.addMenuItemHeader = Menu_addMenuItemHeader;
    Menu.prototype.addMenuSeparator  = Menu_addMenuSeparator;
    Menu.prototype.addSubMenu        = Menu_addSubMenu;
    Menu.prototype.makeMenuItem      = Menu_makeMenuItem;
    Menu.prototype.getHeight         = Menu_getHeight;
    Menu.prototype.show              = Menu_show;
    Menu.prototype.hide              = Menu_hide;
    Menu.prototype.print             = Menu_print;
    Menu.prototype.setLocation       = Menu_setLocation;
}


/* **********************************************************************
 * The following functions operate the menus.
 * **********************************************************************/

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

function highlightLink( menuName, itemidx, highlight ) {
    if ( ver4 ) {
	var anchor = getImage( menuName + itemidx + "link" );
	if ( anchor && IE4 ) {
	    anchor.className = ( highlight ? 'AVIPlinkhi' : 'AVIPlink' );
	}
    }
    return( false );
}

function showMenu( menuName ) {
    if ( allmenus[menuName] ) allmenus[menuName].show();
}
   

function hideMenu( menuName ){
    if ( allmenus[menuName] ) allmenus[menuName].hide();
}
