//////////////////////////////////////////////
// mSpaceHistory Object
//////////////////////////////////////////////
//
// The mSpace History control listens out 
// for information panel changes, and item 
// clicks and stores the details of the current 
// application state as the current URL fragment 
// identifier, i.e. the part of the url after 
// the #.
// 
// Using the fragment identifier allows the 
// browser to keep track of changes within the 
// interface, and users can use their back and 
// forward buttons as usual to go through the 
// stages of navigation without having to reload 
// pages.  
// 
// The history can also be used to bookmark a 
// particular view of the application, enabling 
// sharing of these views with other people.
// 
//////////////////////////////////////////////

var mSpaceHistory = Class.create();
mSpaceHistory.prototype = {
//
// Member Variables
ctlsfc: null, 
//associative array/object to store history items in
historyHash: new Object(),
//history string to passed to the ie iFrame
historyString: "",
// last history string to compare current history string to
lastHistoryString: "",

//
// Constructor
initialize: function(oo)
	{
		window.mSpaceHistory = this;		

		//Inherit all the passed object options
		Object.extend(this, oo);
		
		//If browser is ie, then create an iframe for forcing the browser to 
		//react to back and forward button presses.
		if ((browserDetails[0]=="msie") && (browserDetails[1]!="7.0"))
		{
			this.ctlsfc = Builder.node('iframe', {style: "display: none"});
			document.body.appendChild(this.ctlsfc);
		}
		
		this.m_controlForm = Builder.node('form', {style: "visibility: hidden"});
		document.body.appendChild(this.m_controlForm);
		
		
		
		//Register custom events
		////////////////////////
		document.observe('mSpaceApplication:WorkerPoll',			this.CheckUrl.bindAsEventListener(this));
		
		document.observe('Column:SelectItem',									this.SelectItem.bindAsEventListener(this));
		
		document.observe('mSpaceHistory:IEReload',						this.IEReload.bindAsEventListener(this));
		document.observe('mSpaceHistory:AddHistory',					this.AddHistory.bindAsEventListener(this));
		document.observe('mSpaceHistory:ClearHistory',				this.ClearHistory.bindAsEventListener(this));
		
		this.Start();
	},
	
//start the history object, and check the intital url
Start: function(eventParams)
	{
		this.CheckUrl(false, true);	
	},
	
//Check the current url of the page for a history string
CheckUrl: function(sendMessage, firstCheck)
	{
		//If not ie, check that there is a fragment, if so, then
		//try and load the history
		if ((browserDetails[0]!="msie") || (firstCheck))
		{
			var urlArray=document.location.href.split('#');
			if (urlArray.length>1)
			{
				this.LoadHistory(urlArray[1], sendMessage);
			}
			else
			{
				this.LoadHistory("", sendMessage);
			}
		}
	},
	
//Called by the ie iframe when the back
//and forward buttons are pressed.
IEReload: function(e)
	{
		var historyStr = e.memo;
		if (this.lastHistoryString!=historyStr)
		{
			this.LoadHistory(historyStr, true);
		}
	},

//Load a history string
LoadHistory: function(params, sendMessage)
	{
		//If the history string is not the same as the last one
		//ie history has changed, then parse string and load contents
		if ((params!="") && (this.lastHistoryString!=params))
		{
			
			this.lastHistoryString = params;
			
			var historyObject = new Object();
			
			//split the history array up into relevant parts
			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.historyHash = historyObject;
			
			//if send message flag, then fire a history change event.
			if (sendMessage)
			{			
				document.fire('mSpaceHistory:HistoryChange', historyObject);
			}
			
			
		}
	},	
	

	
//Update the page url with the current history hash.
//or update the ie iframe.
UpdateHistoryUrl: function()
	{
		var newHistoryString = "";
		for (key in this.historyHash)
		{
			newHistoryString += key + "%=%" + URLEncode(this.historyHash[key]) + "|";
		}
		
		if (this.lastHistoryString != newHistoryString)
		{
			this.lastHistoryString = newHistoryString;
			
			if ((browserDetails[0]=="msie") && (browserDetails[1]!="7.0"))
			{
				this.ctlsfc.src = "./inc/history.php?" + this.historyString;
			}
			else
			{			
				window.top.location.hash = this.lastHistoryString;
			}
			
		}
		
		
	},
	
//Add a history item to the history hash
AddHistory: function(e)
	{
		var eventParams = e.memo;
		this.historyHash[eventParams.name] = eventParams.history;
		this.UpdateHistoryUrl();
	},	
	
//clear a history item from the history hash
ClearHistory: function(e)
	{
		var eventParams = null;
		if(e)
		{
			if(e.memo)
				eventParams = e.memo;
			else
				eventParams = e;	
		}
		
		var newHistoryHash = new Object();
		for (key in this.historyHash)
		{
			if (key!=eventParams.name)
			{
				newHistoryHash[key] = this.historyHash[key];
			}
		}
		
		this.historyHash = newHistoryHash;
	},
	
//Listen for item clicks with the column browser, 
//and update the history for the slice
SelectItem: function(e)
	{
		var eventParams = e.memo;
		//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.uri);
			}
			
			if ((eventParams) && (eventParams.listitem))
			{
				selectItemHistoryUrl += "&itemclicked=" + URLEncode(eventParams.listitem.uri);
			}		
		
			if (window.ColumnBrowser)
			{
				selectItemHistoryUrl += window.ColumnBrowser.GetColumnParamString();
			}
			
			
			this.historyHash["Column.SelectItemHistory"] = selectItemHistoryUrl;
			
			this.UpdateHistoryUrl();
		
	},
	
//get the current history string.
GetHistoryHash: function()
	{
		return this.lastHistoryString;
	}

	
}
