//////////////////////////////////////////////
// mSpaceHistory Object VERSION_0.8
//////////////////////////////////////////////
//
// The mSpaceHistory contains an iFrame that is
// used to record the actions of the mSpace
// browser so that the user can click the back
// and forward buttons to go back through the
// slice etc.
//
// Ali Russell 10.2006
//
//////////////////////////////////////////////

var mSpaceHistory = Class.create();
mSpaceHistory.prototype = {
//
// Member Variables
m_controlSurface: null, 
m_historyHash: null,
m_historyStorage: new Object(),
m_historyString: "",
m_xmlRequest: new XMLHttpRequest(),
m_useHash: true, 
m_hasHistory: false,
m_lastHistoryString: "",

//
// Constructor
initialize: function(objectOptions)
	{
		window.mSpaceHistory = this;		
		//Override the defatul object parameters, with those from the objectOptions.
		ParseOptions(objectOptions, this);
		
		
		this.m_historyHash = new Object();
		
		
		///////////////// LOAD THE COLUMN BROWSER STATUS  ///////////////
		/////////////////////////////////////////////////////////////////
		
		//Create the column container div
		if ((browserDetails[0]=="msie") && (browserDetails[1]!="7.0"))
		{
			this.m_controlSurface = document.createElement("iframe");
			this.m_controlSurface.style.display = 'none';
			//this.m_controlSurface.style.width = "0px";
			//this.m_controlSurface.style.height = "0px";
			//this.m_controlSurface.style.visibility = "hidden";
			//this.m_controlSurface.src = "./inc/history.php";
			document.body.appendChild(this.m_controlSurface);
		}
		
		this.m_controlForm = document.createElement("form");
		this.m_controlForm.style.visibility = "hidden";
		this.m_controlForm.action = "";
		document.body.appendChild(this.m_controlForm);
		
		//Register custom events
		////////////////////////
		//Register custom events
		////////////////////////
		mSpaceApplication.Subscribe("mSpaceApplication.WorkerPoll", this);
		
		mSpaceApplication.Subscribe("mSpaceHistory.IEReload", this);
		
		mSpaceApplication.Subscribe("mSpaceHistory.AddHistory", this);
		
		mSpaceApplication.Subscribe("mSpaceHistory.ClearHistory", this);
		
		mSpaceApplication.Subscribe("Column.SelectItem", this);
		
		
		
		//Register DOM events
		/////////////////////
		
		this.Start();
		
		
	},
	
Notify: function(eventName, eventParams)
	{
		switch(eventName)
		{
			case 'mSpaceApplication.WorkerPoll' : this.CheckUrl(true); break;
			case 'mSpaceHistory.IEReload' : this.IEReload(eventParams); break;
			case 'mSpaceHistory.AddHistory' : this.AddHistory(eventParams); break;
			case 'mSpaceHistory.ClearHistory' : this.ClearHistory(eventParams); break;
			case 'Column.SelectItem' : this.SelectItem(eventParams); break;
			default: break;
		}
	},
	
Start: function(eventParams)
	{
		this.CheckUrl(false, true);	
	},
	
CheckUrl: function(sendMessage, firstCheck)
	{
		if ((browserDetails[0]!="msie") || (firstCheck))
		{
			var urlArray=document.location.href.split('#');
			if (urlArray.length>1)
			{
				this.LoadHistory(urlArray[1], sendMessage);
			}
			else
			{
				this.LoadHistory("", sendMessage);
			}
		}
	},
	
IEReload: function(historyStr)
	{
		if (this.m_lastHistoryString!=historyStr)
		{
			this.LoadHistory(historyStr, true);
		}
	},
	
LoadHistory: function(params, sendMessage)
	{
		if ((params!="") && (this.m_lastHistoryString!=params))
		{
			
			this.m_lastHistoryString = params;
			
			var historyObject = new Object();
			
			var objectArray = params.split('|');
			for (var i=0;i<objectArray.length;i++)
			{
				var s = objectArray[i];
				var detailArray = s.split('%=%');
				if (detailArray.length==2)
				{
					var name = detailArray[0];
					var value = detailArray[1];
					
					historyObject[name] = URLDecode(value);
					
				}
			}
			
			this.m_historyHash = historyObject;
			
			if (sendMessage)
			{			
				mSpaceApplication.FireEvent("mSpaceHistory.HistoryChange", historyObject);
			}
			
			
		}
	},	
	
PushHistory: function(eventParams)
	{

	},
	

	
UpdateHistoryHash: function(hash, historyStr)
	{
		
	},
	
ASyncHashHistory: function(s)
	{
	
		
	},
	
	
HashHistory: function(s)
	{
		
	},	
	
AddHashIE: function(params)
	{
	},

UnHashHistory: function(s)
	{
		
	},
	
CompressHistory: function(s)
	{
		
	}	,
	
UnCompressHistory: function(s)
	{
		
	},
	
	
UpdateHistoryUrl: function()
	{
		var newHistoryString = "";
		for (key in this.m_historyHash)
		{
			newHistoryString += key + "%=%" + URLEncode(this.m_historyHash[key]) + "|";
		}
		
		if (this.m_lastHistoryString != newHistoryString)
		{
			this.m_lastHistoryString = newHistoryString;
			
			if ((browserDetails[0]=="msie") && (browserDetails[1]!="7.0"))
			{
				this.m_controlSurface.src = "./inc/history.php?" + this.m_historyString;
			}
			else
			{			
				window.top.location.hash = this.m_lastHistoryString;
			}
			
		}
		
		
	},
	
	
AddHistory: function(eventParams)
	{
		this.m_historyHash[eventParams.name] = eventParams.history;
		this.UpdateHistoryUrl();
	},	
	
ClearHistory: function(eventParams)
	{
		var newHistoryHash = new Object();
		for (key in this.m_historyHash)
		{
			if (key!=eventParams.name)
			{
				newHistoryHash[key] = this.m_historyHash[key];
			}
		}
		
		this.m_historyHash = newHistoryHash;
	},
	
SelectItem: function(eventParams)
	{
		
		//Clear the current Info box url, as an item has been clicked so infobox will use those
		//details for the info box url.
		this.ClearHistory({name:'Info.Url'});
		
		var selectItemHistoryUrl = "";
		
			if ((eventParams) && (eventParams.column))
			{
				selectItemHistoryUrl += "&columnclicked=" + URLEncode(eventParams.column.m_Uri);
			}
			
			if ((eventParams) && (eventParams.listitem))
			{
				selectItemHistoryUrl += "&itemclicked=" + URLEncode(eventParams.listitem.uri);
			}		
		
			if (window.ColumnBrowser)
			{
				selectItemHistoryUrl += window.ColumnBrowser.GetColumnParamString();
			}
			
			
			this.m_historyHash["Column.SelectItemHistory"] = selectItemHistoryUrl;
			
			this.UpdateHistoryUrl();
		
	},
	
GetHistoryHash: function()
	{
		return this.m_lastHistoryString;
	}

	
}
