var objImg = new Image();
var pathImmagini; 
var newDoc; // Finestra popup

function preloadZoomImage(imagePath){ // Funzione principale
		
		pathImmagini = imagePath;
		objImg.src = imagePath;
		
					var tmpW, tmpH;
					tmpW = (screen.width - 300) / 2;
					tmpH = (screen.height - 200) / 2;
		
				// Disegna pagina		
				newDoc = window.open('', 'virtualPage','scrollbars=no,top=' + tmpH +',left=' + tmpW +',width=300,height=200');
				
				// Genera codice pagina
				newDoc.document.write("<html>\n");
				newDoc.document.write("<head>\n");
				newDoc.document.write("<title>");
				newDoc.document.write("ZOOM");
				newDoc.document.write("</title>\n");
				newDoc.document.write("</head>\n");
				newDoc.document.write("<body style=\"margin:0px;\">\n");
				
				// Contenuto della pagina
				newDoc.document.write("<img id=\"mainImage\" src=\"" + imagePath + "\" style=\"display:none;\">");
				newDoc.document.write("<div id=\"msgLoading\" style=\"text-align:center; height:200px; font-weight:bold;\">Sto caricando l'immagine ingrandita.<br>Attendere prego...</div>");
				
				// Crea zoccolo con link "Chiudi"
				newDoc.document.write("<div id=\"zoccolo\" style=\"background-color:#CCC; padding:3px; text-align:center; display:none;\">");
				newDoc.document.write("<a href=\"#\" onClick=\"window.close(this);\">Chiudi</div>");
				newDoc.document.write("</body>\n");
				
				newDoc.document.write("</html>\n");
											
		
		setTimeout("timer()",100);		
					
	}


function timer(){ 
 	
	if (objImg.complete){  
		
		// Se l'immagine è caricata, ridimensiona la popup
		popImageSizer(); 
	
	} else { 
	
		// Prendi tempo finché l'immagine non è completamente caricata
		setTimeout("timer()",100); 
		
		} 
	} 


function popImageSizer(){
																	
				var infoW, infoH;
				
				infoW = objImg.width;
				infoH = objImg.height;
								
				infoH += 60; // Aggiungi spessore zoccolo
				
				// Sistema dimensioni a seconda del browser
				
						var ie4 = document.all;
						var ns4 = document.layers;
						var ns6 = document.getElementById && !document.all; 
											
						if(ns6){
							infoW += 6;
							infoH += 4;
						}
				
				
				newDoc.resizeTo(infoW, infoH);
				
												
				infoW = (screen.width - (infoW * 2)) / 2;
				infoH = (screen.height - (infoH * 2)) / 2;
								
								
				
				newDoc.moveBy(infoW, infoH);
								
				newDoc.document.getElementById("mainImage").style.display = ""; //visibile
				newDoc.document.getElementById("zoccolo").style.display = ""; //visibile
				newDoc.document.getElementById("msgLoading").style.display = "none"; //nascosto 
											
	   }
	

