// JavaScript Document
//GOOGLE MAPS
gmap = true; //essa vari�vel s� serve pra indicar que o arquivo gmaps.js existe na p�gina. 

//INICIA O GMAPS	
function load() { 
  if (GBrowserIsCompatible()) { 
	map = new GMap2(document.getElementById("gmaps")); 
	map.addControl(new GLargeMapControl());
	//map.addControl(new GMapTypeControl());
	map.addControl(new GHierarchicalMapTypeControl()); 
	map.addControl(new GOverviewMapControl());
	map.setCenter(new GLatLng(-15.780148, -47.92917), 4); 
	
	pegaJson($('#elemento_id').val(), $('#categoria_id').val());
	mostrou_mapa = true;
  } 
} 


	//ENVIA UM POST VIA AJAX PRA RECEBER O JSON
	function pegaJson(elemento_id, categoria_id, grafico){
		switch(categoria_id){ //ELE CHECA A CATEGORIA PRA IR BUSCAR NO LUGAR CERTO
			case '1':
				var valores = [
							   { name: 'midia_id',  value: elemento_id},
							   { name: 'categoria_id',  value: categoria_id}
							   ];
				$.post(base_url()+'ajax_modulos/json_veiculos', valores, function(response){
					//processaJson(response);
					var jsonData = eval("(" + response + ")");
					pontos2 = jsonData.markers;
					if (grafico)
						lista_veiculos_completa('lista_grafico'); 
					else
						processaJson(response);
				});
			break;
			
			case '2':
				var valores = [{ name: 'midia_id', value: elemento_id}];
				$.post(base_url()+'ajax_modulos/json_grupos_afiliados/', valores, function(response){
					var jsonData = eval("(" + response + ")")
					pontos2 = jsonData.markers
					processaJson(response)
				});
			break;
			
			case '5': //declarei o 5 para representar pessoa
				var valores = [
							   { name: 'midia_id',  value: elemento_id},
							   { name: 'categoria_id',  value: categoria_id}
							   ];
				$.post(base_url()+'ajax_modulos/json_veiculos', valores, function(response){
					var jsonData = eval("(" + response + ")")
					pontos2 = jsonData.markers
					if (grafico)						
						lista_veiculos_completa('lista_grafico', 'ampie') 
					else
						processaJson(response)
	
				})
			break;
			
			case '6': //declarei o 6 para representar estado
				pegaJsonLocal(elemento_id, categoria_id);
			break;
			
			case '7': //declarei o 7 para representar municipio
				pegaJsonLocal(elemento_id, categoria_id);
			break;
		}		
	} 	
	
	
	//AQUI ELE VAI FAZER UM FOR PRA MOSTRAR O PONTO NO MAPA
	function processaJson(json){
		//var json = $('#json').html();
		
		var jsonData = eval("(" + json + ")");
		bounds = new GLatLngBounds();

		for (var i=0; i<jsonData.markers.length; i++){
			mostra_ponto(jsonData.markers[i], i);
			pontos[i] = jsonData.markers[i];
			
			append_li(i);
			
		}
		ativa_menu_mapa();
		//ativa_lista_grafico();
		
		map.setZoom(map.getBoundsZoomLevel(bounds));
		map.setCenter(bounds.getCenter());
	}
	
	
	function ativa_lista_grafico(){
		var categoria_id = $('#categoria_id').val();
		switch(categoria_id){
			case '1': lista_veiculos_completa('lista_grafico'); 
			break;
		}
		
	}
	
	function mostra_ponto(ponto, i){
		//se houver lat long ele imprime no mapa
		if(ponto.latitude && ponto.longitude){
			var MarkerOptions = new Object();
		
			var point = new GLatLng(ponto.latitude, ponto.longitude); //define lat e long pra criar o marker
			bounds.extend(point);//declara o ponto pra depois ter os extremos
			
			var marker =
			markers[i] =  new GMarker(point, MarkerOptions);
	
			GEvent.addListener(markers[i], "click", function() {
				abre_balao(i);
			});
	
			map.addOverlay(markers[i]);
		}
		

	}
	
	
	
	function append_li(i){
		var p = pontos[i];
		switch(p.categoria_id){
			case '1': append_li_grupo(i);
			break;
			
			case '3': append_li_veiculo(i);
			break;
		}	
	}

	function ativa_menu_mapa(){
		$('#menu_mapa li a').click(function(){
			name = $(this).attr('name')
			if(name)
				abre_balao(name)				
			else	
				alerta_falta_endereco(this)
			return false					
		})
	}
	
	function alerta_falta_endereco(obj){
		console.log($(obj).next())
		if(!$(obj).next().attr('href')){
			$(obj).after('<a href="'+base_url()+'veiculo/'+$(obj).attr('href')+'">Registro sem endereço. <strong class="color_azul">Ver página do veículo</a>')
			$(obj).parent().addClass('cinza_claro')
		}
		
	}
	
	function abre_balao(i){
		var p = pontos[i];
		//alert();
		switch(p.categoria_id){
			case '1': 	
				abre_balao_grupo(i);
			break;
			case '3':
				abre_balao_veiculo(i);
			break;
		}	
	}
	
	
	

	
	

//FIM DO GOOGLE MAPS
