/**
 * @section   : Global JavaScript functions
 * @project   : Installateur
 * @author    : Boye Oomens <boye@e-sites.nl>
 * @version   : 1.0
 */

function setExtLinks(){
	if (!document.getElementsByTagName) return;
	var anchors = document.getElementsByTagName("a");
	var i = anchors.length;
	while (i--) {
		var anchor = anchors[i];
		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") {
			anchor.target = "_blank";
		}		
	}
}

function initCufon() {
	
	if ( typeof Cufon === 'undefined' || Cufon === undefined ) {
		return;
	}	
	
	Cufon
		.set('fontFamily', 'Dutch 811')
			.replace('h1');
	
	if ( document.all ) {
		Cufon.now();
	}
}

function setWrapperHeight() {
	var dim = getPageSize();
	document.getElementById('mdm').style.height = dim[1] + 'px';
	//document.getElementById('innerWrapper').style.height = dim[1] + 'px';
}

var DomLoaded =
{
	onload: [],
	loaded: function()
	{
		if (arguments.callee.done) return;
		arguments.callee.done = true;
		for (i = 0;i < DomLoaded.onload.length;i++) DomLoaded.onload[i]();
	},
	load: function(fireThis)
	{
		this.onload.push(fireThis);
		if (document.addEventListener) 
			document.addEventListener("DOMContentLoaded", DomLoaded.loaded, null);
		if (/KHTML|WebKit/i.test(navigator.userAgent))
		{ 
			var _timer = setInterval(function()
			{
				if (/loaded|complete/.test(document.readyState))
				{
					clearInterval(_timer);
					delete _timer;
					DomLoaded.loaded();
				}
			}, 10);
		}
		/*@cc_on @*/
		/*@if (@_win32)
		var proto = "src='javascript:void(0)'";
		if (location.protocol == "https:") proto = "src=//0";
		document.write("<scr"+"ipt id=__ie_onload defer " + proto + "><\/scr"+"ipt>");
		var script = document.getElementById("__ie_onload");
		script.onreadystatechange = function() {
		    if (this.readyState == "complete") {
		        DomLoaded.loaded();
		    }
		};
		/*@end @*/
	   window.onload = DomLoaded.loaded;
	}
};


function getPageSize() {
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth;
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else {
		pageHeight = yScroll;
	}
	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){
		pageWidth = xScroll;
	} else {
		pageWidth = windowWidth;
	}
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
	return arrayPageSize;
}

function setExtLinks(){
	if (!document.getElementsByTagName) return;
	var anchors = document.getElementsByTagName("a");
	var i = anchors.length;
	while (i--) {
		var anchor = anchors[i];
		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") {
			anchor.target = "_blank";
		}		
	}
}

// Original by Dean Edwards & Tino Zijdel
function addEvent(element, type, handler) {
	var self = this;
	this.fixEvent = function(event) {
		event.preventDefault = function() {this.returnValue = false;}
		event.stopPropagation = function() {this.cancelBubble = true;}
		return event;
	}
	if (typeof this.guid == "undefined") {this.guid = 1;}
	if (element.addEventListener) {
		element.addEventListener(type, handler, false);
	} else {
		if (!handler.$$guid) {handler.$$guid = this.guid++;}
		if (!element.events) {element.events = {};}
		var handlers = element.events[type];
		if (!handlers) {
			handlers = element.events[type] = {};
			if (element['on' + type]) {handlers[0] = element['on' + type];}
			element['on' + type] = function(event) {
				event = event || self.fixEvent(window.event);
				var returnValue = true;
				var handlers = this.events[event.type];
				for (var i in handlers) {
					if (!Object.prototype[i]) {
						this.$$handler = handlers[i];
						if (this.$$handler(event) === false) {returnValue = false;}
					}
				}
				if (this.$$handler) {this.$$handler = null;}
				return returnValue;
			};
		}
		handlers[handler.$$guid] = handler;
	}
}
