/**
 * Includes js files and runs DOM ready code
 */

var LibMan = {
	
	// String : The path to libman.js
	path: null,

	/**
	 * Calculates and sets the relative path to libman.js
	 *
	 * @return void  
	 */
	calculatePath: function() {
		// condition : is object var already set?
		if (this.path == null) {
			// get path from LibraryManager javascript include string
			var libman = document.getElementById("libman");
			if (!libman) return false;
			
			// if so, remove the filename before setting the path 
			//this.path = libman.src.replace(/manager\.js(\?.*)?$/,'../../');
			this.path = "/_includes/js/";
		}
	},


	/**
	 * Wrapper for requireFile, allows single filename or array
	 * of filenames to be passed in
	 * 
	 * @param mixed libFiles string or array of paths to files 
	 * to include, absolute or relative to libman.js
	 *
	 * @return void  
	 */
	requireFiles: function(libFiles) {
		if (typeof(libFiles) == "string") { // single file to include (called from php)
			LibMan.requireFile(libFiles);
		} else { // array of files to include (called from libman.js)
			for (var i=0; i < libFiles.length; i++) {
				LibMan.requireFile(libFiles[i]);
			};
		}
	},
	
	
	/**
	 * Includes a js file
	 * 
	 * @param string libFile path to file to include, 
	 * absolute or relative to libman.js
	 *
	 * @return void  
	 */
	requireFile: function(libFile){
		// match strings starting with 'http://'
		var pattern = /^http:\/\//;
		
		// if libFile matches pattern, path is absolute, otherwise it's relative
		libFile = (pattern.test(libFile)) ? libFile : this.path + libFile;
		document.write('<script type="text/javascript" src="'+ libFile +'"></script>');
	}
};


/*
 * Fixes - no better place... ?
 */
	// stop firebug console errors in browsers that don't support it
	if(!window.console)	 { window.console = {}; };
	if(!window.console.log) { window.console.log = function(){}; };

	// image flicker fix for IE6
	try {
	  document.execCommand('BackgroundImageCache', false, true);
	} catch(e) {}
