var bkahlert = {
	clone: function(obj){
		if(obj == null || typeof(obj) != 'object')
			return obj;
	
		var temp = new obj.constructor(); // changed (twice)
		for(var key in obj)
			temp[key] = bkahlert.clone(obj[key]);
	
		return temp;
	},

	getJsLocation: function(filename) {
		var scripts = document.getElementsByTagName('script');
		if(scripts) {
			for(var i=0; i<scripts.length; i++) {
				var index = scripts[i].src.indexOf(filename);
				if(index != -1) {
					return scripts[i].src.substr(0, index);
				}
			}
		}
		
		return null;
	},
	
	jsRoot: function() {
		return bkahlert.getJsLocation("global.js");
	},
	
	aloadScript: function(url, callback) {
		var script = document.createElement("script")
		script.type = "text/javascript";
	
		if (script.readyState){  //IE
			script.onreadystatechange = function(){
				if (script.readyState == "loaded" ||
						script.readyState == "complete"){
					script.onreadystatechange = null;
					callback();
				}
			};
		} else {  //Others
			script.onload = function(){
				callback();
			};
		}

	    script.src = url;
    	(document.getElementsByTagName('HEAD')[0] || document.body).appendChild(script);
	},
	
	aloadScripts: function(urls, callback) {
		var callsNeeded = urls.length;
		var callbackCalled = false;
		var iCallback = function() {
			callsNeeded--;
			if(callsNeeded <= 0 && !callbackCalled) {
				callbackCalled = true;
				callback();
			}
		}
		
		for(var i=0; i<urls.length; i++) {
			bkahlert.aloadScript(urls[i], iCallback);
		}
	},
	
	init: function() {
		var jsRoot = bkahlert.jsRoot();
		bkahlert.aloadScripts([
			jsRoot+"jquery-1.4.2.min.js",
			jsRoot+"jquery-ui-1.8.1.custom.min.js",
			jsRoot+"raphael.min.js"
			], function() {
				/*bkahlert.aloadScripts([
					jsRoot+"jquery-ui-1.8.1.custom.min.js"
				], function() { bkahlert.pageLoaded(); });*/
				bkahlert.pageLoaded();
			});
	},
	
	pageLoaded: function() {
		$ = jQuery;

		/*
		 * Color the navigation link that corresponds to the actually opened URI.
		 */
		$(document).ready(function() {
			var first_dir = location.pathname.split("/", 2)[1];
			var nav_items = $(".navigation a");
			$(nav_items).each(function() {
				if(this.href.indexOf(first_dir) > 0) {
					$(this).addClass("active");
				}
			});
		});
		
		/*
		 * Add hover effect to images with an src attr of the form 'image.out.png'.
		 * As soon a the user hovers over the image it's source changes to 'image.over.png'
		 */
		$(document).ready(function() {
			$($("img")).each(function() {
				var out_parts = this.src.split(".");
				if(out_parts.length > 1 && out_parts[out_parts.length-2] == "out") {
					var over_parts = bkahlert.clone(out_parts);
					over_parts[over_parts.length-2] = "over";
					
					var out_src = out_parts.join(".");
					var over_src = over_parts.join(".");
					$(this).hover(function() { this.src = over_src; }, function() { this.src = out_src; });
				}
			});
		});
	}
}

bkahlert.init();
