﻿/*
Generic functions for www.ylab.nl
Copyright (c) 2003 Ylab, Utrecht, NL
Author: Yohan Creemers
version 1.0
*/

//generic event handling
/*
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 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;");

//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;
  }
}

/*open popup window
  Sample use:
  <a href="url" title="Pagina opent in een nieuw venster" onclick="return openPopup(this.href, 500, 600, true);" >De pagina</a>

  url      : location of the desired document
  width    : width of the popup window [optional]
  height   : height of the popup window [optional]
*/
function openPopup(url, width, height, resizable, toolbar, menubar) {
  var args = new Array();
  if (width){
    width = Math.min(width, screen.availWidth);
    args.push("width=" + width);
    args.push("left=" + (screen.width - width)/2);
  }
  if (height){
    height = Math.min(height, screen.availHeight);
    args.push("height=" + height);
    args.push("top=" + (screen.height - height)/2);
  }
  if (resizable){args.push("resizable=yes,scrollbars=yes");}
  if (toolbar){args.push("toolbar=yes");}
  if (menubar){args.push("menubar=yes");}
  window.status=args.join();
  window.open(url, '', args.join());
  return false;
}

function styleAbbr() {
  if(document.all){
    var oldBodyText = document.body.innerHTML;
    var reg = /<ABBR([^>]*)>([^<]*)<\/ABBR>/g;
    var newBodyText = oldBodyText.replace(reg, '<abbr $1><span class=\"abbr\" $1>$2</span></abbr>');
    document.body.innerHTML = newBodyText;
    alert(oldBodyText==newBodyText);
  }
}


/* Reference Article:
Dustin Diaz:
http://www.dustindiaz.com/top-ten-javascript/
*/

/* addEvent: simplified event attachment */
function addEvent( obj, type, fn ) {
	if (obj.addEventListener) {
		obj.addEventListener( type, fn, false );
		EventCache.add(obj, type, fn);
	}
	else if (obj.attachEvent) {
		obj["e"+type+fn] = fn;
		obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
		obj.attachEvent( "on"+type, obj[type+fn] );
		EventCache.add(obj, type, fn);
	}
	else {
		obj["on"+type] = obj["e"+type+fn];
	}
}

var EventCache = function(){
	var listEvents = [];
	return {
		listEvents : listEvents,
		add : function(node, sEventName, fHandler){
			listEvents.push(arguments);
		},
		flush : function(){
			var i, item;
			for(i = listEvents.length - 1; i >= 0; i = i - 1){
				item = listEvents[i];
				if(item[0].removeEventListener){
					item[0].removeEventListener(item[1], item[2], item[3]);
				};
				if(item[1].substring(0, 2) != "on"){
					item[1] = "on" + item[1];
				};
				if(item[0].detachEvent){
					item[0].detachEvent(item[1], item[2]);
				};
				item[0][item[1]] = null;
			};
		}
	};
}();
addEvent(window,'unload',EventCache.flush);

/* window 'load' attachment */
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	}
	else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

/* grab Elements from the DOM by className */
function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

/* toggle an element's display */
function toggle(obj) {
	var el = document.getElementById(obj);
	if ( el.style.display != 'none' ) {
		el.style.display = 'none';
	}
	else {
		el.style.display = '';
	}
}

/* insert an element after a particular node */
function insertAfter(parent, node, referenceNode) {
	parent.insertBefore(node, referenceNode.nextSibling);
}

/* Array prototype, matches value in array: returns bool */
Array.prototype.inArray = function (value) {
	var i;
	for (i=0; i < this.length; i++) {
		if (this[i] === value) {
			return true;
		}
	}
	return false;
};

/* get, set, and delete cookies */
function getCookie( name ) {
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
		return null;
	}
	if ( start == -1 ) return null;
	var end = document.cookie.indexOf( ";", len );
	if ( end == -1 ) end = document.cookie.length;
	return unescape( document.cookie.substring( len, end ) );
}

function setCookie( name, value, expires, path, domain, secure ) {
	var today = new Date();
	today.setTime( today.getTime() );
	if ( expires ) {
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );
	document.cookie = name+"="+escape( value ) +
		( ( expires ) ? ";expires="+expires_date.toGMTString() : "" ) + //expires.toGMTString()
		( ( path ) ? ";path=" + path : "" ) +
		( ( domain ) ? ";domain=" + domain : "" ) +
		( ( secure ) ? ";secure" : "" );
}

function deleteCookie( name, path, domain ) {
	if ( getCookie( name ) ) document.cookie = name + "=" +
			( ( path ) ? ";path=" + path : "") +
			( ( domain ) ? ";domain=" + domain : "" ) +
			";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

/* quick getElement reference */
function $() {
	var elements = new Array();
	for (var i = 0; i < arguments.length; i++) {
		var element = arguments[i];
		if (typeof element == 'string')
			element = document.getElementById(element);
		if (arguments.length == 1)
			return element;
		elements.push(element);
	}
	return elements;
}

/*
Copyright (c) 2006 Ylab, http://www.ylab.nl

Example use:
var currentViewport = new Viewport();
window.status = 'Height: ' + currentViewport.height;
20060927:yc:extended with scrollLeft and scrollTop
*/

function Viewport(){
  this.width = 0;
  this.height = 0;
  if (self.innerHeight){
    // all except Explorer
  	this.width = self.innerWidth;
  	this.height = self.innerHeight;
  	this.scrollLeft = self.pageXOffset;
  	this.scrollTop = self.pageYOffset;
  }
  else{
    // Explorer Strict Mode or CompatMode
    var iebody = (document.compatMode && document.compatMode != "BackCompat") ? document.documentElement : document.body;
  	this.width = iebody.clientWidth;
  	this.height = iebody.clientHeight;
  	this.scrollLeft = iebody.scrollLeft;
  	this.scrollTop = iebody.scrollTop;
  }
}

function Outerport(){
  this.width = 0;
  this.height = 0;
  if (window.outerWidth){
    this.width = window.outerWidth;
    this.height = window.outerHeight;
  }
  else if (document.body){
    // other Explorers
  	this.width = document.body.clientWidth;
  	this.height = document.body.clientHeight;
  }
}

function absoluteTop(){
  var t = this.offsetTop;
  if(this.offsetParent){
    t += absoluteTop.apply(this.offsetParent);
  }
  return t;
}

function inView(){
  var abstop = absoluteTop.apply(this);
  var vp = new Viewport();
  var delta = vp.height + vp.scrollTop - abstop - 20;
  if(delta < 0){
    if(this.scrollIntoView){
      this.scrollIntoView();
    }
    else{
	    window.scrollBy(0, -1 * delta);
	  }
	}
}
