/*
Copyright (c) 2004-2008 Ylab, www.ylab.nl
*/

var debugging = true;
var isOpera = (navigator.userAgent.indexOf("Opera") > -1);
var isNav = ((!isOpera) && (navigator.appName == "Netscape"));
var isIE  = ((!isOpera) && (navigator.appName.indexOf("Explorer") > -1 ));
var hasFirebug = (window.console && console.firebug);

//trap error
if(!debugging){
	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 loadFunctions = [];
function addLoadFunction(f) {loadFunctions[loadFunctions.length] = new Function(f);}
window.onload   = function(){for (var i=0; i<loadFunctions.length; i++){loadFunctions[i]();}};

//display mailto link
function printMail(username, linktext){
  username = username.toLowerCase() + "@hooftzaken.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;
}

Array.prototype.in_array = function(element){
	var i;

  for (i=0; i<this.length; i++){
    if (this[i] == element){
      return true;
    }
  }
  return false;
};

//Set focus on first input element
function setFirstFieldFocus(container){
	var list;
  if(container.tagName == "FORM"){
    list = container.elements;
  }
  else{
    list = container.getElementsByTagName("input");
  }
  for (var i=0; i< list.length; i++){
    try{
      if (list[i].type == "button"){continue;}
      list[i].focus();
      if(list[i].select){list[i].select();}
      break;
    }
    catch (exception){}
  }
}

//enable or disable elements and their children
function setEnabled(on){
  for (var i=1; i<arguments.length; i++){
    var obj = id2object(arguments[i]);
    if (!obj){continue;}
    obj.disabled = !on;
    if(on){
    	removeClass(obj, 'disabled');
    }
    else{
    	addClass(obj, 'disabled');
    }
    if (obj.hasChildNodes()){
      for (var j=0; j<obj.childNodes.length; j++){
        if (obj.childNodes[j].nodeType == 1){setEnabled(on, obj.childNodes[j]);}
      }
    }
  }
}

//COOKIES
function getCookie(name){
  //Function to return the value of the cookie specified by "name".
  //returns:String object containing the cookie value, or null if the cookie does not exist.

  var arg = name + "=";
  var alen = arg.length;
  var clen = document.cookie.length;
  var i = 0;
  var j = 0;

  function getCookieVal(offset){
  	//Internal function to return the decoded value of a cookie
    var endstr = document.cookie.indexOf (";", offset);

    if (endstr == -1){
    	endstr = document.cookie.length;
    }
    return unescape(document.cookie.substring(offset, endstr));
  }

  while (i < clen){
    j = i + alen;
    if (document.cookie.substring(i, j) == arg){
      return getCookieVal (j);
    }
    i = document.cookie.indexOf(" ", i) + 1;
    if (i === 0){
    	break;
    }
  }
  return null;
}

function setCookie(name, value){
  //Function to create or update a cookie for calling document.
  document.cookie = name + "=" + escape (value);
}

var hover = function(img, bHover){
	if(bHover && !img.srcNormal){
		img.srcNormal = img.src;
		var extpos = img.srcNormal.lastIndexOf(".");
		img.srcHover = img.src.substring(0,extpos) + "-hover" + img.src.substring(extpos);
	}
	if(img.srcNormal){
		img.src = bHover ? img.srcHover : img.srcNormal;
	}
}
var cols = [1,2,3,6];
var spanCopyright;

function setCopyright(){
	if(!spanCopyright){
		spanCopyright = id2object('copyright');
		return;
	}
	if(!spanCopyright.year || spanCopyright.year == new Date().getFullYear()){
		spanCopyright.year = 2002;
	}
	else{
		spanCopyright.year++;
	}
	spanCopyright.innerHTML = '&copy; ' + spanCopyright.year;
}
if(!debugging || !hasFirebug){
	window.setInterval('setCopyright()', 1000);
}

var prevSpot;
function setSpotlight(){
	var spot = Math.ceil(12*Math.random());
	if(prevSpot == spot){
		return setSpotlight();
	}
	var dur = Math.abs(prevSpot - spot) * 100;
	$('#div1').animate({width:((spot - 1) * 82) + 'px'}, dur);
	$('#div2').animate({width:((12 - spot) * 82) + 'px'}, dur);
	prevSpot = spot;
}

jQuery(document).ready(function($){
	var divT = $('#thumbs');
	$('<div id="div1"></div>').css({left:0}).appendTo(divT);
	$('<div id="div2"></div>').css({right:0}).appendTo(divT);
	$('#thumbs div').fadeTo(0, 0.5);
	setSpotlight();
	if(!debugging || !hasFirebug){
		window.setInterval('setSpotlight()', 5000);
	}
});

//DEBUGGING
function devAlert(){
  if(!debugging){return;}
  var code = "Deze functie is nog niet geï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";
  }
  code += '\nKlik op Annuleren om verdere meldingen te onderdrukken.';
  debugging = confirm(code);
}

function debugConfirm(){
  if(!debugging){return;}
  var code = "";
  for(var i=0; i < arguments.length; i++){
    code += arguments[i] + "\n";
  }
  return confirm(code);
}

