/**
 * This file is reponsible for managing the js library of classes and functions
 *
 * @author Richard Hallows
 * @package tonic
*/
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;
			}
		}
	},
	
	
	/**
	 * Access method for state
	 *
	 * @return void  
	 */
	 getState: function() {
		
		return this.iscms;
	},
	
	
	/**
	 * Includes a js file
	 * 
	 * @param string libraryfile The path from the js library root to the file
	 *
	 * @return void  
	 */
	require: function(libraryfile) {
		
		this.calculatePath();
		document.write('<script type="text/javascript" src="'+ this.path + libraryfile +'"></script>');
	},
	
	
	/**
	 * Ensures functions are only run once the core library is loaded
	 *
	 * @param function func The function to execute
	 *
	 * @return void  
	 */
	addLoadEvent: function (func) {
		
		var oldonload = window.onload;
		
		if (typeof window.onload != 'function') {
			window.onload = func;
		} else {
			window.onload = function() {
				oldonload();
				func();
			}
		}
	},
	
	
	/**
	 * Loads the core js libraries
	 *
	 * @return void  
	 */
	load: function() {

		// calculate state
		this.calculateState();
		
		//load cms-specific js
		if (this.iscms) { 
			
		//load site-specific js
		} else {		

		}

		//global js
		//this.require('library/core/prototype/prototype.js');
		//this.require('library/core/scriptaculous/builder.js');
		//this.require('library/core/scriptaculous/effects.js');
		//this.require('library/core/scriptaculous/dragdrop.js');
		//this.require('library/core/scriptaculous/controls.js');
		//this.require('library/core/scriptaculous/slider.js');
		//this.require('library/core/behaviour/behaviour.js');
		//this.require('library/array/array.js');
		this.require('library/css/suckerfishShoal.js');
		this.require('library/img/png/pngfix.js');
		this.require('library/print/printlinks.js');
		//this.require('/behaviours.js');
	}
}

//load the core libraries before the page finishes loading
LibraryManager.load();

//add onload events
LibraryManager.addLoadEvent( 
	function() {
		
		// function calls for cms
		if (LibraryManager.getState()) { 
		
			//Behaviour.register(cmsRules);
		
		// function calls for site
		} else {

			//Behaviour.register(siteRules);

		}

		//global functions
		//Behaviour.register(globalRules);
		suckerFish.sfFocus("input");
		suckerFish.sfFocus("textarea");
	}
);