/* ************************************************************************************ *
* Inicializa as Variáveis																*
* ************************************************************************************* */
var mapa			= null;
var marcador	= null;
var latitude 	= null;
var longitude	= null;
var endereco  	= null;
var divMapa		= "mapa";

var lbMapaImovel = new LightBox("lbMapaImovel", "lbMapaImovel");
lbMapaImovel.setOverFlow(true, true, "810px", "540px");
lbMapaImovel.setBgOpacidade("50");
lbMapaImovel.setBorderDiv("8px solid #efefef");

function showMap(la, lo, end, ajax){
	if( (la!=null) && (la!=undefined) )
		latitude = la;
	if( (lo!=null) && (lo!=undefined) )
		longitude = lo;
	if( (end!=null) && (end!=undefined) )
		endereco = end;
	
		
	if(ajax==null || ajax==undefined)
		ajax=false;
	else
		ajax=true;
	
		
	
	marcador = null;
	mapa	 = null;
	divMapa	 = "mapa";
	
	if (endereco != null) {
		getLocation();
	}
	
	if( ajax ) {
		var ajax 	= new Ajax();
		var params 	= "";
		var obj = document.getElementById("lbMapaImovel");
		obj.innerHTML = "";
			
		divMapa		= "showmap-map";
		
		ajax.load("POST", SITE+"ajax/showmap.php", params, "lbMapaImovel");
		controlShowMapAjax();
	}
	else
		controleInit();
		
	lbMapaImovel.show();
}



function controlShowMapAjax(){
	var loaded = document.getElementById("showmap-resp");
	if( loaded==null || loaded==undefined || loaded.value!="loaded" )
		reload("controlShowMapAjax()", 1);
	else {
		controleInit();
	}
}


function controleInit() {
	if (latitude!=null && longitude!=null) {
		init();
	} else {
		reload("controleInit()",1);
	}
}

function getLocation(){
	geocoder = new GClientGeocoder();    
	geocoder.getLocations(endereco, setaDadosLongLat);
}

function setaDadosLongLat(response) {
	if(response.Placemark != undefined){
	  place = response.Placemark[0];
	  point = new GLatLng(place.Point.coordinates[1],place.Point.coordinates[0]);
		latitude	= place.Point.coordinates[1];
		longitude	= place.Point.coordinates[0];
	}
}

function init() {
  
	if (GBrowserIsCompatible()) {
		mapa = new GMap2(document.getElementById(divMapa));
	
		mapa.disableDoubleClickZoom();

		var iconeBase        = new GIcon();
		iconeBase.shadow     = SITE+"imagens/admin/mapa_sombra.png";
		iconeBase.iconSize   = new GSize(17, 39);
		iconeBase.shadowSize = new GSize(51, 34);
		iconeBase.iconAnchor = new GPoint(17, 39);
		
		var pino   = new GIcon(iconeBase);
		pino.image = SITE+"imagens/admin/mapa_pin.png";

		var marcadorOptions = { icon: pino, draggable: false };
		var options = {
			onMarkersSetCallback : function(searcher){
				mapa.clearOverlays();
			}
		};

		GEvent.addListener(mapa, "load", function(overlay) {			
			mapa.clearOverlays();
			marcador = new GMarker(new GLatLng(latitude, longitude), marcadorOptions);
			mapa.addOverlay(marcador);		
		});
	
		centralizar();
  }
}

/* ************************************************************************************ *
* Controles																				*
* ************************************************************************************* */
function moverNorte(){
	mapa.panDirection(0, +1);
}

function moverSul(){
	mapa.panDirection(0, -1);
}

function moverLeste(){
	mapa.panDirection(-1, 0);
}

function moverOeste(){
	mapa.panDirection(+1, 0);
}

function zoomMais(){
	if(mapa.getZoom() <= 17){
		mapa.zoomIn();
	}
}

function zoomMenos(){
  if(mapa.getZoom() >= 14){
    mapa.zoomOut();
  }
}

function nivelZoom(nivel){
	if(nivel >= 14 && nivel <= 17){
		mapa.setZoom(nivel);
	}
}

function centralizar(){
	if((latitude!=null) && (longitude!=null))	
		mapa.setCenter(new GLatLng(latitude, longitude), 17);
}

function satelite(){
	mapa.setMapType(G_SATELLITE_MAP);
}

function ruas(){
	mapa.setMapType(G_NORMAL_MAP);
}

function hibrido(){
	mapa.setMapType(G_HYBRID_MAP);
}

