/*************************************************************************
 CDM Script Manager 1.0
 Canwest Digital Media
 February 18, 2009
 
 DESCRIPTION:
	The script manager keeps track of previously loaded scripts and won't 
	re-load the same script name twice (unless specified).

 USAGE:
	Place this script (CDM Script Manager) within the HEAD tags of the
	master page, before any Java Script includes.
		<script type="text/javascript" src="/js/CDMScriptManager-1.0.js"></script>

	Replace existing Java Script includes:
		<script type="text/javascript" src="/js/myscript-1.02.js"></script>
	with the following:
		<script type="text/javascript">CDMScriptManager.load('/js/myscript-1.02.js');</script>

 FUNCTIONS:
	CDMScriptManager.load('[scriptPath]','[continueLoadOnConflict]');
		scriptPath - Path for script to load
		continueLoadOnConflict - [true|false] Force load script if it has 
								 already been loaded (Default = false)
	
	CDMScriptManager.getLoadedScripts();
		Returns an array of objects containing the info for scripts loaded

	CDMScriptManager.getQuickInfo();
		Returns a string showing the loaded scripts.		 
*/

/* Script Object */
function CDMScript(filename, path) {
	this.path = path;
	this.filename = filename;
}

/* Script Array */
function CDMScriptManager(name) {
	this.name = name;
	this.scriptCollection = new Array();
}	

/* Script Manager Functions */
CDMScriptManager.prototype = {								
	load: function(scriptPath, continueLoadOnConflict) {
		if(continueLoadOnConflict == 'undefined')
			continueLoadOnConflict = false;

		if(scriptPath == undefined) {
			document.write('<scr' + 'ipt><!-- Path is required to load "' + scriptPath + '" --></scr' + 'ipt>');
			return false;
		}

		var scriptFilename = this.getFilenameFromPath(scriptPath);				
		var isLoaded = this.isLoaded(scriptFilename);

		if(isLoaded != null)
			document.write('<scr' + 'ipt><!-- Warning: "'+scriptFilename+'" has been previously loaded. --></scr' + 'ipt>');
		
		if(isLoaded == null || continueLoadOnConflict) {										
			this.scriptCollection.push(new CDMScript(scriptFilename, scriptPath));

			document.write('<scr' + 'ipt type="text/javascript" src="' + scriptPath + '"></scr' + 'ipt>');
			
			return true;					
		}
		return false;
	},
	isLoaded: function(scriptFilename) {
		for(i=0;i<this.scriptCollection.length;i++) {
			if(this.scriptCollection[i].filename == scriptFilename) {
				return this.scriptCollection[i];
			}
		}
		return null;
	},
	getFilenameFromPath: function(path) {
		var regex = new RegExp(/([^\/\\]+)$/);
		var result = regex.exec(path);
		if (result == null)
			return null;
		else
			return result[0];
	},
	getLoadedScripts: function() {
		return this.scriptCollection;
	},
	getQuickInfo: function() {
		var result = '';
		for(i=0;i<this.scriptCollection.length;i++) {
			result += this.scriptCollection[i].path;
			result += (i != this.scriptCollection.length - 1) ? ', ' : '';
		}
		return result;
	}
}

/* Initialize Script Manager */
var CDMScriptManager = new CDMScriptManager('CDM Script Manager');