﻿var dragObject = null;
var mouseOffset = null;
var hintContainer = null;
var hintTimer = null;
var dialogContainer = null;
var dialogTimer = null;
var modalOverlay = null;

window.onresize = function()
{
	Forms.resizeScreen();
}

var BrowserInfo =
{
	isIE: function()
	{
		if (navigator.appName == 'Microsoft Internet Explorer')
		{
				return true;
		}
		else
		{
				return false;
		}
	},
	
	isOpera: function()
	{
		if (navigator.appName == 'Opera')
		{
			return true;
		}
		else
		{
			return false;
		}
	}
}

var Forms =
{
	// Obsolete - use getPageSize instead
	getWindowWidth: function(w)
	{
		if (!w)
		{
			w = top;
		}
		if (w.innerWidth)
		{
			return w.innerWidth;
		}
		else if (w.document.documentElement && w.document.documentElement.clientWidth)
		{
			return document.documentElement.clientWidth;
		}
		else if (w.document.body && w.document.body.clientWidth)
		{
			return w.document.body.clientWidth;
		}
		else
		{
			return null;
		}
	},
	
	// Obsolete - use getPageSize instead
	getWindowHeight: function(w)
	{
		if (!w)
		{
			w = top;
		}
		if (w.innerHeight)
		{
			return w.innerHeight;
		}
		else if (w.document.documentElement && w.document.documentElement.clientHeight)
		{
			return w.document.documentElement.clientHeight;
		}
		else if (w.document.body && w.document.body.clientHeight)
		{
			return w.document.body.clientHeight;
		}
		else
		{
			return null;
		}
	},
	
	getPageSize: function()
	{
  		var xScroll, yScroll;

  		if (window.innerHeight && window.scrollMaxY)
  		{	
  			xScroll = document.body.scrollWidth;
  			yScroll = window.innerHeight + window.scrollMaxY;
  		}
  		else if (document.body.scrollHeight > document.body.offsetHeight)
  		// all but Explorer Mac
  		{
  			xScroll = document.body.scrollWidth;
  			yScroll = document.body.scrollHeight;
  		}
  		// Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
  		else
  		{
  			xScroll = document.body.offsetWidth;
  			yScroll = document.body.offsetHeight;
  		}

  		var windowWidth, windowHeight;
		
		// all except Explorer
  		if (self.innerHeight)
  		{
  			windowWidth = self.innerWidth;
  			windowHeight = self.innerHeight;
  		} 
  		// Explorer 6 Strict Mode
  		else if (document.documentElement && document.documentElement.clientHeight)
  		{ 
  			windowWidth = document.documentElement.clientWidth;
  			windowHeight = document.documentElement.clientHeight;
  		}
  		// other Explorers
  		else if (document.body)
  		{
  			windowWidth = document.body.clientWidth;
  			windowHeight = document.body.clientHeight;
  		}	
  		
  		var pageHeight, pageWidth;

  		// for small pages with total height less then height of the viewport
  		if(yScroll < windowHeight)
  		{
  			pageHeight = windowHeight;
  		}
  		else
  		{ 
  			pageHeight = yScroll;
  		}

  		// for small pages with total width less then width of the viewport
  		if(xScroll < windowWidth)
  		{	
  			pageWidth = windowWidth;
  		}
  		else
  		{
  			pageWidth = xScroll;
  		}

  		return {pageWidth: pageWidth, pageHeight: pageHeight, windowWidth: windowWidth, windowHeight: windowHeight};
	},
	
	disableScreen: function()
	{
		if (!modalOverlay)
		{
			modalOverlay = document.createElement('div');
			modalOverlay.id = 'ScreenOverlay';
			modalOverlay.className = 'screenoverlay';
			if (BrowserInfo.isIE())
			{
				modalOverlay.style.position = 'absolute';
			}
			else
			{
				modalOverlay.style.position = 'fixed';
			}
			modalOverlay.style.zIndex = 10;
			document.body.insertBefore(modalOverlay, document.body.firstChild);
		}
		modalOverlay.style.left = '0';
		modalOverlay.style.top = '0';
		Forms.resizeScreen();
		modalOverlay.style.display = 'block';
	},
	
	enableScreen: function()
	{
		if (modalOverlay != null)
		{
			modalOverlay.style.display = 'none';
			modalOverlay.parentNode.removeChild(modalOverlay);
			modalOverlay = null;
		}
	},
	
	resizeModalOverlay: function()
	{
		if (modalOverlay != null)
		{
			var pageSize = Forms.getPageSize();
			modalOverlay.style.width = (pageSize.windowWidth + 'px');
			modalOverlay.style.height = (pageSize.pageHeight + 'px');
		}
	},
	
	showDialog: function(id, dialogTitle, dialogText, width, mode, displayInterval)
	{
		Forms.hideDialog();
		var contentWidth = width - 6;
		Forms.disableScreen();
		if (!dialogContainer)
		{
			dialogContainer = document.createElement('div');
			dialogContainer.id = 'DialogContainer';
			dialogContainer.style.position = 'absolute';
			dialogContainer.style.zIndex = 20;
			document.body.insertBefore(dialogContainer, document.body.firstChild);
		}
		var inputElement = document.getElementById(id);
		var inputElementLength = inputElement.offsetWidth;
		if (dialogTitle == '')
		{
			dialogTitle = '&nbsp;';
		}
		var dialogHTML = '<div class=\"modal\"><div class=\"modal-ct1\" style=\"width:' + width + 'px;\"><div class=\"modal-ct2\" style=\"width:' + contentWidth + 'px;\"><div class=\"modal-title\" id=\"dragArea\">' + dialogTitle + '</div><div class=\"modal-content\">' + dialogText + '</div>';
		
		if (mode == 'info' || mode == 'alert')
		{
			dialogHTML += '<div class=\"modal-buttons\"><input type=\"button\" value=\"OK\" style=\"width:50px;\" onclick=\"Forms.hideDialog();\"/></div>';
		}
		else if (mode == 'confirm')
		{
			dialogHTML += '<div class=\"modal-buttons\"><input type=\"button\" value=\"ANO\" style=\"width:50px;\" onclick=\"Forms.hideDialog();\"/></div>';
		}
		
		dialogHTML += '</div></div><div class=\"modal-footer-box\" style=\"width:' + width + 'px;\"><div class=\"modal-footer\" style=\"width:' + contentWidth + 'px;\"></div></div></div>';
		dialogContainer.innerHTML = dialogHTML;
		Forms.repositionDialog();
		dialogContainer.style.display = 'block';
		DragAndDrop.makeDraggable(dialogContainer);
		if (!isNaN(displayInterval) && displayInterval > 0)
		{
			displayInterval = displayInterval * 1000;
			dialogTimer = setTimeout('Forms.hideDialog()', displayInterval);
		}
	},
	
	hideDialog: function()
	{
		if (dialogTimer != null)
		{
			clearTimeout(dialogTimer);
		}
		if (dialogContainer != null)
		{
			dialogContainer.parentNode.removeChild(dialogContainer);
			dialogContainer.style.display = 'none';
			dialogContainer = null;
			Forms.enableScreen();
		}
	},
	
	repositionDialog: function()
	{
		if (dialogContainer != null)
		{
			var pageSize = Forms.getPageSize();
			dialogContainer.style.left = Math.floor((pageSize.windowWidth / 2) - Math.floor(dialogContainer.offsetWidth / 2)) + 'px';
			dialogContainer.style.top = Math.floor((pageSize.windowHeight / 2) - Math.floor(dialogContainer.offsetHeight / 2)) + 'px';
		}
	},
	
	resizeScreen: function()
	{
		Forms.repositionDialog();
		Forms.resizeModalOverlay();
	},
	
	
	showHint: function(id, hintTitle, hintText, displayInterval, mode)
	{
		var rootElement = document.forms[0];
		if (hintContainer == null)
		{
			hintContainer = document.createElement('div');
			hintContainer.id = 'HintContainer';
			hintContainer.style.position = 'absolute';
			rootElement.appendChild(hintContainer);
		}
		var inputElement = document.getElementById(id);
		var inputElementLength = inputElement.offsetWidth;
		if (hintTitle == '')
		{
			hintTitle = '&nbsp;';
		}
		var cssClass = 'hint';
		if (mode == 2)
		{
			cssClass = 'hint2';
		}
		var hintHTML = '<div class=\"' + cssClass + '\"><div class=\"nib\"></div><div class=\"hint-ct1\"><div class=\"hint-ct2\"><div class=\"hint-title\">' + hintTitle + '</div><div class=\"hint-content\">' + hintText + '</div></div></div><div class=\"hint-footer-box\"><div class=\"hint-footer\"></div></div></div>';
		var positionLeft = inputElement.offsetLeft;
		var positionTop = inputElement.offsetTop;
		while((inputElement = inputElement.offsetParent) && (inputElement != rootElement))
		{
			positionLeft += inputElement.offsetLeft;
			positionTop += inputElement.offsetTop;
		}
		hintContainer.innerHTML = hintHTML;
		var leftMove = 20;
		if (mode == 2)
		{
			leftMove = 42;
		}
		hintContainer.style.left = (positionLeft + inputElementLength + leftMove) + 'px';
		hintContainer.style.top = (positionTop - 25) + 'px';
		hintContainer.style.display = 'block';
		if (!isNaN(displayInterval) && displayInterval > 0)
		{
			displayInterval = displayInterval * 1000;
			hintTimer = setTimeout('Forms.hideHint()', displayInterval);
		}
	},

	hideHint: function()
	{
		if (hintTimer != null)
		{
			clearTimeout(hintTimer);
		}
		if (hintContainer != null)
		{
			hintContainer.style.display = 'none';
		}
	},
	
	cacheImages: function()
	{
		if (document.images)
		{
			var imageArray = ['i/hint/Bottom.gif', 'i/hint/BottomRight.gif', 'i/hint/Dialog.gif', 'i/hint/Nib.gif', 'i/hint/Right.gif',
				'i/hint2/Bottom.gif', 'i/hint2/BottomRight.gif', 'i/hint2/Dialog.gif', 'i/hint2/Nib.gif', 'i/hint2/Right.gif'];

// 'Images/Modal/Bottom.gif', 'Images/Modal/BottomRight.gif', 'Images/Modal/Dialog.gif', 'Images/Modal/Nib.gif', 'Images/Modal/Right.gif'];
			
			var len = imageArray.length;
			for (var i = 0; i < len; i++)
			{
				var cachedImage = new Image();
				cachedImage.src = imageArray[i];
			}
		}
	}
}

var DragAndDrop =
{
	getMouseOffset: function(target, ev)
	{
		ev = ev || window.event;
		var docPos = DragAndDrop.getPosition(target);
		var mousePos = DragAndDrop.mouseCoords(ev);
		return {x:mousePos.x - docPos.x, y:mousePos.y - docPos.y};
	},

	getPosition: function(e)
	{
		var left = 0;
		var top = 0;

		while (e.offsetParent)
		{
			left += e.offsetLeft;
			top += e.offsetTop;
			e = e.offsetParent;
		}
		left += e.offsetLeft;
		top += e.offsetTop;
		return {x:left, y:top};
	},

	mouseMove: function(ev)
	{
		ev = ev || window.event;
		var mousePos = DragAndDrop.mouseCoords(ev);

		if(dragObject && mousePos != null)
		{
			if (dialogTimer != null)
			{
				clearTimeout(dialogTimer);
			}
			dragObject.style.position = 'absolute';
			dragObject.style.top = (mousePos.y - mouseOffset.y) + 'px';
			dragObject.style.left = (mousePos.x - mouseOffset.x) + 'px';
			return false;
		}
	},

	mouseCoords: function(ev)
	{
		if (ev.pageX || ev.pageY)
		{
			return {x:ev.pageX, y:ev.pageY};
		}
		if (ev != null && document.body != null)
		{
			return {
				x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,
				y:ev.clientY + document.body.scrollTop - document.body.clientTop
			};
		}
		return null;
	},

	mouseUp: function()
	{
		// alert('Position to store: ' + dragObject.style.top + ', ' + dragObject.style.left);
		dragObject = null;
	},

	makeDraggable: function(item)
	{
		if (item == null)
		{
			return;
		}
		
		document.onmousemove = DragAndDrop.mouseMove;
		document.onmouseup = DragAndDrop.mouseUp;

		item.onmousedown = function(ev)
		{
			dragObject = this;
			mouseOffset = DragAndDrop.getMouseOffset(this, ev);
			return false;
		}
	}
}