//////////////////////////////////////////////
// ColumnBrowser Object VERSION_0.8
//////////////////////////////////////////////
//
// The ColumnBrowser is the the column container
// component that handles the slice layout, population
// etc
//
// Ali Russell 10.2006
//
//////////////////////////////////////////////

var InformationControl = Class.create();
InformationControl.prototype = {
//
// Member Variables
rsid: "DefaultInformationControlSurfaceID",
init: false,
homeUrl: "",
url: "",

m_renderSurface: null,
m_controlSurface: null, 
m_infoUrl: "",
m_homeUrl: "",
m_homeContent: null,
m_xmlRequest: new XMLHttpRequest(),
m_loadingText: "<div class='loading'><img src='./pics/InformationControl/loading.gif' /><br>Loading Information<br><img src='./pics/InformationControl/Generic/cancel_btn.png' onclick='mSpaceApplication.FireEvent(\'InformationControl.StopLoading\''/></div>",
m_thumbnailInterval: 5,
m_thumbnailPerPage: 12,
m_thumbnailStart: 0,
m_lastPageContents: null,
m_currentPageContents: null,
m_isInitialised: false,
m_beenUsed: false,

//
// Constructor
//	@rsid: render surface id
//	@hist: column history object
initialize: function(rsid, hist)
	{
		this.rsid = rsid;
		
		window.mSpaceInformationControl = this;
		
				
		//create the main control div
		this.csCont = Builder.node('div', {id: "ICCont"});
		this.cs = Builder.node('div', {id: "IC"}, [this.csCont]);	
		
		this.cancelBtn = Builder.node('img', {src: './pics/InformationControl/Generic/cancel_btn.png'});
		
		//create the information control loading surface
		this.lcs = Builder.node('div', { id: "ICls", style: 'display : none'}, 
																				[
																				 Builder.node('div', {className: 'loading'}, 
																							 								[
																															 	Builder.node('img', {src: './pics/InformationControl/loading.gif'}),
																																Builder.node('br'),
																																'Loading Information',
																																Builder.node('br'),
																																this.cancelBtn
																															]
																															)
																				]);
		//create loading surface bg
		this.lcsbg = Builder.node('div', { id: "IClsbg", style: 'display : none'});
		
		

		
		
		//Register custom events
		////////////////////////
		mSpaceApplication.Subscribe("InformationControl.SetUrl", this);
		mSpaceApplication.Subscribe("InformationControl.Refresh", this);
		mSpaceApplication.Subscribe("InformationControl.SubmitForm", this);
		mSpaceApplication.Subscribe("InformationControl.SetUri", this);
		mSpaceApplication.Subscribe("InformationControl.StopLoading", this);
		
		
		mSpaceApplication.Subscribe("Column.SelectItem", this);
		mSpaceApplication.Subscribe("Column.SelectItemWithSlice", this);
		
		mSpaceApplication.Subscribe("mSpaceHistory.HistoryChange", this);
		
		
		mSpaceApplication.Subscribe("InformationControl.Resize", this);
		
		
		RegisterEvent(this.cancelBtn, "click", this, "StopLoading");
		
		this.Start(hist);
		
		
	},
	
Notify: function(eventName, eventParams)
	{
		switch(eventName)
		{
			case 'InformationControl.SetUrl' : this.SetUrl(eventParams); break;
			case 'Column.SelectItem' : this.SelectItem(eventParams); break;
			case 'Column.SelectItemWithSlice' : this.SelectItemWithSlice(eventParams); break;
			case 'InformationControl.Refresh' : this.LoadContents(eventParams); break;
			case 'InformationControl.SubmitForm' : this.SubmitForm(eventParams); break;
			case 'InformationControl.SetContents' : this.SetContents(eventParams); break;
			case 'InformationControl.ShowLoading' : this.ShowLoading(eventParams); break;
			case 'InformationControl.NavigateBack' : this.NavigateBack(eventParams); break;
			case 'InformationControl.StopLoading' : this.StopLoading(eventParams); break;
			case 'mSpaceHistory.HistoryChange' : this.HistoryChange(eventParams); break;
			case 'InformationControl.Resize' : this.ResizeContainer(eventParams); break;
			default: break;
		}		
	},
	
Start: function(histObj)
	{
		if (histObj)
		{
			if (histObj["Info.Url"])
			{
				this.url = histObj["Info.Url"];
				this.LoadContents();
			}		
			else if (histObj["Column.SelectItemHistory"])
			{
				this.url = window.mSpaceApplication.GetServerUrl("infobox") + "&" + histObj["Column.SelectItemHistory"];
				mSpaceApplication.FireEvent("Log.Event", { type: "DataEvent", message: "[getinfo_history] " + this.url});
				this.LoadContents();
			}		
		}
	},

SetUrl: function(url)
	{
		
		mSpaceApplication.FireEvent("DataController.Cancel", this);
		
		mSpaceApplication.FireEvent('LightBox.Hide', 0);
		
		var oldUrl = this.url;
			
		this.url = url;
		this.LoadContents();
		
		//Push the url onto the history stack.
		if (oldUrl!=url)
			mSpaceApplication.FireEvent("mSpaceHistory.AddHistory", { name: 'Info.Url', history: this.url });
	},
	
	
LoadContents: function(params)
	{
		
		
		
		if (!this.init)
		{
			this.init = true;	
		
			this.csCont.innerHTML = $(this.rsid).innerHTML;
			//Append to main surface
			$(this.rsid).update();
			$(this.rsid).appendChild(this.cs);			
			$(this.rsid).appendChild(this.lcs);
			$(this.rsid).appendChild(this.lcsbg);
			
		}		
		
		//make sure the info box is at the top
		var infoTop = document.getElementById("infobox_top_element");
		if (infoTop)
		{
			//infoTop.scrollIntoView(false);
			new Effect.ScrollIntoViewTo("infobox_top_element");
		}
		
			
		var t = this;
		
		Effect.Appear('ICls', { duration: 0.5 });
		Effect.Appear('IClsbg', 
						{
							to:0.75, 
							duration: 0.5 ,
							afterFinish: function()
											{
												mSpaceApplication.FireEvent("DataController.Request", { 'sender' : t, 'id' : 'infobox_get', 'type' : "GET", 'url' : t.url });
											}
						});
	},


ShowLoading: function()
	{
		//this.SetContents(this.m_loadingText);
	},

Home: function()
	{
			this.SetUrl("inc/home.php");
	},
	
SubmitForm: function(form)
	{
				
				
			mSpaceApplication.FireEvent("DataController.Cancel", this);
				
			mSpaceApplication.FireEvent('LightBox.Hide', 0);
				
			var thisObj = this;
			
			//Look at the form and set any parameters in the form string, for the post data
			var formString = "";
			var formUrl = "";
			
			
			formUrl = form.action;
			
				
			for (var i=0;i<form.elements.length;i++)
			{
				if (form.elements[i].name=="js_Url")
				{
					formUrl = form.elements[i].value;
				}
				else
				{
					if (i>0)
					{
						formString += "&";
					}
					formString += form.elements[i].name;
					formString += "=" + URLEncode(form.elements[i].value);
				}
			}
			
			
			this.url = formUrl + "&" + window.mSpaceApplication.m_sessionString;
			

			var t = this;
			
			Effect.Appear('ICls', { duration: 0.5 });
			Effect.Appear('IClsbg', 
							{
								to:0.75, 
								duration: 0.5 ,
								afterFinish: function()
												{
													mSpaceApplication.FireEvent("DataController.Request", { 'sender' : t, 'id' : 'infobox_post', 'type' : "POST", 'postvars' :  formString,'url' : t.url });
												}
							});
					
	},
	
HandleResponse: function(id, data)
	{
		if ((id!="infobox_post") && (id!="infobox_get"))
			return;
			
		this.m_beenUsed = true;
			
		var sh = $("IC").offsetHeight;
		$("IC").setStyle({height:(sh + "px"), overflow: 'hidden'});
		//Update the contents. this automatically executes any <SCRIPT elements
		new Effect.Opacity('ICCont', 
						{ 
							duration: 0.5, 
							from: 1,
							to: 0,
							afterFinish: function()
											 {
													$("ICCont").update(data); 
													new Effect.Opacity('ICCont', 
																  	{ 
																  		duration: 0.5, 
																		from: 0,
																		to: 1,																		
																		afterFinish: function()
																					  {
						
																							$("ICCont").setStyle({height:'auto'});
																							var eh = $("ICCont").offsetHeight;
																							
																							//alert(eh);
																							
																					
																							new Effect.MoveAndResizeTo("IC",
																												   parseFloat(Element.getStyle($("IC"),'top')  || 0),
																												   parseFloat(Element.getStyle($("IC"),'left')  || 0),
																												   parseFloat(Element.getStyle($("IC"),'width')  || 0),
																												   eh );
																							
																							Effect.Fade('ICls', { duration: 0.5 });
																							Effect.Fade('IClsbg', { from: 0.75, duration: 0.5 });	
																					  }
																	});
											 } 
																	 
						});
		

		
		

		
		
		//mSpaceApplication.Resize();	
	},
	
SelectItem: function(eventParams)
	{
		if ((eventParams.column) && (eventParams.listitem))
		{
			
			mSpaceApplication.FireEvent("DataController.Cancel", this);
			
			this.url = window.mSpaceApplication.GetServerUrl("infobox");

			if ((eventParams) && (eventParams.column))
			{
				this.url += "&columnclicked=" + URLEncode(eventParams.column.m_Uri);
			}
			
			if ((eventParams) && (eventParams.listitem))
			{
				this.url += "&itemclicked=" + URLEncode(eventParams.listitem.uri);
			}
			
			if (window.ColumnBrowser)
			{
				this.url += window.ColumnBrowser.GetColumnParamString();
			}
			
			if (window.AdvancedSearch)
			{
				//this.url += window.AdvancedSearch.GetSearchString();
			}
			
			mSpaceApplication.FireEvent("Log.Event", { type: "DataEvent", message: "[getinfo] " + this.url});
			this.LoadContents();
		}		
	},
	
SelectItemWithSlice: function(eventParams)
	{
		if ((eventParams) && (eventParams.column) && (eventParams.listitem))
		{
			
			mSpaceApplication.FireEvent("DataController.Cancel", this);
			
			this.url = window.mSpaceApplication.GetServerUrl("infobox");

			if (eventParams.column)
			{
				this.url += "&columnclicked=" + URLEncode(eventParams.column.m_Uri);
			}
			
			if (eventParams.listitem)
			{
				this.url += "&itemclicked=" + URLEncode(eventParams.listitem.uri);
			}
						
			if (eventParams.sliceString)
			{
				this.url += eventParams.sliceString;
			}			
			
			mSpaceApplication.FireEvent("Log.Event", { type: "DataEvent", message: "[getinfo] " + this.url});
			this.LoadContents();
		}		
	},
	
HistoryChange: function(histObj)
	{
		
		if (histObj["Info.Url"])
		{
			this.url = histObj["Info.Url"];
			this.LoadContents();
		}		
		else if (histObj["Column.SelectItemHistory"])
		{
			this.url = window.mSpaceApplication.GetServerUrl("infobox") + "&" + histObj["Column.SelectItemHistory"];
			mSpaceApplication.FireEvent("Log.Event", { type: "DataEvent", message: "[getinfo_history] " + this.url});
			this.LoadContents();
		}
		
	},
	
StopLoading: function()
	{
		
		mSpaceApplication.FireEvent("DataController.Cancel", this);
		
		Effect.Fade('ICls', { duration: 0.5 });
		Effect.Fade('IClsbg', { from: 0.75, duration: 0.5 });
		
	},
	
ResizeContainer: function()
	{
		$("ICCont").setStyle({height:'auto'});
		var eh = $("ICCont").offsetHeight;
		
		//alert(eh);
		
	
		new Effect.MoveAndResizeTo("IC",
							   parseFloat(Element.getStyle($("IC"),'top')  || 0),
							   parseFloat(Element.getStyle($("IC"),'left')  || 0),
							   parseFloat(Element.getStyle($("IC"),'width')  || 0),
							   eh );		
	}
	
}
