//////////////////////////////////////////////
// mSpaceApplication Object
//////////////////////////////////////////////
//
// The mSpaceApplication class provides a 
// main controller interface, which oversees 
// several key areas of the application logic.
// 
// The mSpaceApplication class includes 
// methods for simple event subscription 
// and notification, including a worker 
// poll event for periodical housecleaning.  
// It also takes care of session handling 
// when cookies are disabled, by re-writing 
// any server based url’s with the correct 
// session id.
//
//////////////////////////////////////////////

var mSpaceApplication = Class.create();
mSpaceApplication.prototype = {
//
// Member Variables
resizeHandler: null,
serverUrl: "http://server.mspace.net/",
knowledgeBase: "local:nfo",
dataModel: "#mSpaceModel",
sessionString: "",
eventControlObject: new Object(),

//
// Constructor
initialize: function(oo)
	{
		
		//Inherit all the passed object options
		Object.extend(this, oo);
		
		//set global mspace application variable
		window.mSpaceApplication = this;		
		
		//start a session
		this.StartSession();
		
		//////////////////////////////////////////////////////////////////////
		//Create a dummy list item, for font size change checking
		//And also working out how large list items are for dynamic scrolling
		//
		//The ColCont div is currently being used to set the size of the text
		//for all elements in the ColumnBrowser so must add that as a container
		//for the dummy list item - TODO: This doesn't feel very robust?!?
		dmyDiv = Builder.node('div', {style: "height:0px; width:0px; overflow:hidden; visibility:hidden;"},
					 [
					 	Builder.node('div', {className: "ColCont", style: "visibility:hidden;"},
									 [
									  	Builder.node('div', {id: "DummyLi", className: "Li", style: "visibility:hidden;"},
															 [
															  	Builder.node('div',{className: "Even"},["ListItem"])
															 ])
									 ])
					 ]);
		
		document.body.appendChild(dmyDiv);
		////////////////////////////////////////
		
		//Get the current text size based on the dummy font size div
		this.m_fontSizeDiv = $("DummyLi");		
		this.m_lastFontSize = this.m_fontSizeDiv.offsetHeight;
		
		//register an on move mouse event for the whole page
		Event.observe(document.body, "mousemove", this.MouseMove.bindAsEventListener(this));
		
		//Create a new DataController Object
		new DataController();
		
		//Set an interval for a work poll thread that does worker functions every 0.5 seconds
		var thisObj = this;
		window.setInterval(function() { thisObj.WorkerPoll() }, 500);
		
	},
	
//forces a page resize using the resize handler defined 
//upon construction, if there is one.
Resize: function(animating)
	{
		if (this.resizeHandler!=null)
		{
			this.resizeHandler.Resize(animating);
		}
	},
	
//Fire an application start event
Start: function(history)
	{
		document.fire('mSpaceApplication:Start', history);
		
		//return false for any anchor tags calling this method using onclick
		return false;
	},

//Fire an application history start.
HistoryStart: function(history)
	{
		document.fire('mSpaceApplication:HistoryStart', history);
		
		//return false for any anchor tags calling this method using onclick
		return false;
	},

//Workerpoll function to periodically update other
//controls and do general housekeeping
//this method also checks to see if the page text size 
//has changed, and if so, forces a resize.
WorkerPoll: function ()
	{
		//fire worker poll event, so that other obejcts can do an update
		document.fire('mSpaceApplication:WorkerPoll');
		
		//If the dummy div size has changed, then force a resize
		//i.e. the user has changed the text size
		if (this.m_lastFontSize != this.m_fontSizeDiv.offsetHeight)
		{			
			this.Resize();
		}
		
		//store new div size.
		this.m_lastFontSize = this.m_fontSizeDiv.offsetHeight;
		
	},

//General mouse move event
MouseMove: function(e)
	{
		//Fire a mouse move event for other objects
		document.fire("mSpaceApplication:MouseMove", e);
	},

//Start a new session if cookies are not turned on,
//by calling an ajax server document to get the
//current php session id, and then storing locally
//to append to all AJAX server calls.
StartSession: function()
	{
		//check to see if cookies are enabled, and if not, then use the returned session id as a global session variable
		window.cookiesEnabled = false;
		setCookie( 'cookietest', 'none', '', '/', '', '' );	
		if (getCookie('cookietest'))
		{
			window.cookiesEnabled = true;
		}
		
		//Get session id from session.php
		var url = "inc/session.php";
		var ajaxRequest = new Ajax.Request(url, {
		  requestType: 'get',			  
		  onSuccess: function(transport) 
		  {			
		  	window.mSpaceApplication.sessionString = transport.responseText;
		  },
		  onFailure: function(transport) {
			  alert(transport);
		  }
		});			
			
	},
	

//Takes an action, and returns a server url built with curretn
//session, server, kb details etc.
//14/03/08: removed the server details, as this is stored in config
//and is available through mspace.php, so no need to keep sending it
GetServerUrl: function(action)
	{
		return "./inc/mspace.php?jsmode=true&" + this["sessionString"] + "&action=" + action + "&";
	},
	
// Global accessor method for retrieving config params
// eg mSpaceApplication.GetConfig('ColumnBrowser.Resizing.Horizontal');
// Returns false if the requested string is not found
GetConfig: function(config_string)
	{
		if(!window.config)
			return false;
			
		// Check each item of the string exists
		var params = config_string.split('.');
		
		var conf = window.config;
		
		for(var i=0; i < params.length; i++)
		{
			if(conf[params[i]])
				conf = conf[params[i]];	
			else
				return false;
		}
		
		return conf;
	}
	
}; 
