// this code will only included when it's running in debug mode.
var DEBUG = false;
var BASE_JS_URL = "/includes/javascript/";
// These 2 variables indicate whether DOMContentLoaded event and window.onload
// have already happened
var isContentLoaded = false;
var isLoaded = false;
Event.observe(document, "dom:loaded", function() {
	isContentLoaded = true;
});
Event.observe(window, "load", function() {
	isLoaded = true;
});
var enableConsole = Prototype.Browser.Gecko || Prototype.Browser.WebKit;

var scripts = [{
	name: "effects.js"
}, {
	name: "effect_wrapper.js"
}, {
	name: "dragdrop.js"
}, {
	name: "popup.js"
}, {
	name: "general.js"
}, {
	name: "Pagebase.js"
}, {
	name: "ada_key.js"
}, {
	name: "top_menu.js"
}, {
	name: "gallery.js"
}, {
	name: "videos.js"
}, {
	name: "quote.js"
}, {
	name: "playlist.js"
}, {
	name: "map_lib.js"
}, {
	name: "billboard.js"
}, {
	name: "billpromotions.js"
}, {
	name: "Information.js"
}, {
	name: "map.js"
}, {
	name: "explorer.js"
}];

function evalJS() {
	try {
		var scriptContent = "";
		scripts.each(function(script, index) {
			if (DEBUG && Prototype.Browser.Gecko) {
				var tag = new Element("script");
				tag.type = "text/javascript";
				tag.src = BASE_JS_URL + script.name;
				document.documentElement.appendChild(tag);
			} else {
				scriptContent += script.content + "\n";
			}
		});
		if (!DEBUG || (DEBUG && !Prototype.Browser.Gecko)) {
			if (Prototype.Browser.IE) {
				window.execScript(scriptContent);
			} else if (Prototype.Browser.WebKit) {
				window.setTimeout(scriptContent, 0);
			} else {
				window.eval(scriptContent);
			}
		}
		scripts = null;
	} catch (e) {
		if (DEBUG && enableConsole) {
			console.log(e.message);
		}
	}
}

(function () {
	if (DEBUG && enableConsole) {
		console.log("Application is running in debug mode 2.\n"
			+ "To disable debug mode, modify the following line in prototype.js:\n"
			+ "var DEBUG=false;");
	}
	var loadedCount = 0;
	scripts.each(function(script, index) {
		new Ajax.Request(BASE_JS_URL + script.name, {
			method: "GET",
			evalJS: false,
			onComplete: function(response) {
				script.content = response.responseText;
				if (++loadedCount != scripts.length) 
					return;
				if (isContentLoaded) {
					evalJS();
				}
				else {
					Event.observe(document, "dom:loaded", evalJS);
				}
			},
			onFailure: function(response) {
				if (DEBUG && enableConsole) {
					console.log("failed to load one or more files, please refresh the page.");
				}
			}
		});
	});
})();

