/**
 * 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) {		
		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() {
		
		// condition : different core files if cms
		if (this.iscms) { 
			this.require('tonic/core/prototype/prototype.js');
			this.require('tonic/core/behaviour/behaviour.js');
			this.require('tonic/css/suckerfishshoal.js');
			this.require('tonic/form/date.js');
			this.require('behaviours.js');
		} else {
			this.require('tonic/core/prototype/prototype.js');
			this.require('tonic/core/behaviour/behaviour.js');
			this.require('tonic/form/date.js');
			this.require('tonic/form/calendar/calendar.js');
			this.require('tonic/form/calendar/lang/calendar-en.js');
			this.require('tonic/form/calendar/calendar-setup.js');
			this.require('behaviours.js');
			
			// remove for live
			//this.require('tonic/img/grid/grid.js');
		}
	}
}


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

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

// condition : only load tiny when in cms
if (LibraryManager.iscms) {
	// tiny mce setup
	tinyMCE.init({
				 
		mode : "textareas",
		editor_selector : "tinymce", 
		theme : "advanced",
		language : "en",
		
		content_css : LibraryManager.path+"tonic/form/themes/advanced/css/mycontent.css",
		width : "600", 
		height : "150", 
	
		theme_advanced_layout_manager : "RowLayout",
		theme_advanced_containers : "mceEditor,bottom1",
		
		theme_advanced_containers_default_class : "mceToolbar",
		theme_advanced_containers_default_align : "center",
		
		theme_advanced_container_bottom1 : "bold, italic, undo, redo, cleanup, bullist, numlist, link, unlink, copy, paste",
		theme_advanced_container_bottom1_class :  "mceToolbarBottom",
	
		object_resizing : "false",
		inline_styles : "true",
		apply_source_formatting : "true",
		invalid_elements : "font,center"
	});
}


// load core files
LibraryManager.load();

// add onload events
LibraryManager.addLoadEvent( 
	function() {
				
		// condition : different function calls for cms
		if (LibraryManager.iscms) { 
		
			// initalise suckfisher
			suckerFish.sfHover("tr");
			// initalise behaviours
			Behaviour.register(myrules);	
				
		} else {
			// initalise behaviours
			Behaviour.register(myrules);
			
			//var grid = Grid.initialize("template", "url('/_staging/_templates/04_BookingForm.jpg')", "left top", "no-repeat", true);
		}
	}
);