/**
 * This file is reponsible for managing the js library of classes and functions
 *
 * @author Pete Goodman
*/
var LibraryManager = {

	/**
	 * The path to the root of the site
	 *
	 * @var string path 
	 */
	path: null,
	
	/**
	 * Flag to indicate if we're in the cms or not
	 *
	 * @var bool is cms flag 
	 */
	iscms: null,
	
	
	/**
	 * Calculates and sets the relative path from the js library to the root
	 *
	 * @return void  
	 */
	 calculatePath: function() {
		
		// condition : is object var already set?
		if (this.path==null) {
			
			// get path from LibraryManager javascript include string
			var scriptTags = document.getElementsByTagName("script");
			for(var i=0;i<scriptTags.length;i++) {
				if(scriptTags[i].src && scriptTags[i].src.match(/librarymanager\.js(\?.*)?$/)) {
					this.path = scriptTags[i].src.replace(/librarymanager\.js(\?.*)?$/,'');
					break;
				}
			}
		}
	},
	
	
	/**
	 * Calculates state of site, are we in cms or not?
	 *
	 * @return void  
	 */
	 calculateState: function() {
		
		// condition : is object var already set?
		if (this.iscms==null) {
			// condition : is _cms in url?
			if(location.href.match(/_cms/)) {
				this.iscms = true;
			} else {
				this.iscms = false;
			}
		}
	},
	
	
	/**
	 * Includes a js file
	 * 
	 * @param string libraryfile The path from the js library root to the file
	 *
	 * @return void  
	 */
	require: function(libraryfile) {
		document.write('<script type="text/javascript" src="'+ this.path + libraryfile +'"></script>');
	},
	
	
	/**
	 * Ensures functions are only run once the core library is loaded
	 * Detects when the DOM tree has loaded, then triggers all onload events
	 * 
	 * @param function func The function to execute
	 *
	 * @return void  
	 */
	addLoadEvent: function (func) {
		
		//set timer to check every quarter of a second whether the body tag has been detected
		var domcheck = setInterval(function() {

			if(document.getElementsByTagName("body")[0] != null) {

				//run functions and remove timer
				clearInterval(domcheck);
				func();

			}
		}, 250);

		//check if the browser is firefox
		if (typeof document.addEventListener != "undefined") {

			//for firefox, use the built in event listener for the DOM
			document.addEventListener("DOMContentLoaded", function() { 
				
				//run functions and remove timer
				clearInterval(domcheck);
				func();
			
			} , null);
		}
	},
	

	/**
	 * function to add an event-listener (cross-browser compatible)
	 * By Scott Andrew 
	 * 
	 * @param 
	 * @author Scott Andrew 
	 * @return void  
	 */
	addEvent: function(elm, evType, fn, useCapture) {
		  
		  // condition : for Firefox, Safari & Opera
		  if (elm.addEventListener) { 
				elm.addEventListener(evType, fn, useCapture); 
				return true; 

		  // condition : for Win IE 5+
		  } else if (elm.attachEvent) { 
				var r = elm.attachEvent('on' + evType, fn); 
				return r; 
		  
		  // condition : for mac IE 5, Legacy...
		  } else {
				elm['on' + evType] = fn;
		  }
	},
	
	
	/**
	 * Loads the core js libraries
	 *
	 * @return void  
	 */
	load: function() {
		
		// calculate state
		this.calculateState();

		this.require('library/flash/ufo.js');
		
		// condition : different core files if cms
		if (this.iscms) { 
			
		} else {
			
		}
	}
}


// =========================================================

// get path + state (globally required)
LibraryManager.calculatePath();
LibraryManager.calculateState();

// load core files
LibraryManager.load();

// add onload events
LibraryManager.addLoadEvent( 
	function() {

		var FO = { movie:"./website.swf", width:"1100", height:"700", quality:"high", 
			majorversion:"7", build:"0", bgcolor:"ffffff", 
			flashvars:"" };
		UFO.create(FO, "wrapper");	



		// condition : different function calls for cms
		if (LibraryManager.iscms) { 

				
		} else {
						
		}
	}
);
