// JavaScript Document
mapa = {
	map : null,
	geocoder : null,
	mgr : null,
	markers : new Array(),
	categorias : new Array(),
	empreendimento : null,
	empreendimentoAtual :0,
	zoom: 16,
	empreendimentosBairro : new Array(),
	pontoAtual : null,
	idEmpreendimento : 0,
	empreendimentoPonto : null,
	dtPontos : null,
	pontoAtual : 0,
	directions : null,
	iconeAtual : '',

	inicia : function(){
		if (GBrowserIsCompatible()) {
			this.idEmpreendimento=document.getElementById('hidIdEmpreendimento').value;
			var latitude = document.getElementById('hidLatitude').value;
			var longitude = document.getElementById('hidLongitude').value;
			var nome = document.getElementById('hidNome').value;
			var ponto = new GLatLng(latitude,longitude);
			mapa.empreendimentoPonto = ponto;
			mapa.map = new GMap2(document.getElementById("divMapa"));
			//GEvent.addListener(mapa.map, "click", mapa.mapClick); 
			directionsPanel = document.getElementById("divRota");
			mapa.directions = new GDirections(mapa.map,directionsPanel);
			mapa.geocoder = new GClientGeocoder();
			mapa.map.addControl(new GSmallMapControl());
			mapa.map.addControl(new GMapTypeControl());
			mapa.map.setCenter(ponto, 15);
			mapa.mgr = new GMarkerManager(mapa.map);
			
			this.empreendimento = this.createMarker(ponto,nome,'icone.png',21,36);
			mapa.map.addOverlay(this.empreendimento);
		}
	},
	
	mapClick : function(marker, point) { 
		if (!marker) { 
			document.getElementById('bLocalizacaoEndereco').innerHTML = point; 
		} 
	},
	
	createMarker : function(posn, title,iconeImg,alt,lar) {
		var baseIcon = new GIcon();
       // baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
        baseIcon.iconSize = new GSize(20, 34);
        baseIcon.shadowSize = new GSize(37, 34);
        baseIcon.iconAnchor = new GPoint(9, 34);
        baseIcon.infoWindowAnchor = new GPoint(9, 2);
        baseIcon.infoShadowAnchor = new GPoint(18, 25);
		var icone = new GIcon(baseIcon);
		icone.image = "_IMG/" + iconeImg;
		icone.iconSize = new GSize(alt, lar);
		var marker = new GMarker(posn, {title: title,icon:icone });
		return marker;
    },
	
	manipulaPontos : function(categoria,subCategoria,status,icone){
		if(status && mapa.pontoAtual==0){
			mapa.iconeAtual = icone;
			empreendimento.retornaPontos(categoria,subCategoria,mapa.idEmpreendimento,mapa.manipulaPontosCarrega);
		}else if(mapa.pontoAtual!=0){
			setTimeout("mapa.manipulaPontos('" + categoria + "','" + subCategoria + "','" + status + "','" + icone + "')",100);
		}else{
			categoria = categoria + subCategoria;
			var arrayMarkersNovo = new Array();
			var arrayCategoriasNovo = new Array();
			for(i=0;i<mapa.markers.length;i++){
				
				if(mapa.categorias[i].toString().indexOf(categoria)!=-1){
					mapa.map.removeOverlay(mapa.markers[i]);
				}else{
					arrayCategoriasNovo.push(mapa.categorias[i]);
					arrayMarkersNovo.push(mapa.markers[i]);
				}
			}
			mapa.markers = arrayMarkersNovo;
			mapa.categorias = arrayCategoriasNovo;
		}
	},
	
	manipulaPontosCarrega : function(retorno){
		mapa.dtPontos = retorno.value;
		if(mapa.dtPontos!=null && typeof(mapa.dtPontos) == "object"){
			mapa.montaMarkers();
		}
	},
	
	montaMarkers : function(){
		if(mapa.pontoAtual==mapa.dtPontos.Rows.length){
			mapa.pontoAtual=0;
			//mapa.mgr.addMarkers(mapa.markers, 3, 17);
			//setTimeout(mapa.recarregaMapa, 100);
		}else{
			mapa.adicionaMarker();
		}
	},
	
	adicionaMarker : function(){
		var row = mapa.dtPontos.Rows[mapa.pontoAtual];
		var nome = row.nome;
		var latitude = row.longitude;
		var longitude = row.latitude;
		var icone = mapa.iconeAtual + '.png';
		var ponto = new GLatLng(latitude,longitude);
		var marker = mapa.createMarker(ponto,nome,icone,36,36);
		
		var atual = mapa.pontoAtual;
		GEvent.addListener(marker, "click", function() {
			var rowAtual = mapa.dtPontos.Rows[atual];
			var conteudo = '<b>' + rowAtual.nome + '</b><br>';
			conteudo += '<b>Endereço: </b>' + rowAtual.endereco + '<br>';
			if(rowAtual.telefone!='')
				conteudo += '<b>Telefone: </b>' + rowAtual.telefone + '<br>';
			
			marker.openInfoWindowHtml(conteudo);										 
		});
		
		mapa.markers.push(marker);
		mapa.categorias.push(row.categoria + row.subCategoria);
		mapa.pontoAtual++;
		mapa.map.addOverlay(marker);
		setTimeout(mapa.montaMarkers, 0.5);
	},
	
	recarregaMapa : function(){
		mapa.mgr.refresh();
	},
	//mapa.empreendimentoPonto
	tracarRota : function(){
		var endereco = document.getElementById('txtEndereco').value;
		var enderecos = new Array();
		if(endereco!=''){
			enderecos.push(endereco);
			enderecos.push(mapa.empreendimentoPonto);
			mapa.directions.clear();
			mapa.directions.loadFromWaypoints(enderecos,{ "locale": "pt_BR"});
			setTimeout(mapa.mostraRota, 1000);
		}
	},
	
	mostraRota : function(){
		var conteudoHTM='Endereço não encontrado, preencha o campo corretamente.';
		if(mapa.directions.getNumRoutes()>=1){
			conteudoHTM='';
			var rota = mapa.directions.getRoute(0);
			document.getElementById('divTrajeto').style.display = '';
			document.getElementById('divRotaMostra').innerHTML='';
			var classe='bg_vazio_localizacao';
			for(i=0;i<rota.getNumSteps();i++){
				passo = rota.getStep(i);
				if (classe=='bg_vazio_localizacao')
					classe='bg_branco_localizacao';
				else
					classe='bg_vazio_localizacao';
				conteudoHTM += '<div class="' + classe + '">';
				conteudoHTM += '<div class="bullet-localizacao-trajeto"></div>';
				conteudoHTM += '<div class="txt-localizacao-trajeto">' + passo.getDescriptionHtml() + '</div>';
				conteudoHTM += '</div>';
			}
		}
		document.getElementById('divTrajeto').style.display = '';
		document.getElementById('divRotaMostra').innerHTML = conteudoHTM;
	}
	
}
