var ValikkoRivi_Supported = false;
var Frame_Supported   = false;
var DoInstrumentation = false;

if (navigator.userAgent.indexOf("MSIE")    != -1 && 
	navigator.userAgent.indexOf("Windows") != -1 && 
	navigator.appVersion.substring(0,1) > 3)
{
	ValikkoRivi_Supported = true;
}

if (ValikkoRivi_Supported)
{
	var newLineChar = String.fromCharCode(10);
	var char34 = String.fromCharCode(34);
	var IsMenuDropDown = true;
	var HTMLStr;
	var x = 0;	
	var y = 0;
	var x2 = 0;
	var y2 = 0;
	var ValikkoRiviMinwidth;
	var ValikkoRiviMenu;
	var ValikkoRivibgcolor;
	var ValikkoRiviLoaded = false;
	var aDefOmaColor = new Array(3);
	var aCurOmaColor = new Array(3);
	var OmaFont;
	var arrMenuInfo = new Array(30);

	// Tulostetaan ValikkoRivin ID ja tyylissä estetään alleviivaus										
	document.write("<span ID='StartMenu' STYLE='display:none;'></span>");

	// Kootaan ValikkoRivi
	HTMLStr = 
		//"<DIV ID='idValikkoRivi' STYLE='background-color:#00404C;width:100%'>" +	// Sivun taustaväri
		"<DIV ID='idValikkoRivi' STYLE='background:TaustaJSR1.jpg;width:100%'>" +	// Sivun taustakuvio
		//"<DIV ID='idRivi1'	 STYLE='position:relative;height:0;'>" + "</DIV>" +	// tee muutos riville 257, jos otat käyttöön
		//"<DIV ID='idRivi2'	 STYLE='position:relative;left:0;height:40;'>" +	// tee muutos riville 257, jos otat käyttöön
		//"<DIV ID='idJSRBanner' STYLE='position:absolute;top:0;left:0;height:40;width:100;vertical-align:top;overflow:hidden;' align='left'>" + 
		//						 "<img src='JSR_logo2.gif' border=0</DIV>" +
		//"<DIV ID='idADSBanner' STYLE='position:absolute;top:0;left:0;height:40;width:200;vertical-align:top;overflow:hidden;'><!--BEG_ADS_BANNER--><!--END_ADS_BANNER--></DIV>" +
		//"</DIV>" +

		// ** Rivillä 3 valikkorivin määrittelly. Alasvetovalkon paikka lasketaan funtiossa "doMenu" noin rivillä 253
		"<DIV ID='idRivi3' STYLE='position:relative;height:20;width:100%'>" + 
		"<DIV ID='idOmaMenuPane' STYLE='position:absolute;top:0;left:0;height:20;background-color:black;' NOWRAP><!--Oma_MENU_TITLES--></DIV>" +
		"</DIV>" + "</DIV>" +
		"<SCRIPT type='text/javascript'>" + "   var ValikkoRiviMenu = StartMenu;" + "</script>" + 
		"<DIV width=100%>";

	window.onresize  = resizeValikkoRivi;	// Määritellään tapahtumakäsittelijä

	//	ValikkoRivibgcolor	= "white";		// ValikkoRivin taustaväri

	// kirjasimet ja värit
	OmaFont = "bold x-small MS Sans Serif"
	//OmaFont = "bold small Arial";		//OmaFont = "bold xx-small Verdana";	
	aDefOmaColor[0]	= aCurOmaColor[0] = "#00404C";		// taustaväri 
	aDefOmaColor[1] = aCurOmaColor[1] = "lightblue";	// tekstin väri
	aDefOmaColor[2] = aCurOmaColor[2] = "dodgerblue";	// tekstin väri hiiren ollessa tekstin päällä
}

function drawValikkoRivi()
{
	HTMLStr += "</DIV>";
	document.write(HTMLStr);
	ValikkoRiviLoaded = true;

	ValikkoRiviMinwidth = (250+2) 
//	ValikkoRiviMinwidth = (250+18) 

//	idValikkoRivi.style.backgroundColor = ValikkoRivibgcolor;
	idOmaMenuPane.style.backgroundColor = aDefOmaColor[0];
	resizeValikkoRivi();

	var i = 0;
	thisMenu = document.all(arrMenuInfo[i].IDStr);
	if (thisMenu != null) 	{
		if (arrMenuInfo[i].type == "R")	{
			arrMenuInfo[i].type = "A";
			arrMenuInfo[i].unit = 200;
		}
		if (arrMenuInfo[i].type == "A")
			thisMenu.style.width = arrMenuInfo[i].unit;
		else 
			thisMenu.style.width = Math.round(arrMenuInfo[i].width * arrMenuInfo[i].unit) + 'em';
}		} 

function resizeValikkoRivi()
{
	if (ValikkoRivi_Supported == false) return;

	w = Math.max(ValikkoRiviMinwidth, document.body.clientwidth) - ValikkoRiviMinwidth;	
	idOmaMenuPane.style.width = ValikkoRiviMinwidth + w;
}

function setSubMenuwidth(MenuIDStr, widthType, widthUnit) //OK
{
	var i=0;	
	arrMenuInfo[i] = new menuInfo(MenuIDStr);  
	arrMenuInfo[i].type = (widthType.toUpperCase().indexOf("ABSOLUTE") != -1)? "A" : "R";
	arrMenuInfo[i].unit = widthUnit;
} 

// This function creates a menuInfo object instance.
function menuInfo(MenuIDStr)
{
	this.IDStr = MenuIDStr;
	this.type  = "";
	this.unit  = 0;
	this.width = 0;
	this.count = 0;
} 

function addOmaMenu(MenuIDStr, MenuDisplayStr, MenuHelpStr, MenuURLStr)
{ 	
	if (addOmaMenu.arguments.length > 4)
		targetStr = addOmaMenu.arguments[4];
	else
		targetStr = "_top";
	tempID = "Oma_" + MenuIDStr;
	addMenu(tempID, MenuDisplayStr, MenuHelpStr, MenuURLStr, targetStr, true); 
}

function addMenu(MenuIDStr, MenuDisplayStr, MenuHelpStr, MenuURLStr, targetStr, bOmaMenu)
{
	cFont   = OmaFont; 
	cColor0 = aDefOmaColor[0];
	cColor1 = aDefOmaColor[1]; 
	cColor2 = aDefOmaColor[2]; 
	tagStr  = "<!--Oma_MENU_TITLES-->"

	// Tässä tehdään valikkorivi
	MenuStr = newLineChar;
	if (bOmaMenu == false)
		MenuStr += "<span STYLE='font:" + cFont + ";color:" + cColor1 + "'>|&nbsp;</span>"; 
	MenuStr += "<A target='" + targetStr + "' title='" + MenuHelpStr + "'" +
			   "   ID='AM_" + MenuIDStr + "'" +
			   "   STYLE='text-decoration:none;cursor:hand;font:" + cFont + ";background-color:" + cColor0 + 
			   ";color:" + cColor1 + ";'";
	if (MenuURLStr != "")	 MenuStr += " href='" + formatURL(MenuURLStr, ("Oma_" + MenuDisplayStr)) + "'"; 
	else	MenuStr += " href='' onclick='window.event.returnValue=false;'";

	MenuStr += 	" onMouseOut="  + char34 + "mouseMenu('out' ,'" + MenuIDStr + "'); hideMenu();" +
				"window.status=''; return true;" + char34 + 
				" onMouseOver=" + char34 + "mouseMenu('over','" + MenuIDStr + "'); doMenu('"+ MenuIDStr + "');" +
				"window.status=''; return true;" + char34 + ">" +	// Tähän tilarivin asiat
				"&nbsp;" + MenuDisplayStr + "&nbsp;</a>";

	//if (bOmaMenu)
	MenuStr += "<span STYLE='font:" + cFont + ";color:" + cColor1 + "'>&nbsp;|</span>";
	MenuStr += tagStr;
	
	HTMLStr = HTMLStr.replace(tagStr, MenuStr);	
	setSubMenuwidth(MenuIDStr,"default",0);
}

function addOmaSubMenu(MenuIDStr, SubMenuStr, SubMenuURLStr)
{	
	if (addOmaSubMenu.arguments.length > 3)
		targetStr = addOmaSubMenu.arguments[3];
	else
		targetStr = "_top";
	tempID = "Oma_" + MenuIDStr;
	addSubMenu(tempID,SubMenuStr,SubMenuURLStr,targetStr,true); 
}

function addSubMenu(MenuIDStr, SubMenuStr, SubMenuURLStr, targetStr, bOmaMenu)
{
	cFont   = OmaFont
	cColor0 = aDefOmaColor[0]
	cColor1 = aDefOmaColor[1]
	cColor2 = aDefOmaColor[2] 
	
	var MenuPos = MenuIDStr.toUpperCase().indexOf("MENU");
	if (MenuPos == -1) { MenuPos = MenuIDStr.length; }
	InstrumentStr = MenuIDStr.substring(0 , MenuPos) + "|" + SubMenuStr;;
	URLStr        = formatURL(SubMenuURLStr, InstrumentStr);

	var LookUpTag  = "<!--" + MenuIDStr + "-->";
	var sPos = HTMLStr.indexOf(LookUpTag);
	if (sPos <= 0) 	{
		HTMLStr += newLineChar + newLineChar +
				"<span ID='" + MenuIDStr + "'" +
				" STYLE='display:none;position:absolute;width:160;background-color:" + cColor0 + 
				";padding-top:0;padding-left:0;padding-bottom:20;z-index:9;'" +
				" onmouseout='hideMenu();'>";
		if (Frame_Supported == false || bOmaMenu == false)
			HTMLStr += "<hr  STYLE='position:absolute;left:0;top:0;color:" + cColor1 + "' size=1>";
		HTMLStr += "<DIV STYLE='position:relative;left:0;top:8;'>";
	}
	TempStr = newLineChar +
			  "<A ID='AS_" + MenuIDStr + "'" +
				" STYLE='text-decoration:none;cursor:hand;font:" + cFont + ";color:" + cColor1 + "'" +
				" href='" + URLStr + "' target='" + targetStr + "'" +
				" onMouseOut=" 	+ char34 + 	" mouseMenu('out' ,'" + MenuIDStr + "');" + char34 + 
				" onMouseOver=" + char34 +	" mouseMenu('over','" + MenuIDStr + "');" + char34 +  ">" +
				"&nbsp;" + SubMenuStr + "</a><br />" + LookUpTag;

	if (sPos <= 0)	HTMLStr += TempStr + "</DIV></span>";
	else			HTMLStr = HTMLStr.replace(LookUpTag, TempStr);	

	arrMenuInfo[0].count = arrMenuInfo[0].count + 1; 
}

function addOmaSubMenuLine(MenuIDStr) 
{	
	tempID = "Oma_" + MenuIDStr;
	MenuIDStr = tempID
	var LookUpTag = "<!--" + MenuIDStr + "-->";
	var sPos = HTMLStr.indexOf(LookUpTag);
	if (sPos > 0)
	{
		cColor = aDefOmaColor[1];
		TempStr = newLineChar + "<hr STYLE='color:" + cColor + "' size=1>" + LookUpTag;
		HTMLStr = HTMLStr.replace(LookUpTag, TempStr);
}	} 

function mouseMenu(id, MenuIDStr) 
{
	IsMouseout = (id.toUpperCase().indexOf("OUT") != -1);
	if (IsMouseout) color = aDefOmaColor[1];
	else color = aDefOmaColor[2];
	window.event.srcElement.style.color = color;
}

function doMenu(MenuIDStr) 
{
	var thisMenu = document.all(MenuIDStr);
	if (ValikkoRiviMenu == null || thisMenu == null || thisMenu == ValikkoRiviMenu) 
	{
		window.event.cancelBubble = true;
		return false;
	}
	// Reset dropdown menu
	window.event.cancelBubble = true;
	ValikkoRiviMenu.style.display = "none";
	showElement("SELECT");
	showElement("OBJECT");
	ValikkoRiviMenu = thisMenu;

	// Aseta alasvetovalikon paikka
	x  = window.event.srcElement.offsetLeft +
	 	 window.event.srcElement.offsetParent.offsetLeft;
	x2 = x + window.event.srcElement.offsetwidth;

	//y =	 (idRivi1.offsetHeight + idRivi2.offsetHeight + idRivi3.offsetHeight);
	y =	 (idRivi3.offsetHeight + 15); // Lisätään 15, koska Rivit 1-2 puuttuvat
	thisMenu.style.top  = y;
	thisMenu.style.left = x;
	thisMenu.style.clip = "rect(0 0 0 0)";
	thisMenu.style.display = "block";

	// delay 2 millsecond to allow the value of ValikkoRiviMenu.offsetHeight be set
	window.setTimeout("showMenu()", 2);
	return true;
}

function showMenu() 
{
	if (ValikkoRiviMenu != null) 
	{ 
		IsMenuDropDown = (Frame_Supported)? false : true;
		if (IsMenuDropDown == false) {
			y = (y - ValikkoRiviMenu.offsetHeight - idRivi3.offsetHeight);
			if (y < 0) y = 0;
			ValikkoRiviMenu.style.top = y;
		}
		y2 = y + ValikkoRiviMenu.offsetHeight;

		ValikkoRiviMenu.style.clip = "rect(auto auto auto auto)";
		hideElement("SELECT");
		hideElement("OBJECT");
}	}

function hideMenu()
{
	if (ValikkoRiviMenu != null && ValikkoRiviMenu != StartMenu) 
	{
		// Estä valikon piilotus jos hiiri on valikkorivin ja alavalikon välillä
		cY = event.clientY + document.body.scrollTop;
		if ( (event.clientX >= (x+5) && event.clientX <= x2) &&
			 ((IsMenuDropDown == true  && cY > (y-10) && cY <= y2)      ||
			  (IsMenuDropDown == false && cY >= y     && cY <= (y2+10)) ))
		{
			window.event.cancelBubble = true;
			return; 
		}

		ValikkoRiviMenu.style.display = "none";
		ValikkoRiviMenu = StartMenu;
		window.event.cancelBubble = true;

		showElement("SELECT");
		showElement("OBJECT");
}	}

function hideElement(elmID)
{
	for (i = 0; i < document.all.tags(elmID).length; i++)
	{
		obj = document.all.tags(elmID)[i];
		if (! obj || ! obj.offsetParent)
			continue;

		// Find the element's offsetTop and offsetLeft relative to the BODY tag.
		objLeft   = obj.offsetLeft;
		objTop    = obj.offsetTop;
		objParent = obj.offsetParent;
		while (objParent.tagname.toUpperCase() != "BODY")
		{
			objLeft  += objParent.offsetLeft;
			objTop   += objParent.offsetTop;
			objParent = objParent.offsetParent;
		}
		// Adjust the element's offsetTop relative to the dropdown menu
		objTop = objTop - y;

		if (x > (objLeft + obj.offsetwidth) || objLeft > (x + ValikkoRiviMenu.offsetwidth));
		else if (objTop > ValikkoRiviMenu.offsetHeight);
		else if ((y + ValikkoRiviMenu.offsetHeight) <= 80);
		else obj.style.visibility = "hidden";
}	}

function showElement(elmID)
{
	for (i = 0; i < document.all.tags(elmID).length; i++)
	{
		obj = document.all.tags(elmID)[i];
		if (! obj || ! obj.offsetParent)
			continue;
		obj.style.visibility = "";
}	} 

function formatURL(URLStr, InstrumentStr)
{
	var tempStr = URLStr;

	if (DoInstrumentation && URLStr != "" )
	{
		var ParamPos1 = URLStr.indexOf("?");
		var ParamPos2 = URLStr.lastIndexOf("?");
		var ParamPos3 = URLStr.toLowerCase().indexOf("target=");
		var ParamPos4 = URLStr.indexOf("#");
		var Bookmark  = "";
		var URL = URLStr;
		if (ParamPos4 >= 0)	{
		 	URL = URLStr.substr(0, ParamPos4);
			Bookmark = URLStr.substr(ParamPos4);
		}
		tempStr = URL + tempStr + InstrumentStr.replace(" ","%20") + Bookmark;
	}
	return tempStr;
} 
