/**
 * onDOMLoad Javascript Function
 * Author: Ross Illingworth
 * URL: www.techmale.com
 * License: GPL.
 * This takes the work from Dean Edwards,Matthias Miller & John Resig, 
 * at http://dean.edwards.name/weblog/2006/06/again/#comment5338 and 
 * makes it into a useable function for all browsers.
 */
	
	// SafariTimer: holds the id of the timer for safari browser
	window.sFT = false;
	//DOMLoadFunctions: Array of functions to run when DOM is loaded
	window.onDLFs = [];
	// Function to call when onDOMload activates
	window.callDLFs = function(){
		// check if we have already been here.
		if (arguments.callee.done) {
			//alert("onDOMLoad Functions already run");
			return;
		}
		arguments.callee.done = true;
		// Stop safariTimer if Active
		if (sFT) {
			clearInterval(sFT);
		}
		//Now loop through functions and call them
		var func;
		for(i in  onDLFs){
			if(typeof (func = onDLFs[i]) == "function"){
				//alert(func +" = "+ typeof func);
				func.apply(document);
			}
			
		}
	};
	
	// MAIN Function
	function onDOMLoad(func){
		// add your function to the function array
		onDLFs.push(func);
		// check if we have already done the setup
		if (arguments.callee.done) {
			//alert("Already setup doOnDOMLoaded");
			return;
		}
		arguments.callee.done = true;
		
		//this will work on Firefox or IE
		if (document.addEventListener){
			//FOR FIREFOX
			//alert("Using addEventListener");
			document.addEventListener("DOMContentLoaded",callDLFs, false);
		}else if (document.all && !window.opera){
			//FOR IE
			document.write('<script type="text/javascript" id="contentloadtag" defer="defer" src="javascript:void(0);"><\/script>');
			var contentloadtag=document.getElementById("contentloadtag");
			contentloadtag.onreadystatechange=function(){
				if (this.readyState=="complete"){
					//alert("Using readystate");
					callDLFs.apply(document);
				}
			};
		}
		
		// THIS WORKS on SAFARI
		if(/Safari/i.test(navigator.userAgent)){
		  sFT = setInterval(function(){
		  	if(/loaded|complete/.test(document.readyState)){
		    	clearInterval(sFT);
				//alert("Using Safari");
		    	// call target function
				callDLFs.apply(document);
		  	}
		  }, 10);
		}
		
		//and finally if all else fails, use onload
		// first check if anything has been set up previously
		var old = (window.onload) ? window.onload : function () {};
		window.onload = function(e) {
			//alert("Now calling onLoad"); 
			old(e); 
			callDLFs();
		};
	}
