	var map,xmlhttp,geocoder;
	var idx=0;
	var imno=0;
	var queue = new Array();
	var lastlat=0.0,lastlng=0.0;
	var bounds=null;
	var _isdraggable=false;
	var reqstring;
  var actualpage=0;
  var loadinglabel=null;
	
	function createLoadingLabel() {
		loadinglabel = new ELabel(map.getCenter(), s_loading+' <img  align="absmiddle" src="img/loading.gif" hspace="4" />',"loadinglabel",new GSize(-105,15),70);
		map.addOverlay(loadinglabel);
	}
	
	function gnext() {
		var ap = document.getElementById("asip"+(actualpage+1));
		if (ap) {
			document.getElementById("asip"+actualpage).style.display = "none";
			actualpage++;
			document.getElementById("asip"+actualpage).style.display = "block";
		}
	}
	
	function gprev() {
		if (actualpage!=0) {
  	  document.getElementById("asip"+actualpage).style.display = "none";
  		actualpage--;
  	  document.getElementById("asip"+actualpage).style.display = "block";
		}
	}

  function sendDataRequest()
	{
		xmlhttp.onreadystatechange=receiveData; 
	  xmlhttp.open("GET",reqstring+'&idx='+idx,true);
		xmlhttp.send(null);
	}

  function getAppHtml( tipotext, fields ) {
		var imgtd,mhtml;
		if (tipotext=='') 
		  tipotext = tipoimmo[fields[1]];
		mhtml = tipotext+" - "+s_code+" <a href=\"vc_det.php?id="+fields[0]+"\" target=\"_new\"><b>"+ fields[3] 
		+ "</b></a><br><img src=\"img/bed.png\" align=\"absmiddle\" /> "+s_tpostiletto+" <b>"+fields[2]+'</b>&nbsp;&nbsp;'+parkingtext[fields[4]]+"<br /><p>" +fields[10]+"</p>";
		if (fields[5]!="") {
			imgtd =  "<a href=\"vc_det.php?id="+fields[0]+"\" target=\"_new\"><img width=\""+fields[5]+"\" height=\""+fields[6]+"\" border=\"0\" src=\""+fields[7]+"__mainfoto__.jpg\"></a>";
		} else {
			imgtd =  "<a href=\"vc_det.php?id="+fields[0]+"\" target=\"_new\"><img width=\"100\" height=\"75\" border=\"0\" src=\"img/nondisp.png\"></a>";
		}
		return "<table border=\"0\"><tr><td valign=\"middle\"> "+imgtd+"</td><td class=\"maptip\" width=\"300px\">"+mhtml+"</td></tr></table>";
	}

  function getBoxHtml( tipotext, fields ) {
		var mhtml,imgtd;
		if (tipotext=='') 
		  tipotext = tipoimmo[fields[1]];
		mhtml = tipotext+" - Codice <a href=\"../vc_det.php?id="+fields[0]+"\" target=\"_new\"><b>"+ fields[3] 
		+ "</b></a><br>"+fields[11]+"<p>"+fields[10]+"</p>";
		if (fields[5]!="") {
			imgtd =  "<a href=\"vc_det.php?id="+fields[0]+"\" target=\"_new\"><img width=\""+fields[5]+"\" height=\""+fields[6]+"\" border=\"0\" src=\""+fields[7]+"__mainfoto__.jpg\" /></a>";
		} else {
			imgtd = "";
		}
		return "<table border=\"0\"><tr><td valign=\"middle\"> "+imgtd+"</td><td class=\"maptip\" width=\"300px\">"+
									 mhtml+"</td></tr></table>";
	}

  function isNumeric( s ) {
    floatValue=parseFloat(s)
    if (isNaN(floatValue)) {
      return false;
		}  else {
     return true;
    }
	}
	
  function AggiungiMarker(fields) {
		var numero,tipotext;
  	var _coords = fields[8].split(" / ");
		var point = new GLatLng(_coords[0],_coords[1]);
		var icon = new GIcon();
		if (fields[9]!=0) 
  		icon.image = "img/_casaok.png";
		else
  		icon.image = "img/_casano.png";
		
		icon.shadow = "img/_shadow1.png";
		icon.iconSize = new GSize(32, 32);
		icon.shadowSize = new GSize(59, 32);
		icon.iconAnchor = new GPoint(10, 33);
		icon.infoWindowAnchor = new GPoint(7, 15);
		
		function createAppMarker(point, _icon, tipotext, fields) {
			var _title = tipotext+" - "+fields[2]+s_postiletto;			
			var marker = new GMarker(point,{icon:_icon, title:_title, draggable: _isdraggable});
			marker.html = getAppHtml(tipotext,fields);
			GEvent.addListener(marker, "click", function() {
				this.openInfoWindowHtml(this.html);							    
			});
			return marker;
		}								
		function createBoxMarker(point, _icon, tipotext, fields) {
			var marker = new GMarker(point,{icon:_icon, title:tipotext, draggable: _isdraggable});
//			var marker = new GMarker(point,{icon:_icon, title:tipotext});
			marker.html = getBoxHtml(tipotext,fields);
			GEvent.addListener(marker, "click", function() {
				this.openInfoWindowHtml(this.html);							    
			});
			return marker;
		}								
		
		if (parseInt(fields[1])<=1) {
			map.addOverlay(createAppMarker(point,icon,tipotext,fields));
		} else {
			map.addOverlay(createBoxMarker(point,icon,tipotext,fields));
		}	
	}

  function AggiungiMultiMarker() {
		function createGroupMarker(point, _icon, _queue) {
			var _title = _queue.length+s_sameplace;
			var mmarker = new GMarker(point,{icon:_icon, title:_title});
			var tabbody = '';
			var displayst;
			for (i in _queue) {
			  if (i==0) 
				  displayst = 'block';
				else
				  displayst = 'none';
  			switch (parseInt(_queue[i][1])) {
					case 0:
					case 1:
						tabbody += '<div id="asip'+i+'" style="display:'+displayst+'">'+getAppHtml('',_queue[i])+'</div>';
						break;
					case 2:
					case 3:
						tabbody += '<div id="asip'+i+'" style="display:'+displayst+'">'+getBoxHtml('',_queue[i])+'</div>';
						break;
				}
			}
			mmarker.html = '<div style="width:500px;height:140px;margin-right:7px">'+
			               '<table border="0"><tr><td><a href="#" onclick="gprev(); return false;"><img src="img/prev.gif" border="0" /></a></td>'+
			               '<td width="100%" align="center" class="mmtip"><b>'+_queue.length+'</b> '+s_immos+'</td><td><a href="#" onclick="gnext();return false;"><img src="img/next.gif" border="0" /></a></td></tr><tr><td colspan="3">'+tabbody+
										 '</td></tr></table></div>';
			//alert(mmarker.html);							 
			GEvent.addListener(mmarker, "click", function() {
				this.openInfoWindowHtml(this.html);							    
			});
			return mmarker;
		}							
  	var _coords = queue[0][8].split(" / ");
		var point = new GLatLng(_coords[0],_coords[1]);
		var icon = new GIcon();
		icon.image = "img/_agenzia.png";
		icon.shadow = "img/_shadow1.png";
		icon.iconSize = new GSize(32, 32);
		icon.shadowSize = new GSize(59, 32);
		icon.iconAnchor = new GPoint(10, 33);
		icon.infoWindowAnchor = new GPoint(7, 15);
		map.addOverlay(createGroupMarker(point,icon,queue));										
	}

  function AggiungiCoda() {
		if (queue.length>1) {
			AggiungiMultiMarker();
			queue = new Array();
		} else if (queue.length>0) {
			AggiungiMarker(queue[0]);
			queue = new Array();
		}
	}
	
	function NoImmoMessage(nimarker) {
		nimarker.openInfoWindowHtml(s_notfound);							
	}
	
  function receiveData() {
		var
		  afields,coords,point,key;
			
		if (xmlhttp.readyState == 4) {
			if (xmlhttp.status == 200) {
				if (xmlhttp.responseText.substr(0,4)!="____") {
					var lines = xmlhttp.responseText.split("\n");
					for ( key in lines ) {
						var line = lines[key];
						if (line!="") {
							afields = line.split(";");
							if (afields[8]!='') {
								imno++;
								coords = afields[8].split(" / ");
								point = new GLatLng(coords[0],coords[1]);
								if (bounds!=null)
  								bounds.extend(point);
								if (coords[0]==lastlat && coords[1]==lastlng) {
									queue[queue.length] = afields;
								} else {
									lastlat = coords[0]; lastlng = coords[1];
									AggiungiCoda();
									queue[queue.length] = afields;
								}
							}
						}	
					}
					idx = idx + 1;
					setTimeout("loadData();",10);		
				}	else {
					if (idx!=0) {
						AggiungiCoda();
						if (bounds!=null && (imno>0)) {
						  map.setCenter(bounds.getCenter(),map.getBoundsZoomLevel(bounds));
						}
						var imnotd = document.getElementById("imno");
						if (imnotd!=null) {
  						imnotd.innerHTML = "<strong>"+imno + "</strong> "+s_found;
						}
					} else {
     			  var noimmo = new GMarker(map.getCenter());
						GEvent.addListener(noimmo, "click", function() {
							NoImmoMessage(this);							    
						});
        		map.addOverlay(noimmo);										
  					NoImmoMessage(noimmo);							    
			    }
					var loadingdiv = document.getElementById("loading");
					if (loadingdiv) {
						loadingdiv.style.display = "none"; 
					}
					if (loadinglabel) {
						map.removeOverlay(loadinglabel);
						loadinglabel=null;
					}
				}
			} else {
					alert('Si è verificato un problema con la richiesta');
			}
		}
  }
	
  function loadData() {
		xmlhttp = null;
		if (window.XMLHttpRequest) {
			xmlhttp=new XMLHttpRequest();
		}	else if (window.ActiveXObject) {
			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
		if (xmlhttp!=null) {
			sendDataRequest();
		}
	}
	
	function beginLoadData(doCenter) {
		GEvent.addListener(map, "infowindowopen", function() {
	    actualpage = 0;
 	  });
		idx = 0;
		imno = 0;
		if (doCenter)
  		bounds = new GLatLngBounds;
		else {
		  bounds = null;
		}
  	loadData();
	}

