// general namespace (NS) declaration
if (NS == null || typeof(NS) != "object") { var NS = new Object();}
// gallery namespace
if (NS.Tools == null || typeof(NS.Tools) != "object") { NS.Tools = new Object();}

/*
 * ensure the availability of required JS objects
 * To use the class, 
 * 1. create an instance for each domain which its own dependencies
 * 		dep = new NS.Tools.Dependencies();
 * 2. add all dependencies (path to the file which defines them and a function name to test the complete load)
 * 		dep.add('/js/tools/Loader.js', 'NS.Tools.Loader');
 * 3. To ensure that all defined dependencies are met call
 *		dep.check()
 * If the return value is false, you might better wait a couple of milliseconds more before starting the srcipt
 * 
 * 
 */
NS.Tools.Dependencies = function () {

	/**
	 * array containing paths to all dependencies which have to be met
	 * each dependency depends of to parts: 
	 * - the path to the file where the dependency is defined
	 * - and the function name which will be tested to verify the load
	 * dependency.functionname	= String	> name of the function
	 */
	var dependencies = new Array();

	/**
	 * add a dependency 
	 * @path path to the file where the function is defined
	 * @functionname name (String) of the function which should be checked
	 */
	this.add = function(path, functionname) {
		// create dependency object
		var dependency = new String(path);
		dependency.functionname = functionname;
		// add dependency if it not already exists
		if(!eval(functionname)) {
			loadDependency(dependency);
			dependencies.push(dependency);
		}
	}	

	/**	
	 * checks if all passed dependencies are met
	 * @return true, if all dependencies are met
	 */
	this.check = function() {
		pout = true;
		for(i=0; i<dependencies.length; i++) {
			if(!eval(dependencies[i].functionname)) { 
//log.debug('The dependency ' + dependencies[i].functionname + ' does not exist (' + dependencies[i] +')');
				pout = false;
			}
		}
		return pout;
	}
	
	/**	
	 * loads a JavaScript file into the document.
	 * this function is copied from the loadJSfile method of the Loader class
	 * @dependency dependency to load
	 */
	var loadDependency = function(dependency) {
		var script = document.createElement('script');
		script.type = 'text/javascript';
		script.src = dependency;
		document.getElementsByTagName('head')[0].appendChild(script); 
	}	
}
