	var readSizeFromCookie = false;	// Determines if size and position of windows should be set/retreved by use of cookie
	var windowMinSize = [80,30];	// Mininum width and height of windows.
	var moveCounter = -1;	
	var startEventPos = new Array();
	var startPosWindow = new Array();
	var startWindowSize = new Array();
	var initResizeCounter = -1;	
	var activeWindow = false;
	var activeWindowContent = false;	
	var windowSizeArray = new Array();
	var windowPositionArray = new Array();
	var currentZIndex = 10000;
	var windowStateArray = new Array();	// Minimized or maximized
	var activeWindowIframe = false;
	var divCounter = 0;
	var zIndexSet = false;
	var MSIEWIN = (navigator.userAgent.indexOf('MSIE')>=0 && navigator.userAgent.indexOf('Win')>=0 && navigator.userAgent.toLowerCase().indexOf('opera')<0)?true:false;
	var opera = navigator.userAgent.toLowerCase().indexOf('opera')>=0?true:false;
	var ajaxObjects = new Array();
	function cancelEvent()
	{
		return (moveCounter==-1 && initResizeCounter==-1)?true:false;
	}
	
	
	function switchElement(e,inputElement)
	{
		if(!inputElement)inputElement = this;
		var numericId = inputElement.id.replace(/[^0-9]/g,'');
		var state = '0';
		if(windowStateArray[numericId])state = '1';
		currentZIndex = currentZIndex/1 + 1;
		activeWindow = document.getElementById('id' + numericId);	
		activeWindow.style.zIndex = currentZIndex;
		activeWindowContent = document.getElementById('windowContent' + numericId);
	}
	function hideWindow()
	{
		switchElement(false,document.getElementById('id' + this.id.replace(/[^\d]/g,'')));	
		activeWindow.style.display='none';
	}
	
	function initWindows(e,divObj)
	{
		var divs = document.getElementsByTagName('DIV');
		if(divObj){
			var tmpDivs = divObj.getElementsByTagName('DIV');
			var divs = new Array();
			divs[divs.length] = divObj;
			for(var no=0;no<tmpDivs.length;no++){
				divs[divs.length] = tmpDivs[no];
			}
		}
		for(var no=0;no<divs.length;no++){
			if(divs[no].className=='window'){	
				if(MSIEWIN){
					var iframe = document.createElement('IFRAME');
					iframe.style.border='0px';
					iframe.frameborder=0;
					iframe.style.position = 'absolute';
					iframe.style.backgroundColor = '#FFFFFF';
					iframe.style.top = '0px';
					iframe.style.left = '0px';
					iframe.style.zIndex = 100;
					var subDiv = divs[no].getElementsByTagName('DIV')[0];
					divs[no].insertBefore(iframe,subDiv);
				}					
				if(divObj){
					divs[no].style.zIndex = currentZIndex;
					currentZIndex = currentZIndex /1 + 1;
				}
				divCounter = divCounter + 1;	
				if(divCounter==1)activeWindow = divs[no];		
				divs[no].id = 'id' + divCounter;	
				divs[no].onmousedown = switchElement;
				if(readSizeFromCookie)var cookiePos = Get_Cookie(divs[no].id + '_attr') + '';	else cookiePos = '';
				if(divObj)cookiePos='';
				var cookieValues = new Array();
				
				
				if(windowPositionArray[divCounter]){
					divs[no].style.left = windowPositionArray[divCounter][0] + 'px';	
					divs[no].style.top = windowPositionArray[divCounter][1] + 'px';	
				}
				var subImages = divs[no].getElementsByTagName('IMG');
				for(var no2=0;no2<subImages.length;no2++){
					
					if(subImages[no2].className=='closeButton'){
						subImages[no2].id = 'closeImage' + divCounter;
						subImages[no2].onclick = hideWindow;	
					}	
					
				}			
			}	
			if(divs[no].className=='windowMiddle' || divs[no].className=='window_bottom'){
				divs[no].style.zIndex = 1000;
			}
			if(divs[no].className=='window_top'){
				
				divs[no].id = 'top_bar'+divCounter;
				divs[no].style.zIndex = 1000;
			}
			if(divs[no].className=='windowContent'){
				divs[no].id = 'windowContent'+divCounter;
				divs[no].style.zIndex = 1000;
				if(cookieValues && cookieValues.length>3){
					if(!windowSizeArray[divCounter])windowSizeArray[divCounter] = new Array();
					windowSizeArray[divCounter][0] = cookieValues[2];
					windowSizeArray[divCounter][1] = cookieValues[3];
				}	
				if(cookieValues && cookieValues.length==5){
					activeWindowContent = document.getElementById('windowContent' + divCounter);	
				}		
				if(windowSizeArray[divCounter]){
					divs[no].style.height = windowSizeArray[divCounter][1] + 'px';
					divs[no].parentNode.parentNode.style.width = windowSizeArray[divCounter][0] + 'px';
					if(MSIEWIN){
						iframe.style.width = (windowSizeArray[divCounter][0]) + 'px';
						iframe.style.height = (windowSizeArray[divCounter][1]+20) + 'px';
					}
				}
			}
		}	
		if(!divObj){
			document.documentElement.onmouseup = stopMove;	
			document.documentElement.onmousemove = moveWindow;
			document.documentElement.ondragstart = cancelEvent;
			document.documentElement.onselectstart = cancelEvent;
		}
		return divCounter;
	}
	function createNewWindow(width,height,left,top)
	{
		var div = document.createElement('DIV');
		div.className='window';
		document.body.appendChild(div);
		var topDiv = document.createElement('DIV');
		topDiv.className='window_top';
		div.appendChild(topDiv);
		var img = document.createElement('IMG');
		img.src = '../images/top_left.gif';
		img.align='left';
		topDiv.appendChild(img);
		var img = document.createElement('IMG');
		img.src = '../images/top_center.gif';
		img.className='topCenterImage';
		topDiv.appendChild(img);
		var buttonDiv = document.createElement('DIV');
		buttonDiv.className='top_buttons';
		topDiv.appendChild(buttonDiv);
		var img = document.createElement('IMG');
		img.src = '../images/top_left.gif';
		img.className='minimizeButton';
		buttonDiv.appendChild(img);	
		var img = document.createElement('IMG');
		img.src = '../images/close.gif';
		img.className='closeButton';
		buttonDiv.appendChild(img);	
		var img = document.createElement('IMG');
		img.src = '../images/top_right.gif';
		buttonDiv.appendChild(img);		
		var middleDiv = document.createElement('DIV');
		middleDiv.className='windowMiddle';
		div.appendChild(middleDiv);
		var contentDiv = document.createElement('DIV');
		contentDiv.className='windowContent';
		middleDiv.appendChild(contentDiv);
		var bottomDiv = document.createElement('DIV');
		bottomDiv.className='window_bottom';
		div.appendChild(bottomDiv);
		var img = document.createElement('IMG');
		img.src = '../images/bottom_right.gif';
		img.className='resizeImage';
		bottomDiv.appendChild(img);		
		windowSizeArray[windowSizeArray.length] = [width,height];
		windowPositionArray[windowPositionArray.length] = [left,top];
		div.style.width =  width + 'px';
		contentDiv.style.height = height  + 'px';		
		div.style.left =  left + 'px';
		div.style.top = top  + 'px';	
		return initWindows(false,div);
	}
	function showAjaxContent(ajaxIndex,windowId)
	{
		document.getElementById('windowContent' + windowId).innerHTML = ajaxObjects[ajaxIndex].response;
    		
	}
	function addContentFromUrl(url,windowId)
	{
		var ajaxIndex = ajaxObjects.length;
		ajaxObjects[ajaxIndex] = new sack();
		ajaxObjects[ajaxIndex].requestFile = url;
		
		ajaxObjects[ajaxIndex].onCompletion = function(){ showAjaxContent(ajaxIndex,windowId); };	
		ajaxObjects[ajaxIndex].runAJAX();	
	}

	function customFunctionCreateWindow(urlToExternalFile,width,height,left,top)
	{			
		var divId = createNewWindow(width,height,left,top);
		document.getElementById('windowContent' + divId).innerHTML = 'caricamento in corso';
		if(urlToExternalFile)addContentFromUrl(urlToExternalFile,divId);
	}
	window.onload = initWindows;
