/*
Copyright (c) 2005 Ylab, Utrecht, NL
2005-10-04; yohan@ylab.nl: Created
*/

//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 paperwidth = 540;
var siderwidth = 190;
centerPage();
function centerPage(){
  var margin = getCookie('margin');
  document.write('<style type="text/css">');
  document.write('body {padding-left: '+(1 * siderwidth + margin)+'px; }');
  document.write('</style>');
}
window.onresize = function(){
  var viewport = new Viewport();
  var margin = (viewport.x - paperwidth - siderwidth)/3;
  if (margin < 0){margin = 0;}
  document.body.style.paddingLeft = (1 * siderwidth + margin) + 'px';
  setCookie('margin', margin);
}
//init document
window.onload = function (){
  window.status = document.title;
  window.onresize();
}

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;
  }
}

function getCookie(name){
  //Function to return the value of the cookie specified by "name".
  //name: String object containing the cookie 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;

  while (i < clen) {
    var 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, expires, path, domain, secure){
  //Function to create or update a cookie.
  //name: String object containing the cookie name.
  //value: String object containing the cookie value. May contain any valid string characters.
  //[expires]: Date object containing the expiration data of the cookie.
  //  If omitted or null, expires the cookie at the end of the current session.
  //  If +n, expires n month after the current session.
  //[path]: String object indicating the path for which the cookie is valid. If omitted or null, uses the path of the calling document.
  //[domain]: String object indicating the domain for which the cookie is valid. If omitted or null, uses the domain of the calling document.
  //[secure]: Boolean (true/false) value indicating whether cookie transmission requires a secure channel (HTTPS).
  var numberOfMonths = "" + expires;
	if (numberOfMonths.indexOf("+") == 0){
		var expires = new Date();
		expires.setMonth(expires.getMonth() + numberOfMonths.substring(1));
	}

  document.cookie = name + "=" + escape (value) +
    ((expires) ? "; expires=" + expires.toGMTString() : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");
}

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));
}
