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 layerList = new Object();
var layerIdx  = 0;

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

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;
		obj.style.top  = y;
		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" );

	// 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 )
	{
		// cover layer disable the clickability of the link on netscape 7
		// by delaying the show of this layer, the surfer is able to click on it
		setTimeout( "UBS_showLayer('coverlayer')", 1000 );

		if ( isW3C || isIE4 )
		{
			disableHide = true;
			setTimeout( "UBS_enableHide()", 1000 );
		}
	}

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

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

function UBS_hideAll( layerToIgnore )
{
	for ( var i in layerList )
	{
		var layerName = layerList[i].name;
		if ( layerName != layerToIgnore ) UBS_hideLayer(layerName);
	}
}

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=\"img/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 UBS_layerCover();

	if ( isNS4 )
	{
		html = "<layer name=\""+layerName+"\" bgcolor=\"#e6eaee\" z-index=\"15\" onmouseover=\"UBS_showLayer('"+layerName+"')\" onmouseout=\"UBS_hideLayer('"+layerName+"')\" 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+"\" onmouseover=\"UBS_showLayer('"+layerName+"')\" onmouseout=\"UBS_hideLayer('"+layerName+"')\" style=\""+style+"\">"+UBS_tableHTML(layerObj)+"</div>";
	}

	return html;
}

function UBS_layerCover()
{
	// special layer to hide layers (ie: bug when moving the mouse to fast)
	var html = "";

	if ( isNS4 )
	{
		var width  = document.width;
		var height = document.height;
		html = "<layer name=\"coverlayer\" onmouseover=\"UBS_hideAll()\" left=\"0\" top=\"0\" width=\""+width+"\" height=\""+height+"\" z-index=\"1\" visibility=\"hidden\"></layer>";
	}
	else
	{
		var width  = document.body.offsetWidth;
		var height = document.body.offsetHeight;
		html = "<div id=\"coverlayer\" onmouseover=\"UBS_hideAll()\" onmousemove=\"UBS_hideAll()\" style=\"position:absolute;visibility:hidden;width:"+width+";height:"+height+";left:0;top:0;z-index:1;\"></div>";
	}

	return html;
}

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()
{
	disableHide = false;
}

