/*
	Author: 			tom@info.nl
	Last change date: 	2007-02-23
	Comments: 			--
	
	Structure of this file:		1. Definition global variables
								2. Implementation of these classes:
									- Main
									- InfoMain
									- EventCache
								3. Call to start application:
									- Main.myDOMContentLoaded()
*/

/*
===================================================================
	G L O B A L   V A R I A B L E S
	This code defines the following global variables
	Please avoid name clashing
		-	Main
		-	InfoMain
		-	EventCache
===================================================================
*/
/* variables: */
var _bodybg = ""; // global variable for setting the background image of the body dynamically
var _imgdir = "/web/static/project/img/"; // path to image directory

/* 'classes': */
var Main;
var InfoMain;
var EventCache;

/*	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;
	};
};


/*
===================================================================
	E N D   G L O B A L   V A R I A B L E S
===================================================================
*/


/*
===================================================================
	C L A S S   M A I N 
===================================================================
	The 'class' Main contains important JavaScript code which is not specific for this site
	This class implements the following methods:
		-	init
		-	myDOMContentLoaded
*/
Main = {
	/*
		Init function to start all JavaScript
		Please note that this function is already called when DOM is loaded,
			so not on window.onload!
	*/
	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;
	
		// A D D   Y O U R   C U S T O M   C A L L S   H E R E :
		InfoMain.init();
		
		// Garbage collector
		window.onunload = EventCache.flush;
	
	},

	/*
		startBeforeOnload
		This function will be called as soon as DOM is loaded
		(so we don't have to wait until onload triggers)
		See : http://dean.edwards.name/weblog/2006/06/again/
	*/
	myDOMContentLoaded: function() {
		/* for Mozilla/Opera9 */
		if (document.addEventListener) {
		    document.addEventListener("DOMContentLoaded", Main.init, false);
		}
		
		/* for Internet Explorer */
		/*@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;
	}
}
/*
===================================================================
	E N D 	C L A S S   M A I N 
===================================================================
*/

/*
===================================================================
	C L A S S   I N F O M A I N 
===================================================================
	The 'class' InfoMain contains all Info-specific code
	All code is wrapped in the object literal InfoMain to prevent function clashing
	This class has the following private variables:
		-	tel
	This class implements the following methods:
		- 	init
		-	addHomepageMOHandlers
		-	addNewWindowHandlers
		-	addPortfolioImages
		-	changeBgImage
		- 	openNewWindow
		-	pascha
		-	paschaGordo
		-	writeLinkImg
*/
InfoMain = {
	tel: 0,
	/* sort of class constructor - called from Main.init() */
	init: function() {
		InfoMain.changeBgImage();
		InfoMain.addNewWindowHandlers();
		InfoMain.addHomepageMOHandlers();
		InfoMain.addPortfolioImages();
	},
	/* Add mouse over handlers for the homepage */
	addHomepageMOHandlers: function() {
		var homepage = (document.body.className.indexOf("homepage")>-1) ? true : false;
		if (homepage) {
			var img = document.getElementById("content").getElementsByTagName("img");
			for (var i=0; i<img.length; i++) {
				img[i].onmouseover = function() {this.style.top = '-145px';}
				img[i].onmouseout = function() {this.style.top = '0';}
				EventCache.add(img[i], "mouseover", function() {this.style.top = '-145px';}, false)
				EventCache.add(img[i], "mouseout", function() {this.style.top = '0';}, false)
			}
			
		}
	},
	/* Add handlers for links that must be opened in a new window */
	addNewWindowHandlers: function() {
		if (document.getElementById("content")) {
			var a = document.getElementById("content").getElementsByTagName("a");
			for (var i=0; i<a.length; i++) {
				if (a[i].className.indexOf("_blank") > -1) {
					a[i].onclick = function() {return InfoMain.openNewWindow(this);}
					EventCache.add(a[i], "click", function() {return InfoMain.openNewWindow(this);}, false)
				}
			}
		}
	},
	/* Add images to the links on the Portfolio overview page */
	addPortfolioImages: function() {
		var t;
		if (document.body.className.indexOf("portfolio ") > -1 && document.getElementById("content")) {
			var c = document.getElementById("content").getElementsByTagName("a");
			for (var i=0; i < c.length; i++) {
				t = c[i].innerHTML;
				c[i].innerHTML = "";
				c[i].appendChild(InfoMain.writeLinkImg());
				c[i].innerHTML += t;
			}
		}
	},
	/* Change the global background image, or - if it is not set - set the default background image */
	changeBgImage: function() {
		if (document.body) {
			if (_bodybg != "") {
				document.body.style.backgroundImage = "url(" + _bodybg + ")";
			} else {
				document.body.className += " defaultimg";
			}
		}
	},
	/* Open link in new window */
	openNewWindow: function(el) {
		if (!el.href) return true;
		window.open(el.href);
		return false;
	},
	/* Create the following img <img src="img/link_same_window.gif" alt="" /> - needed in Portfolio overview */
	writeLinkImg: function () {
		var n = document.createElement("img");
		n.setAttribute("src", _imgdir + "link_same_window.gif");
		n.setAttribute("alt", "");
		return n;
	}
}
/*
===================================================================
	E N D 	C L A S S   I N F O M A I N 
===================================================================
*/


/*
===================================================================
	C L A S S   E V E N T C A C H E
===================================================================
	EventCache Version 1.0
	Copyright 2005 Mark Wubben

	Provides a way for automagically removing events from nodes and thus preventing memory leakage.
	See <http://novemberborn.net/javascript/event-cache> for more information.
	
	This software is licensed under the CC-GNU LGPL <http://creativecommons.org/licenses/LGPL/2.1/>
*/

/*	Event Cache uses an anonymous function to create a hidden scope chain.
	This is to prevent scoping issues. */
EventCache = function(){
	var listEvents = [];
	
	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];
				};
				
				if(item[0].detachEvent){
					item[0].detachEvent(item[1], item[2]);
				};
				
				item[0][item[1]] = null;
			};
		}
	};
}();
/*
===================================================================
	E N D   C L A S S   E V E N T C A C H E
===================================================================
*/


/*
	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();