//  Create global namespace and add include function
var global = {
	
	//  Object properties
	includes: [],
	includePath: "",
	popups: [],
	
	//  This function is used to include other scripts
	include: function include(path, prependIncludePath) {

		//  If the second argument isn't passed, default to true
		if (arguments.length < 2) {
			prependIncludePath = true;	
		}
		
		//  Prepend the include path is necessary
		if (prependIncludePath) {
			path = this.getIncludePath() + path;	
		}

		//  Only include the script if it hasn't already been included
		if (this.includes.toString().indexOf(path) == -1) {
			
			//  Write the script tag
			document.write('<script type="text/javascript" src="' + path + '"></script>');
			
			//  Append the path to the includes array
			this.includes.push(path);
			
		}
	
	},
	
	//  Sets the include path
	setIncludePath: function() {
		var scripts = document.getElementsByTagName("script");
		var scriptPath = scripts[scripts.length-1].getAttribute("src");
		this.includePath = scriptPath.substring(0, scriptPath.lastIndexOf("/"));
	},
	
	//  Get the include path
	getIncludePath: function() {
		return this.includePath;	
	},
	
	//  Toggle Popup
	togglePopup: function(ev) {
		global.hidePopups(ev, this);
		this.fade("toggle");
		return false;
	},
	
	//  Hide Popups
	hidePopups: function(ev, obj) {
		for (var i = 0, numPopups = this.popups.length; i < numPopups; ++i) {
			if ((this.popups[i] != ev.target) && !this.popups[i].hasChild(ev.target) && (this.popups[i] != obj)) {
				this.popups[i].fade("hide");
			}
		}
	},
	
	initPopups: function() {
		var thisObj = this;
		document.addEvent("click", function(ev) {
			thisObj.hidePopups(ev);				
		});
	}
	
};
global.setIncludePath();

//  Include the config file
global.include("/config.js");

//  Include the JS library
global.include("/lib.js");
global.include("/lib-plugins.js");

//  Include utilities library
global.include("/utils.js");

//  Include the DropDownMenu object
global.include("/DropDownMenu.js");

//  Include the Calendar object
//global.include("/Calendar/Calendar.js");

//  Include the DaoForm object and its helpers
global.include("/DaoForm.js");
global.include("/DaoFieldHint.js");
global.include("/Textarea.js");

//  Include the SaveForm object
global.include("/SaveForm.js");

//  Include the AutoComplete Objects
global.include("/Autocompleter/Autocompleter.js");
global.include("/Autocompleter/Autocompleter.Request.js");
global.include("/Observer.js");

//  Include the CriteriaManager Object
global.include("/CriteriaManager.js");

//  Include the AlertBox
global.include("/AlertBox/AlertBox.js");

//  Include the Slimbox
global.include("/Slimbox/Slimbox.js");

//  Include the AlbumManager Object
global.include("/AlbumManager.js");

//  Include AlbumViewer
global.include("/AlbumViewer.js");

//  Include InviteForm
global.include("/InviteForm.js");

//  Include the main controller
global.include("/run.js");