var xhttp;

function createXHTTP() {
	try {
		xhttp = new XMLHttpRequest();
	} catch (e) {
		var MSXmlVersion = new Array('MSXML2.XMLHttp.6.0',
				'MSXML2.XMLHttp.5.0', 'MSXML2.XMLHttp.4.0',
				'MSXML2.XMLHttp.3.0', 'MSXML2.XMLHttp.2.0', 'Microsoft.XMLHttp');
		for ( var i = 0; i < MXSmlVersion.length; i++) {
			try {
				xhttp = new ActiveXObject(MSXmlVersion[i]);
				break;
			} catch (e) {
				continue;
			}
		}
	}
	if (!xhttp) {
		alert('Došlo k neočekávané chybě');
	}
}

createXHTTP();

if (document.getElementById) {

	( function() {

		// Stop Opera selecting anything whilst dragging.
		if (window.opera) {
			document.write('<input type="hidden" id="dragClear" value="" />');
		}

		//var n = 500;
		var dragok = false;
		var y, x, d, dy, dx;

		function windowMove(e) {
			if (!e) {
				e = window.event;
			}
			if (dragok) {
				document.getElementById(d).style.left = dx + e.clientX - x + 'px';
				document.getElementById(d).style.top = dy + e.clientY - y + 'px';
				return false;
			}
			return true;
		}

		function mouseDownProcess(e) {
			if (!e) {
				e = window.event;
			}
			var temp = (typeof e.target != 'undefined') ? e.target
					: e.srcElement;

			if (temp.id == 'tm-windowText' || temp.id == 'tm-windowFormText'  || temp.id == 'tm-windowDetailText') {
				d = temp.id;
				d = d.replace('Text', '');
				document.getElementById(d).style.top = document.getElementById(d).offsetTop + 'px';
				document.getElementById(d).style.left = document.getElementById(d).offsetLeft + 'px';
				document.getElementById(d).style.marginTop = '0';
				document.getElementById(d).style.marginLeft = '0';
					
			
				if (window.opera) {
					document.getElementById('dragClear').focus();
				}
				dragok = true;
				//temp.style.zIndex = n++;
				//d = temp;
				dx = parseInt(document.getElementById(d).style.left + 0);
				dy = parseInt(document.getElementById(d).style.top + 0);
				x = e.clientX;
				y = e.clientY;
				document.onmousemove = windowMove;
				return false;
			}
			return true;
		}

		function mouseUpProcess() {
			if (dragok == true) {
				if (d == 'tm-window') {
					windowPosX = parseInt(document.getElementById(d).style.left + 0);
					windowPosY = parseInt(document.getElementById(d).style.top + 0);
				}
				else if (d == 'tm-windowForm') {
					windowFormPosX = parseInt(document.getElementById(d).style.left + 0);
					windowFormPosY = parseInt(document.getElementById(d).style.top + 0);
				}
				else if (d == 'tm-windowDetail') {
					windowDetailPosX = parseInt(document.getElementById(d).style.left + 0);
					windowDetailPosY = parseInt(document.getElementById(d).style.top + 0);
				}
				dragok = false;
			}
			document.onmousemove = null;
		}
		
		function keyDownProcess(e) {
			if (!e) {
				e = window.event;
			}
			/*if (e.ctrlKey == true) {
				if (e.keyCode == 82) {
					return false;
				}
				else {
					return true;
				}
			}*/
			switch (e.keyCode) {
				case 27:
				/*case 116:
					return false;
				break;*/
				default:
					return true;
				break;
			}
			return false;
		}
		
		function keyPressProcess(e) {
			if (!e) {
				e = window.event;
			}
			/*if (e.ctrlKey == true) {
				if (e.keyCode == 82) {
					return false;
				}
				else {
					return true;
				}
			}*/
			switch (e.keyCode) {
				case 27:
				/*case 116:
					return false;
				break;*/
				default:
					return true;
				break;
			}
			return false;
		}
		
		function keyUpProcess(e) {
			if (!e) {
				e = window.event;
			}
			/*if (e.ctrlKey == true) {
				if (e.keyCode == 82) {
					return false;
				}
				else {
					return true;
				}
			}*/
			switch (e.keyCode) {
				case 27:
					if (document.getElementById(idPre + 'window').style.visibility == 'visible') {
						hideWindow('window');
					}
					else if (document.getElementById(idPre + 'windowForm').style.visibility == 'visible') {
						hideWindow('windowForm');
					}
					else if (document.getElementById(idPre + 'windowDetail').style.visibility == 'visible') {
						hideWindow('windowDetail');
					}
					return false;
				break;
				/*case 116:
					return false;
				break;*/
				default:
					return true;
				break;
			}
			return false;
		}

		document.onmousedown = mouseDownProcess;
		document.onmouseup = mouseUpProcess;
		document.onkeydown = keyDownProcess;
		document.onkeypress = keyPressProcess;
		document.onkeyup = keyUpProcess;
	})();
}
