/* (CC) 2007 Info.nl
Default javascript file
Version 1.1 - release date: march 5, 2007

Structure of this file:         
1. Implementation of class:
	- Main
2. Definition global variables
3. Implementation of functions:
	- Main.init
	- Main.myDOMContentLoaded
	- Main.eventCache (function add & flush)
	- Main.debugAlert (alert with cancel)
4. Call to start application:
	- Main.myDOMContentLoaded()
	
'classes': */
var Main = {}

/* global variables */
Main.debug = false; //used for debugging - do not change
Main.allowAlert = true; //used for debugging - do not change
Main.IE5 = navigator.appVersion.indexOf("MSIE 5")!=-1 ? true:false;

/* Init function to start all JavaScript
Please note that this function is already called when DOM is loaded,
so not on window.onload! */
Main.init = function() {
	// quit if this function has already been called
	if (arguments.callee.done) return;

	// flag this function so we don't do the same thing twice
	arguments.callee.done = true;
	
	//turns native alert into confirm while debugging -> is not supported in ie5.5
	if(!Main.IE5) { window.alert = Main.debugAlert; } 
	
	doCustomCalls();

	// Garbage collector
	window.onunload = Main.eventCache.flush;
}

/* startBeforeOnload: This function will be called as soon as DOM is loaded
(so we don't have to wait until onload triggers)
For further documentation on this code see:
http://dean.edwards.name/weblog/2005/09/busted/
http://dean.edwards.name/weblog/2006/06/again/ */
Main.myDOMContentLoaded = function() {
	/* for Mozilla/Opera9 */
	if (document.addEventListener) { document.addEventListener("DOMContentLoaded", Main.init, false); }

 	/* for Internet Explorer - yes this correct */
	/*@cc_on @*/
	/*@if (@_win32)
   	document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
    	var script = document.getElementById("__ie_onload");
    	script.onreadystatechange = function() {
        	if (this.readyState == "complete") {
				Main.init(); // call the onload handler
        	}
    	};
	/*@end @*/

	/* for Safari */
	if (/WebKit/i.test(navigator.userAgent)) { // sniff
		var _timer = setInterval(function() {
			if (/loaded|complete/.test(document.readyState)) {
				Main.init(); // call the onload handler
			}
		}, 10);
	}

	/* for other browsers */
	window.onload = Main.init;
}


/* keeping track on all the attached events (add) and detaching them (flush)
for more documenatation see: http://novemberborn.net/javascript/event-cache */
Main.eventCache = function(){
	var listEvents = [];
	
	/*  Implement array.push for browsers which don't support it natively. (used in EventCache)
	Please remove this if it's already in other code */
	if(Array.prototype.push == null){
		Array.prototype.push = function(){
			for(var i = 0; i < arguments.length; i++){
				this[this.length] = arguments[i];
	       	};
	        return this.length;
		};
	};
     
    return {
		listEvents : listEvents,
     
		add : 	function(node, sEventName, fHandler, bCapture){
					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]); };
                             
					/* From this point on we need the event names to be prefixed with 'on" */
					if(item[1].substring(0, 2) != "on") { item[1] = "on" + item[1]; };
                     
					//see http://msdn2.microsoft.com/en-us/library/ms536411.aspx       
					if (item[0].detachEvent) {  
						item[0].detachEvent(item[1], item[2]); 
						//Main.debugAlert(item[0].id + "." + "detachEvent (" + item[1] + ", " + item[2] + ")");
					};
                    
					item[0][item[1]] = null;
				};
		}
      };
}();

Main.debugAlert = function (message) {
		if(Main.allowAlert && Main.debug) { Main.allowAlert = confirm(message); }
	}

Main.errHandler = function (err) {
		var errorText = "";
		for (var i in err) { errorText += i + "=" + err[i] + "\n"; }
		Main.debugAlert("An error has occured: \n\n" + errorText + "\nSee Firefox browser for correct linenumbers."); 			
		return true;	
	}	

/* Start javascript application
Note: Call must be placed at the end of the script file, to be sure that all JS-code is loaded! */
Main.myDOMContentLoaded(); 
