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;

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 (!disableHide && !UBS_isInVisibleLayer(new UBS_point(UBS_cursorX, UBS_cursorY))) {
		UBS_hideAll();
	}
}

function UBS_HideIfLinkClicked(e){
	var targ;

	if (!e) var e = event;
	if (e.target) targ = e.target;
	else if (e.srcElement) targ = e.srcElement;
	if (targ.nodeType == 3) // defeat Safari bug
	targ = targ.parentNode;
	if (targ.nodeName.toLowerCase() == 'a') {
		UBS_hideLayer(activeDropdownMenu.name, true);
	}
}

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
	}

	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 )
{
	if ( disableHide ) return;

	UBS_setLayerVisiblity( layerName, "hidden" );

	if (lastLayer == layerName) {
		lastLayer = "";
	}
}

function UBS_showLayer( layerName )
{
	UBS_setLayerVisiblity( layerName, "visible" );
}

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 )
{
	var pairs = str.split("|");
	var links = new Object();
	
	// compatibility: check splitting character
	var 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_point(x, y) {
	this.x = x;
	this.y = y;
}