/*

	Dialog library. Creates an in page popup dialog with [Ok]/[Cancel]
	see examples below to understand how to use
	<script>
*/



// sample dialog use
function sampleDialog() {
	// pageDialog is a page level var that points to an instance of a dialog.
	pageDialog.message = "Are you sure you want to delete selected items?"
	pageDialog.title   = "Deleting Items..."
	pageDialog.onOk = sampleOk;
	pageDialog.onCancel = sampleCancel;
	pageDialog.modal = true;
					
	pageDialog.show();
}

// default ok function
function sampleOk() {
	// do what is needed for Ok.
}
// default cancel function
function sampleCancel() {
	// do what is needed on cancel
}


var _DialogDragging = false;
var _DialogTarget;
var _DialogMouseX;
var _DialogMouseY;
var _DialogPosX;
var _DialogPosY;
var _DialogOldRef;
var _DialogIe = navigator.userAgent.indexOf("MSIE")>=0;

// page level dialog instance
var pageDialog = new Dialog();

// default contructor
function Dialog() {
	this.message  = "Message unspecified. Use message property to set it.";
	this.title    = "Title unspecified. Use title property to set it.";
	this.modal    = true;
	this.onOk     = DialogClose;
	this.onCancel = DialogClose;
	this.close    = DialogClose;
	this.show     = DialogShow;
	this._onOk     = DialogOk;
	this._onCancel = DialogCancel;
}

// default ok function
function DialogOk() {
	DialogClose();
	pageDialog.onOk();
}
// default cancel function
function DialogCancel() {
	DialogClose();
	pageDialog.onCancel();
}

// closes dialog with either Ok/Cancel action
function DialogClose() {
	// hide dialog
	document.getElementById("dialog").style.display="none";
	document.getElementById("dialogModal").style.display="none";
}
				
// shows dialog
function DialogShow() {
	// set text
	with (document) {
		var dialogElem = getElementById("dialog");
		// set message
		getElementById("dialogBody").innerHTML  = this.message;
		getElementById("dialogTitle").innerHTML = this.title;

		// if modal, show modal
		var numWidth  = 0;
		var numHeight = 0;
		var chrome    = navigator.userAgent.indexOf("Chrome")>=0;
						
		if (this.modal) {
			// if (document.documentElement.height) {
			numHeight = Math.max(documentElement.clientHeight, documentElement.scrollHeight);
			numWidth =  Math.max(documentElement.clientWidth, documentElement.scrollWidth);
							
			// special case for chrome
			if (chrome) numHeight = document.height;
							
			var modalDiv = getElementById("dialogModal");
			modalDiv.style.top = 0;
			modalDiv.style.left = 0;
			modalDiv.style.width = numWidth + "px";
			modalDiv.style.height = numHeight + "px";
			modalDiv.style.display="block";
		}

		numWidth  = window.innerWidth  ? window.innerWidth  : documentElement.clientWidth;
		numHeight = window.innerHeight ? window.innerHeight : documentElement.clientHeight;
						
		if (chrome) body.scrollIntoView();
						
		// move to ctr and show dialog
		dialogElem.style.display="block";
	    dialogElem.style.left = (document.documentElement.scrollLeft+(numWidth -dialogElem.clientWidth )/2)+'px';
		dialogElem.style.top  = (document.documentElement.scrollTop +(numHeight-dialogElem.clientHeight)/2)+'px';
						
		// dialog actions
		getElementById("dialogOk").onclick     = this._onOk;
		getElementById("dialogCancel").onclick = this._onCancel;
		getElementById("dialogClose").onclick  = this._onCancel;
		getElementById("dialogCancel").focus();
						
		// wire drag events
		titleElement = getElementById("dialogCaption"); //dialogTitle
		titleElement['target']   = "dialog";
		titleElement.onmousedown = DialogMouseDownWindow;
						
		DialogAttachEvents();
	}
					
	function DialogMouseDownWindow() {
		if ( _DialogIe && window.event.button != 1) return;
		//if (!_DialogIe && e.button            != 0) return;

		_DialogDragging = true;
		_DialogTarget   = this['target'];
		_DialogMouseX   = _DialogIe ? window.event.clientX : e.clientX;
		_DialogMouseY   = _DialogIe ? window.event.clientY : e.clientY;

		if (_DialogIe) {
		     _DialogOldRef = document.onselectstart;
		     document.onselectstart = function() {return false;};
		}
		else {
			_DialogOldRef = document.onmousedown;
			document.onmousedown   = function() {return false;};
		}
	}

	// mouse down
	function DialogMouseDown(e) {
		_DialogPosX = _DialogIe ? window.event.clientX : e.clientX;
		_DialogPosY = _DialogIe ? window.event.clientY : e.clientY;
	}

	// mouse move logic
	function DialogMouseMove(e)	{
		var dialogElement = document.getElementById(_DialogTarget);
		var mouseX  = _DialogIe ? window.event.clientX : e.clientX;
		var mouseY  = _DialogIe ? window.event.clientY : e.clientY;

		if (!_DialogDragging) return;

		dialogElement.style.left = (dialogElement.offsetLeft+mouseX-_DialogMouseX)+'px';
		dialogElement.style.top  = (dialogElement.offsetTop +mouseY-_DialogMouseY)+'px';

		_DialogMouseX = _DialogIe ? window.event.clientX : e.clientX;
		_DialogMouseY = _DialogIe ? window.event.clientY : e.clientY;
	}

	// mouse up function
	function DialogMouseUp(e) {
		var dialogElement = document.getElementById(_DialogTarget);
		if (!_DialogDragging) return;

		_DialogDragging = false;
		if (_DialogIe) 
			document.onselectstart = _DialogOldRef;
		else 
			document.onmousedown   = _DialogOldRef;
	}

					
	// Attach Events
	function DialogAttachEvents() {
		if (_DialogIe) {
			document.attachEvent('onmousedown', DialogMouseDown);
			document.attachEvent('onmousemove', DialogMouseMove);
			document.attachEvent('onmouseup', DialogMouseUp);
		}
		else {
			document.addEventListener('mousedown', DialogMouseDown, false);
			document.addEventListener('mousemove', DialogMouseMove, false);
			document.addEventListener('mouseup', DialogMouseUp, false);
		}
	}
}
