if ( typeof isIE6 == "undefined" ) var isIE6 = (navigator.userAgent.toLowerCase().indexOf('msie 6.0') != -1) ? true : false;

var menuBgColor = "#e6eaee";
var menuBgColorOver = "#ccd6e0";

var disableHide = false;

var lastLayer = ""; // keep the last open layer name
var layerId = 0;

var layerList = new Object();
var layerIdx  = 0;

var UBS_cursorX, UBS_cursorY;

// compatibility
if ( typeof isW3C == "undefined" ) var isW3C = (document.getElementById) ? true : false;
if ( typeof isIE4 == "undefined" ) var isIE4 = (document.all && !isW3C) ? true : false;
if ( typeof isNS4 == "undefined" ) var isNS4 = (document.layers && !isW3C) ? true : false;
if ( typeof isOpera == "undefined" ) var isOpera = (window.opera) ? true : false;

if (document.layers){
	document.captureEvents(Event.MOUSEMOVE);
}
document.onmousemove = UBS_mouseMovementHandler;

var activeDropdownMenu = new UBS_dropdownMenu(-1, -1, 0, 0);

function UBS_mouseMovementHandler( e ){
	var evt = (e) ? e : event;

	if (evt.pageX) {
		UBS_cursorX = evt.pageX;
		UBS_cursorY = evt.pageY;
	} else if (evt.clientX) {
		UBS_cursorX = document.body.scrollLeft + evt.clientX;
		UBS_cursorY = document.body.scrollTop + evt.clientY;
	}
	// If we do not do our own onmouseleave then the menu sometimes sticks around too long
	if (activeDropdownMenu.active 
		&& !activeDropdownMenu.menu.isInsidePoint(new UBS_point(UBS_cursorX, UBS_cursorY))) {
		// alert('hiding layer'+activeDropdownMenu.name);
		UBS_hideLayer(activeDropdownMenu.name, true);
	}
	
	if (!disableHide && !UBS_isInVisibleLayer(new UBS_point(UBS_cursorX, UBS_cursorY))) {
		UBS_hideAll();
	}
	flag=1
}

function UBS_seekLayerImage( doc, name )
{
	var theImg;
	for ( var i=0; i < doc.layers.length; i++ )
	{
		var laydoc = doc.layers[i].document;
		if ( laydoc.images && laydoc.images[ name ] ) return laydoc.images[ name ];

		// search sub layers as well
		if ( laydoc.layers.length > 0 ) theImg = UBS_seekLayerImage( laydoc, name );
	}
	return theImg;
}

function UBS_getImagePosition( imgName )
{
	var obj = document.images[imgName];
	if ( !obj && isNS4 ) obj = UBS_seekLayerImage( document, imgName );

	var coords = new Object();
	coords.x = UBS_getX( obj );
	coords.y = UBS_getY( obj );

	return coords;
}

function UBS_getX( obj )
{
	var x = 0;
	if ( obj )
	{
		if ( isNS4 ) x = obj.x;
		else x = ( obj.offsetParent == null ) ? obj.offsetLeft : obj.offsetLeft+UBS_getX( obj.offsetParent );
	}
	return parseInt( x );
}

function UBS_getY( obj )
{
	var y = 0;
	if ( obj )
	{
		if ( isNS4 ) y = obj.y;
		else y = ( obj.offsetParent == null ) ? obj.offsetTop : obj.offsetTop+UBS_getY(obj.offsetParent);
	}
	return parseInt( y );
}

// compatibility
function UBS_updateLayerPosition( layerName, imgName )
{
	return UBS_setLayer( layerName, imgName );
}

function UBS_writeLayer( layerName, html, x, y, isVisible )
{
	if ( isNS4 )
	{
		var obj = document.layers[layerName];
		if ( html != "" )
		{
			obj.document.open();
			obj.document.write(html);
			obj.document.close();
		}
		obj.x = x;
		obj.y = y;
		if ( isVisible ) obj.visibility = "show";
	}
	else
	{
		var obj = ( isIE4 ) ? document.all[layerName] : document.getElementById(layerName);
		obj.align = "left"; // override td alignment
		if ( html != "" ) obj.innerHTML = html;
		obj.style.left = x + 'px';
		obj.style.top  = y + 'px';
		if ( isVisible ) obj.style.visibility = "visible";
	}
}

function UBS_setLayer(layerName,imgName,adjustX,adjustY,id)
{
	// set default values
	if ( typeof adjustX == "undefined" ) adjustX = 0;
	if ( typeof adjustY == "undefined" ) adjustY = 6;
	
	var html = "";
	var hideLayer = true;
	
	var isDropdownLayer = ( layerName.substring(0,10) == "LeftColumn" || layerName.substring(0,11) == "RightColumn" );

	if (!isDropdownLayer && layerName == lastLayer) return;
	
	// special handling for homepage column layers
	if ( layerName == "copyrightsignlayer" )
	{
		hideLayer = false;
	}
	else if ( isDropdownLayer )
	{
		// get layer height: calculate position to open layer "upwards"
		var coordsTop    = UBS_getImagePosition("t"+id);
		var coordsBottom = UBS_getImagePosition("b"+id);
		var height = coordsBottom.y - coordsTop.y;
		var border = ( isNS4 ) ? 12 : 6;
		if ( height > 0 ) adjustY -= ( height + border ); // add border
	}

	// check list for layers to hide
	if ( hideLayer )
	{
		var isFound=false;
		for ( var i in layerList )
		{
			if (layerList[i].name==layerName)
			{
				isFound=true; break;
			}
		}
		if ( !isFound )
		{
			var obj=new Object();
			obj.name=layerName;
			layerList[layerIdx++]=obj;
		}
	}

	if ( lastLayer == layerName )
	{
		UBS_hideAll(layerName);
	}
	else
	{
		disableHide = false;
		UBS_hideAll();
	}

	var coords = UBS_getImagePosition(imgName);
	var x = coords.x + adjustX;
	var y = coords.y + adjustY;

	// check if layer can be opened "upwards"
	if ( isDropdownLayer )
	{
		var top = ( document.all ) ? document.body.scrollTop : window.pageYOffset;

		if ( y < top || adjustY == 0 ) y = coords.y + 18; // open layer downwards
		
		activeDropdownMenu.x = x;
		activeDropdownMenu.y = y;
		activeDropdownMenu.w = 150;
		activeDropdownMenu.h = height;
		activeDropdownMenu.name = layerName;
		activeDropdownMenu.menu = new UBS_rectangle(x, y, 150, height);
		activeDropdownMenu.numOverlappedFlashImages = 0;
		activeDropdownMenu.active = true;
		// alert(activeDropdownMenu.print());

		// RRL:  If Opera hide overlapped flash images
		if (isOpera) {
			UBS_hideOverlappedFlashImages(x, y, height);
		}
	}

	UBS_writeLayer( layerName, html, x, y, true );

	if ( hideLayer )
	{

		if ( isW3C || isIE4 )
		{
			disableHide = true;
			setTimeout( "UBS_enableHide(" + ++layerId + ")", 2000 );
		}
	}

	lastLayer = layerName;
	
	return false;
}

function UBS_setLayerVisiblity( layerName, status )
{
	if ( isNS4 )
	{
		document.layers[layerName].visibility = ( status == "hidden" ) ? "hide" : "show";
	}
	else
	{
		var obj = (isIE4) ? document.all[layerName] : document.getElementById(layerName);
		if ( !obj ) return;

		obj.style.visibility = status;
	}
}

function UBS_hideLayer( layerName , onfocusout)
{
	var cursor = new UBS_point(UBS_cursorX, UBS_cursorY);

	if ( disableHide ) return;

	if (activeDropdownMenu.active && activeDropdownMenu.menu.isInsidePoint(cursor) && !onfocusout) return;

	UBS_setLayerVisiblity( layerName, "hidden" );
	// RRL:  If Opera make visible any flash images hidden due to overlapping menus
	if (isOpera) {
		UBS_displayAllFlashImages();
	}
	activeDropdownMenu.active = false;
	if (lastLayer == layerName) {
		lastLayer = "";
	}
}

function UBS_hideAll( layerToIgnore )
{
	if (!disableHide) {
		for ( var i in layerList )
		{
			var layerName = layerList[i].name;
			if ( layerName != layerToIgnore ) UBS_hideLayer(layerName);
		}
		disableHide = true;
	}
}

function UBS_tableHTML( layerObj )
{
	var html = "<table cellspacing=\"0\" cellpadding=\"0\" border=\"0\" width=\"136\">";
	// ns4 bug: first row is not displayed
	if (isNS4) html += "<tr><td><img src=\"/0.gif\" alt=\"\"></td></tr>";

	for (var i in layerObj.links)
	{
		var text = layerObj.links[i].text;
		var link = layerObj.links[i].link;
		var bold = layerObj.links[i].bold ? "bold":"";

		if (link=="")
		{
			html += "<tr bgcolor=\""+menuBgColor+"\">";
			html += "<td><p class=\"metamenuitem\"><span class=\"menutext"+bold+"\">"+text+"</span></p></td>";
			html += "</tr>";
		}
		else
		{
			html += "<tr bgcolor=\""+menuBgColor+"\" onmouseover=\"this.bgColor='"+menuBgColorOver+"';\" onmouseout=\"this.bgColor='"+menuBgColor+"';\">";
			html += "<td><p class=\"metamenuitem\"><a href=\""+link+"\" class=\"menutext"+bold+"\">"+text+"</a></p></td>";
			html += "</tr>";
		}
	}
	html += "</table>";

	return html;
}

function UBS_layerHTML( layerObj )
{
	layerList[layerIdx++] = layerObj;

	var layerName = layerObj.name;
	var html = "";

	if ( layerName == "coverlayer" ) return "";

	if ( isNS4 )
	{
		html = "<layer name=\""+layerName+"\" bgcolor=\"#e6eaee\" z-index=\"15\" visibility=\"hidden\">"+UBS_tableHTML(layerObj)+"</layer>";
	}
	else
	{
		var style = "font-family:arial,helvetica,sans-serif;font-size:11px;color:#003366;position:absolute;background-color:#e6eaee;border:1px solid #ccd6e0;visibility:hidden;z-index:15;";

		html = "<div id=\""+layerName+"\" style=\""+style+"\">"+UBS_tableHTML(layerObj)+"</div>";
	}

	return html;
}

function UBS_isInVisibleLayer( point ) 
{

	var layerName;
	var visible;
	var result;
	var obj;
	
	for ( var i in layerList ) {
		layerName = layerList[i].name;

		visible = UBS_isLayerVisible(layerName);
		if (visible) {
		
			if ( isNS4) {
				obj = document.layers[layerName];
			} else {
				obj = (isIE4) ? document.all[layerName] : document.getElementById(layerName);
			}
			if (obj) {
				result = (point.x >= obj.offsetLeft && 
					point.x <= obj.offsetLeft + obj.offsetWidth && 
					point.y >= obj.offsetTop && 
					point.y <= obj.offsetTop + obj.offsetHeight);
				
				if (result) 
					return result;
			}
		}
	}
	
	return false;
}

function UBS_isLayerVisible(layerName) {

	var visible;
	var obj;
	
	if ( isNS4) {
		obj = document.layers[layerName];
		if ( obj ) {
			visible = (obj.visibility == "show");
		} else {
			visible = false;
		}
	} else {
		obj = (isIE4) ? document.all[layerName] : document.getElementById(layerName);
		if ( obj ) {
			visible = (obj.style.visibility == "visible");
		} else {
			visible = false;
		}
	}
	
	return visible;
}

function UBS_buildLinks( str , sep )
{
	var pairs = str.split("|");
	var links = new Object();
	var c = ",";
	if (sep) 
	{
		c = sep;
	}
	else
	{
	// compatibility: check splitting character
		c = (str.indexOf("¦")>0) ? "¦" : ",";
	}
	for (var i in pairs)
	{
		var values = pairs[i].split(c);

		links[i] = new Object();
		links[i].text = values[0];
		links[i].link = values[1];
		if (values[2]) links[i].bold = values[2];
	}
	
	return links;
}

function UBS_enableHide(id)
{	
	if ( layerId == id) {
		disableHide = false;
		if (!UBS_isInVisibleLayer(new UBS_point(UBS_cursorX, UBS_cursorY))) {
			UBS_hideAll();
		}
	}
}

function UBS_hideOverlappedFlashImages(x, y, h) {
	var menuWidth = 150;
	var menu = new UBS_rectangle(x, y, menuWidth, h);
	// alert('Menu:\n' + menu.print());
	var docObjects = document.getElementsByTagName("OBJECT");
	for (var i = 0; i < docObjects.length; i++) {
		var object = docObjects[i];
		var c = object.getAttribute('classid');
		if (c == 'clsid:D27CDB6E-AE6D-11cf-96B8-444553540000'){
			var parent = object.parentNode;
			var x = UBS_getX(object);
			// Object has wrong y in Opera, so we must use y of parent DIV
			// adjusted by any top padding
			var p = parseInt(document.defaultView.getComputedStyle(parent, "").getPropertyValue('padding-top'));
			var y = UBS_getY(parent) + p;
			var w = parseInt(object.getAttribute('width'));
			var h = parseInt(object.getAttribute('height'));
			var flash = new UBS_rectangle(x, y, w, h);
			// alert('Flash:\n' + flash.print());
			var overlap = menu.isOverlapping(flash) || flash.isOverlapping(menu);
			// alert(overlap);
			if (overlap) {
				activeDropdownMenu.overlappedFlashImages[activeDropdownMenu.numOverlappedFlashImages++] = object;
				object.style.visibility = "hidden";
			}
		}
	}
	// alert(activeDropdownMenu.print());
}

function UBS_displayAllFlashImages() {
	var docObjects = document.getElementsByTagName("OBJECT");
	for (var i = 0; i < activeDropdownMenu.numOverlappedFlashImages; i++) {
		activeDropdownMenu.overlappedFlashImages[i].style.visibility = "visible";
	}
}

function UBS_point(x, y) {
	this.x = x;
	this.y = y;
	this.print = UBS_point_print;
}

function UBS_line(p1, p2) {
	this.p1 = p1;
	this.p2 = p2;
	this.print = UBS_line_print;
}

function UBS_point_print(){
	var p = "(" + this.x + "," + this.y +")";
	return p;
}

function UBS_line_print() {
	var p = "L[" + this.p1.print() + "-" + this.p2.print() + "]";
	return p;
}

function UBS_rectangle(x, y, w, h) {
	this.upperLeft = new UBS_point(x, y);
	this.upperRight = new UBS_point(this.upperLeft.x + w, this.upperLeft.y);
	this.lowerLeft = new UBS_point(this.upperLeft.x, this.upperLeft.y + h);
	this.lowerRight = new UBS_point(this.upperRight.x, this.lowerLeft.y);
	this.print = UBS_rectangle_print;
	this.isInsidePoint = UBS_rectangle_isInsidePoint;
	this.isIntersectingLine = UBS_rectangle_isIntersectingLine;
	this.isOverlapping = UBS_rectangle_isOverlapping;
}

function UBS_rectangle_print() {
	var p = 'UL ' + this.upperLeft.print() + 
		'  UR ' + this.upperRight.print() + 
		'  LL ' + this.lowerLeft.print() + 
		'  LR ' + this.lowerRight.print();
	return p;
}
function UBS_rectangle_isInsidePoint(point) {
	var xBetweenLeftAndRight = this.upperLeft.x <= point.x && point.x <= this.upperRight.x;
	var yBetweenUpperAndLower = this.upperLeft.y <= point.y && point.y <= this.lowerLeft.y;
	return xBetweenLeftAndRight && yBetweenUpperAndLower;
}

function UBS_rectangle_isIntersectingLine(line) {
	var x = line.p1.x;
	var y = line.p1.y;
	var intersect = false;

	if (x == line.p2.x) {				// vertical line
		var ymax = (line.p1.y < line.p2.y) ? line.p2.y : line.p1.y;
		var ymin = (line.p1.y < line.p2.y) ? line.p1.y : line.p2.y;
		intersect = this.upperLeft.x <= x && x <= this.upperRight.x && (ymin <= this.lowerLeft.y && ymax >= this.upperLeft.y);
		// alert (line.print() + " intersects " + this.print() + "? " + intersect);
	} else if (y == line.p2.y) {	// horizontal line
		var xmax = (line.p1.x < line.p2.x) ? line.p2.x : line.p1.x;
		var xmin = (line.p1.x < line.p2.x) ? line.p1.x : line.p2.x;
		intersect = this.upperLeft.y <= y && y <= this.lowerLeft.y && (xmin <= this.lowerRight.x && xmax >= this.lowerLeft.x);
		// alert (line.print() + " intersects " + this.print() + "? " + intersect);
	} else {
		alert(this.line + "not horizontal or vertical line");
	}
	return intersect;
}

function UBS_rectangle_isOverlapping(rect) {
	var top = new UBS_line(rect.upperLeft, rect.upperRight);
	var right = new UBS_line(rect.upperRight, rect.lowerRight);
	var bottom = new UBS_line(rect.lowerLeft, rect.lowerRight);
	var left = new UBS_line(rect.upperLeft, rect.lowerLeft);
	var rectInMe = this.isIntersectingLine(top) ||
		this.isIntersectingLine(right) ||
		this.isIntersectingLine(bottom) ||
		this.isIntersectingLine(left);
	// alert(rect.print() + " overlaps " + this.print() + "? " + rectInMe);
	return rectInMe;
}

function UBS_dropdownMenu(x, y, w, h) {
	this.x = x;
	this.y = y;
	this.w = w;
	this.h = h;
	this.name = '';
	this.active = false;
	this.menu = new UBS_rectangle(x, y, w, h);
	this.overlappedFlashImages = new Array();
	this.numOverlappedFlashImages = 0;
	this.print = UBS_dropdownMenu_print;
}

function UBS_dropdownMenu_print() {
	var p = 'dropdown menu: x = '+this.x+', y = '+this.y+', w = '+this.w+', h = '+this.h+'\n'+
		'name = '+this.name+', menu rectangle = '+this.menu.print()+', active = '+this.active+'\n'+
		'numOverlappedFlashImages = '+this.numOverlappedFlashImages;
	return p;
}
