/*****************************************************
 * Dialog.js
 * 05.01.2003
 * Eddie Lim <elim@eecs.harvard.edu>
 * www.netsymbiosis.com
 *****************************************************/

var Dialog = {
	
	newInstance : function(text, width, height)
	{
		var canvasEl = document.createElement("div");		
		canvasEl.style.position = "absolute";
		canvasEl.style.overflow = "visible";
		canvasEl.style.padding = "15px 5px 15px 15px";
		canvasEl.style.top = "17px";
		canvasEl.style.left = "17px";
		canvasEl.style.width = width + "px";
		canvasEl.style.height = height + "px";
		canvasEl.style.background = "#E0E0E0";
		canvasEl.style.border = "1px solid #A8A9BB";
	
		var textEl = document.createElement("div");
		textEl.style.position = "absolute";
		textEl.style.top = canvasEl.style.paddingTop;
		textEl.style.left = canvasEl.style.paddingLeft;
		textEl.style.fontSize = "11px";
		textEl.style.fontStyle ="normal";
		textEl.style.fontFamily = "tahoma, arial, verdana, sans-serif";
		textEl.style.color = "#000000";
		textEl.innerHTML = text;
		canvasEl.appendChild(textEl);

		// only enable dragging on the borders.
		canvasEl.onmousemove = function(e)
		{
			if(_dragObj)
			{
				this.style.cursor = _dragObj.style.cursor;
				return;
			}
			this.style.cursor = "default";
		}
		canvasEl.onmousedown = function(e)
		{
			var o = this;
			if(o.root.getZIndex() < Memo.bestZIndex)
			{
				o.root.setZIndex(++Memo.bestZIndex);
			}
			e = fixE(e);
			if(e.stopPropagation)
			{
				e.stopPropagation();
			}
			else
			{	
				e.cancelBubble = true;
			}
		}

		var containerEl = document.createElement("div");
		containerEl.style.background = "#EDEDED";
		containerEl.style.overflow = "visible";
		containerEl.style.position = "absolute";
		containerEl.style.top = "50px";
		containerEl.style.left = "50px";
		containerEl.style.width = (parseInt(canvasEl.style.width) + parseInt(canvasEl.style.paddingLeft) + parseInt(canvasEl.style.paddingRight) + 2 + 2 * parseInt(canvasEl.style.left)) + "px";
		containerEl.style.height = (parseInt(canvasEl.style.height) +parseInt(canvasEl.style.paddingTop) + parseInt(canvasEl.style.paddingBottom) + 2 + 2 * parseInt(canvasEl.style.top)) + "px";
		containerEl.style.border = "1px solid #A8A9BB";
		
		containerEl.setText = function(text)
		{
			this.textEl.innerHTML = text;
		}
		containerEl.addElement = function(el)
		{
			el.root = this;
			this.canvasEl.appendChild(el);
		}
		containerEl.addButton = function(button)
		{
			button.root = this;
			this.canvasEl.appendChild(button);
		}
		containerEl.addIcon = function(icon)
		{
			icon.root = this;
			this.appendChild(icon);
		}
		containerEl.addInput = function(input)
		{
			input.root = this;
			this.input = input;
			this.canvasEl.appendChild(input);
		}
		containerEl.getInputValue = function()
		{
			if(this.input)
			{
				return this.input.value;
			}
		}
		containerEl.setInputValue = function(value)
		{
			if(this.input)
			{
				this.input.value = value;
			}
		}
		containerEl.inputFocus = function()
		{
			if(this.input)
			{
				this.input.focus();
			}
		}

		containerEl.repaint = function()
		{
			if(!_activeObj) { return; }

			var o = this;
			o.style.left = (parseInt(_activeObj.style.left) + parseInt(_activeObj.menuIconEl.style.left) - 2) + "px";
			o.style.top = (parseInt(_activeObj.style.top) + parseInt(_activeObj.menuIconEl.style.top) + parseInt(_activeObj.menuIconEl.style.height) + 2) + "px";
		
		}
		containerEl.hide = function()
		{
			this.style.visibility = "hidden";
			_activeDialog = null;
		}
		containerEl.show = function()
		{
			_activeDialog = this;
			var o = this;
			o.setZIndex(++Memo.bestZIndex);
			o.style.visibility = "visible";
		}
		containerEl.flicker = function()
		{			
			this.setZIndex(++Memo.bestZIndex);

			// flicker.			
			for(var i = 0; i < 4; i++)
			{
				if(Browser.isMozilla)
				{
					if(i % 2)
					{
						window.setTimeout("if(_activeDialog) { _activeDialog.style.background = \"#EDEDED\"; }", i * 90);
					}
					else
					{
						window.setTimeout("if(_activeDialog) { _activeDialog.style.background = \"#FFFFFF\"; }", i * 90);
					}
				}
				else
				{
					if(i % 2)
					{
						window.setTimeout("if(_activeDialog) { _activeDialog.style.background = \"#EDEDED\"; }", i * 70);
					}
					else
					{
						window.setTimeout("if(_activeDialog) { _activeDialog.style.background = \"#FFFFFF\"; }", i * 70);
					}
				}
			}		
		}
		containerEl.setZIndex = function(zIndex)
		{
			this.style.zIndex = zIndex;
		}
		containerEl.getZIndex = function()
		{
			return this.style.zIndex;
		}
		containerEl.onmouseover = function(e)
		{
			var o = this;
			o.style.cursor = "move";
		}
		containerEl.onmousedown = function(e)
		{
			var o = _dragObj = this;
			e = fixE(e);

			var y = parseInt(o.style.top);
			var x = parseInt(o.style.left);		
			o.lastMouseX = e.clientX;
			o.lastMouseY = e.clientY;	
			document.onmousemove = o.drag;
			document.onmouseup = o.end;
			o.style.cursor = "move";

			if(o.getZIndex() < Memo.bestZIndex)
			{
				o.setZIndex(++Memo.bestZIndex);
			}
		}
		containerEl.drag = function(e)
		{
			var o = _dragObj;
			e = fixE(e);
	
			var ey	= e.clientY;
			var ex	= e.clientX;
			var y = parseInt(o.style.top);
			var x = parseInt(o.style.left);		
			var nx = x + (ex - o.lastMouseX);
			var ny = y + (ey - o.lastMouseY);
	
			o.style["left"] = nx + "px";
			o.style["top"] = ny + "px";
		
			o.lastMouseX = ex;
			o.lastMouseY = ey;
		}
		containerEl.end = function(e)
		{		
			var o = _dragObj;
			document.onmousemove = null;
			document.onmouseup   = null;
			o.style.cursor = "default";		
			_dragObj = null;
		}
		
		containerEl.appendChild(canvasEl);
		containerEl.canvasEl = canvasEl;
		containerEl.textEl = textEl;
		canvasEl.root = containerEl;
		containerEl.hide();
		document.body.insertBefore(containerEl, null);
		return containerEl;
	}
};

var Button = {

	onClickHandlers :
	{
		CancelHandler : function(o, e) 
		{
			o.root.hide();
		},

		PreferencesHandler : function(o, e)
		{
			var dialog = o.root;
			dialog.hide();

			var selectedIndex;
/*
			// update the menu fonts.
			selectedIndex = dialog.selectMenuSize.selectedIndex;
			Preferences.menuFontSize = selectedIndex != -1 ? dialog.selectMenuSize.options[selectedIndex].value : "";

			selectedIndex = dialog.selectMenuFont.selectedIndex;
			Preferences.menuFontFamily = selectedIndex != -1 ? dialog.selectMenuFont.options[selectedIndex].value : "";
*/
			// update the text areas.
			selectedIndex = dialog.selectTextAreaSize.selectedIndex;
			Preferences.textAreaFontSize = selectedIndex != -1 ? dialog.selectTextAreaSize.options[selectedIndex].value : "";

			selectedIndex = dialog.selectTextAreaFont.selectedIndex;
			Preferences.textAreaFontFamily = selectedIndex != -1 ? dialog.selectTextAreaFont.options[selectedIndex].value : "";
			for(var memoId in Memo.Registry)
			{
				var memo = Memo.Registry[memoId];
				memo.textAreaEl.style.fontSize = Preferences.textAreaFontSize;
				memo.textAreaEl.style.fontFamily = Preferences.textAreaFontFamily;
			}

			// update the titles.
			selectedIndex = dialog.selectTitleSize.selectedIndex;
			Preferences.titleFontSize = selectedIndex != -1 ? dialog.selectTitleSize.options[selectedIndex].value : "";

			selectedIndex = dialog.selectTitleFont.selectedIndex;
			Preferences.titleFontFamily = selectedIndex != -1 ? dialog.selectTitleFont.options[selectedIndex].value : "";
			for(var memoId in Memo.Registry)
			{
				var memo = Memo.Registry[memoId];
				memo.titleTextEl.style.fontSize = Preferences.titleFontSize;
				memo.titleTextEl.style.fontFamily = Preferences.titleFontFamily;
				memo.simulatedTitleTextEl.style.fontSize = Preferences.titleFontSize;
				memo.simulatedTitleTextEl.style.fontFamily = Preferences.titleFontFamily;
				memo.verticallyAlignTitle();
				memo.updateTitle(true);
			}

		},

		DeleteHandler : function(o, e)
		{		
			o.root.hide();

			var id = "";
			if(_activeObj)
			{
				id = _activeObj.memoId;
			}

			var memo = Memo.Registry[id];
			if(!memo) { return; }
			
			memo.hide();
			_memoMenu.removeMenuItem(document.getElementById("menuItemMemo" + id));
			delete Memo.Registry[id];

			if((Memo.getVisibleCount()) == 0)
			{
				_mainMenu.displayRootless();
			}

		},

		RenameHandler : function(o, e)
		{		
			o.root.hide();

			var id = "";
			if(_activeObj)
			{
				id = _activeObj.memoId;
			}

			var memo = Memo.Registry[id];
			if(!memo) { return; }

			var title = o.root.getInputValue();
			memo.setTitle(title);
			memo.updateTitle(true);
			memo.menuItem.setText(title);
		},

		NewHandler : function(o, e)
		{
			o.root.hide();

			// setup a pseudo-random position and size.
			var midX = Math.floor(getViewportWidth() / 2);
			var midY = Math.floor(getViewportHeight() / 2);
			var minX = midX - 350 > 0 ? midX - 350 : (midX - 250 > 0 ? midX - 250 : (midX - 100 > 0 ? midX - 100 : 0));
			var maxX = midX; 
			var minY = midY - 350 > 0 ? midY - 350 : (midY - 250 > 0 ? midY - 250 : (midY - 100 > 0 ? midY - 100 : 0));
			var maxY = midY; 

			// abusing globals here.
			switch(_newMemoType)
			{
				case Memo.SAME_COLOR:
					new Memo(Memo.USER_MEMO, o.root.getInputValue(), _activeObj.colorSchemeId, getRandom(minY, maxY), getRandom(minX, maxX), getRandom(250, 400), getRandom(125, 275), true, false);	
					break;
				case Memo.CHANCE_COLOR:
					new Memo(Memo.USER_MEMO, o.root.getInputValue(), -1, getRandom(minY, maxY), getRandom(minX, maxX), getRandom(250, 400), getRandom(125, 275), true, false);	
					break;
			}				
		}

	},

	newInstance : function(text, onClickHandlerKey, top, left, bottom, right)
	{
		var o = document.createElement("div");

		o.style.position = "absolute";				
		o.style.right = right + "px";
		o.style.bottom = bottom + "px";
		o.style.border = "2px solid #808080";
		o.style.padding = "2px 0px 2px 0px";
		o.style.width = "52px";
		o.style.verticalAlign = "middle";
		o.style.textAlign = "center";
		o.style.fontSize = "11px";
		o.style.fontStyle = "normal";
		o.style.fontFamily = "tahoma, arial, verdana, sans-serif";
		o.style.color = "#000000";
		o.style.background = "#EDEDED";
		o.innerHTML = text;

		o.onmouseover = function(e)
		{
			this.style.background = "#FFFFFF";
		}
		o.onmouseout = function(e)
		{
			this.style.background = "#EDEDED";
		}
		o.onmousedown = function(e)
		{
			if(Button.onClickHandlers[onClickHandlerKey])
			{
				Button.onClickHandlers[onClickHandlerKey](o, e);
			}
		}

		return o;
	}
};

var Icon = {
	newInstance : function(src, srcOver, onClickHandlerKey, top, left, right, bottom, width, height)
	{
		var o = document.createElement("img");
		o.src = src;
		o.style.position = "absolute";
		o.style.height = width + "px";
		o.style.width = height + "px";
		o.style.right = right + "px";
		o.style.top = top + "px";

		o.onmousedown = function(e)
		{
			if(Button.onClickHandlers[onClickHandlerKey])
			{
				Button.onClickHandlers[onClickHandlerKey](o, e);
			}
		}
		
		o.onmouseover = function()
		{
			if(_dragObj)
			{
				this.style.cursor = _dragObj.style.cursor;
				return;
			}
		
			this.style.cursor = "pointer";
		}
		
		return o;
	}
};

var Input = {
	newInstance : function(top, left, right, bottom, width, height)
	{
		var o = document.createElement("input");
		o.type = "text";
		o.maxLength = 128;
		o.style.fontSize = "11px";
		o.style.fontStyle = "normal";
		o.style.fontFamily = "tahoma, arial, verdana, sans-serif";
		o.style.width = width + "px";
		o.style.position = "absolute";
		o.style.padding = "2px 2px 2px 2px";
		o.style.top = top + "px";
		o.style.left = left + "px";
		o.style.border = "1px solid #808080";
		o.style.background = "#FFFFFF";

		return o;
	}
};

var Div = {
	newInstance : function(top, left, right, bottom, width, height, background)
	{
		var o = document.createElement("div");
		o.style.position = "absolute";
		o.style.top = top + "px";
		o.style.left = left + "px";
		o.style.width = width + "px";
		o.style.height = height + "px";
		o.style.background = background;
		o.style.overflow = "hidden";
		return o;
	}
};

var Text = {
	newInstance : function(text, top, left, right, bottom, fontWeight)
	{
		var textEl = document.createElement("div");
		textEl.style.position = "absolute";
		textEl.style.top = top + "px";
		textEl.style.left = left + "px";
		textEl.style.fontSize = "11px";
		textEl.style.fontStyle ="normal";
		textEl.style.fontFamily = "tahoma, arial, verdana, sans-serif";
		textEl.style.fontWeight = fontWeight;
		textEl.style.color = "#000000";
		textEl.innerHTML = text;
		return textEl;
	}
}
