// menu-settings
var width = 160;
var top = 125;
var mouseOn = -1;
var timerOn = false;
var timerID = 0;
var IE = 0;
var NS = 0;
var NS6 = 0;
// browser test
if (document.all) {
	IE = 1;
} else if (document.layers) {
	NS = 1;
	origWidth = innerWidth;
	origHeight = innerHeight;
} else if (document.getElementById) {
	NS6 = 1;
}
if (NS) {
	window.onresize = NS_resize;
}

// menu-data
var menut = new Array(
	0, // etusivut -no menu
	new Array(8, // NTK I

		"joukkue","team.php","",

		"ottelut","games.php","",

		"pistepörssi","points.php","",

		"sarjatilanne","serie.php","",

		""
	),
	new Array(84, // NTK II

		"joukkue","team2.php","",

		"ottelut","games2.php","",

		"pistepörssi","points2.php","",

		"sarjatilanne","serie2.php","",
		
		""
	),
	new Array(168, // b-jun

		"joukkue","teamb.php","",

		"ottelut","gamesb.php","",

		"pistepörssi","pointsb.php","",

		"sarjatilanne","serieb.php","",
		
		""
	),
	new Array(247, // m35

		"joukkue","team35.php","",
		
		"ottelut","games35.php","",
		
		"pistepörssi","points35.php","",
		
		"sarjatilanne","serie35.php","",
		
		""
	), 
	new Array(322, // kontakti

		"yhteystiedot","/ohjelmat/","",

		"keskustelu","/ohjelmat/paivanOhjelmatiedot.shtml","",

		"vieraskirja","/ohjelmat/viikonOhjelmatiedot.shtml","",

		""
	),
	new Array(433, // sekalainen

		"yhteisty&ouml;kumppanit","/toimitus/","",

		"info","/toimitus/vkl.shtml","",

		"historia","/toimitus/sivu.5.shtml","",

		"linkit","/toimitus/sivu.2.shtml","",

		"valokuvia","/arkisto/sivu.32.shtml","",

		"stats","/toimitus/sivu.3.shtml","",
		""
	),
	new Array(420, // keskustelu

		"chat","http://chat.yle.fi/chat/","width=702,height=501",

		"foorumi","http://chat.yle.fi/ubbthreads/index.php?Cat=","width=700,height=500,resizable=yes,scrollbars=yes,menubar=yes,toolbar=yes,location=yes",
		""
	),
	new Array(495, // palaute

		"anna palautetta","/palaute/","",
		""
	)
);

function NS_resize() {
	if (origWidth != innerWidth || origHeight != innerHeight) {
 		window.location.reload();
 	}
}

// printing menus
if (IE||NS||NS6) {
	for (var i = 0; i < menut.length; i++) {
		if (menut[i]) {
			var left = menut[i][0];
			if (IE || NS6) { // IE & NS6
				document.writeln('<div id="palikka' + i + '" style="visibility: hidden; z-index: auto; position: absolute; top: ' + top + '; left: ' + left + ';" onMouseOver="stopTimer()" onMouseOut="suljeMenu(' + i + ')">');
				tulostaMenu(i);
				document.writeln('</div>');
			} else { // NS
				document.writeln('<layer id="palikka' + i +'" top="' + top + '" left="' + left + '" visibility="hide" onMouseOver="stopTimer()" onMouseOut="suljeMenu(' + i + ')">');
				tulostaMenu(i);
				document.writeln('</layer>');
			} // if
		} // if
	} // for
} // if

var old_pic = null;

// preloading images
if (document.images) {
	for (i=0; i<=8; i++) {
		rmPreload("navi_"+i, "gif");
	}
	wma = new Image();
	wma.src = '/img/top10_wma.gif';
	real = new Image();
	real.src = '/img/top10_real.gif';
	wc = new Image();
	wc.src = '/img/top10_wc.gif';
	wma_a = new Image();
	wma_a.src = '/img/top10_wma_a.gif';
	real_a = new Image();
	real_a.src = '/img/top10_real_a.gif';
	wc_a = new Image();
	wc_a.src = '/img/top10_wc_a.gif';
}

function act(pic) {
	if (document.images) {
		inact();
		document[pic].src = eval(pic + '_a.src');
		old_pic = pic;
	}
}

function inact() {
	if (document.images && old_pic) {
		document[old_pic].src = eval(old_pic + '.src');
		old_pic = null;	
	}
}



// menu functions
function nop() {
	return;
}

function tulostaMenu(i) {
	document.writeln('<table width="' + width + '" border="0" cellspacing="0" cellpadding="0" bgcolor="#0061a6">');
	document.writeln('<tr><td colspan="3" bgcolor="#5d779c" width="' + width + '"><img src="files/blanco.gif" width="' + width + '" height="1" border="0" alt=""></td></tr>');
	document.writeln('<tr><td width="1" bgcolor="#5d779c"><img src="files/blanco.gif" width="1" height="1" border="0" alt=""></td><td width="' + (width - 2) + '" valign="top"><img src="files/blanco.gif" width="' + (width - 2) + '" height="1" border="0" alt=""><div class="menu">');
	for (var j = 1; j < menut[i].length; j += 3) {
		if (menut[i][j] != "" && menut[i][j + 2] == "")
			document.writeln('<a href="' + menut[i][j + 1] + '">' + menut[i][j] + '</a><br>');
		else if (menut[i][j] != "" && menut[i][j + 2])
			document.writeln('<a href="#" onclick="window.open(\'' + menut[i][j + 1] + '\',\'ylex' + i + '_' + j + '\',\'' + menut[i][j + 2] + '\')">' + menut[i][j] + '</a><br>');
	}
	document.writeln('</div></td><td width="1" bgcolor="#5d779c"><img src="files/blanco.gif" width="1" height="1" border="0" alt=""></td></tr>');
	document.writeln('<tr><td colspan="3" bgcolor="#5d779c" width="' + (width - 2) + '"><img src="files/blanco.gif" width="' + (width - 2) + '" height="1" border="0" alt=""></td></tr>');
	document.writeln('</table>');  
}

function startTimer() {
	timerID = setTimeout('hide()',700);
	timerOn = true;
}

function stopTimer() {
	if (timerOn) {
		clearTimeout(timerID);
		timerOn = false;
	}
}

function avaaMenu(num) {
	stopTimer();
	if (mouseOn > -1 && (mouseOn != num)) {
		hide();
	} 
	mouseOn = num;
	if (document.images) {
		document['navi_' + num].src = eval('navi_' + num + "_on.src");
	}
	if (menut[num]) {
		if (IE) {
			var dom = eval('document.all.palikka' + num + '.style');
			dom.left = menut[num][0] + leftOffset();
			dom.visibility = 'visible';
		} else if (NS) {
			document.layers['palikka' + num].left = menut[num][0]  + leftOffset();
			document.layers['palikka' + num].visibility = 'show';
		} else if (NS6) {
			document.getElementById('palikka' + num).style.left = menut[num][0] + leftOffset();	
			document.getElementById('palikka' + num).style.visibility = 'visible';	
		}
	}
}			

function suljeMenu(num) {
	if (num == mouseOn) {
		startTimer();
	}
}
function hide() {
	var num = mouseOn;
	if (document.images && num > -1) {
		document['navi_' + num].src = eval('navi_' + num +"_off.src");
	}
	if (num > -1 && menut[num]) {
		if (IE) {
			var dom = eval('document.all.palikka' + num + '.style');
			dom.visibility = 'hidden';
		} else if (NS) {
			document.layers['palikka' + num].visibility = 'hide';
		} else if (NS6) {
			document.getElementById('palikka' + num).style.visibility = 'hidden';	
		}
		timerOn = false;
		mouseOn = -1;
	}
}

function leftOffset() {
	var rmScreenWidth=-1;
	if (IE && document.body.clientWidth) {
		rmScreenWidth = document.body.clientWidth;
	} else { // NS & NS6
		rmScreenWidth = window.innerWidth-15;
	}
	return rmScreenWidth / 2 - 689 / 2 + 11;
}

function rmPreload(nimi, paate) {
 eval(nimi+"_on=new Image()");
 eval(nimi+"_on.src=\"files/"+nimi+"_on."+paate+"\"");
 eval(nimi+"_off=new Image()");
 eval(nimi+"_off.src=\"files/"+nimi+"_off."+paate+"\"");
}

function naytaOhjelmatietoNyt() {
	if (SettingOhjelmatietoLayerShow != false) {
		if (!naytaOhjelmatietoNytNakyvissa) {
			var ohjelmatietoLeft= 553 + leftOffset() - 11;
	
			if (IE) {
				eval("document.all.ohjelmatiedotNytData.style.left = \""+ohjelmatietoLeft+"\";");
				eval("document.all.ohjelmatiedotNytData.style.visibility = \"visible\";");
			} else if (NS) {
				eval("document.ohjelmatiedotNytData.left = \""+ohjelmatietoLeft+"\";");
				eval("document.ohjelmatiedotNytData.visibility = \"show\";");
			} else if (NS6) {
				document.getElementById('ohjelmatiedotNytData').style.left = ohjelmatietoLeft;
				document.getElementById('ohjelmatiedotNytData').style.visibility = "visible";
			}
			naytaOhjelmatietoNytNakyvissa = true;
	
		} else {
			if (IE) {
				eval("document.all.ohjelmatiedotNytData.style.visibility = \"hidden\";");
			} else if (NS) {
				eval("document.ohjelmatiedotNytData.visibility = \"hide\";");			
			} else if (NS6) {
				document.getElementById('ohjelmatiedotNytData').style.visibility = "hidden";
			}
			naytaOhjelmatietoNytNakyvissa = false;
		}
	}
}
