/*
Generic functions for www.kindermonument.nl
Copyright (c) 2003 Ylab, Utrecht, NL
Author: Yohan Creemers
version 1.6
1.1: Added event handling
1.2: Added propertie handling (style, top and height)
1.3: Set document title in status bar
1.4: Hide horizontal scrollbar in msie 5.5+
     Added printMail()
1.5: Added generic relativeRoot() function
1.6: Added printAge() function
*/

//generic event handling
window.onerror = reportError;
function reportError(msg, url, line){
  window.status = "Er is een fout opgetreden. Meld dit a.u.b. aan de webmaster. | " + line + ": " + msg;
  return true;
}

var scrollFunctions = new Array();
function addScrollFunction(f){scrollFunctions[scrollFunctions.length] = new Function(f);}
window.onscroll = function() {for (var i=0; i<scrollFunctions.length; i++){scrollFunctions[i]();}}  

var loadFunctions = new Array();
function addLoadFunction(f)  {loadFunctions[loadFunctions.length] = new Function(f);}
window.onload   = function() {for (var i=0; i<loadFunctions.length; i++){loadFunctions[i]();}} 

addLoadFunction("window.status = document.title;");

var isOpera = (navigator.userAgent.indexOf("Opera") > -1);
var isNav = ((!isOpera) && (navigator.appName == "Netscape"));
var isIE  = ((!isOpera) && (navigator.appName.indexOf("Explorer") > -1 ));
var versionMajor = parseInt(navigator.appVersion);
if (isOpera){ var versionMinor = parseFloat(navigator.userAgent.substring(navigator.userAgent.indexOf("Opera")+6));}
else if (isIE){ var versionMinor = parseFloat(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE")+5));}
else{ var versionMinor = parseFloat(navigator.appVersion);}
//test for not supported browsers
if ((isIE) && (versionMinor < 5) || (isNav) && (versionMinor < 5) || (isOpera) && (versionMinor <= 6)){
  location.replace("/default.htm");
}
if (isNav){
  document.write('<link rel="stylesheet" type="text/css" href="' + relativeRoot() + 'stylesheets/navigator.css">');
}
if (isOpera){
  document.write('<link rel="stylesheet" type="text/css" href="' + relativeRoot() + 'stylesheets/opera.css">');
}

//hide horizontal scrollbar
if ((isIE) && (versionMinor > 5)){addLoadFunction("document.body.style.overflowX = 'hidden';");}

//----------------------------------------------------------------------

//preload hover images when document is loaded
var hoverImages = new Array();
addLoadFunction("if (hoverImages.length==0){return;} for (var i=0; i<hoverImages.length; i++){(new Image()).src = hoverImages[i];}");

/*Prepare image for hover effect
  Sample use:
  <img onload="imgHover('img01')">
  <img onload="imgHover(this)">
  <img onload="imgHover(this, 'hover.gif')">
  <img onload="imgHover(this, 'text=image')">
  
  img: image object or image id;
  srcHover: url of hover image [optional]
           : when omitted, the postfix "-hover" is added to the original image path
           : when containing an is-character (=) a part of the original url is altered (e.g. 'text=image')
*/
function imgHover(img, srcHover, hotspot){
  //Initialize only once
  img = id2object(img);
  if (img.initialized){return;}

  //Set properties
  img.initialized = true;//been here
  img.srcNormal = img.src;
  if (srcHover){
    if (srcHover.indexOf("=") != -1){
      //replace part of original url
      var pair = srcHover.split("=");
      img.srcHover=img.src.replace(pair[0],pair[1])
    }
    else{img.srcHover = srcHover;}
  } 
  else{
    //add -hover by default
    var extpos = img.src.lastIndexOf("."); 
    img.srcHover = img.src.substring(0,extpos) + "-hover" + img.src.substring(extpos);
  }
  hoverImages[hoverImages.length] = img.srcHover;
  //Assign hotspot events
  if (arguments.length < 3){var el=img;} 
  else {var el = id2object(hotspot)}
  el.onmouseover = function() {img.src = img.srcHover;}
  el.onmouseout  = function() {img.src = img.srcNormal;}
  
}

//Convert id into object
function id2object(el){
  if (typeof(el)=="string"){el = document.getElementById(el);}
  return el;
}

//set a style property or fail gracefully
function setStyle(objectId, prop, value){
  var obj = document.getElementById(objectId);
  if ((obj) && (obj.style[prop] != value)){
    obj.style[prop] = value;
  }
}

//reading and setting the Y position 
setLayerTop = new Function('arguments[0].style.pixelTop = arguments[ 1 ];');
getLayerTop = new Function('return arguments[0].style.pixelTop;');

function calculateHeight(){
  if (document.height){
    document.pageHeight = document.height; 
  } 
  else{
	  var curHeight = parseInt(document.body.clientHeight) + parseInt(document.body.scrollTop);
	  if (!document.pageHeight){document.pageHeight = 0;}
	  document.pageHeight = Math.max(curHeight, document.pageHeight);
	}
}

function fitToPageHeight(objId){
  calculateHeight();
  var code = "setStyle('" + objId + "', 'height', " + document.pageHeight + ")";
  //wait until browser is idle
  setTimeout(code, 1);
}

/*display mailto link
  Sample use:
  printMail("Pim");
  printMail("Pim", "Secretaris");
  
  username: the username part of the email address
  linktext: the visible text of the hyperlink [optional]
*/
function printMail(username, linktext){
  username = username.toLowerCase() + "@kindermonument.nl";
  if (!linktext) {linktext = username;}
  document.write(linktext.link("mailto:" + username));
}

/*display age
  Sample use:
  printAge(1925, 10, 25);
  
  yy: year in four digits
  mm: month; 1 (jan) - 12 (dec)
  dd: day; 1-31
*/
function printAge(yy,mm,dd){
  var now = new Date();
  var bday = new Date(yy, mm-1, dd);
  var anniversary = new Date(now.getYear(), mm-1, dd);
  var age = now.getFullYear() - bday.getFullYear();
  //no anniversary yet this year
  if (now.getTime() < anniversary.getTime()){age--};
  document.write(age);
}

function relativeRoot(){
  //determine directory level (count slashes) 
  if (!window.location.pathname) return "/";
  var curPath = (new String(window.location.pathname)).split("\\").join("/");
  /*
  var relPath = "";
  var n = (window.location.protocol == "file:") ? -4 :  -2;
  if (isOpera && window.location.protocol == "file:"){n++}
  for (var i=0; i<curPath.length; i++){
    if (curPath.substring(i,i+1) == "/"){n++;}
  }
  for (var i=0; i<n; i++){relPath += "../";}*/
  var relPath = "/";
  var pos = curPath.indexOf("/dev/");
  if (pos > -1){relPath = curPath.substring(0, pos + 5);}
  return relPath;
}

