
function Dialog(){}


Dialog.instances	= {};
Dialog.minZIndex	= 1000;
Dialog.maxZIndex	= 10000;
Dialog.modalHider	= null;

/**
 * Returns count of dialog instances
 */
Dialog.getCount = function() {
	var count = 0;
	for (var i in Dialog.instances) {
		count++;
	}
	return count++;
}


Dialog.mouseMove = function(el, ev) {

	ev = ev || event;

	if (el.draggingStarted) {

		var mouse = getMouseOffset(el, ev);

		//window.status = 'Dialog.mouseMove: ' + [mouse.x, mouse.y];

		var dx = mouse.x - el.draggingStartOffset.x;
		var dy = mouse.y - el.draggingStartOffset.y;
		//alert(dx, dy);
		Dialog.shift(el, dx, dy);

		Dialog.activate(el);
	}
}

Dialog.mouseUp = function(el, ev) {

	ev = ev || event;

	if (el.draggingStarted) {
		el.draggingStarted = false;
	//	alert(el.draggingStarted);

	/**
		if (settings.autofocus) {
			Dialog.activate(el);
		}
	/**/
	}
}

Dialog.mouseDown = function(el, ev) {

	ev = ev || event;

	Dialog.activate(el);

	var mouse = getMouseOffset(el, ev);

	if (mouse.y < 24) {

		el.draggingStarted = true;

		el.draggingStartOffset = getMouseOffset(el, ev);
	}
}

/**
 * Shifts a dialog by deltaX and deltaY
 */
Dialog.shift = function(element, dx, dy) {
	element.style.left	= (element.offsetLeft	+ dx) + "px";
	element.style.top	= (element.offsetTop	+ dy) + "px";
}

/**
 * Registers new dialog instance
 */
Dialog.register = function(id, element) {
	//alert(uniqid("dialog"));

	Dialog.showModalHider();

	var count = Dialog.getCount();

	Dialog.instances[id] = element;

	Dialog.shift(element, 10 * count, 10 * count);

	Dialog.activate(element);
}

/**
 * Unregisters dialog instance
 */
Dialog.unregister = function(dialog) {
	if (typeof dialog == "object") {
		dialog = Dialog.elementToId(dialog);
	}
	if (dialog === false) {
		return false;
	}
	delete Dialog.instances[dialog];

	if (! Dialog.getCount()) {
		Dialog.hideModalHider();
	}
}

/**
 * Converts element object of dialog to its id
 */
Dialog.elementToId = function(element) {
	for (var id in Dialog.instances) {
		if (Dialog.instances[id] == element) {
			return id;
		}
	}
	return false;
}

/**
 * Converts id of dialog to its element object
 */
Dialog.idToElement = function(id) {
	//return Dialog.instances[id] === undefined ? false : Dialog.instances[id];
	return (typeof Dialog.instances[id] == "undefined") ? false : Dialog.instances[id];	// IE 5.0 compatible
}

/**
 * Returns id of top instance of dialog
 */
Dialog.getTopId = function() {

	if (!Dialog.getCount()) return false;

	var maxZ = 0;
	var topId = false;

	for (var id in Dialog.instances) {

		var element = Dialog.instances[id];

		if (element.style.zIndex >= maxZ) {
			maxZ	= element.style.zIndex;
			topId	= element.id;
		}
	}
	return topId;
}

/**
 * Returns top instance of dialog
 */
Dialog.getTop = function() {

	var id = Dialog.getTopId();
	return id ? Dialog.idToElement(id) : false;
}

/**
 * Activates dialog
 */
Dialog.activate = function(dialog)
{
	if (typeof dialog != "object") {
		dialog = Dialog.idToElement(dialog);
	}

	//dialog.style.zIndex = ++Dialog.maxZIndex;
	Dialog.bringToFront(dialog);

	var inputs = dialog.getElementsByTagName("input");

	if (inputs[0] && "hidden" != inputs[0].type) {
		inputs[0].focus();
		//FocusQueue.add(inputs[0])
	}

	window.reserveFocus = true;
}

/**
 * Brings Window to The Front
 */
Dialog.bringToFront = function(dialog)
{
	if (typeof dialog != "object") {
		dialog = Dialog.idToElement(dialog);
	}

	dialog.style.zIndex = ++Dialog.maxZIndex;
}

/**
 * Brings Window to The Back
 */
Dialog.bringToBack = function(dialog)
{
	if (typeof dialog != "object") {
		dialog = Dialog.idToElement(dialog);
	}

	dialog.style.zIndex = --Dialog.minZIndex;
}

Dialog.backgroundMove = function(ev) {
	//var mouse = mouseCoords(ev);
	//window.status = 'Dialog.backgroundMove: ' + [mouse.x, mouse.y];

	var top = Dialog.getTop();
	if (top) {
		Dialog.mouseMove(top, ev);
	} 
}

Dialog.backgroundUp = function(ev) {
//	alert('backgroundUp occured');
	var top = Dialog.getTop();
	if (top) {
		Dialog.mouseUp(top, ev);
	} 
}

Dialog.getModalHider = function() {

	if (! Dialog.modalHider) {

		var el = document.getElementById("modalHider");

		if (!el) {
			el = document.createElement("div");
			el.setAttribute("id", "modalHider");
			document.body.appendChild(el);
		}

/**
		el.onmousemove = Dialog.backgroundMove;
		el.onmouseup = Dialog.backgroundUp;
/**/
		document.body.onmousemove = Dialog.backgroundMove;
		document.body.onmouseup= Dialog.backgroundUp;

		el.setAttribute("style", "opacity: 0.3;");

		Dialog.modalHider = el;
	}
	return Dialog.modalHider;
}


Dialog.showModalHider = function() {
	Dialog.getModalHider().style.display = "block";
}


Dialog.hideModalHider = function() {
	Dialog.getModalHider().style.display = "none";
}

/**
 * Returns container for all dialogs
 */
Dialog.getContainer = function() {
	var el = document.getElementById("dialogContainer");

	if (!el) {
		el = document.createElement("div");
		el.setAttribute("id", "dialogContainer");
		document.body.appendChild(el);
	}

	//el.onmousemove = Dialog.backgroundMove;

	return el;
}

/**
 * Closes (destroys) dialog
 */
Dialog.close = function(dialog) {

	Dialog.unregister(dialog);

	Dialog.getContainer().removeChild(dialog);


	var top = Dialog.getTop();

	if (top) {
		Dialog.activate(top);
	}

	//dialog.style.display = "none";	
}

/**
 * Creates new dialog
 */
Dialog.create = function(message, title, settings)
{
	var namespace = "dialog";
	var dialogId = namespace + uniqid(namespace);
	//var dialogId	= uniqid(namespace);
	var titleId		= dialogId + "_title";
	var messageId	= dialogId + "_message";

	var settings = settings || {};

	var dialogElement = document.createElement("div");
	dialogElement.className = "dialog";
	dialogElement.id = dialogId;

	var html = 
		//'<div class="dialog" id="' + dialogId + '">' +
		'<div class="title" onmousedown="Dialog.mouseDown(this.dialogElement, event);"><div class="l"><div class="r" id="' + titleId + '">' + title + '</div></div></div>' +
		'<table>' +
		'<thead><tr><td class="l"><img src="/img/spacer.gif" alt=""></td><th></th><td class="r"></td></tr></thead>' +
		'<tbody><tr><td class="l"></td><th id="' + messageId + '">' +
		message +
		'</th><td class="r"><img src="/img/spacer.gif" alt=""></td></tr></tbody>' +
		'<tfoot><tr><td class="l"></td><th></th><td class="r"></td></tr></tfoot>' +
		'</table>';
		//+ '</div>'

	dialogElement.innerHTML = html;

/**
	dialogElement.onclick = function() {
		//Dialog.activate(this);
	}
/**/

	var titleElement = document.getElementById(dialogId + "_title");

/**
	dialogElement.onmousedown = function(ev) {
		return Dialog.mouseDown(this, ev);
	}
/**/
	dialogElement.onmousemove = function(ev) {
		//var mouse = mouseCoords(ev);
		//window.status = 'Dialog.onmousemove: ' + [mouse.x, mouse.y];

		return Dialog.mouseMove(this, ev);
	}


	dialogElement.onmouseup = function(ev) {
		return Dialog.mouseUp(this, ev);
	}
/**/

	//Dialog.getContainer().innerHTML += html;
	Dialog.getContainer().appendChild(dialogElement);
	
	var titleElement	= document.getElementById(titleId);
	var messageElement	= document.getElementById(messageId);

	dialogElement.titleElement		= titleElement;
	dialogElement.messageElement	= messageElement;

	// dialogElement.all is required for IE 5.5 as it doesn't understand getElementsByTagName("*")
	var elements = dialogElement.all || dialogElement.getElementsByTagName("*");

	//var cnt = 0;	var str = "";
	for (var i in elements) {
		if (! elements[i].tagName) continue;
		elements[i].dialogElement = dialogElement;
		//cnt++;	str += "\n" + elements[i].tagName;
	}
	//alert(cnt + "\n" + str);

	// TMP: fixing horizontal & vertical position. TODO: do it via CSS.
	Dialog.shift(dialogElement, -dialogElement.offsetWidth/2, -dialogElement.offsetHeight/2);

	Dialog.register(dialogId, dialogElement);

	return dialogElement;
}


/**
Dialog.toggleContainer = function(state) {
	if (state === undefined) {
		alert("undefined container state");
	}
}
/**/


MessageBox = function() {}

MessageBox.create = function(text, title)
{
		var html = text +
		'<br /><br />' +
		'<input id="dialogSubmit" type="submit" value="    OK    " onclick="Dialog.close(this.dialogElement)">';

		var dialogElement = Dialog.create(html, title);

		return dialogElement;
}
