// JavaScript Document
  	var mo_cursorElement;
	var ml_offsetX, ml_offsetY, ml_zOrder;
	var mb_systemLoaded;
	
	mo_cursorElement = null;
	ml_offsetX = 0;
	ml_offsetY = 0;
	ml_zOrder = 0;
	mb_systemLoaded = false;
	
  	function setImage( ctrl, newImage ){
		ctrl.src = newImage.src;
	}
	
	function swapVisible( ctrl1, ctrl2 ){
		oldDisplay = ctrl2.style.display
		ctrl2.style.display = ctrl1.style.display;
		ctrl1.style.display = oldDisplay;
	}

	function getZOrder(){
		var desktopObj;
		desktopObj = document.getElementById("eDesktop");
		if ( desktopObj.childNodes.length == 0 ) return 0;
//		alert( a_container.childNodes.item( a_container.childNodes.length - 1 ).style.zIndex );
		return desktopObj.childNodes.item( desktopObj.childNodes.length - 1 ).style.zIndex + 1;
//		ml_zOrder = ml_zOrder + 1;
//		return ml_zOrder;
	}
	
	// vrati tabulku
	function getElement( aHandle )
	{
		var apps = document.getElementById("eDesktop").childNodes;

		for ( i=0 ;i<apps.length; i++){
			var aElement = apps.item(i);
			if( aElement.nodeName == "TABLE" ){
				if( aElement.id == aHandle ){
					return aElement;
				}
			}
		}
	}
	
	function insertElement( elementSrc, aState, aHandle, templateName ){
		var appTable;
		appTable = document.getElementById(templateName).cloneNode(true);
		appTable.id = aHandle;
		appTable.style.display="";
		appTable.style.possition = "absolute";
		var desktopObj;
		desktopObj = document.getElementById("eDesktop");
		appTable.style.zIndex = getZOrder();
		desktopObj.appendChild(appTable);
		
		var aElement= getWindow( appTable );	// document.createElement("IFRAME");
		
		switch(aState){
			case "centered":{
				moveElement(appTable, (document.body.clientWidth - aElement.clientWidth)/2,(document.body.clientHeight - aElement.clientHeight)/2);
				break;
			}
			case "minimized":{
				minimizeWindow(appTable);
				break;
			}
			case "maximized":{
				maximizeWindow(appTable);
				break;
			}
		}

		aElement.src= elementSrc;
		return appTable;
	}
	
	function startMoveElement( aElement, aOffsetX, aOffsetY ){
		// test if eletemnt is not minimalized
		mo_cursorElement = aElement;
		if ( mo_cursorElement != null && mo_cursorElement.tBodies.item("tApp",0).style.display != "none" ){
			ml_offsetX = aOffsetX - aElement.offsetLeft;
			ml_offsetY = aOffsetY - aElement.offsetTop;
			
			// set window to front
//			document.getElementById("eDesktop").appendChild( document.getElementById("eDesktop").removeChild( aElement ) ); // neviem preco ale mozila refresuje obsah

			// toto je alternativa k hornemu riadku
			aElement.style.zIndex = getZOrder();		// na mozile to nefunguje
			
			var wnd = getWindow( aElement );
			document.getElementById("ePageSrc").innerHTML = getWindowMETAValue( wnd.contentWindow, "source", wnd.src );
		}else{
			mo_cursorElement = null;
		}
	}
	function moveElement( aElement, aLeft, aTop ){
 		if ( aElement != null ){
			aElement.style.left = aLeft;
			aElement.style.top = aTop;
		}
	}
	function getWindow( aElement ){
		return aElement.tBodies.item("tApp",0).rows.item(0).cells.item(0,0).lastChild;
	}
	function setTitle( aElement, aTitle ){
		var divs = aElement.rows[0].cells.item(0).getElementsByTagName("SPAN");
		divs[0].innerHTML = aTitle;
		divs = aElement.rows[3].cells.item(0).getElementsByTagName("SPAN");
		divs[0].innerHTML = aTitle;
	}
	function setStatus( aElement, aStatus ){
		var divs = aElement.rows[2].cells.item(0).getElementsByTagName("SPAN");
		divs[0].innerHTML = aStatus;
//		aElement.rows[2].cells.item(1,0).innerHTML = aStatus;
	}
	function setOnMouseMoveEvent( aElement, aFunction ){
	}
	function minimizeWindow( aElement ){
		aElement.tBodies.item("tApp",0).style.display = "none";
		aElement.tFoot.style.display = "";
		aElement.tHead.style.display = "none";
		aElement.tFoot.style.left = aElement.style.left;
		aElement.tFoot.style.top = aElement.style.top;

		desktopResize();
	}
	function restoreWindow( aElement ){
		aElement.tBodies.item("tApp",0).style.display = "";
		aElement.tHead.style.display = "";
		aElement.tFoot.style.display = "none";
		aElement.style.left = aElement.tFoot.style.left;
		aElement.style.top = aElement.tFoot.style.top;
		aElement.tFoot.style.left = "0px";
		aElement.tFoot.style.top = "0px";

		desktopResize();
	}
	
	function maximizeWindow( aElement ){
		aElement.tBodies.item("tApp",0).style.display = "";
		aElement.tHead.style.display = "";
		aElement.tFoot.style.display = "none";
		
		wnd = getWindow( aElement );
		if( aElement.tFoot.rows[1].style.top == "0px" ){
			aElement.tFoot.rows[1].style.left = aElement.style.left;
			aElement.tFoot.rows[1].style.top = aElement.style.top;
			aElement.tFoot.rows[1].style.width = wnd.width;
			aElement.tFoot.rows[1].style.height = wnd.height;
			
			aElement.style.top= 88 + aElement.parentNode.offsetTop;
			aElement.style.left= aElement.parentNode.offsetLeft; // + aElement.parentNode.clientLeft;
			wnd.width = aElement.parentNode.clientWidth;
			wnd.height = aElement.parentNode.offsetHeight - aElement.rows[0].offsetHeight - 20;
		}else{
			// nastavime povodne suradnice
			aElement.style.left = aElement.tFoot.rows[1].style.left;
			aElement.style.top = aElement.tFoot.rows[1].style.top;
			wnd.width = aElement.tFoot.rows[1].style.width ;
			wnd.height = aElement.tFoot.rows[1].style.height;
			aElement.tFoot.rows[1].style.left = "0px";
			aElement.tFoot.rows[1].style.top = "0px";
		}
//		window.alert( aElement.parentNode.clientLeft );	//wnd.contentWindow.document.body.clientWidth) ;//aElement.parentNode.offsetWidth );
		if( wnd.src.substr( 0, document.URL.length )  == document.URL || wnd.src.substr( 0, 7 ) != "http://" ){
			setTitle( aElement, wnd.contentWindow.document.title );
		}else{
			setTitle( aElement, "Window" );
		}
	}
	function getWindowMETAValue( aWindow, aValue, srci ){
		if( srci.substr( 0, document.URL.length )  == document.URL || srci.substr( 0, 7 ) != "http://" ){
			var metaVal = aWindow.document.getElementsByTagName("META");
			for ( i=0; i< metaVal.length; i++){
				if( metaVal.item(i).name == aValue ){
					return metaVal.item(i).content;
				}
			}
		}else{
			return "Window";
		}
	}
	function onLoadWindowEvent( aElement ){
		if ( !mb_systemLoaded ) return;
		var wnd = getWindow( aElement );
//	alert( wnd.src.substr( 0, 7 ) + "  " + document.URL + " " + document.domain.length );
		if( wnd.src.substr( 0, document.URL.length )  == document.URL || wnd.src.substr( 0, 7 ) != "http://" ){
			setTitle( aElement, wnd.contentWindow.document.title );
		}else{
			setTitle( aElement, "Window" );
		}
		setStatus( aElement, "Ready" );
		document.getElementById("ePageSrc").innerHTML = getWindowMETAValue( wnd.contentWindow, "source", wnd.src );
	}
	function closeWindow( aElement ){
		aElement.parentNode.removeChild( aElement );
		document.getElementById("ePageSrc").innerHTML = "";
	}
	function desktopResize(){
		var apps = document.getElementById("eDesktop").childNodes;
		var freeX = 100;
		var freeY = document.body.clientHeight-35;
		var maxHeight = 0;
		for ( i=0 ;i<apps.length; i++){
			var aElement = apps.item(i);
			if( aElement.nodeName == "TABLE" ){
				if( aElement.tBodies.item("tApp",0).style.display == "none" ){
					if( freeX + aElement.clientWidth > document.body.clientWidth ){
						freeX = 100;
						freeY = freeY - maxHeight;
					}
					moveElement( aElement, freeX, freeY - aElement.clientHeight );
//					alert(aElement.tFoot.style.position);
					if ( maxHeight < aElement.clientHeight ) maxHeight = aElement.clientHeight;
					freeX = freeX + aElement.clientWidth;
//					window.status = aElement.clientHeight;	// aElement.style.height;	// window.height;	// freeY;
				}
			}
		}
	}
	
	function openTest( bgColor, txtColor ){
		var p1 = insertElement( "blank.php?lang=E&bgColor=" + txtColor + "&txtColor=" + bgColor, 'centered', 'testHandle', 'windowTemplate' )
		moveElement(p1, p1.offsetLeft - (p1.clientWidth/2) + 10,p1.offsetTop - (p1.clientHeight/4));

		var p2 = insertElement( "blank.php?lang=SK&bgColor=" + bgColor + "&txtColor=" + txtColor, 'centered', 'testHandle', 'windowTemplate' )
		moveElement(p2, p2.offsetLeft + (p2.clientWidth/2) - 10,p2.offsetTop + (p2.clientHeight/4));
	}

	function reOpenAligned( elementSrc, aAlign, aVAlign, aWidth, aHeight, aIcons, aHandle, templateName ){
		var p1 = getElement( aHandle );
		if ( p1 == null ) 
		{
			openAligned( elementSrc, aAlign, aVAlign, aWidth, aHeight, aIcons, aHandle, templateName );
		}else{
			var wnd= getWindow( p1 );
			wnd.src= elementSrc;
			aligneElement( p1, aAlign, aVAlign, aWidth, aHeight, aIcons );
			p1.style.zIndex = getZOrder();		// na mozile to nefunguje
		}
	}

	function openAligned( elementSrc, aAlign, aVAlign, aWidth, aHeight, aIcons, aHandle, templateName ){
		var p1 = insertElement( elementSrc, '', aHandle, templateName );
		aligneElement( p1, aAlign, aVAlign, aWidth, aHeight, aIcons );
	}
	
	function aligneElement( p1, aAlign, aVAlign, aWidth, aHeight, aIcons ){
		var wnd = getWindow( p1 );
		wnd.width = aWidth;
		wnd.height = aHeight;

		var imgs = p1.rows[0].getElementsByTagName('IMG');
		
		imgs[0].style.display = ( aIcons.indexOf('M') == -1 ) ? 'none' : '';	// MAXIMIZE ICON
		imgs[1].style.display = ( aIcons.indexOf('N') == -1 ) ? 'none' : '';	// MINIMIZE ICON
		imgs[2].style.display = ( aIcons.indexOf('X') == -1 ) ? 'none' : '';	// CLOSE ICON

		switch(aAlign){
			case "center":{
				moveElement(p1, (p1.parentNode.clientWidth - p1.clientWidth)/2, p1.style.top );
				break;
			}
			case "left":{
				moveElement(p1, 0, p1.style.top );
				break;
			}
			case "right":{
				moveElement(p1, p1.parentNode.clientWidth - p1.clientWidth, p1.style.top );
				break;
			}
		}
		
		switch(aVAlign){
			case "center":{
				moveElement(p1, p1.style.left,88 + (p1.parentNode.clientHeight - p1.clientHeight)/2);
				break;
			}
			case "top":{
//				alert( p1.parentNode.offsetTop )
				moveElement(p1, p1.style.left, 88 + p1.parentNode.offsetTop );
				break;
			}
			case "bottom":{
				moveElement(p1, p1.style.left, p1.parentNode.clientHeight - p1.clientHeight - 70 );
				break;
			}
		}
	}
	
	function openSized( elementSrc, aWidth, aHeight, aHandle){
		var p1 = insertElement( elementSrc, '', aHandle, 'windowTemplate' );
		var wnd = getWindow( p1 );

		if ( aWidth == 0 ) aWidth = p1.parentNode.offsetWidth;
		if ( aHeight == 0 ) aHeight = p1.parentNode.offsetHeight - p1.rows[0].offsetHeight - 14;
		wnd.width = aWidth;
		wnd.height = aHeight;
		moveElement(p1, (document.body.clientWidth - p1.clientWidth)/2,84 + p1.parentNode.offsetTop);
	}

	function openSized2( elementSrc, aWidth, aHeight, aHandle, templateName){
		var p1 = insertElement( elementSrc, '', aHandle, templateName );
		var wnd = getWindow( p1 );

		if ( aWidth == 0 ) aWidth = p1.parentNode.offsetWidth;
		if ( aHeight == 0 ) aHeight = p1.parentNode.offsetHeight - p1.rows[0].offsetHeight - 14;
		wnd.width = aWidth;
		wnd.height = aHeight;
		moveElement(p1, (document.body.clientWidth - p1.clientWidth)/2,84 + p1.parentNode.offsetTop);
	}

	function reOpenSized( elementSrc, aWidth, aHeight, aHandle ){
		var p1 = getElement( aHandle );
		if ( p1 == null ) 
		{
			openSized( elementSrc, aWidth, aHeight, aHandle );
		}else{
			// nastavime suradnice
			p1.tBodies.item("tApp",0).style.display = "";
			p1.tHead.style.display = "";
			p1.tFoot.style.display = "none";
			var wnd = getWindow( p1 );
			wnd.src= elementSrc;
			if ( aWidth == 0 ) aWidth = p1.parentNode.offsetWidth;
			if ( aHeight == 0 ) aHeight = p1.parentNode.offsetHeight - p1.rows[0].offsetHeight - 14;
			wnd.width = aWidth;
			wnd.height = aHeight;
			moveElement(p1, (document.body.clientWidth - p1.clientWidth)/2,84 + p1.parentNode.offsetTop);
			p1.style.zIndex = getZOrder();		// na mozile to nefunguje
		}
	}

	function reOpenSized2( elementSrc, aWidth, aHeight, aHandle, templateName ){
		var p1 = getElement( aHandle );
		if ( p1 == null ) 
		{
			openSized2( elementSrc, aWidth, aHeight, aHandle, templateName );
		}else{
			// nastavime suradnice
			p1.tBodies.item("tApp",0).style.display = "";
			p1.tHead.style.display = "";
			p1.tFoot.style.display = "none";
			var wnd = getWindow( p1 );
			wnd.src= elementSrc;
			if ( aWidth == 0 ) aWidth = p1.parentNode.offsetWidth;
			if ( aHeight == 0 ) aHeight = p1.parentNode.offsetHeight - p1.rows[0].offsetHeight - 14;
			wnd.width = aWidth;
			wnd.height = aHeight;
			moveElement(p1, (document.body.clientWidth - p1.clientWidth)/2,84 + p1.parentNode.offsetTop);
			p1.style.zIndex = getZOrder();		// na mozile to nefunguje
		}
	}

	function openMaximized( elementSrc, aHandle, templateName){
		var p1 = insertElement( elementSrc, 'maximized', aHandle, templateName );
		var imgs = p1.rows[0].getElementsByTagName('IMG');
		imgs[1].style.display = 'none';	// hide MAXIMIZE ICON
	}
	
	function reOpenMaximized( elementSrc, aHandle, templateName ){
		var p1 = getElement( aHandle );
		if ( p1 == null ) 
		{
			openMaximized( elementSrc, aHandle, templateName );
		}else{
			setStatus( p1, "Loading..." );
			// nastavime suradnice
			var wnd= getWindow( p1 );
			wnd.src= elementSrc;

			p1.tFoot.rows[1].style.top = "0px"
			maximizeWindow(p1);
			p1.style.zIndex = getZOrder();		// na mozile to nefunguje
		}
	}

	function startEngine(){
		var elem = document.getElementById("hLoading");
		elem.style.display="none";
		elem = document.getElementById("mainSystem");
		elem.style.display="";
		
		mb_systemLoaded = true;
	}

	function showSubMenu( menu, tdmenu ){
		var mnu = document.getElementById( menu );
//		window.alert(tdmenu.offsetParent.offsetTop);
		mnu.style.display="";
		mnu.style.left = tdmenu.offsetLeft + 10;
		mnu.style.top =  document.body.clientHeight - tdmenu.offsetHeight - tdmenu.offsetTop - tdmenu.offsetHeight + 10;
		mnu.style.zIndex = getZOrder();
//		alert( tdmenu.offsetParent.offsetParent.offsetHeight );
//		mnu.style.top = tdmenu.offsetParent.offsetTop + tdmenu.offsetHeight;
	}
	function showSubMenuTop( menu, tdmenu ){
		var mnu = document.getElementById( menu );
		if ( mnu != null ){
//		window.alert(tdmenu.offsetParent.offsetTop);
			mnu.style.display="";
			mnu.style.left = tdmenu.offsetLeft;	// - (mnu.offsetWidth - tdmenu.offsetWidth)*0.5;
			mnu.style.top = 88;	//tdmenu.offsetParent.offsetTop + tdmenu.offsetHeight + 35;
		}
	}
	function hideSubMenu( menu ){
		var mnu = document.getElementById( menu );
		if ( mnu != null ){
			mnu.style.display="none";
		}
//		alert( mnu + "..."+menu + "...." + window.Event.srcElement.uniqueID);
//		window.status = "hiding menu:" + menu;
	}
