/*
Copyright (c) 2004 Ylab, http://www.ylab.nl
2004-05-01; extended for Om en nabij
*/

//trap error
window.onerror = function(msg, url, line){
  window.status = "Er is een fout opgetreden. Meld dit a.u.b. aan de webmaster. " + line + ": " + msg;
  return true;
}

var debugging = false;
var isOpera = (navigator.userAgent.indexOf("Opera") > -1);
var isNav = ((!isOpera) && (navigator.appName == "Netscape"));
var isIE  = ((!isOpera) && (navigator.appName.indexOf("Explorer") > -1 ));
//var isMac = ((isIE) && navigator.userAgent.indexOf("Mac") > -1));

var nails;
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('initDocument();');

//init document
function initDocument(){
  window.status = document.title;
  nails = id2object('leftbar-inset').getElementsByTagName('img');
  resize();
}
window.onresize = function (){
  resize();
  resizeSlide();
}
function resize(){
  if(!nails || !nails.length){return;}
  var viewport = new Viewport();
  var h = viewport.y - 270;
  //var n = isIE ? Math.floor(h/105) : (document.height) ? Math.floor((document.height-270)/105) : 3;
  var n = isIE ? Math.floor(h/105) : 3;
  for (var i=0; i<nails.length; i++){
    nails[i].style.display = (i<n) ? 'block' : 'none';
  }
}
//display mailto link
function printMail(username, linktext){
  username = username.toLowerCase() + "@Acrobatiek.nl";
  if (!linktext) {linktext = username;}
  document.write(linktext.link("mailto:" + username));
}

//Convert id into object
function id2object(el){
  if (typeof(el)=="string"){el = document.getElementById(el);}
  return el;
}

function Viewport(){
  this.x = 0;
  this.y = 0;
  if (self.innerHeight) {
    // all except Explorer
  	this.x = self.innerWidth;
  	this.y = self.innerHeight;
  }
  else if (document.documentElement && document.documentElement.clientHeight){
    // Explorer 6 Strict Mode
  	this.x = document.documentElement.clientWidth;
  	this.y = document.documentElement.clientHeight;
  }
  else if (document.body){
    // other Explorers
  	this.x = document.body.clientWidth;
  	this.y = document.body.clientHeight;
  }
}

//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;}

}
var slide;
var imgs;
var spotlightWhat;
var spotlightWho;
var spotlightBody;
function slideIn(w, h, type, src, n){
  var border = 2;
  var code = '';
  var alt = '';
  var titlebar = '<div class="titlebar" onmousedown="slide.style.display = \'none\';">'+document.title+'</div>';

  if (arguments.length < 4){return;}
  if (!slide){
    //1st time, init variables
    spotlightWhat = id2object('spotlightWhat');
    spotlightWho  = id2object('spotlightWho');
    spotlightBody = id2object('spotlight');
    //create slide
    slide = document.createElement('div');
    slide.className = 'window';
    slide.style.position = "absolute";
    slide.style.margin = "auto";
    slide.style.padding = 0;
    slide.style.borderWidth = border + "px";
    slide.style.top = "50px";
    document.body.appendChild(slide);
    imgs = id2object('leftbar-inset').getElementsByTagName('img');
  }

  switch (type){
    case 'img':
      if (imgs && !isNaN(n) && imgs[n] && imgs[n].alt > ''){
        alt = '<div class="workspace">' + imgs[n].alt + '</div>';
      }
      slide.innerHTML = titlebar + imgHTML(src, w, h, (h > w && alt > '') ? 'float-left' : '') + alt;
      w = (h > w && alt > '') ? 470 : w;
      break;
    case 'serie':
      if (window.photos && !isNaN(n) && photos[n] && photos[n].alt > ''){
        alt = '<div class="workspace">' + photos[n].alt + '</div>';
      }
      slide.innerHTML = titlebar + imgHTML(src, w, h, (h > w && alt > '') ? 'float-left' : '') + serie(n) + alt;
      w = (h > w && alt > '') ? 470 : w;
      break;
    case 'txt':
      if (spotlightWhat){code += '<div class="what">' + spotlightWhat.innerHTML + '</div>';}
      if (spotlightWho){code += '<div class="who">' + spotlightWho.innerHTML + '</div>';}
      code += imgHTML(src, w, h, (h > w) ? 'float-left' : '');
      code += serie(n);
      if (spotlightBody){code += spotlightBody.innerHTML;}
      w = (h > w) ? 480 : 460;
      h = 470;
      slide.innerHTML = titlebar + '<div class="workspace" style="height: ' + h + 'px">' + code + '</div>';
      break;
  }
  function serie(n){
    if (!window.photos){return '';}
    if (photos && photos.length > 2){
      var code = '<div class="serie' + (n>1 ? '-img' : '-txt') + '"><a href="javascript:forcePopup();"><img src="images/' + (!n ? '1s' : '1') + '.gif" /></a> '
      for(var i = 2; i < photos.length; i++){
        code += '<a href="javascript:slideIn(' + photos[i].w + ', ' + photos[i].h + ', &#039;serie&#039;, &#039;' + photos[i].url + '&#039;, ' + (i) + ');"><img src="images/' + (n==i ? i+'s' : i) + '.gif" /></a> ';
      }
      code += '</div>'
    }
    return code;
  }
  slide.w = w + (2*border);
  slide.h = h + (2*border) + 10;
  slide.style.width  = slide.w +'px';
  slide.style.height = slide.h + 'px';
  slide.style.height = 'auto';
  resizeSlide();
  slide.style.display = 'block';
  if(slide.getElementsByTagName('img').length){
    slide.getElementsByTagName('img')[0].src = src;
  }
  function imgHTML(s, w, h, c){
    if (!s){return '';}
    var result = '<img onmousedown="slide.style.display=\'none\';"';
    if (s)  {result += ' src="' + s + '"';}
    if (w)  {result += ' width="' + w + '"';}
    if (h)  {result += ' height="' + h + '"';}
    if (c)  {result += ' class="' + c + '"';}
    result += ' />';
    return result;
  }
}
function Photo(url, alt, w, h){
  this.url= url;
  this.alt = alt;
  this.w = w;
  this.h = h;
  return this;
}
function resizeSlide(){
  if (!slide)return;
  var viewport = new Viewport();
  var x = (viewport.x - slide.w)/2;
  slide.style.left = x+"px";
}
function forcePopup(){
  var a = id2object("spotlightLink");
  if (a){location.href=a;}
}
//DEBUGGING
function devAlert(){
  if (!debugging){return;}
  var code = "Deze functie is nog niet ge"+String.fromCharCode(239)+"mplementeerd.\n";
  for(var i=0; i < arguments.length; i++) {
    code += arguments[i] + "\n";
  }
  alert(code);
}

function debugAlert(){
  if (!debugging){return;}
  var code = "";
  for(var i=0; i < arguments.length; i++) {
    code += arguments[i] + "\n";
  }
  window.status = code;
  alert(code);
}
function debugConfirm(){
  if (!debugging){return;}
  var code = "";
  for(var i=0; i < arguments.length; i++) {
    code += arguments[i] + "\n";
  }
  window.status = code;
  return confirm(code);
}
