var dialog_enableDebug = false;

var dialog = {};
dialog.dragging = false;
dialog.position = {"x":0, "y":0};
dialog.drag_start = {"x":0, "y":0};
dialog.drag_mouse = {"x":0, "y":0};

/* wrappers */
function openDialog(disableCentering) {
	debugPrint("openDialog","dialog");
	return dialog_open(disableCentering);
}
function centerDialog() {
	debugPrint("centerDialog()", "dialog");
	return dialog_center();
}
function closeDialog() {
	return dialog_close();
}
function loadDialog(url, callback) {
	return dialog_load(url, callback);
}
function refreshDialog(url, callback) {
	return dialog_refresh(url, callback);
}
/* end wrappers */


function dialog_open(disableCentering) {
	el = document.getElementById('hiddenDialog');
	el.style.display = 'block';
	if(!disableCentering) dialog_center();
}

function dialog_center() {
	el = document.getElementById('hiddenDialog');
	if(el) {
		var cArea = common_getClientArea();
		var dSize = common_getSize(el);

		dialog.position.x = (cArea.width - dSize.width) / 2;
		dialog.position.y = (cArea.height - dSize.height) / 2;
		dialog_keepPosition();
	}
}

function dialog_close() {
	el = document.getElementById('hiddenDialog');
	el.style.display= 'none';	// ja piiloon
	el.innerHTML = "";	// putsataan turha koodi hidastamasta sivua
	dialog_updatePosition();
}

function dialog_load(url, callback) {
	var slicePos = url.indexOf('?');
	var postData = url.substr(slicePos+1);
	var url = url.substr(0,slicePos);

	debugPrint("dialog_load: "+postData,"dialog");

	var dialogrequest = createXMLHttpRequest();
	dialogrequest.open("POST", url, true);

	dialogrequest.onreadystatechange=function() {
		if(dialogrequest.readyState==4) {
			if(dialogrequest.status == 200) {
				debugPrint("dialog_load: dialog loaded","dialog");
				var el = document.getElementById('hiddenDialog');

				var regString = '<!-- <dialog> -->([^\000]*?)<!-- </dialog> -->';
				var rx = new RegExp(regString, "mig");
				var match = rx.exec(dialogrequest.responseText);
				if(match) {
					debugPrint("dialog_load: updating dynamic dialog innerHTML","dialog");
					el.innerHTML = match[0];
					dialog_open();
					if(callback) callback();
				} else {
					alert("dialog_load: xmlhttprequest returned no dialog-content. What it returned was:\n"+dialogrequest.responseText);
				}
			} else {
				alert("status != 200");
			}
		}
	}
	// vältetään cachetus
	dialogrequest.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
	dialogrequest.setRequestHeader('Content-Type','application/x-www-form-urlencoded');

	dialogrequest.send(postData);

	debugPrint("dialog_load: sent XMLHttpRequest","dialog");

	return;
}

function dialog_refresh(url, callback) {
	var slicePos = url.indexOf('?');
	var postData = url.substr(slicePos+1);
	var url = url.substr(0,slicePos);

	debugPrint("dialog_refresh: "+postData,"dialog");

	var dialogrequest = createXMLHttpRequest();
	dialogrequest.open("POST", url, true);

	dialogrequest.onreadystatechange=function() {
		if(dialogrequest.readyState==4) {
			if(dialogrequest.status == 200) {
				debugPrint("dialog_refresh: dialog loaded","dialog");
				var el = document.getElementById('hiddenDialog');

				var regString = '<!-- <dialog> -->([^\000]*?)<!-- </dialog> -->';
				var rx = new RegExp(regString, "mig");
				var match = rx.exec(dialogrequest.responseText);
				if(match) {
					debugPrint("dialog_refresh: updating dynamic dialog innerHTML","dialog");
					el.innerHTML = match[0];
					dialog_open(true);
					if(callback) callback();
				} else {
					alert("dialog_refresh: xmlhttprequest returned no dialog-content. What it returned was:\n"+dialogrequest.responseText);
				}
			} else {
				alert("status != 200");
			}
		}
	}
	// vältetään cachetus
	dialogrequest.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
	dialogrequest.setRequestHeader('Content-Type','application/x-www-form-urlencoded');

	dialogrequest.send(postData);

	debugPrint("dialog_refresh: sent XMLHttpRequest","dialog");

	return;
}

function dialog_focusFirst() {
	el = document.getElementById('hiddenDialog');
	if(el) {
		var inputs = document.getElementsByName("dialogProp");
		if(inputs) inputs[0].focus();
	}
}

function dialog_focusById(id) {
	var dlg = document.getElementById('hiddenDialog');
	var focusElement = dialog_searchChildWithId(dlg, id);
	if(focusElement) {
		focusElement.focus();
		focusElement.select();
	}
}

function dialog_searchChildWithId(el, id) {
	var ret = null;
	if(el.hasChildNodes()) {
//		var failSafe = 16;
//		debugPrint("childCount: "+el.childNodes.length,"dialog");
		for(var i=0; i<el.childNodes.length; i++) {
//			debugPrint("child "+i+": "+el.childNodes[i].tagName,"dialog");
			if(el.childNodes[i].id == id) {
				return el.childNodes[i];
			}
			if(ret = dialog_searchChildWithId(el.childNodes[i], id)) {
				break;
			}
//			failSafe--;
//			if(failSafe < 0) break;
		}
	}
	return ret;
}

function dialog_keepPosition() {
	if(!dialog.dragging) dialog_updatePosition();
	setTimeout("dialog_keepPosition()", 10);
}

function dialog_updatePosition() {
	el = document.getElementById("hiddenDialog");
	if(el) {
		var scroll = common_getScroll();
		el.style.left = dialog.position.x + scroll.x;
		el.style.top = dialog.position.y + scroll.y;
	}
}

// kertoo onko dialogin DIV näkyvissä.
function isDialogVisible() {
	var el = document.getElementById('hiddenDialog');

	if(el) {
		if(el.style) {
			if(el.style.display == "block") {
				return true;
			} else {
				return false;
			}
		} else {
			return false;
		}
	} else {
		return false;
	}
}

function dialog_drag_start(e) {
	if(!e) e = window.event;
	debugPrint("dialog_drag_start(): e.button="+e.button, "dialog");
	var el = document.getElementById("hiddenDialog");
	if(el) {
		dialog.drag_start = common_getPosition(el);
		dialog.drag_mouse = common_getMousePosition(e);
		dialog.dragging = true;
		if (document.addEventListener){
			document.addEventListener('mousemove', dialog_drag, true);
			document.addEventListener('mouseup', dialog_drag_end, true);
		} else if(document.attachEvent){
			document.attachEvent('onmousemove', dialog_drag);
			document.attachEvent('onmouseup', dialog_drag_end);
		}
	}

	if(e.preventDefault) e.preventDefault();
	e.cancelBubble = true;
	return false;
}

function dialog_drag(e) {
	if(!e) e = window.event;

	var el = document.getElementById("hiddenDialog");
	if(el) {
		var mousePos = common_getMousePosition(e);
		var scroll = common_getScroll();
		var dx = (mousePos.x - dialog.drag_mouse.x);
		var dy = (mousePos.y - dialog.drag_mouse.y);

		dialog.position.x = dialog.drag_start.x + dx - scroll.x;
		dialog.position.y = dialog.drag_start.y + dy - scroll.y;
		dialog_updatePosition();
	}

	if(e.preventDefault) e.preventDefault();
	e.cancelBubble = true;
	return false;
}

function dialog_drag_end(e) {
	debugPrint("dialog_drag_end()", "dialog");
	dialog_reset_drag();
	e.cancelBubble = true;
	return false;
}

function dialog_reset_drag() {
	debugPrint("dialog_reset_drag()", "dialog");
	dialog.dragging = false;
	if (document.removeEventListener){
		document.removeEventListener('mousemove', dialog_drag, true);
		document.removeEventListener('mouseup', dialog_drag_end, true);
	} else if(document.detachEvent){
		document.detachEvent('onmousemove', dialog_drag);
		document.detachEvent('onmouseup', dialog_drag_end);
	}
}

