/* 
 * Browser detection
 */

var ns6 = (!document.all && document.getElementById)?1:0;
var ns = (document.layers)?1:0;
var agt = navigator.userAgent.toLowerCase();
var mac = (agt.indexOf("mac")!=-1);

var browserName=navigator.appName; 
var browserVer=parseInt(navigator.appVersion); 
/* 
 * General variables
 */
// Subnavi-positions from top (top image height + navi height)
var naviY2 = 0;
// Array consisting of the x-positions of the subnavis
if ((browserName=="Microsoft Internet Explorer")) {
	var subNaviX2 = new Array(null, '-80', '-80', '46', '46', '46', '46', '46', '110', '110', '110', '260', '260', '260', '290', '330', '330');
}else{
	var subNaviX2 = new Array(null, '-80', '-80', '18', '18', '18', '18', '18', '80', '80', '80', '235', '235', '235', '255', '295', '295');
}
// Script uses this variable to store layer id when visible
var actLyr2 = 0;
// Time in milliseconds which the menus are open after mouseOut
var menuDelay = 1;

/* Image preloads here, only mouseOver images. */
function fireStarter2() {
  if (document.images) {
    var imglist = new Array (
      "images/bg_topnavi.gif"
    );
    var tempImgArr = new Array();
    var count;
    for (count=0; count<imglist.length; count++) {
      tempImgArr[count]=new Image(); tempImgArr[count].src=imglist[count];
    }
  } 
}


/* Main function to handle opening and closing of the main menu. */
function toggleMenu2(lyr,state) {
    if(state == "visible") {
      if(actLyr2) {
        toggleMenu2(actLyr2, 'hidden');
        clearTimeout( lyrTO2 );
      }
      actLyr2 = lyr;
    }
    if(state == "hidden") {
      actLyr2 = 0;
      //eval("imSwap2('naviIm"+lyr+"','kuvat/navi/navi_"+lyr+".gif','')");
    }
    if (ns) {
        if(state == "visible") state = "show";
        if(state == "hidden") state = "hide";
        setPos2(getCPos2(lyr), 0, lyr);
        eval("document.layers['subMenux" + lyr + "'].visibility = '" + state + "'");
    }
    else if (ns6) {
		if (document.body.scrollHeight > document.body.clientHeight) { setPos2(getCPos2(lyr)-10, 0, lyr); }
		else { setPos2(getCPos2(lyr), 0, lyr); }
		    eval("document.getElementById('subMenux" + lyr + "').style.visibility = '" + state + "'");
    }
    else if (document.all) {
        setPos2(getCPos2(lyr), 0, lyr);
        eval("document.all['subMenux" + lyr + "'].style.visibility = '"+state+"'");
    }
}

/* As above, handles submenu actions, but without image swap (Text links) -jh 091202 */
/* TODO: Could simply be implemented to above */
function toggleMenuTxt2(lyr,state) {
    if(state == "visible") {
      if(actLyr2) {
        toggleMenu2(actLyr2, 'hidden');
        clearTimeout( lyrTO2 );
      }
      actLyr2 = lyr;
    }
    if(state == "hidden") {
      actLyr2 = 0;
    }
    if (ns) {
        if(state == "visible") state = "show";
        if(state == "hidden") state = "hide";
        setPos2(getCPos2(lyr), 0, lyr);
        eval("document.layers['subMenux" + lyr + "'].visibility = '" + state + "'");
    }
    else if (ns6) {
		if (document.body.scrollHeight > document.body.clientHeight) { setPos2(getCPos2(lyr)-10, 0, lyr); }
		else { setPos2(getCPos2(lyr), 0, lyr); }
		    eval("document.getElementById('subMenux" + lyr + "').style.visibility = '" + state + "'");
    }
    else if (document.all) {
        setPos2(getCPos2(lyr), 0, lyr);
        eval("document.all['subMenux" + lyr + "'].style.visibility = '"+state+"'");
    }
}

/* Image swap handler to navi-elements without submenu */
function toggleMenuSimple2 (el, state) {
  if(state) {
    if(actLyr2) {
      toggleMenu2(actLyr2,'hidden');
      clearTimeout( lyrTO2 );
    }
    //eval("imSwap2('naviIm"+el+"','kuvat/navi/navi_"+el+"_over.gif','')"); 
  }
  else {
    //eval("imSwap2('naviIm"+el+"','kuvat/navi/navi_"+el+".gif','')");
  }
  actLyr2 = 0;
}

/* Subnavi element toggle handler */
function toggleSub2(mainLyr2, subNo, vis) {
  subId = mainLyr2 + "_" + subNo;
  if(vis) {
    clearTimeout( lyrTO2 );
    //eval("imSwap2('naviSubIm"+subId+"','kuvat/navi/navi_"+subId+"_over.gif','subMenux" + mainLyr2 + "')");
  } else {
    lyrTO2 = setTimeout("toggleMenu2(" + mainLyr2 + ", 'hidden')",menuDelay);
    //eval("imSwap2('naviSubIm"+subId+"','kuvat/navi/navi_"+subId+".gif','subMenux" + mainLyr2 + "')");
  }
}


/* NOT IN USE. Idea was to keep the menu open when mouse is on any submenu element. Netscape 4.x doesn't support 
 * mouseover on images. Support may be added to IE and NS6 later. */
function menuStay2(mainLyr2, state) {
  if(state == "stay") {
    clearTimeout( lyrTO2 );
  } else {
    lyrTO2 = setTimeout("toggleMenuTxt2(" + mainLyr2 + ", 'hidden')",menuDelay);
  }
}

/* Activated when mouseOut happens in main navi images. */
function closeMenuHandler2(lyr) {
  lyrTO2 = setTimeout("toggleMenuTxt2(" + lyr + ", 'hidden')",menuDelay);
}

/* Sets layer positions to the given location in given layer. */
function setPos2(x,y,lyr){
  y = naviY2;
	if(ns){
		if(x)eval("document.layers['subMenux" + lyr + "'].left = "+x);
		if(y)eval("document.layers['subMenux" + lyr + "'].top = "+y);
	} else if(ns6){
		if(x)eval("document.getElementById('subMenux" + lyr + "').style.left = "+x+"+\"px\"");
		if(y)eval("document.getElementById('subMenux" + lyr + "').style.top = "+y+"+\"px\"");
	}
	else if(typeof XDomainRequest != 'undefined' && XDomainRequest)
	{
		if(x){document.getElementById('subMenux'+ lyr).style.left = (x-11)+"px";}
		if(y){document.getElementById('subMenux'+ lyr).style.top = y+"px";}
	}  
	else {
		if(x)eval("document.all['subMenux" + lyr + "'].style.left = "+(x-11));
		if(y)eval("document.all['subMenux" + lyr + "'].style.top = "+y);
	}
}

/* Returns the center point of the screen. */
function getCPos2(lyr) {
  if (document.all) {
  	normalW = document.body.offsetWidth;
	if(normalW < 945){
		normalW = 945;
	}
    return parseInt(normalW / 2) + parseInt(subNaviX2[lyr]);
  } else if (ns || ns6) {
  	normalW = window.innerWidth;
	if(normalW < 945){
		normalW = 945;
	}
    return parseInt(normalW / 2) + parseInt(subNaviX2[lyr]);
  }
}

/* Function to handle mouseOver image changes. */
function imSwap2(im,imSrc,lyr) {
  if(document.images) {
    if(ns) {
      dstLyr = (lyr) ? "document." + lyr + "." : ""; 
      var img = eval(dstLyr+'document.'+im);
      img.src = imSrc;
    } else {
      var img = eval('document.'+im);
      img.src = imSrc;
    }
  }
}

/* Mouseclick capture to close open menus when clicking anywhere in the document. */
if (ns) {
    document.captureEvents(Event.CLICK);
}
document.onclick = mouseEvent;
function mouseEvent() {
  if(actLyr2) {
    toggleMenu2(actLyr2, 'hidden');
    if( typeof lyrTO2 != 'undefined')
    {
    	clearTimeout( lyrTO2 );
    }
  }
}

