//////////////////////////////////////////////
// mSpaceApplication Object VERSION_0.8
//////////////////////////////////////////////
//
// The new mSpaceApplication object is the main
// controller for the mSpace application
//
// Ali Russell 10.2006
//
//////////////////////////////////////////////

var mSpaceApplication = Class.create();
mSpaceApplication.prototype = {
//
// Member Variables

//local settings
m_resizeHandler: null,
m_serverUrl: "http://server.mspace.net/",
m_knowledgeBase: "local:nfo",
m_dataModel: "#mSpaceModel",
m_globalSettings: new Object(),
m_sessionString: "",

//
// Constructor
initialize: function(optionsObject)
	{

		
		
		//Parse the url query params into some global parameters
		var query=window.location.search.substring(1);
		if (query.length > 0){
			var params=query.split("&");
			for (var i=0 ; i<params.length ; i++){
				var pos = params[i].indexOf("=");
				var name = params[i].substring(0, pos);
				var value = params[i].substring(pos + 1);
				this.m_globalSettings[name] = value;
			}
		}		
		///////////////////////////////
		
		//Override the defatul object parameters, with those from the objectOptions.
		ParseOptions(optionsObject, this);
		
		window.mSpaceApplication = this;
		//Extend the EventHandlerInterface.  This allows
		//the mSpaceApplication to use and extend the functions
		//within the eventHandlerObject.
		ImplementInterface(EventHandlerInterface, this);
		
		
		this.StartSession();
		
		
		
		//Create a dummy element that is used to check if font size has
		//been updated, and also create a dummy ListItem used by the List objec
		//to check the current size of the lists.
		
		//Dummy container, so that the dummy items dont add to the page height
		var dummyContainer = document.createElement("div");
		dummyContainer.style.height="0px";
		dummyContainer.style.width="0px";
		dummyContainer.style.overflow="hidden";
		dummyContainer.style.visibility = "hidden";
		
		
		/////////////////////////
		// DUMMY LIST ITEM
		/////////////////////////
		var m_dummyListItem = createDiv("ListItem");
		m_dummyListItem.style.visibility = "hidden";
		
		var m_dummyListItemContent = createDiv("Even", true);
		m_dummyListItemContent.innerHTML = "ListItem";
		m_dummyListItem.appendChild(m_dummyListItemContent);
		
		dummyContainer.appendChild(m_dummyListItem);
		
		document.body.appendChild(dummyContainer);
		window.ListItemSizeGuide = this.m_dummyListItem;
		//////////////////////////////////////////////////
		//////////////////////////////////////////////////
		
		//Create a font size
		this.m_fontSizeDiv = m_dummyListItemContent;
		
		this.m_lastFontSize = this.m_fontSizeDiv.offsetHeight;
		
		RegisterEvent(document.body, "mousemove", this, "MouseMove");
		
		
		new DataController();
		
		var thisObj = this;
		window.setInterval(function() { thisObj.WorkerPoll() }, 500);
		
	},
	
Resize: function(animating)
	{
		if (this.m_resizeHandler!=null)
		{
			this.m_resizeHandler.Resize(animating);
		}
	},
	
Start: function(history)
	{
		this.FireEvent("mSpaceApplication.Start", history);
		
		//return false for any anchor tags calling this method using onclick
		return false;
	},
	
HistoryStart: function(history)
	{
		this.FireEvent("mSpaceApplication.HistoryStart", history);
		
		
		//return false for any anchor tags calling this method using onclick
		return false;
	},	
	
StartMinmised: function(history)
	{
		this.FireEvent("mSpaceApplication.StartMinimised", history);
		
		
		//return false for any anchor tags calling this method using onclick
		return false;
	},	

WorkerPoll: function ()
	{
		
		this.FireEvent("mSpaceApplication.WorkerPoll");
	
		
		if (this.m_lastFontSize != this.m_fontSizeDiv.offsetHeight)
		{			
			this.Resize();
		}
		
		this.m_lastFontSize = this.m_fontSizeDiv.offsetHeight;
		
	},

MouseMove: function(e, element)
	{
		this.FireEvent("mSpaceApplication.MouseMove", e);
	},
	
GetSetting: function(settingName)
	{
		return this["m_" + settingName];
	},

GetGlobalSetting: function(settingName)
	{
		for (key in this.m_globalSettings)
		{
			if (key==settingName)
			{
				return this.m_globalSettings[key];
			}
		}
		
		return null;
		
	},
	
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;
		}
		
		
		var myXMLHttpRequest = new XMLHttpRequest();
		var sessionUrl = "inc/session.php";
		
			// Use the onload function if the browser is anything other than ie <7.0.  Fixes problems on some firefox and mozilla
			// versions
			if ((browserDetails[0]=="msie") && (browserDetails[1]!="7.0"))
			{
				myXMLHttpRequest.onreadystatechange = function() {
					try {
						if(myXMLHttpRequest.readyState == 4 && (myXMLHttpRequest.status == 200 || myXMLHttpRequest.status == 304)) {
							//#################
							//RESULTS
							//#################
							if (!window.cookiesEnabled)
							{
								window.mSpaceApplication.m_sessionString = myXMLHttpRequest.responseText;
							}
							
						}
					}catch (e){
					}				  
				};
			}else{
				myXMLHttpRequest.onload = function() {
					try {
						//#################
						//RESULTS
						//#################
							if (!window.cookiesEnabled)
							{
								window.mSpaceApplication.m_sessionString = myXMLHttpRequest.responseText;
							}						
						
					}
					catch (e)
					{
					}				  
				};			
			}
		
			try
			{
				if ((browserDetails[0]=="msie") && (browserDetails[1]!="7.0"))
				{
					window.setTimeout( function()
												{
													myXMLHttpRequest.open('GET', sessionUrl);
													myXMLHttpRequest.send(null);
												}
										,500);
				}
				else
				{
					myXMLHttpRequest.open('GET', sessionUrl);
					myXMLHttpRequest.send(null);			
				}	
			}
			catch(e){}		
	},
	
	
GetServerUrl: function(action)
	{
		return "./inc/mspace.php?jsmode=true&" + window.mSpaceApplication.m_sessionString + "&mSpaceServer=" + mSpaceApplication.GetSetting("serverUrl") + "&action=" + action + "&fpred=" + URLEncode(mSpaceApplication.GetSetting("previewPredicate")) + "&server=" + URLEncode(mSpaceApplication.GetSetting("knowledgeBase")) + "&modeluri=" + URLEncode(mSpaceApplication.GetSetting("dataModel")) + "&previewLimit=4&";
	}
	
};
