/********************************************************************************************************** 
 ***************** JS - Fabio Lucattini ******************************************************************
 **********************************************************************************************************/
 
 
  /************ Definizione var http **************************************/
	function createRequestObject() {  
		var ro;  
		var browser = navigator.appName;  
		if(browser == "Microsoft Internet Explorer"){  
		   ro = new ActiveXObject("Microsoft.XMLHTTP");  
		}else{  
		   ro = new XMLHttpRequest();  
		}  
		return ro;  
	}  
	
	var http = createRequestObject();  
	var httpFoto = createRequestObject();  
	/***********************************************************************/
 
 var x = 0;
 var y = 0;
 var click = 0;
 

 
 var ns4 = (document.layers)? true:false
 var ie4 = (document.all)? true:false
 var fotoSelect;
 
 /*******************************************************************************************************************
 		stato del caricamento
 *******************************************************************************************************************/
 var caricamento = 0;
 function cCaricamento(){

	if(caricamento >= 0 && caricamento < 3){
		
		if(fotoRisp == 1){
		
			allargaFoto(fotoSelect);
			
		}else if(fotoRisp == 2){
		
			allargaFoto2(globalId, globalCat);
		
		}else if(fotoRisp == 3){
		
			allargaFoto3(globalId, globalCat);
		
		}	
		
		caricamento ++;
		
	} else if(caricamento >= 3){
		
		//chiude la frase di loading
		document.getElementsByTagName("body")[0].removeChild(document.getElementById("conteinerLoad"));
		document.getElementsByTagName("body")[0].removeChild(document.getElementById("sfondoDiv"));
	
	}
	
 }
 /*******************************************************************************************************************/
 
 function creaDivOfferte(){	
	
	if(!document.getElementById("offerte")){
		
		var offerte = document.createElement('div');
		document.getElementsByTagName("body")[0].appendChild(offerte);			
		offerte.setAttribute('id',"offerte");	
		
		var sfondoOfferte = document.createElement('div');
		offerte.appendChild(sfondoOfferte);			
		sfondoOfferte.setAttribute('id',"sfondoOfferte");
		
		var ombraOfferte = document.createElement('div');
		offerte.appendChild(ombraOfferte);			
		ombraOfferte.setAttribute('id',"ombraOfferte");	
		
		var elencoOfferte = document.createElement('div');
		offerte.appendChild(elencoOfferte);			
		elencoOfferte.setAttribute('id',"elencoOfferte");	
		elencoOfferte.innerHTML ="";
		
		var elencoOfferteTitolo = document.createElement('div');
		offerte.appendChild(elencoOfferteTitolo);			
		elencoOfferteTitolo.setAttribute('id',"elencoOfferteTitolo");	
		elencoOfferteTitolo.innerHTML ="<span class='offerta14'>Offerte:</span>";
		
		var offerteClose = document.createElement('div');
		sfondoOfferte.appendChild(offerteClose);			
		offerteClose.setAttribute('id',"offerteClose");	
		
		ombraOfferte.style.marginLeft = "2px" ;
		ombraOfferte.style.marginTop = "2px" ;	
	
		if(!ie4){
			offerteClose.setAttribute("onMouseDown", "setta();");
			offerteClose.setAttribute("onMouseUp", "setta();");
		}else{
			document.getElementById("offerteClose").onmousedown = setta;
			document.getElementById("offerteClose").onmouseup = setta;	
		}
		
		offertaText();
		
	}
	  
  }

/**************************************************************************************************************************
	GESTISCE IL MOVIMENTO DEI DIV CON CON IL MOUSE		
***************************************************************************************************************************/
 	document.onmousemove = mouseMove;
	
	function mouseMove(ev){
		//ev           = ev || window.event;
		var mousePos = mouseCoords(ev);
	}
	function mouseCoords(ev){
		if(!ie4){
			x = ev.pageX;
			y = ev.pageY;
		}else{
			x = event.x;
			y = event.y;
		}
		
		if(click == 1){
			document.getElementById("offerte").style.marginLeft =  (x-173)+"px";
			document.getElementById("offerte").style.marginTop = (y-9)+"px";
		}
		if(click == 2){
			document.getElementById("contenitoreMap").style.marginLeft = (x-173)+"px";
			document.getElementById("contenitoreMap").style.marginTop = (y-9)+"px";
		}
		return 0;
	}
	
 	function setta(){
		click = (click==1)? 0 : 1;
	}
	function setta2(){
		click = (click==2)? 0 : 2;
	}
	function disattivaSetta2(){
		
		click = 0;
	}
/***************************************************************************************************************************/
	
/**************************************************************************************************************************
	GESTISCE LE OFFERTE DA DATABASE		
***************************************************************************************************************************/	
function offertaText() { 
   var action = "script/offerte.php";
   http.open('get', action);  
   http.onreadystatechange = offerteRisposta;  
   http.send(null);  
}  
 
function offerteRisposta() {  

   if(http.readyState < 4){	   
   		document.getElementById("elencoOfferte").innerHTML = "<center><span class='testoOfferta11'>... Caricamento ...</span></center>";
   }else if(http.readyState == 4){
	   if(http.status == 200){	
	   			var risp = http.responseText ;
				var i = 0;
				var scrivi = "";
				var ele = "";
				notizie = risp.split(" <&&&> ");
		
				if(notizie.length != 0){ 
					scrivi = "<table align='right'  width='100%'>";
					while( i < notizie.length ){ 
							// load offerte
							ele = notizie[i].split("&");
							
							/** elementi letti */
							id = ele[0].split("="); id = id[1];
							titolo = ele[1].split("="); titolo = titolo[1]; 
							
							scrivi += "<tr ><td align='right'><a id=\"offerta"+id+"\" class='testoOfferta11' href='#' onClick='setPage(\"dettaglioOfferta.php?d="+id+"\")'>"+ titolo +"</a><br></td></tr>";
							scrivi += "<tr style='line-height:1px;'><td align='right' style='background-color:#FFFFFF;' ></td></tr>";
							i++;
					}
					scrivi += "</table>";
					document.getElementById("elencoOfferte").innerHTML = scrivi;
					
				}else{
					
	   			}
	   }
   }
}
/***************************************************************************************************************************/

	
	var fotoRisp ;
	var globalId, globalCat;
	
	/*
		div contenitori immagine
	*/
	var conteinerLoad;  //contenitore GLOBALE
	var foto;			//contenitore foto
	var fraseFoto;		//frase Load
	var sfondoDiv; 		//sfondo div
	
	var wImage; // contiene la larghezza dell'immagine
	var hImage; // contiene la larghezza dell'immagine
	
	function apriImg( larghezzaIniziale, larghezzaFinale, altezzaIniziale, altezzaFinale ){
		
		if(larghezzaIniziale < larghezzaFinale){
			
			var rapporto =  larghezzaFinale/altezzaFinale;
			
			var incremento = 50;
			
			larghezzaIniziale += (larghezzaIniziale < larghezzaFinale)?  (incremento* rapporto) : 0;
			document.getElementById("fotoSelect").width = larghezzaIniziale;
			altezzaIniziale +=(altezzaIniziale < altezzaFinale)? incremento :0;
			document.getElementById("fotoSelect").height = altezzaIniziale;

			var topFoto = document.getElementById("fotoSelect").style.marginTop;
			topFoto =  (parseInt(topFoto.substr(0, topFoto.length-2)) - (incremento/2) )+"px";
			document.getElementById("fotoSelect").style.marginTop = (parseInt(topFoto.substr(0, topFoto.length-2)) > 0)? topFoto : document.getElementById("fotoSelect").style.marginTop;
		
			setTimeout("apriImg("+ larghezzaIniziale +","+ larghezzaFinale +","+ altezzaIniziale +","+ altezzaFinale +");", (1));
			
		}else{	
			//posizione X chiusura foto
			document.getElementById("chiudiFoto").style.marginLeft=parseInt(((screenW-document.getElementById("fotoSelect").width)/2)+document.getElementById("fotoSelect").width-22)+"px";
			document.getElementById("chiudiFoto").style.marginTop=-(document.getElementById("fotoSelect").height+3)+"px";
			document.getElementById("chiudiFoto").style.visibility = "visible";
			return true;
		}
	
	}
	
	function chiudiFrasi(){
			
		//chiude la frase di loading
		conteinerLoad.removeChild(document.getElementById("fraseFoto"));
		
		if (navigator.appName != "Microsoft Internet Explorer"){
			
			
			
			
			if( document.getElementById("fotoGrande") ){
				
				//larghezza image iniziale
				wImage = document.getElementById("fotoSelect").width;
				document.getElementById("fotoSelect").width = "0px";
				hImage = document.getElementById("fotoGrande").height;
				hImage = hImage / document.getElementById("fotoGrande").width;
				hImage = hImage * wImage;
			
			}else{
				
				//larghezza image iniziale
				wImage = document.getElementById("fotoSelect").width;
				
				hImage = document.getElementById("imgOfferta").height;
				hImage = hImage / document.getElementById("imgOfferta").width;
				hImage = hImage * wImage;
				
			}
			
			document.getElementById("fotoSelect").height = "0px";
			
			document.getElementById("fotoSelect").style.visibility = "visible";
			document.getElementById("fotoSelect").style.marginTop = "350px";
		
			//resizo l'immagine
			apriImg( 0, wImage, 0, hImage );
			
		}else{
			
			//posizione X chiusura foto
			var XChiudi = (screenH > 768) ? 22 : 22;
			
			document.getElementById("chiudiFoto").style.marginLeft=parseInt(((screenW-document.getElementById("fotoSelect").width)/2)+document.getElementById("fotoSelect").width-20)+"px";			
			document.getElementById("chiudiFoto").style.marginTop=-(document.getElementById("fotoSelect").height+3)+"px";
			document.getElementById("chiudiFoto").style.visibility = "visible";
			document.getElementById("fotoSelect").style.visibility = "visible";
			
		}
		
		
	}
	
	
	function chiudiFoto(){		
		
		document.getElementsByTagName("body")[0].removeChild(document.getElementById("conteinerLoad"));
		conteinerLoad = "";
		
	}	
	
	
	function allargaFoto( id ){
		
		fotoRisp = 1;
	  	fotoSelect = id;
		fotoSelect = fotoSelect.split('+');
		fotoSelect = fotoSelect[1];
		if(document.getElementById("conteinerLoad")){
			document.getElementsByTagName("body")[0].removeChild(document.getElementById("conteinerLoad"));
			conteinerLoad = "";
		}
		conteinerLoad = document.createElement('div');
		document.getElementsByTagName("body")[0].appendChild(conteinerLoad);			
		conteinerLoad.setAttribute('id',"conteinerLoad");	
		
		foto = document.createElement('div');
		conteinerLoad.appendChild(foto);			
		foto.setAttribute('id',"foto");
		
		sfondoDiv = document.createElement('div');
		conteinerLoad.appendChild(sfondoDiv);			
		sfondoDiv.setAttribute('id',"sfondoDiv");
		
		fraseFoto = document.createElement('div');
		conteinerLoad.appendChild(fraseFoto);			
		fraseFoto.setAttribute('id',"fraseFoto");
	   	
		
		/* setto l'altezza di sfondo */
		if(screenH > 768){
			sfondoDiv.style.height=screenH+"px";
			
		}else{
			sfondoDiv.style.height=screenH+((768-screenH)/2)+"px";
		}
		sfondoDiv.style.width=screenW+"px";
		
	   action = "script/visualizzaImmagini.php?id="+fotoSelect+"&height=600";
	   httpFoto.open('get', action);  
	   httpFoto.onreadystatechange = allargaFotoRisp;  
	   httpFoto.send(null);  
	   		
	}  
	 
	 
	 
	function allargaFotoRisp() { 
		
	   if(httpFoto.readyState < 4){	   
	   
	   		//preload
			//fraseFoto.style.height = h;
	   		fraseFoto.innerHTML = "<center>... Caricamento ...<br><img src='image/loader.gif'></center>";
			
	   }else if(http.readyState == 4){
	
		    if(httpFoto.status == 200)
		    {			 	
			
				if(fotoRisp == 1){
					
					src = "script/visualizzaImmagini.php?id="+fotoSelect+"&height=600";
					src.toString();
					foto.innerHTML = "<div id='fotoSelectDiv'><center><img id='fotoSelect'  onload='chiudiFrasi()'  src='"+ src +"'></center><div id='chiudiFoto' onclick='chiudiFoto()'><img src='image/xClose.jpg' height='20'></div></div>";	
					
				}else if(fotoRisp == 2){
				
					src = "script/visualizzaFaidate.php?id="+ globalId +"&cat="+globalCat+"&width=900";
					src.toString();
					foto.innerHTML = '<div id="fotoSelectDiv"><center><img id="fotoSelect" width="900" onload="chiudiFrasi()"   src="'+src+'"></center><div id="chiudiFoto" onclick="chiudiFoto()"><img src="image/xClose.jpg" height="20"></div></div>';	
				
				}else if(fotoRisp == 3){
		
					src = "script/visualizzaRifinitureInterni.php?id="+ globalId +"&cat="+globalCat+"&width=700";
					src.toString();
					foto.innerHTML = '<div id="fotoSelectDiv"><center><img id="fotoSelect" onload="chiudiFrasi()"   src="'+src+'"></center><div id="chiudiFoto" onclick="chiudiFoto()"><img src="image/xClose.jpg" height="20"></div></div>';	
				
				}
			}
			else
			{
			}
		   }
	   }
	
