	var map_canvas = null;
	var geocoder_map_canvas = null;
	var address = new Array();
	var add1 = null;
	var aux = null;
	var _mapDiv;
	var div_b = null;
	var sobre_div_b=0;
	
    function initialize() {
      if (GBrowserIsCompatible()) {
        map_canvas = new GMap2(document.getElementById("map_canvas"));
        _mapDiv = document.getElementById("map_canvas");
        
        map_canvas.setCenter(new GLatLng(-21.1851999, -47.8085055),14);
        map_canvas.addControl(new GLargeMapControl());
        
        G_NORMAL_MAP.getMaximumResolution = function () { return 14 };
        
        //map_canvas.addControl(new GMapTypeControl());
        map_canvas.disableDoubleClickZoom();
        //map_canvas.enableContinuousZoom();
        geocoder_map_canvas = new GClientGeocoder();


        // Initialize the local searcher
        //gLocalSearch = new GlocalSearch();
        //gLocalSearch.setSearchCompleteCallback(null, OnLocalSearch);


        // Initialize the local searcher
        //gLocalSearch2 = new GlocalSearch();
        //gLocalSearch2.setSearchCompleteCallback(null, OnLocalSearch2);
        
        GEvent.addListener(map_canvas, "mousemove", function() {
			remove_div();
		});
      }
    }
	
    
    function checar_favoritos( div ) { 
	 	var parent = document.getElementById(div);
    	// collection of all children of parent 
    	var children = parent.childNodes; 
    	var last = children.length; 
		var favoritos = LerCookie("favoritos");
    	for (var i = 0; i < last; i++) { 
		
    		var child = children[i]; 
			
    		// if any child has the right name, return a // reference to it, exiting immediately 
    		if ( 'fav' == child.name) {
				if (favoritos.indexOf(child.value +"-") !=-1) {
    				child.checked = true;
				}else {
					child.checked = false;
				}
    		} 
    	} 
    	// none found above - return null 
		
    	return null; 
    }
    
    
    	
    	

    function showImoveis(local, al, v1, v2) {
		var tipo = new Array();
		
    	checkbox = document.getElementsByTagName("input");

    	var j=0;
    	var i;

   	   	for(i=0; i<checkbox.length; i++) // percorre os radios e verifica qual radio est� marcado
    	{
    	    if (checkbox[i].getAttribute("type") == "CHECKBOX" && checkbox[i].checked == true && checkbox[i].getAttribute("id") != "escolas" && checkbox[i].getAttribute("id") != "hospitais") // se for input do tipo radio e estiver marcado
	        {
	            tipo[j] = checkbox[i].value;
	            j++;
    	    }
    	}
   	
		if (local!="0"){
			map_canvas.clearOverlays();
			
			address = null;
			
			add1=null;
			
			var enderecos = document.getElementsByName("enderecos");
			var ids = document.getElementsByName("ids");
			var html = document.getElementsByName("html");
			var aux = 0;
			var indice = "";
			
			var st_abas_header =  "<ul id=\"ul_abas\" style=\"padding: 0; background-color:#cccccc;\">";
			

			
			var st_abas = "";
			
			var st_conteudo = "";
			
			var script = "";
				
			for(i=0; i<enderecos.length; i++){
				
				st_abas += "<li style=\"margin:0 0 -1px; hover background-color:#000000;\"><a href=\"#nav-" + (i+1) + "\" style='line-height:0.2; padding:0.5em 0.4em;'><span>" + (i+1) + "</span></a></li>";
				st_conteudo += "<div id=\"nav-" + (i+1) + "\" style=\"padding:2px 0; font-size:10px;\">" + html[i].value +
					"<a href=\"javascript:document.getElementById('id').value='"+ ids[i].value +"';document.forms[0].action='imovel-detalhes.jsp';document.forms[0].submit();\"><img src=\"images/detalhes.png\"></a> " +
					
					"</div>";
				script +=" checar_favoritos(\"nav-" + (i+1) + "\"); " ;
				if ((i==enderecos.length-1) || (enderecos[i].value !=enderecos[i+1].value) ) {
					indice = i+1;
					if (aux != i){
						indice = (aux+1)+" a "+(i+1)
					}
					st_abas += "</ul>";
					
					var balao = 
						st_abas_header + 
						st_abas + 
						st_conteudo + 
						"</div>";
						
					showAddress_lista(ids[i].value,Base64.decode(enderecos[i].value), balao, indice, script );
					aux = i+1;
					st_abas = "";
					st_conteudo = "";		
					script = "";		
				}
			}
			//document.getElementById(ref).checked=false;
			//document.getElementById("hospitais").checked=false;
		} else if (local=="0" && j!=0){
			alert("Selecione um bairro adequadamente");
		} else {
			alert("Selecione pelo menos um tipo de im�vel");
		}
    }

	function createMarker(point, id, html, indice, script) {
		var indice_ic = new GIcon();
		indice_ic.iconSize = new GSize(40, 20);
		indice_ic.iconAnchor = new GPoint(20, 20);
		indice_ic.infoWindowAnchor = new GPoint(19, 1);
		
		indice_ic.image = "../servlet/com.intelinet.util.ImageCreateCode?conteudo="+indice+"";
		var markerOptions = { icon:indice_ic };

		var marker = new GMarker(point, markerOptions);
		//html = html + "" + "<a href=\"javascript:document.getElementById('id').value='"+ id +"';document.forms[0].action='imovel-detalhes.jsp';document.forms[0].submit();\"><img src=\"images/detalhes.png\"></a></td></tr></table>";
		
		GEvent.addListener(marker, 'mousemove', function() {
			sobre_div_b = 1;
		});
		
		GEvent.addListener(marker, 'mouseover', function() {
			//marker.openInfoWindowHtml(html);
			
			sobre_div_b = 0;
			remove_div();
			var point = marker.getLatLng();
			var ponto_div = map_canvas.fromLatLngToDivPixel(map_canvas.fromContainerPixelToLatLng(point));

			var p1 = map_canvas.fromLatLngToDivPixel(map_canvas.fromContainerPixelToLatLng(new GPoint(0,0),true));

			var p2 = map_canvas.fromLatLngToDivPixel( point );

			div_b = document.createElement("div");
			
			div_b.id = "tabs";
			
			div_b.innerHTML = html;

			div_b.onmousemove = function() {
				sobre_div_b = 1;
			}; 
	
			div_b.onmouseout = function() {
				sobre_div_b = 0;
			} 

			//div_b.setAttribute("style", "border-width:1px; border-style:solid; border-color:#000000; background-color:#FAEAD3");

			var Width_div = 250;
			var Height_div = 96;
			div_b.style.position = 'absolute';
			div_b.style.display = 'block';

			div_b.style.height = Height_div+"px";
			div_b.style.width = Width_div+"px";

			if ( (p2.x-p1.x)+Width_div > eval(_mapDiv.style.width.replace("px","")) ){
				div_b.style.left = ""+(p2.x-p1.x-Width_div)+"px";
			}else{
				div_b.style.left = ""+(p2.x-p1.x)+"px";
			}
		
			div_b.style.top = ""+((p2.y-p1.y)-(Height_div+26) 	)+"px";
			
			window.setTimeout("sobre_div_b = 1; _mapDiv.appendChild(div_b); $('#tabs').tabs(); "+ script, 200);
			
		});
		
		GEvent.addListener(marker, "mouseout", function() {
			var point = marker.getLatLng();
			//marker.closeInfoWindow();
			window.setTimeout("sobre_div_b = 0;", 100);
		});
		
		return marker;
	}
	
	function remove_div(){
		try {
			if ((div_b!= null) && (sobre_div_b == 0)){
				_mapDiv.removeChild(div_b);
				div_b = null;
			}	
		}catch (e) {
		}	
	}
	
	function showAddress_lista(id, add, html, indice, script) {
		if (geocoder_map_canvas) {
			geocoder_map_canvas.getLatLng(add, function(point) {
				
				if (!point) {
				} else {
					if (add1==null){
						add1=add;
					}
					map_canvas.setCenter(point, 14);
					var marker = new createMarker(point, id, html, indice, script);
					map_canvas.addOverlay(marker);
				}
			});
		}
	}

	
var Search_referencia = function (field_id) {
	var gLocalSearch;
    var gCurrentResults = [];
    
    var vl = field_id;
    gLocalSearch = new google.search.LocalSearch();
    
    var gSmallIcon = new GIcon();
    gSmallIcon.image = "images/"+vl+".png";
    gSmallIcon.iconSize = new GSize(12, 20);
    gSmallIcon.shadowSize = new GSize(22, 20);
    gSmallIcon.iconAnchor = new GPoint(6, 20);
    gSmallIcon.infoWindowAnchor = new GPoint(5, 1);
    
    gLocalSearch.setSearchCompleteCallback(null, function () {
    	
    	if (!gLocalSearch.results) return;

        for (var i = 0; i < gCurrentResults.length; i++) {
          if (!gCurrentResults[i].selected()) {
            map_canvas.removeOverlay(gCurrentResults[i].marker());
          }
        }

        gCurrentResults = [];
        for (var i = 0; i < gLocalSearch.results.length; i++) {
          gCurrentResults.push(new LocalResult(gLocalSearch.results[i], gSmallIcon));
        }
    });
    
    var anElement = field_id;
	if (!anElement) {
		return false;
	}
	if (typeof anElement == "string") { // try for the ID
		anElement = document.getElementById ? document.getElementById(field_id) : document.all ? document.all[field_id] : document.layers ? document.layers[field_id] : field_id;
	}
	
	if (typeof anElement == "string") { // the grab failed: typeof null yields "object"
		return false;
	}
	
	this.element = anElement;
	
	
	
	this.getValor = function () {
		return vl;
	}
	
	this.getGcurrentResults = function () {
		return gCurrentResults;
	}
	
	this.getGlocalSearch = function () {
		return gLocalSearch;
	}
	
	// on blur listener
	this.element.onclick = function () {
	    
		var ck = document.getElementById(vl);
		if (ck.checked==true){
			aux = vl;
			gLocalSearch.setCenterPoint(add1);
			gLocalSearch.execute(vl);
		} else {
			if (!gLocalSearch.results) return;
			for (var i = 0; i < gCurrentResults.length; i++) {
				if (!gCurrentResults[i].selected()) {
					map_canvas.removeOverlay(gCurrentResults[i].marker());
				}
			}
		}
	};
    
}


      function LocalResult(result, gSmallIcon) {
        this.result_ = result;
        map_canvas.addOverlay(this.marker(gSmallIcon));
      }

      LocalResult.prototype.marker = function(opt_icon) {
        if (this.marker_) return this.marker_;
        var marker = new GMarker(new GLatLng(parseFloat(this.result_.lat),
                                           parseFloat(this.result_.lng)),
                                 opt_icon);
        GEvent.bind(marker, "mouseover", this, function() {
          marker.openInfoWindow(this.result_.html.cloneNode(true));
        });
        this.marker_ = marker;
        return marker;
      }

      LocalResult.prototype.selected = function() {
        return this.selected_;
      }
      
      

