/* 
 * 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);

/* 
 * General variables
 */
// Subnavi-positions from top (top image height + navi height)
var naviY = 147;
// Array consisting of the x-positions of the subnavis
var subNaviX = new Array(null, '9', '101', '256', '385', '599', '703', '799');
// Script uses this variable to store layer id when visible
var actLyr = 0;
// Time in milliseconds which the menus are open after mouseOut
var menuDelay = 1;

/* Image preloads here, only mouseOver images. */
function fireStarter() {
  if (document.images) {
    var imglist = new Array (
      "pics_laaketeollisuus/navinew/navi_1.gif",
      "pics_laaketeollisuus/navinew/navi_2.gif",
      "pics_laaketeollisuus/navinew/navi_3.gif",
      "pics_laaketeollisuus/navinew/navi_4.gif",
      "pics_laaketeollisuus/navinew/navi_5.gif",
      "pics_laaketeollisuus/navinew/navi_6.gif",
      "pics_laaketeollisuus/navinew/navi_7.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 toggleMenu(lyr, state, currlyr) {
    if(state == "visible") {
      if(actLyr) {
        toggleMenu(actLyr, 'hidden', currlyr);
        clearTimeout( lyrTO );
      }
      actLyr = lyr;
	  if(currlyr != lyr)
      	eval("imSwap('naviIm"+lyr+"','pics_laaketeollisuus/navinew/navi_"+lyr+"_over.gif','')");
    }
    if(state == "hidden") {
      actLyr = 0;
	  if(currlyr != lyr)
      	eval("imSwap('naviIm"+lyr+"','pics_laaketeollisuus/navinew/navi_"+lyr+".gif','')");
    }
    if (ns) {
        if(state == "visible") state = "show";
        if(state == "hidden") state = "hide";
        setPos(getCPos(lyr), 0, lyr);
        eval("document.layers['subMenu" + lyr + "'].visibility = '" + state + "'");
    }
    else if (ns6) {
		/* if (document.body.scrollHeight > document.body.clientHeight) { setPos(getCPos(lyr)-10, 0, lyr); }
		else { setPos(getCPos(lyr), 0, lyr); } */
		setPos(getCPos(lyr), 0, lyr);
		eval("document.getElementById('subMenu" + lyr + "').style.visibility = '" + state + "'");
    }
    else if (document.all) {
        setPos(getCPos(lyr), 0, lyr);
        eval("document.all['subMenu" + 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 toggleMenuTxt(lyr,state) {
    if(state == "visible") {
      if(actLyr) {
        toggleMenu(actLyr, 'hidden');
        clearTimeout( lyrTO );
      }
      actLyr = lyr;
    }
    if(state == "hidden") {
      actLyr = 0;
    }
    if (ns) {
        if(state == "visible") state = "show";
        if(state == "hidden") state = "hide";
        setPos(getCPos(lyr), 0, lyr);
        eval("document.layers['subMenu" + lyr + "'].visibility = '" + state + "'");
    }
    else if (ns6) {
		if (document.body.scrollHeight > document.body.clientHeight) { setPos(getCPos(lyr)-10, 0, lyr); }
		else { setPos(getCPos(lyr), 0, lyr); }
		    eval("document.getElementById('subMenu" + lyr + "').style.visibility = '" + state + "'");
    }
    else if (document.all) {
        setPos(getCPos(lyr), 0, lyr);
        eval("document.all['subMenu" + lyr + "'].style.visibility = '"+state+"'");
    }
}

/* Image swap handler to navi-elements without submenu */
function toggleMenuSimple (el, state) {
  if(state) {
    if(actLyr) {
      toggleMenu(actLyr,'hidden');
      clearTimeout( lyrTO );
    }
    eval("imSwap('naviIm"+el+"','pics_laaketeollisuus/navinew/navi_"+el+"_over.gif','')"); 
  }
  else {
    eval("imSwap('naviIm"+el+"','pics_laaketeollisuus/navinew/navi_"+el+".gif','')");
  }
  actLyr = 0;
}

/* Subnavi element toggle handler */
function toggleSub(mainLyr, subNo, vis) {
  subId = mainLyr + "_" + subNo;
  if(vis) {
    clearTimeout( lyrTO );
    eval("imSwap('naviSubIm"+subId+"','pics_laaketeollisuus/navinew/navi_"+subId+"_over.gif','subMenu" + mainLyr + "')");
  } else {
    lyrTO = setTimeout("toggleMenu(" + mainLyr + ", 'hidden')",menuDelay);
    eval("imSwap('naviSubIm"+subId+"','pics_laaketeollisuus/navinew/navi_"+subId+".gif','subMenu" + mainLyr + "')");
  }
}


/* 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 menuStay(mainLyr, state, currlyr) {
	/* modified below since imageswap is not in use. edi@naviatech.fi */
	if (!currlyr)
	{
		currlyr = 1;
	}
  if(state == "stay") {
    clearTimeout( lyrTO );
  } else {
    lyrTO = setTimeout("toggleMenu(" + mainLyr + ", 'hidden', " + currlyr + ")",menuDelay);
  }
}

/* Activated when mouseOut happens in main navi images. */
function closeMenuHandler(lyr, currlyr) {
  lyrTO = setTimeout("toggleMenu(" + lyr + ", 'hidden', " + currlyr + ")",menuDelay);
}

/* Sets layer positions to the given location in given layer. */
function setPos(x,y,lyr){
  y = naviY;
	if(ns){
		if(x)eval("document.layers['subMenu" + lyr + "'].left = "+x);
		if(y)eval("document.layers['subMenu" + lyr + "'].top = "+y);
	} else if(ns6){
		if(x)eval("document.getElementById('subMenu" + lyr + "').style.left = "+x+"+\"px\"");
		if(y)eval("document.getElementById('subMenu" + lyr + "').style.top = "+y+"+\"px\"");
	} else {
		if(x)eval("document.all['subMenu" + lyr + "'].style.left = "+(x-1));
		if(y)eval("document.all['subMenu" + lyr + "'].style.top = "+y);
	}
}

/* Returns the center point of the screen. */
function getCPos(lyr) {
  if (document.all) {
  	normalW = document.body.offsetWidth;
	if(normalW < 783){
		normalW = 783;
	}
    return parseInt(subNaviX[lyr]) + 1;
  } else if (ns || ns6) {
  	normalW = window.innerWidth;
	if(normalW < 783){
		normalW = 783;
	}
    return parseInt(subNaviX[lyr]);
  }
}

/* Function to handle mouseOver image changes. */
function imSwap(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(actLyr) {
    toggleMenu(actLyr, 'hidden');
    clearTimeout( lyrTO );
  }
}
