/* 
 * 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 = 12;
// Array consisting of the x-positions of the subnavis
var subNaviX = new Array(null, '-210', '-115');
// 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 (
      "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 toggleMenu(lyr,state) {
    if(state == "visible") {
      if(actLyr) {
        toggleMenu(actLyr, 'hidden');
        clearTimeout( lyrTO );
      }
      actLyr = lyr;
    }
    if(state == "hidden") {
      actLyr = 0;
     // eval("imSwap('naviIm"+lyr+"','kuvat/navi/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); }
		    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+"','kuvat/navi/navi_"+el+"_over.gif','')"); 
  }
  else {
    //eval("imSwap('naviIm"+el+"','kuvat/navi/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+"','kuvat/navi/navi_"+subId+"_over.gif','subMenu" + mainLyr + "')");
  } else {
    lyrTO = setTimeout("toggleMenu(" + mainLyr + ", 'hidden')",menuDelay);
   // eval("imSwap('naviSubIm"+subId+"','kuvat/navi/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) {
  if(state == "stay") {
    clearTimeout( lyrTO );
  } else {
    lyrTO = setTimeout("toggleMenu(" + mainLyr + ", 'hidden')",menuDelay);
  }
}

/* Activated when mouseOut happens in main navi images. */
function closeMenuHandler(lyr) {
  lyrTO = setTimeout("toggleMenu(" + lyr + ", 'hidden')",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-11));
		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 < 950){
		normalW = 950;
	}
    return parseInt(normalW / 2) + parseInt(subNaviX[lyr]);
  } else if (ns || ns6) {
  	normalW = window.innerWidth;
	if(normalW < 950){
		normalW = 950;
	}
    return parseInt(normalW / 2) + 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 );
  }
}
