//////////////////////////////////////////////
// ScratchPad Object VERSION_0.8
//////////////////////////////////////////////
//
// The Scratchpad is a place to temporarily store 
// items of interest.  It tags the items with 
// the tag 'scratchpad' if users are registered
// otherwise it just locally stores items using
// cookies.
//
// Ali Russell 10.2006
//
//////////////////////////////////////////////

var ScratchPad = Class.create();
ScratchPad.prototype = {

//local variables
m_controlSurface: null,
m_renderSurfaceId: null,
m_itemList: new Array(),
m_intialTagValue: "Enter Tags...",
m_tagOkValue: "Items Tagged!",
m_lastXpos: 20,
m_lastYpos: 20,
//
// Constructor
initialize: function(objectOptions)
	{
		
		ParseOptions(objectOptions, this);
		
		if (this.m_buttonID)
		{
			var btn = document.getElementById(this.m_buttonID);
			if (btn)
			{
				btn.style.display="";
			}
		}
		
		this.m_controlSurface = createDiv("ScratchPad");
		
		
		
		
		var intitalWidth = "200px";
		var intialHeight = Math.min(300, (getWindowHeight())) + "px";
		this.m_controlSurface.style.width = intitalWidth;
		this.m_controlSurface.style.height = intialHeight;
		
		if (this.m_renderSurfaceId==null)
		{
			document.body.appendChild(this.m_controlSurface);
		}
		else
		{
			document.getElementById(this.m_renderSurfaceId).appendChild(this.m_controlSurface);
		}
		
		
		this.m_controlSurface.style.display = "none";
		this.m_controlSurface.style.left = "-8000px";
		
		
		this.m_headerDiv = createDiv("Header", true);

		this.m_closeBtn = createDiv("CloseBtn", true);
		this.m_closeBtn.title = "Close the Scratch Pad";
		this.m_headerDiv.appendChild(this.m_closeBtn);
		
		this.m_headerImg = createDiv("SPIcon", true);
		this.m_headerDiv.appendChild(this.m_headerImg);		


		this.m_headerDiv.appendChild(document.createTextNode("Scratch Pad"));
		this.m_controlSurface.appendChild(this.m_headerDiv);
		
		

		
		
		this.m_contentList = createDiv("ContentList", true);
		this.m_controlSurface.appendChild(this.m_contentList);
		
		this.m_tagPanelDiv = createDiv("TagPanel", true);
		this.m_controlSurface.appendChild(this.m_tagPanelDiv);	
		
		
			this.m_tagBox = document.createElement("INPUT");
			this.m_tagBox.type = "text";
			this.m_tagBox.className = "tagBox";
			this.m_tagBox.value = this.m_intialTagValue;
			this.m_tagPanelDiv.appendChild(this.m_tagBox);
			
			
			this.m_tagBtn = document.createElement("INPUT");
			this.m_tagBtn.type = "button";
			this.m_tagBtn.className = "tagBtn";	
			this.m_tagBtn.value = "Tag";
			this.m_tagPanelDiv.appendChild(this.m_tagBtn);
		
		
		this.m_footerDiv = createDiv("Footer", true);
		this.m_controlSurface.appendChild(this.m_footerDiv);
		
				
		
		this.m_delBtn = createDiv("DeleteButton", true);
		this.m_delBtn.title = "Delete currently checked items";
		this.m_footerDiv.appendChild(this.m_delBtn);			
		
		this.m_resizeBtn = createDiv("ResizeBtn", true);
		this.m_resizeBtn.title = "Resize the Scratch Pad";
		this.m_footerDiv.appendChild(this.m_resizeBtn);	
		
		
		
		
		//Subscribe Events
		mSpaceApplication.Subscribe("mSpaceApplication.PageResize", this);
		mSpaceApplication.Subscribe("mSpaceApplication.MouseMove", this);
		
		mSpaceApplication.Subscribe("Column.DoublecClick", this);
		
		mSpaceApplication.Subscribe("ScratchPad.ItemClick", this);
		mSpaceApplication.Subscribe("ScratchPad.Open", this);
		mSpaceApplication.Subscribe("ScratchPad.Close", this);
		
		
		//Register Dom events
		
		RegisterEvent(this.m_headerDiv, "mousedown", this, "MouseDown");
		RegisterEvent(this.m_headerDiv, "mouseup", this, "MouseUp");	
		
		RegisterEvent(this.m_resizeBtn, "mousedown", this, "MouseDown");
		RegisterEvent(this.m_resizeBtn, "mouseup", this, "MouseUp");
		
		RegisterEvent(this.m_delBtn, "click", this, "DeleteItems");
		
		RegisterEvent(this.m_closeBtn, "click", this, "Close");
		
		RegisterEvent(this.m_tagBtn, "click", this, "TagItems");
		RegisterEvent(this.m_tagBox, "click", this, "BlurTagBox");
		
		this.LoadCookie();
	},
	
Notify: function(eventName, eventParams)
	{
		switch(eventName)
		{
			case 'mSpaceApplication.PageResize' : this.Resize(eventParams); break;
			case 'mSpaceApplication.MouseMove' : this.MouseMove(eventParams); break;
			case 'Column.DoublecClick' : this.AddItem(eventParams); break;
			case 'ScratchPad.ItemClick' : this.ItemClick(eventParams); break;
			case 'ScratchPad.Open' : this.Open(eventParams); break;
			case 'ScratchPad.Close' : this.Close(eventParams); break;
			default: break;
		}
	},

Resize: function(eventParams)
	{
		this.m_contentList.style.height = (this.m_controlSurface.clientHeight - (this.m_footerDiv.offsetHeight + this.m_headerDiv.offsetHeight + this.m_tagPanelDiv.offsetHeight)) + "px";
		
		var size = (this.m_controlSurface.clientWidth - this.m_tagBtn.offsetWidth - 10);
		
		if (size<0)
			size = 0;
		this.m_tagBox.style.width = size +  "px";
	},
	
Open: function()
	{
		this.m_controlSurface.style.display = "";
		this.m_controlSurface.style.left = this.m_lastXpos + "px";
		this.m_controlSurface.style.top = this.m_lastYpos + "px";
		this.Resize();
	},
	
Close: function()
	{
		this.m_DragWindow = false;
		this.m_lastXpos = this.m_controlSurface.offsetLeft;
		this.m_lastYpos = this.m_controlSurface.offsetTop;
		this.m_controlSurface.style.display = "none";
		this.m_controlSurface.style.left = "-8000px";		
	},
	
MouseDown: function(e, element)
	{
		if (element==this.m_headerDiv)
		{
			this.m_mouseDragXOffset = e.X - findPosX(this.m_controlSurface);
			this.m_mouseDragYOffset = e.Y - findPosY(this.m_controlSurface);			
			this.m_DragWindow = true;
		}
		else if (element==this.m_resizeBtn)
		{
			this.m_mouseResizeXOffset = (findPosX(this.m_controlSurface) + this.m_controlSurface.offsetWidth) - e.X;
			this.m_mouseResizeYOffset = (findPosY(this.m_controlSurface) + this.m_controlSurface.offsetHeight) - e.Y;			
			this.m_DragResize = true;
		}
	},
	
MouseUp: function(e, element)
	{
		if (element==this.m_headerDiv)
		{
			this.m_DragWindow = false;
		}		
		else if (element==this.m_resizeBtn)
		{
			this.m_DragResize = false;
		}		
	},
	
MouseMove: function(e)
	{
		if (this.m_DragWindow)
		{
			if (e.X>0)			
				this.m_controlSurface.style.left = (e.X - this.m_mouseDragXOffset ) + "px";
			if (e.Y>0)
				this.m_controlSurface.style.top = (e.Y - this.m_mouseDragYOffset) + "px";			
		}
		else if (this.m_DragResize)
		{
			var w = (e.X - findPosX(this.m_controlSurface) + this.m_mouseResizeXOffset );
			if (w<80)
				w = 80;
				
			var h = (e.Y - findPosY(this.m_controlSurface) + this.m_mouseResizeYOffset);
			if (h<80)
				h = 80;
				
			this.m_controlSurface.style.width = w + "px";
			this.m_controlSurface.style.height = h + "px";	
			this.Resize();
		}
	},
	
AddItem: function(eventParams)
	{
		
		this.Open();
		
		var sliceStr = "";
		if (window.ColumnBrowser)
		{
			sliceStr += window.ColumnBrowser.GetColumnParamString();
		}	
		
		var newItem = new ScratchPadListItem(this.m_itemList.length);
		
		newItem.SetItem( eventParams.listitem, eventParams.column , sliceStr);
		
		this.m_itemList.push(newItem);
		
		this.m_contentList.appendChild(newItem.m_controlSurface);
		
		this.UpdateCookies();
	},
	
DeleteItems: function(eventParams)
	{
		var newArray = new Array();
		for (var i=0;i<this.m_itemList.length;i++)
		{
			var li = this.m_itemList[i];
			if (li.m_checkBox.checked)
			{
				this.m_contentList.removeChild(li.m_controlSurface);
			}
			else
			{
				newArray.push(li);
			}
		}	
		
		this.m_itemList = newArray;
		
		this.UpdateCookies();
		
	},
	
TagItems: function(eventParams)
	{
		if ((this.m_tagBox.value=="") || (this.m_intialTagValue==this.m_tagBox.value) || (this.m_tagOkValue==this.m_tagBox.value))
			return;
			
		var itemArray = new Array();
		for (var i=0;i<this.m_itemList.length;i++)
		{
			var li = this.m_itemList[i];
			if (li.m_checkBox.checked)
			{
				itemArray.push(li.m_listitem);
			}
		}		
		
		if (itemArray.length<1)
			return;
		var thisObj = this;
		mSpaceApplication.FireEvent("TagControl.ExternalAddTags", {tags : this.m_tagBox.value, items : itemArray, notifyFunction: function() {thisObj.TagReturnedOk();}});
	},
	
TagReturnedOk: function(success)
	{
		
		if (success=="ok")
		{
		
			this.m_highlightTagBoxCount = 0;
			
			this.m_tagBox.value = this.m_tagOkValue;
			this.m_tagPanelDiv.className = "TagPanelHigh";
			
			var thisObj = this;
			setTimeout(function() { thisObj.m_tagPanelDiv.className = "TagPanel"; thisObj.m_tagBox.value = thisObj.m_intialTagValue;}, 3000);
		}
		
	},
	
	
BlurTagBox: function(eventParams)
	{
		if ((this.m_intialTagValue==this.m_tagBox.value) || (this.m_tagOkValue==this.m_tagBox.value))
		{
			this.m_tagBox.value = "";
			this.m_tagBox.className = "tagBox";
		}
	},
	
ItemClick: function(spItem)
	{
		var col = new Object();
		col.m_Uri = spItem.m_column;
		
		
		var li = new Object();
		li.uri = spItem.m_listitem;		
		
		mSpaceApplication.FireEvent("Column.SelectItemWithSlice", { listitem: li, column: col, sliceString: spItem.m_sliceString });		
	},
	
UpdateCookies: function()
	{
		var cookieArray = new Array();
		for (var i=0;i<this.m_itemList.length;i++)
		{
			cookieArray.push(this.m_itemList[i].GetObject());
		}
		
		var cookieString = URLEncode(ObjectToString(cookieArray));
		
		
		setCookie( 'mspace_scratchpad', cookieString, 365*10, '/', '', '' );
		
	},
	
LoadCookie: function()
	{
		var cookieString = getCookie('mspace_scratchpad')
		if (cookieString)
		{						
			try
			{
				var cookieArray = eval(URLDecode(cookieString));
				
				for (var i=0;i<cookieArray.length;i++)
				{
					var newItem = new ScratchPadListItem(this.m_itemList.length);
					
					newItem.LoadObject(cookieArray[i]);
					
					this.m_itemList.push(newItem);
					
					this.m_contentList.appendChild(newItem.m_controlSurface);
				}
			}
			catch (e)
			{
				alert("Error Loading Scratchpad: " + e.message);
			}
		}
	}
	
	
}




var ScratchPadListItem = Class.create();
ScratchPadListItem.prototype = {

//local variables
m_controlSurface: null,
m_title: "Scratch Pad Item",
m_sliceString: "",
m_listitem: null,
m_column: null,
m_checkBox: null,
//
// Constructor
initialize: function(no)
	{
			//Create the main holder
			this.m_controlSurface = createDiv("ListItem", true);
			
			
			this.m_controlSurfaceChildContainer = createDiv("LiContainer", true);
			this.m_controlSurface.appendChild(this.m_controlSurfaceChildContainer);	

			
			this.m_checkBox = document.createElement("INPUT");
			this.m_checkBox.type = "checkbox";
			this.m_checkBox.className = "chk";
			this.m_checkBox.checked = false;
			
			this.m_controlSurfaceChildContainer.appendChild(this.m_checkBox);	
			
			//create the subContainer
			if (isOdd(no))
			{
				this.m_controlSurfaceChild = createDiv("Odd", true);	
			}
			else
			{
				this.m_controlSurfaceChild = createDiv("Even", true);					
			}
			this.m_controlSurfaceChildContainer.appendChild(this.m_controlSurfaceChild);
			

			
			
			//Create the label
			this.m_controlSurfaceLabel = createDiv("Label", true);
			this.m_controlSurfaceLabel.innerHTML =  this.m_title;
			this.m_controlSurfaceChild.appendChild(this.m_controlSurfaceLabel);		
			
			//Register events
			//RegisterEvent(this.m_controlSurface , "mouseover", this, "MouseOverListItem");
			//RegisterEvent(this.m_controlSurface , "mouseout", this, "MouseOutListItem");
			RegisterEvent(this.m_controlSurfaceChild , "click", this, "MouseClickListItem", true);			
	},
	
SetItem: function(li, col, sliceString)
	{
		this.m_listitem = li.uri;
		this.m_column = col.m_Uri;
		this.m_title = li.label + " (" + col.m_Label + ")";
		this.m_sliceString = sliceString;
		this.m_controlSurfaceLabel.innerHTML =  this.m_title;
	},
	
MouseOverListItem: function(e, element)
	{
	},
	
MouseOutListItem: function(e, element)
	{
	},
	
MouseClickListItem: function(e, element)
	{
		mSpaceApplication.FireEvent("ScratchPad.ItemClick", this);
	},
	
GetObject: function()
	{
		var retObj = new Object();
		
		retObj.title = this.m_title;
		retObj.itemUri = this.m_listitem;
		retObj.colUri = this.m_column;
		retObj.sliceString = this.m_sliceString;
		
		return retObj;
	},
	
LoadObject: function(obj)
	{
		this.m_title = obj.title;
		this.m_listitem = obj.itemUri;
		this.m_column = obj.colUri;
		this.m_sliceString = obj.sliceString;	
		
		this.m_controlSurfaceLabel.innerHTML =  this.m_title;
	}
}




