//Copyright - 2000-2008 Alfredo J G A Borba. Todos os Direitos Reservados (www.webcalc.com.br)


//Carrega Mapa
var map;
var center_default;
var lat_1 = -12.98701587614574;
var long_1 = -38.45333397388458;
var zoom_default = 2;
Map_Control = null;
TypeControl = null;
viewMap_Control = null;
Scale_Control = null;
function load() {
	if (GBrowserIsCompatible()) {
		Map_Control = new GSmallMapControl();
		//TypeControl = new GMapTypeControl();
		viewMap_Control = new GOverviewMapControl();
		Scale_Control = new GScaleControl();
		map = new GMap2(document.getElementById("map"));
		center_default = new GLatLng(lat_1, long_1);
		map.addControl(Map_Control);
		//map.addControl(TypeControl);
		map.addControl(new GHierarchicalMapTypeControl());
		map.addMapType(G_PHYSICAL_MAP);
		//map.addMapType(G_SATELLITE_3D_MAP);	
		map.addControl(viewMap_Control);
		map.setCenter(center_default, zoom_default);
		map.enableDoubleClickZoom();
		map.enableScrollWheelZoom();		
		//trafficInfo.hide(); //Verificar como acrescentar botao no mapa (????)
		//map.addOverlay(trafficInfo);	//Idem
		//map.addOverlay(geoXml);							
	}
	else {
		document.getElementById("map").innerHTML = "<br /><br /><br /><br /><br /><span style='font-size: 12pt; color: #0000FF; font-weight: bold;'>O seu Navegador n&atilde;o &eacute; compat&iacute;vel com o <em>Google Maps</em></span>";
	}
}
//Fim de Carrega Mapa


//Inicializa Mapa
var obj_format = null;
var obj_url = null;
var ent;
var decSep;
var milSep;
var tipo;
var cdec;
var prec;
var lang;
var pg_carreg = false;
var painel_id = 1;
var painel_id_ant;
var event_map_click;
var hasOverlay = false;
var buscaGeocode = false;
var campoGeocodeIniciado = false;
var lat_1_index, long_1_index;
var pt_ant = "";
var hist_dis_txt = "";
var dist_m = 0;
var dist_m_acum = 0;
var unid_dist = " m";
var pt_1 = null;
var pt_2 = null;
var linkAlt = false;
var zoomLink = zoom_default;
var tpLink = "";
var exLink = "";
var pilhaMapas = null;
var pilha_index = 0;
var movePilha = false;
var posInicial = ["", "", "", ""];
var lat_dec, long_dec;
var divBarra_top;
var divMapa_top;
var divConfig_top;
var divPlaceMarks_top;
var mapaExpandido = false;
var cor_lnh = "#FF0000";
var opac_lnh = 0.5;
var larg_lnh = 5;
var jan_zoom, jan_center_x, jan_center_y, jan_map_type;
var de_stg = "";
var para_stg = "";
var dest_int_stg = "";
var optObj = "";
var opt = "";
function Inicializar() {	
	if (GBrowserIsCompatible()) {		
		var linkInterno = parent.frames['frame_menu'].linkInterno;		
		var pilhaZoom = "";
		var top_href = top.location.href;
		var w_href = document.location.href;
		var href = (top_href.indexOf("?pi=")!=-1 && !linkInterno)? top_href : w_href;
		parent.frames['frame_menu'].linkInterno = false;		
		obj_url = new UrlQueryWbc(href);
		if (obj_url.queryParam.pi) {			
			painel_id = parseFloat(obj_url.queryParam.pi);
		}
		if (obj_url.queryParam.ld && obj_url.queryParam.lg) {		
			lat_1 = parseFloat(obj_url.queryParam.ld);
			long_1 = parseFloat(obj_url.queryParam.lg);
			linkAlt = true;
		}
		if (obj_url.queryParam.zm) {		
			zoomLink = parseFloat(obj_url.queryParam.zm);
			zoomLink = (!isNaN(zoomLink))? zoomLink : 2;
			linkAlt = true;
		}
		if (obj_url.queryParam.tp) {		
			tpLink = (obj_url.queryParam.tp=="m" || obj_url.queryParam.tp=="k" || obj_url.queryParam.tp=="h" || obj_url.queryParam.tp=="p")? obj_url.queryParam.tp : "m"; //m - k - h			
			linkAlt = true;
		}		
		if (obj_url.queryParam.ex) {
			exLink = obj_url.queryParam.ex;			
			linkAlt = true;
		}
		
		var classes = parent.frames["frame_menu"];
		ent = false;
		decSep = classes.format_decSep;
		milSep = classes.format_milSep;
		tipo = 0;
		cdec = 20;
		prec = classes.format_prec;
		lang = classes.format_lang;
		obj_format = new FormatWbc("", ent, decSep, milSep, tipo);		
		
		pilhaMapas = parent.frames["frame_menu"].pilhaMapas;
		if (pilhaMapas!=null) {
			pilhaMapas[0] = (linkAlt)? [lat_1, long_1, zoomLink, tpLink, ""] : [lat_1, long_1, zoom_default, "m", ""];
			pilha_index = parent.frames["frame_menu"].pilhaIndex;			
			lat_1 = (!isNaN(parseFloat(pilhaMapas[pilha_index][0])))? parseFloat(pilhaMapas[pilha_index][0]) : lat_1;
			long_1 = (!isNaN(parseFloat(pilhaMapas[pilha_index][1])))? parseFloat(pilhaMapas[pilha_index][1]) : long_1;
			var pilhaCenter = new GLatLng(lat_1, long_1);
			var pilhaZoom = (!isNaN(parseFloat(pilhaMapas[pilha_index][2])))? parseFloat(pilhaMapas[pilha_index][2]) : zoom_default;
			map.setCenter(pilhaCenter, pilhaZoom);
			var pilhaTp = (pilhaMapas[pilha_index][3]!="")? pilhaMapas[pilha_index][3] : "m";
			if (pilhaTp=="k") {
				map.setMapType(G_SATELLITE_MAP);			
			}
			else if (pilhaTp=="h") {
				map.setMapType(G_HYBRID_MAP);
			}
			else if (pilhaTp=="p") {
				map.setMapType(G_PHYSICAL_MAP);
			}
			if (pilhaMapas[pilha_index][4]!="") {
				document.getElementById("end_geocoder").value = pilhaMapas[pilha_index][4];
				document.getElementById("end_geocoder").style.color = "#000000";
				campoGeocodeIniciado = true;
			}
			else {
				document.getElementById("end_geocoder").value = "Digite o Local Desejado";
			}
			document.getElementById("desfazer").src = (pilha_index!=0)? "../imagens/desfazer_on.gif" : "../imagens/desfazer_off.gif";
			document.getElementById("refazer").src = (pilha_index!=pilhaMapas.length-1)? "../imagens/refazer_on.gif" : "../imagens/refazer_off.gif";
		}
		lat_dec = lat_1;
		long_dec = long_1;
								
		if (is.fox && !is.chr) {
			document.getElementById("painel").style.height = "180px";
			document.getElementById("barra").style.top = "228px";
			document.getElementById("map").style.top = "267px";
			document.getElementById("map_rodape").style.top = "573px";
			document.getElementById("placeMarks").style.top = "265px";
			document.getElementById("placeMarks").style.left = "282px";
			document.getElementById("config").style.top = "263px";
			document.getElementById("carregando2").style.top = "165px";
		}
		if (is.chr) {
			document.getElementById("painel").style.height = "187px";
			document.getElementById("barra").style.top = "235px";
			document.getElementById("map").style.top = "273px";
			document.getElementById("map_rodape").style.top = "579px";
			document.getElementById("placeMarks").style.top = "272px";
			document.getElementById("placeMarks").style.left = "276px";
			document.getElementById("config").style.top = "270px";
			document.getElementById("carregando2").style.top = "165px";
		}
		
		if (linkAlt) {
			var centLink = new GLatLng(lat_1, long_1);
			document.mapa.zoom_level.selectedIndex = zoomLink; 			
			map.setCenter(centLink, zoomLink);
			if (tpLink=="k") {
				map.setMapType(G_SATELLITE_MAP);			
			}
			else if (tpLink=="h") {
				map.setMapType(G_HYBRID_MAP);
			}
			else if (tpLink=="p") {
				map.setMapType(G_PHYSICAL_MAP);
			}
		}
		else {
			document.mapa.zoom_level.selectedIndex = (pilhaZoom=="")? zoom_default : pilhaZoom;
		}

		if (map) {
			alteraPainel(painel_id, true);
		}
		else {
			var map_txt = "O mapa n" + unescape("\u00E3") + "o foi carregado. Tente novamente." ;
			alert(map_txt);
			return;
		}
		pg_carreg = true;
		hasOverlay = false;				
		GEvent.addListener(map, "zoomend", function(oldLevel, newLevel) {							
			document.mapa.zoom_level.selectedIndex = newLevel;
		});
		GEvent.addListener(map, "moveend", function() {							
			preenchePainel(painel_id);			
			preencheRodape();									
			buscaGeocode = false;						
			if (!movePilha) { //Alterar codigo para outros valores de painel_id				
				if (pilha_index<100) {
					pilha_index++;
					pilhaMapas.length = pilha_index+1;
					pilhaMapas[pilha_index] =  ["", "", "", "", ""];
					parent.frames["frame_menu"].pilhaIndex = pilha_index;
					pilhaMapas[pilha_index][0] = lat_1;
					pilhaMapas[pilha_index][1] = long_1;
					pilhaMapas[pilha_index][2] = document.mapa.zoom_level.selectedIndex;
					pilhaMapas[pilha_index][3] = map.getCurrentMapType().getUrlArg();
					pilhaMapas[pilha_index][4] = (campoGeocodeIniciado)? document.getElementById("end_geocoder").value : "";
				}
				else {
					var idProv0 = lat_1;
					var idProv1 = long_1;
					var idProv2 = document.mapa.zoom_level.selectedIndex;
					var idProv3 = map.getCurrentMapType().getUrlArg();
					var idProv4 = (campoGeocodeIniciado)? document.getElementById("end_geocoder").value : "";
					pilhaMapas.shift();
					pilhaMapas.push([idProv0, idProv1, idProv2, idProv3, idProv4]);
				
				}				
				if (pilha_index!=0) {document.getElementById("desfazer").src = "../imagens/desfazer_on.gif";}
				document.getElementById("refazer").src = "../imagens/refazer_off.gif";
			}			
		});		
		event_map_click = GEvent.addListener(map, "click", function(mark, point) {  
			if (mark) {				
				if (painel_id!=2) {map.setCenter(mark.getPoint());}	
				if (painel_id==1) {preenchePainel (painel_id);}
			}
			else {document.getElementById("placeMarks").style.display = "none";}
		});		
		var keyboard_event = new GKeyboardHandler(map);
		document.getElementById("selec_painel").selectedIndex = 0;
		document.getElementById("restaura").click();				

		divBarra_top = document.getElementById("barra").style.top;
		divMapa_top = document.getElementById("map").style.top;
		divConfig_top = document.getElementById("config").style.top;
		divPlaceMarks_top = document.getElementById("placeMarks").style.top;
		document.getElementById("config").style.display = "none";		
		document.getElementById("end_geocoder").onfocus = iniciaEndGeocoder;
		document.getElementById("end_geocoder").onchange = textEndGeocoder;
		document.getElementById("end_geocoder").onblur = textEndGeocoder;			
		document.getElementById("painel_aux_td_title").onmousedown = alteraPainelAux;
		document.getElementById("painel_aux_td_title").onmouseup = alteraPainelAux;		
		document.getElementById("painel_aux_td_fig").onmousedown = alteraPainelAux;
		document.getElementById("painel_aux_td_fig").onmouseup = alteraPainelAux;		
		document.getElementById("btPainelAuxBarra").onclick = alteraPainelAux;
		document.getElementById("link_url").onfocus = linkURL;		
		document.getElementById("btPainelLink").onclick = linkURL;
		document.getElementById("abre_link").onclick = linkURL;
		if (is.fox) {
			document.getElementById("painel_aux_barra").style.width = "99%";
			document.getElementById("painel_aux_conteudo").style.height = "92%";
		}		
		if (exLink=="1") {
			altMap(document.getElementById("expande_mapa"));
		}
		if (obj_url.queryParam.pt) {			
			var pt = (obj_url.queryParam.pt).split("_");
			var pt_y = parseFloat(pt[0]);
			var pt_x = parseFloat(pt[1]);
			var mark = new GMarker(new GLatLng(pt_y, pt_x));
			map.addOverlay(mark);			
			hasOverlay = true;							
			GEvent.addListener(mark, "dblclick", function() {							
				map.removeOverlay(mark);
			});
		}		
		if (obj_url.queryParam.mk) {			
			var mk = (obj_url.queryParam.mk).split("_");
			var mk_y = parseFloat(mk[0]);
			var mk_x = parseFloat(mk[1]);
			var mkIcon = new GIcon(G_DEFAULT_ICON);
			mkIcon.image = "http://www.webcalc.com.br/viagens/imagens/mk_blue.png";
			mkIcon.iconSize = new GSize(32, 32);
			mkIcon.iconAnchor = new GPoint(16, 32);
			mkIcon.shadowSize = new GSize(49, 32);
			mkOptions = { icon:mkIcon };
			var mark2 = new GMarker(new GLatLng(mk_y, mk_x), mkOptions);
			map.addOverlay(mark2);			
			hasOverlay = true;							
			GEvent.addListener(mark2, "dblclick", function() {							
				map.removeOverlay(mark2);
			});
		}
		if (obj_url.queryParam.km) {
			linkAlt = true;
			document.getElementById("ativa_kml").checked = true;
			document.configura.kml_url.value = obj_url.queryParam.km;
			addKML(document.getElementById("ativa_kml"));
			addKML(document.getElementById("carregar_url"), true);
		}
		if (exLink=="2") {
			document.configura.nova_janela.checked = true;
			altMap(document.getElementById("expande_mapa"));
		}		
		mira();
	}
  else {
	  document.getElementById("painel").innerHTML = "<br /><br /><br /><span style='font-size: 12pt; color: #0000FF; font-weight: bold; margin-left: 20px;'>O seu Navegador n&atilde;o &eacute; compat&iacute;vel com o <em>Google Maps</em></span>";
  }
}

function iniciaEndGeocoder() {
	document.getElementById("placeMarks").style.display = "none";
	if (!campoGeocodeIniciado) {
		document.getElementById("end_geocoder").value = "";
		document.getElementById("end_geocoder").style.color = "#000000";
		campoGeocodeIniciado = true;
	}	
}

function textEndGeocoder() {
	if (document.getElementById("end_geocoder").value=="") {
		document.getElementById("end_geocoder").style.color = "#909090";
		document.getElementById("end_geocoder").value = "Digite o Local Desejado";
		campoGeocodeIniciado = false;
	}	
}
//Fim de Inicializar Mapa


//Altera Painel
function alteraPainel(pn_id, ini) {
	if ((painel_id==pn_id && !ini) || pn_id==0) {
		return;
	}	
	painel_id_ant = painel_id;
	painel_id = pn_id;	
	var hasScript = addScript(pn_id);
	if (document.getElementById("config").style.display=="block") {
		altMap(document.getElementById("abre_config"));
		MudaBtVz(document.getElementById("abre_config"), 3);
		MudaBtVz(document.getElementById("abre_config"), 2);
	}
	switch (pn_id) {
		case 1 :
			if (hasScript) {				
				mudaPainelPid1();
				mudaPainelInstru1();				
			}							
		break;
		case 2 :
			if (hasScript) {		
				mudaPainelPid2();
				mudaPainelInstru2();			
			}
		break;
		case 3 :
			if (hasScript) {				
				mudaPainelPid1();
				mudaPainelInstru1();			
			}
			abreJanGE();							
		break;
	}
	
	var selec_op = document.getElementById("selec_painel").options;
	if (pn_id==3) {pn_id = 1;}
	for (var i = 1; i < selec_op.length; i++) {
		if (i==pn_id ) {
			document.getElementById("selec_painel").options[i].style.color = "#a1a1a1";
		}
		else {
			document.getElementById("selec_painel").options[i].style.color = "#000000";
		}		 
	}
	document.getElementById("selec_painel").selectedIndex = 0;
	if (mapaExpandido && (document.getElementById("altPainel").src).indexOf("expand")!=-1) {
		altMap(document.getElementById("altPainel"));
	}
}
//Fim de Altera Painel



//Adiciona Script On-Demand
function addScript(pn_id) {
	var head = document.getElementsByTagName("head")[0];
	if (pn_id==3) {painel_id = 1; pn_id=1};
	switch (pn_id) {
		case 1 :
			if (window.mudaPainelPid1) {
				return true;
			}
			else {				
				return false;
			}					
		break;
		case 2 :
			if (window.mudaPainelPid2) {
				return true;
			}
			else {
				var script_pid_2 = document.createElement("script");
				script_pid_2.id = "script_pid2";
				script_pid_2.type = "text/javascript";
				script_pid_2.src = "scripts/mapas_wbc_pid_2.js";
				head.appendChild(script_pid_2);
				return false;
			}
		break;
	}
}
//Fim de Adiciona Script On-Demand


//Preenche Painel
function preenchePainel (pn_id) {	
	if (document.configura.ativa_format.checked) {
		var classes = parent.frames["frame_menu"];
		ent = false;
		decSep = classes.format_decSep;
		milSep = classes.format_milSep;
		tipo = 0;
		cdec = 20;	
	}
	else {		
		cdec = (document.configura.opdec.checked)? document.configura.dec.value : 20;
		decSep = document.configura.dec_sep.options[document.configura.dec_sep.selectedIndex].value;
		milSep = document.configura.mil_sep.checked;
	}		
	obj_format.setFormat(decSep, milSep, tipo, cdec, "auto");
	var center = map.getCenter();					
	lat_1 = center.y;
	lat_1_index = (lat_1>=0)? 0 : 1;				
	long_1 = center.x;			
	long_1_index = (long_1>=0)? 0 : 1;
	lat_dec = lat_1;
	long_dec = long_1;	
	switch (pn_id) {
		case 1 :
			if (window.preenchePainel1) {preenchePainel1();}
		break;
	}
}
//Fim de Preenche Painel


//Preenche Rodape
function preencheRodape () {
	if (document.configura.ativa_format.checked) {
		var frame = parent.frames["frame_menu"];
		ent = false;
		decSep = frame.format_decSep;
		milSep = frame.format_milSep;
		tipo = 0;
		cdec = 20;	
	}
	else {		
		cdec = (document.configura.opdec.checked)? document.configura.dec.value : 20;
		decSep = document.configura.dec_sep.options[document.configura.dec_sep.selectedIndex].value;
		milSep = document.configura.mil_sep.checked;
	}		
	obj_format.setFormat(decSep, milSep, tipo, cdec, "auto");			
	
	var lt_txt = (lat_1<0)? "&nbsp;&nbsp;S&nbsp;" : "&nbsp;&nbsp;N&nbsp;";
	var lg_txt = (long_1<0)? "W&nbsp;" : "E&nbsp;";	
	var coordFormat = document.getElementById("coord_format");
	if (coordFormat.value=="0") {		
		obj_format.setValue(lat_1);
		var lat_format = obj_format.getValueFormat();
		obj_format.setValue(long_1);
		var lg_format = obj_format.getValueFormat();
		document.getElementById("map_rodape_coord").innerHTML = "&nbsp;&nbsp;" + lat_format + "&deg;&nbsp;&nbsp;&nbsp;" + lg_format + "&deg;";
	}
	else if (coordFormat.value=="1") {
		document.getElementById("map_rodape_coord").innerHTML = lt_txt + ConvertCoord(Math.abs(lat_1), 1, obj_format) + "&nbsp;&nbsp;&nbsp;" + lg_txt + ConvertCoord(Math.abs(long_1), 1, obj_format);
	}
	else if (coordFormat.value=="2") {
		document.getElementById("map_rodape_coord").innerHTML = lt_txt + ConvertCoord(Math.abs(lat_1), 2, obj_format) + "&nbsp;&nbsp;&nbsp;" + lg_txt + ConvertCoord(Math.abs(long_1), 2, obj_format);
	}
}
//Fim de Preenche Rodape


//Altera Mapa
var marcador;
var ponto = null;
var hasMarker = false;
var event_func;
var event_func_marker;
var title2 = "Desativar Marca" + unescape("\u00E7") + unescape("\u00E3") + "o de Coordenadas";
var title_prov = "";
function altMap(comp) {
	if (GBrowserIsCompatible() && pg_carreg) {
		var comp_id = comp.id;
		switch (comp_id) {
			case "reset" : //Restaura Centro e Zoom Default do Mapa				
				map.setCenter(center_default, zoom_default);
				document.mapa.zoom_level.selectedIndex = zoom_default;
				map.setMapType(G_NORMAL_MAP);
				document.getElementById("end_geocoder").style.color = "#909090";
				document.getElementById("end_geocoder").value = "Digite o Local Desejado";
				campoGeocodeIniciado = false;
				map.enableDoubleClickZoom();
				ponto = null;
				if (painel_id==1) {
					var marca_border = document.getElementById("marca_coord").style.borderStyle;
					if (marca_border.indexOf("inset")!=-1) {
						document.getElementById("marca_coord").style.borderStyle = "";
						document.getElementById("marca_coord").style.backgroundColor = "#ffffff";
						altMap(document.getElementById("marca_coord"));
					}
					if (document.mapa.calc_dist_op.checked) {document.getElementById("calc_dist_op").click();}
					if (document.mapa.calc_dist_op2.checked) {document.getElementById("calc_dist_op2").click();}
					preenchePainel (painel_id);
				}				
			break;
			case "marca_coord" : //Altera Evento onClick para Inluir Marca e retornar as Coordenadas					
				var comp_border = comp.style.borderStyle;
				var win_txt = "";
				if (comp_border.indexOf("inset")!=-1) {
					if (event_map_click) {GEvent.removeListener(event_map_click);}
					event_func = GEvent.addListener(map, "click", function(mark, point) {  
						if (mark) {														
							map.removeOverlay(mark);
							hasMarker = false;
							if (document.mapa.calc_dist_op.checked) {
								pt_ant = "";
								document.mapa.dist.value = "";
								if (document.configura.apaga_dist_acum.checked) {
									dist_m_acum = 0;
									document.mapa.dist_acum.value = "";
								}
							}
						} 
						else {
							map.disableDoubleClickZoom();
							if (hasMarker) {map.removeOverlay(marcador);}															
							var icon = new GIcon();
							icon.image = "../imagens/arrow.png"							
							icon.shadow = "http://www.google.com/mapfiles/shadow50.png";
							icon.iconSize = new GSize(24, 24);
							icon.shadowSize = new GSize(37, 24);
							icon.iconAnchor = new GPoint(12, 23);							
							marcador = new GMarker(point, {draggable: true, icon: icon, bounceGravity: 2, dragCrossMove: true});
							map.addOverlay(marcador);
							hasMarker = true;
							if (!document.mapa.calc_dist_op.checked) {map.setCenter(point);}							
							if (painel_id==1) {preenchePainel (painel_id);}									
							event_func_marker = GEvent.addListener(marcador, "dragend", function() {
								if (document.mapa.calc_dist_op.checked) {
									var dist_unid_elem = document.getElementById("dist_unid");
									var fator_conv = parseFloat(dist_unid_elem.options[dist_unid_elem.selectedIndex].value);
									var pt_atual = new GLatLng(marcador.getPoint().y, marcador.getPoint().x);
									if (pt_ant!="") {
										dist_m = pt_atual.distanceFrom(pt_ant);										
										dist_m = obj_format.setValue(dist_m, false, decSep, milSep, tipo, "0");										
										if (dist_unid_elem.selectedIndex!=0) {											
											var dist = dist_m * fator_conv;
											obj_format.setValue(dist, false, decSep, milSep, tipo, "3");
										}
										document.mapa.dist.value = obj_format.getValueFormat() + unid_dist;
										if (document.configura.calc_dist_acum.checked) {
											dist_m_acum += dist_m;
											obj_format.setValue(dist_m_acum, false, decSep, milSep, tipo, "0");											
											if (dist_unid_elem.selectedIndex!=0) {												
												var dist_acum = dist_m_acum * fator_conv;
												obj_format.setValue(dist_acum, false, decSep, milSep, tipo, "3");
											}											
											document.mapa.dist_acum.value = obj_format.getValueFormat() + unid_dist;
										}										
										if (document.configura.linha_dist.checked) {
											var polyline = new GPolyline([pt_ant, pt_atual], cor_lnh, larg_lnh, opac_lnh);
											map.addOverlay(polyline);
										}										
										pt_ant = pt_atual;																										
									}
									else {									
										pt_ant = pt_atual;
										document.mapa.dist.value = "0";
									}
								}
								else {														
									map.setCenter(marcador.getPoint());
									if (painel_id==1) {preenchePainel (painel_id);}
								}
								
							});
							hasOverlay = true;
							document.getElementById("placeMarks").style.display = "none";																			
						}
					});
					title_prov = document.getElementById("marcaCoord").title;
					document.getElementById("marcaCoord").title = title2;
					document.getElementById("marca_coord").title = title2;					
				}
				else {
					GEvent.removeListener(event_func);
					if (event_func_marker) {GEvent.removeListener(event_func_marker);}
					document.getElementById("marcaCoord").title = title_prov;
					document.getElementById("marca_coord").title = title_prov;
					if (hasMarker) {map.removeOverlay(marcador);}
					map.enableDoubleClickZoom();
					event_map_click = GEvent.addListener(map, "click", function(mark, point) {  
						if (mark) {
							map.setCenter(mark.getPoint());								
							if (painel_id==1) {preenchePainel (painel_id);}
						}
					});
				}
			break;
			case "apaga_marcas" : //Apaga as Marcas do Mapa
				if (hasOverlay) {
					var apagar = confirm("Deseja apagar todas as marcas do mapa?");				
					if (apagar) {
						map.clearOverlays();
						hasOverlay = false;
						hasMarker = false;
					}
				}
			break;			
			case "busca_geocode" : //Localiza Endereços
				var geocoder = new GClientGeocoder();
				function showAddress(address) {
					geocoder.getLocations(address, function(resposta) {						
						if (!resposta || resposta.Status.code!=200) {
							alert("O endere" + unescape("\u00E7") + "o: \"" + address + "\"\n" + unescape("n\u00E3o") + " foi encontrado");
						}
						else {													
							buscaGeocode = true;							
							if (resposta.Placemark.length==1) {							
								var placemark_0 = resposta.Placemark[0];
								ponto = new GLatLng(placemark_0.Point.coordinates[1], placemark_0.Point.coordinates[0]);							
								map.setCenter(ponto);		
								if (painel_id==1) {preenchePainel (painel_id);}
								var marker = new GMarker(ponto);
								map.addOverlay(marker);
								document.mapa.zoom_level.selectedIndex = map.getZoom();
								hasOverlay = true;							
								GEvent.addListener(marker, "dblclick", function() {							
									map.removeOverlay(marker);
									ponto = null;
								});
								document.getElementById("placeMarks").style.display = "none";
							}
							else {
								var places_txt = "";
								for (var i = 0; i < resposta.Placemark.length; i++) {
									//var pto = new GLatLng(resposta.Placemark[i].Point.coordinates[1], resposta.Placemark[i].Point.coordinates[0]);

									places_txt += "<span style='font-size: 11px; color: #0000ff'>" + i + "-</span>&nbsp;<a href='javascript: localizaPlace(" + resposta.Placemark[i].Point.coordinates[1] + "," + resposta.Placemark[i].Point.coordinates[0] + ", \"" + escape(resposta.Placemark[i].address) + "\")' style='font-size: 11px;'>";
									places_txt += resposta.Placemark[i].address + "</a><br />";								
								}
								document.getElementById("placeMarksText").innerHTML = places_txt;
								document.getElementById("placeMarks").style.display = "block";
							}
						}
					});
				}
				if (document.mapa.end_geocoder.value!="" && campoGeocodeIniciado) {
					showAddress(document.mapa.end_geocoder.value);					
				}				
			break;
			case "busca_coord" : //Localiza Coordenadas	
			var lat = lat_dec;
			var longi = long_dec;
			if (!isNaN(lat) && !isNaN(longi)) {			
				var pt = new GLatLng(lat, longi);
				map.setCenter(pt);
				var marker_coord = new GMarker(pt);
				map.addOverlay(marker_coord);
				hasOverlay = true;
				GEvent.addListener(marker_coord, "dblclick", function() {							
					map.removeOverlay(marker_coord);
				});
			}
			break;
			case "apaga_dist" : //Apaga as Marcas do Mapa
				if (dist_m_acum!=0) {
					var apagar = confirm("Deseja apagar a dist" + unescape("\u00E2") + "ncia acumulada?");				
					if (apagar) {
						dist_m_acum = 0;
						document.mapa.dist_acum.value = "";
					}
				}
			break;
			case "mostra_link" : //Constroi Link Permanente				
				var urlPerm = "http://www.webcalc.com.br/viagens/mapas_wbc.html?"
				var exp_stg = (mapaExpandido)? "1" : "0";
				var ativa_kml = document.getElementById("ativa_kml").checked;
				var kml_url_stg = document.configura.kml_url.value;				
				urlPerm += "pi=" + painel_id;
				urlPerm += "&ld=" + lat_1;
				urlPerm += "&lg=" + long_1;
				urlPerm += "&zm=" + document.mapa.zoom_level.selectedIndex;
				urlPerm += "&tp=" + map.getCurrentMapType().getUrlArg();
				if (ponto!=null) {
					urlPerm += "&pt=" + ponto.y + "_" + ponto.x;
				}				
				if (hasMarker) {					
					urlPerm += "&mk=" + marcador.getPoint().y + "_" + marcador.getPoint().x;
				}				
				if (painel_id==2 && de_stg!="" && para_stg!="") {					
					var rt_stg = de_stg + dest_int_stg + " to:" + para_stg;
					rt_stg = rt_stg.replace(/\s/g, "_");					
					urlPerm += "&rt=" + rt_stg;
					urlPerm += "&op=" + opt;
				}																
				urlPerm += "&ex=" + exp_stg;
				if (ativa_kml && kml_url_stg!="" && de_stg=="") {			
					urlPerm += "&km=" + kml_url_stg; //Manter sempre como ultimo parametro
				}				
				document.getElementById("painel_link").style.visibility = "visible";
				document.getElementById("link_url").value = urlPerm;
			break;			
			case "desfaz" : //Volta na Pilha de Mapas			
				if ((document.getElementById("desfazer").src).indexOf("_off")==-1) {				
					movePilha = true;
					pilha_index--;					
					if (pilha_index==0) {						
						var bt = document.getElementById("desfaz");
						bt.style.paddingTop = "4px";
						bt.style.paddingLeft = "2px";
						bt.style.marginBottom = "0px";
						if (is.fox) {bt.style.paddingBottom = "1px";}
						bt.style.borderWidth = "0px";
						bt.style.borderStyle = "";
						bt.style.backgroundColor = "#ffffff";
					}
					parent.frames["frame_menu"].pilhaIndex = pilha_index;					
					lat_1 = (!isNaN(parseFloat(pilhaMapas[pilha_index][0])))? parseFloat(pilhaMapas[pilha_index][0]) : lat_1;
					long_1 = (!isNaN(parseFloat(pilhaMapas[pilha_index][1])))? parseFloat(pilhaMapas[pilha_index][1]) : long_1;
					var pilhaCenter = new GLatLng(lat_1, long_1);
					var pilhaZoom = (!isNaN(parseFloat(pilhaMapas[pilha_index][2])))? parseFloat(pilhaMapas[pilha_index][2]) : zoom_default;					
					map.setCenter(pilhaCenter, pilhaZoom);
					var pilhaTp = (pilhaMapas[pilha_index][3]!="")? pilhaMapas[pilha_index][3] : "m";
					if (pilhaTp=="m") {
						map.setMapType(G_NORMAL_MAP);			
					}
					if (pilhaTp=="k") {
						map.setMapType(G_SATELLITE_MAP);			
					}
					else if (pilhaTp=="h") {
						map.setMapType(G_HYBRID_MAP);
					}
					else if (pilhaTp=="p") {
						map.setMapType(G_PHYSICAL_MAP);
					}
					
					if (document.getElementById("end_geocoder").value!="") {
						document.getElementById("end_geocoder").style.color = "#000000";
						campoGeocodeIniciado = true;
					}
					document.getElementById("end_geocoder").value = pilhaMapas[pilha_index][4];
					document.getElementById("desfazer").src = (pilha_index>0)? "../imagens/desfazer_on.gif" : "../imagens/desfazer_off.gif";
					document.getElementById("refazer").src = (pilha_index!=pilhaMapas.length-1)? "../imagens/refazer_on.gif" : "../imagens/refazer_off.gif";					
					movePilha = false;
				}
			break;
			case "refaz" : //Avança na Pilha de Mapas			
				if ((document.getElementById("refazer").src).indexOf("_off")==-1) {				
					movePilha = true;					
					pilha_index++;					
					if (pilha_index==pilhaMapas.length-1) {						
						document.getElementById("refazer").src = "../imagens/refazer_off.gif";						
						var bt = document.getElementById("refaz");
						bt.style.paddingTop = "4px";
						bt.style.paddingLeft = "2px";
						bt.style.marginBottom = "0px";
						if (is.fox) {bt.style.paddingBottom = "1px";}
						bt.style.borderWidth = "0px";
						bt.style.borderStyle = "";
						bt.style.backgroundColor = "#ffffff";
					}
					parent.frames["frame_menu"].pilhaIndex = pilha_index;					
					lat_1 = (!isNaN(parseFloat(pilhaMapas[pilha_index][0])))? parseFloat(pilhaMapas[pilha_index][0]) : lat_1;
					long_1 = (!isNaN(parseFloat(pilhaMapas[pilha_index][1])))? parseFloat(pilhaMapas[pilha_index][1]) : long_1;
					var pilhaCenter = new GLatLng(lat_1, long_1);
					var pilhaZoom = (!isNaN(parseFloat(pilhaMapas[pilha_index][2])))? parseFloat(pilhaMapas[pilha_index][2]) : zoom_default;
					map.setCenter(pilhaCenter, pilhaZoom);
					var pilhaTp = (pilhaMapas[pilha_index][3]!="")? pilhaMapas[pilha_index][3] : "m";
					if (pilhaTp=="m") {
						map.setMapType(G_NORMAL_MAP);			
					}
					if (pilhaTp=="k") {
						map.setMapType(G_SATELLITE_MAP);			
					}
					else if (pilhaTp=="h") {
						map.setMapType(G_HYBRID_MAP);
					}
					else if (pilhaTp=="p") {
						map.setMapType(G_PHYSICAL_MAP);
					}				
					
					if (document.getElementById("end_geocoder").value!="") {
						document.getElementById("end_geocoder").style.color = "#000000";
						campoGeocodeIniciado = true;
					}					
					document.getElementById("end_geocoder").value = pilhaMapas[pilha_index][4];
					document.getElementById("desfazer").src = (pilha_index>0)? "../imagens/desfazer_on.gif" : "../imagens/desfazer_off.gif";
					document.getElementById("refazer").src = (pilha_index!=pilhaMapas.length-1)? "../imagens/refazer_on.gif" : "../imagens/refazer_off.gif";
					movePilha = false;
				}			
			break;
			case "abre_config" : //Abre Janela de Configurações
				var configDisplay = document.getElementById("config").style.display;							
				switch (painel_id) {
					case 1 :			
						document.getElementById("config_dist").style.display = "block";
						document.getElementById("config_roteiro").style.display = "none";
					break;
					case 2 :			
						document.getElementById("config_dist").style.display = "none";
						document.getElementById("config_roteiro").style.display = "block";												
					break;
				}				
				document.getElementById("config").style.display = (configDisplay=="block")? "none" : "block";			
			break;
			case "expande_mapa" : //Expande/Reduz Mapa						
				if (document.configura.nova_janela.checked && !mapaExpandido) {					
					abreJanMapa();
				}
				else {					
					var divPainel = document.getElementById("painel");
					var divSubPainel = document.getElementById("sub_painel");
					var divBarra = document.getElementById("barra");			
					var divMapa = document.getElementById("map");
					var divConfig = document.getElementById("config");
					var divPlaceMarks = document.getElementById("placeMarks");
					var elAd_1 = document.getElementById("ad_1");
					var elAd_2 = document.getElementById("ad_2");
					var elAd_3 = document.getElementById("ad_3");
					var elAd_4 = document.getElementById("ad_4");
					var mapWidth = window.screen.availWidth - 180;
					var mapCenter = map.getCenter();
					var divMapa_height = "458px";
					if (is.fox) {divMapa_height = "487px";}					
					mapaExpandido = (mapaExpandido)? false : true;										
					divPainel.style.visibility = (!mapaExpandido)? "visible" : "hidden";					
					elAd_1.style.display = (!mapaExpandido)? "block" : "none";
					elAd_2.style.display = (mapaExpandido)? "block" : "none";
					elAd_3.style.display = (mapaExpandido)? "block" : "none";
					elAd_4.style.display = (!mapaExpandido)? "block" : "none";
					divBarra.style.top = (!mapaExpandido)? divBarra_top : "43px";				
					divMapa.style.height = (!mapaExpandido)? "300px" : divMapa_height;			
					divMapa.style.width = (!mapaExpandido)? "468px" : mapWidth + "px";
					divMapa.style.top = (!mapaExpandido)? divMapa_top : "80px";						
					if (mapaExpandido) {
						document.getElementById("mira").style.top = (parseInt(divMapa.style.top) + parseInt(divMapa_height)/2 - 7) + "px";
						document.getElementById("mira").style.left = (9 + parseInt(divMapa.style.width)/2) + "px";
					}
					else {mira();}
					if (!is.chr) {map.checkResize();}				
					if (document.configura.ativa_control.checked) {
						map.setCenter(mapCenter);
						map.removeControl(viewMap_Control);
						map.removeControl(Map_Control);
						viewMap_Control = new GOverviewMapControl();
						map.addControl(viewMap_Control);
					}
					if (mapaExpandido) {							
						document.getElementById("expandeMapa").src = "../imagens/menos.gif";
						document.getElementById("expandeMapa").title = "Reduzir Mapa";
						document.getElementById("expande_mapa").title = "Reduzir Mapa";
						document.getElementById("altPainel").title = "Mostrar Painel";
						document.getElementById("alt_painel").style.visibility = "visible";										
						Map_Control = new GLargeMapControl();
						map.addControl(Scale_Control);
						divConfig.style.top = "78px";
						divPlaceMarks.style.top = "73px";					
					}
					else {
						document.getElementById("expandeMapa").src = "../imagens/mais.gif";
						document.getElementById("expandeMapa").title = "Ampliar Mapa";
						document.getElementById("expande_mapa").title = "Ampliar Mapa";
						document.getElementById("pn_expand_fundo").style.visibility = "hidden";						
						document.getElementById("alt_painel").style.visibility = "hidden";					
						Map_Control = new GSmallMapControl();
						map.removeControl(Scale_Control);					
						document.getElementById("alt_painel").style.top = "83px";
						document.getElementById("altPainel").src = "../imagens/expand.gif";
						divConfig.style.top = divConfig_top;
						divPlaceMarks.style.top = divPlaceMarks_top;
					}					
					if (document.configura.ativa_control.checked) {map.addControl(Map_Control);}
					if (is.chr) {							
						if (document.getElementById("config").style.display!="none") {							
							MudaBtVz(document.getElementById("abre_config"), 4);
							altMap(document.getElementById("abre_config"));
							MudaBtVz(document.getElementById("abre_config"), 3);
							MudaBtVz(document.getElementById("abre_config"), 2);
						}
						map.checkResize();
					}
				}
				if (document.getElementById("config").style.display!="none") {							
					MudaBtVz(document.getElementById("abre_config"), 4);
					altMap(document.getElementById("abre_config"));
					MudaBtVz(document.getElementById("abre_config"), 3);
					MudaBtVz(document.getElementById("abre_config"), 2);
				}
			break;			
			case "altPainel" : //Mostra/Esconde Painel				
				var altPainel = document.getElementById("altPainel");
				var divPainel = document.getElementById("painel");
				var divBarra = document.getElementById("barra");
				var divPnExpandFundo = document.getElementById("pn_expand_fundo");
				var divConfig = document.getElementById("config");
				var divPlaceMarks = document.getElementById("placeMarks");
				altPainel.src = ((altPainel.src).indexOf("expand")!=-1)? "../imagens/reduz.gif" : "../imagens/expand.gif";
				if ((altPainel.src).indexOf("expand")==-1) {				
					if (document.configura.ativa_control.checked) {map.removeControl(Map_Control);}				
					divPnExpandFundo.style.height = (is.fox)? "186px" : "159px";
					divPnExpandFundo.style.visibility = "visible";					
					divBarra.style.top = (is.fox)? "228px" : "200px";
					divPainel.style.visibility = "visible";
					document.getElementById("alt_painel").style.top = (is.fox)? "268px" : "241px";
					altPainel.title = "Esconder Painel";
					divConfig.style.top = divConfig_top;
					divPlaceMarks.style.top = divPlaceMarks_top;
				}
				else {
					if (document.configura.ativa_control.checked) {
						Map_Control = new GLargeMapControl();
						map.addControl(Map_Control);
					}
					divBarra.style.top = "43px";
					divPainel.style.visibility = "hidden";
					divPnExpandFundo.style.visibility = "hidden";
					document.getElementById("alt_painel").style.top = "83px";
					altPainel.title = "Mostrar Painel";
					divConfig.style.top = "78px";
					divPlaceMarks.style.top = "73px";					
				}
			break;
			case "ativa_control" : //Alterna Controles							
				if (!comp.checked) {
					map.removeControl(Map_Control);
					map.removeControl(TypeControl);
				}
				else {
					if ((document.getElementById("altPainel").src).indexOf("expand")!=-1) {
						if (mapaExpandido) {Map_Control = new GLargeMapControl();}
						map.addControl(Map_Control);
					}
					map.addControl(TypeControl);
				}
			break;
			case "calc_dist_acum" : //Ativa/Desativa Calculo da Distancia Acumulada							
				if (comp.checked) {
					if (document.mapa.calc_dist_op.checked || document.mapa.calc_dist_op2.checked) {
						document.getElementById("calc_op_txt_acum").style.color = "#000000";
						document.getElementById("dist_acum").style.backgroundColor = "";
						document.getElementById("dist_acum").style.color = "#000000";
					}
				}
				else {
					document.getElementById("calc_op_txt_acum").style.color = "#b4b4b4";	
					document.getElementById("dist_acum").style.backgroundColor = "#dddddd";
					document.getElementById("dist_acum").style.color = "#8f8f8f";
				}
			break;
			case "zera_odometro" :							
				preenchePainel2();
			break;
			case "imprime" :							
				if  (painel_id==1) {
				 alert("Falta desenvolver");
				}
				else if (painel_id==2) {
					abreJanImpRot();
				}
			break;
		}
	}
}
//Fim de Altera Mapa


//Altera Painel Auxiliar
var html_roteiro = "";
var left_dest = "205px";
var top_dest = "160px";
var left_roteiro = "60px";
var top_roteiro = "230px";
function alteraPainelAux(event) {
	var evento = event || window.event;
	var srcElem = evento.target || evento.srcElement;
	var dif_left, dif_top;
	switch (srcElem.id) {
		case "painel_aux_td_title" :			
			if (evento.type=="mousedown") {
				document.getElementById("painel_aux_barra").style.cursor = "move";
				dif_left = evento.clientX - parseInt(document.getElementById("painel_aux").style.left);
				dif_top = evento.clientY - parseInt(document.getElementById("painel_aux").style.top);
				if (document.addEventListener) {
					document.addEventListener("mousemove", mouseMovePainel, true);
					document.addEventListener("mouseup", mouseUpPainel, true);
				}
				else if (document.attachEvent) {
					document.attachEvent("onmousemove", mouseMovePainel);
					document.attachEvent("onmouseup", mouseUpPainel);
				}
			}
			if (evento.type=="mouseup") {
				document.getElementById("painel_aux_barra").style.cursor = "default";
			}
		break;
		case "painel_aux_td_fig" :			
			if (evento.type=="mousedown") {
				document.getElementById("painel_aux_barra").style.cursor = "move";
				dif_left = evento.clientX - parseInt(document.getElementById("painel_aux").style.left);
				dif_top = evento.clientY - parseInt(document.getElementById("painel_aux").style.top);
				if (document.addEventListener) {
					document.addEventListener("mousemove", mouseMovePainel, true);
					document.addEventListener("mouseup", mouseUpPainel, true);
				}
				else if (document.attachEvent) {
					document.attachEvent("onmousemove", mouseMovePainel);
					document.attachEvent("onmouseup", mouseUpPainel);
				}
			}
			if (evento.type=="mouseup") {
				document.getElementById("painel_aux_barra").style.cursor = "default";
			}
		break;
		case "btPainelAuxBarra" :
			if (evento.type=="click") {
				fecharPainelAux();
			}
		break;
		case "alt_painel_aux" :
			if (document.getElementById("roteiro_dist").innerHTML=="") {
				preenchePainel2();
			}
			else if (document.getElementById("roteiro_dist").innerHTML!="") {
				if (document.getElementById("painel_aux").title!="Roteiro") {
					document.getElementById("painel_aux_conteudo").innerHTML = html_roteiro;
				}			
				document.getElementById("painel_aux").className = "roteiro";
				document.getElementById("painel_aux").style.left = left_roteiro;
				document.getElementById("painel_aux").style.top = top_roteiro;			
				document.getElementById("painel_aux").title = "Roteiro";
				document.getElementById("painel_aux_td_title").innerHTML = "WebCalc Mapas - Roteiro";
				if (is.ie7) {
					document.getElementById("painel_aux_barra").style.width = "99%";
					document.getElementById("painel_aux").style.height = "452px";
				}
				document.getElementById("painel_aux").style.display = "block";
				html_roteiro = "";
			}
			MudaBtVz(srcElem, 4);
		break;
		case "altPainelAux" :
			if (document.getElementById("roteiro_dist").innerHTML=="") {
				preenchePainel2();
			}
			if (document.getElementById("roteiro_dist").innerHTML!="") {
				if (document.getElementById("painel_aux").title!="Roteiro") {
					document.getElementById("painel_aux_conteudo").innerHTML = html_roteiro;
				}
				document.getElementById("painel_aux").className = "roteiro";
				document.getElementById("painel_aux").style.left = left_roteiro;
				document.getElementById("painel_aux").style.top = top_roteiro;
				document.getElementById("painel_aux").title = "Roteiro";
				document.getElementById("painel_aux_td_title").innerHTML = "WebCalc Mapas - Roteiro";
				if (is.ie7) {
					document.getElementById("painel_aux_barra").style.width = "99%";
					document.getElementById("painel_aux").style.height = "452px";
				}
				document.getElementById("painel_aux").style.display = "block";
				html_roteiro = "";
			}
			document.getElementById("painel_aux").style.width = "550px";
			MudaBtVz(document.getElementById("alt_painel_aux"), 4);
		break;
		case "dest_inter" :
			if (document.getElementById("painel_aux").title=="Roteiro") {
				html_roteiro = document.getElementById("painel_aux_conteudo").innerHTML;
			}
			mudaHtmlPainelAux_DestInt();
			document.getElementById("painel_aux").className = "dest_int";
			document.getElementById("painel_aux").style.left = left_dest;
			document.getElementById("painel_aux").style.top = top_dest;
			document.getElementById("painel_aux").title = "Destinos";
			document.getElementById("painel_aux_td_title").innerHTML = "WebCalc Mapas - Destinos Intermedi&aacute;rios";
			if (is.fox) {
				document.getElementById("painel_aux").style.height = "275px";
				document.getElementById("painel_aux").style.width = "452px";
			}
			if (is.ie7) {
				document.getElementById("painel_aux_barra").style.width = "99%";
				document.getElementById("painel_aux").style.height = "245px";
				document.getElementById("painel_aux").style.width = "470px";
			}
			document.getElementById("painel_aux").style.display = "block";			
		break;		
	}
		
	function mouseMovePainel() {	
		document.getElementById("painel_aux").style.left = (evento.clientX - dif_left) + "px";
		document.getElementById("painel_aux").style.top = (evento.clientY - dif_top) + "px";
	}
	
	function mouseUpPainel() {
		if (document.removeEventListener) {
			document.removeEventListener("mouseup", mouseUpPainel, true);
			document.removeEventListener("mousemove", mouseMovePainel, true);					
		}
		else if (document.detachEvent) {
			document.detachEvent("onmouseup", mouseUpPainel);
			document.detachEvent("onmousemove", mouseMovePainel);					
		}		
	}
}

function fecharPainelAux() {
	document.getElementById("painel_aux").style.display = "none";
	var left_coord = document.getElementById("painel_aux").style.left;
	var top_coord = document.getElementById("painel_aux").style.top;
	switch (document.getElementById("painel_aux").title) {
		case "Destinos" :			
			left_dest = left_coord;
			top_dest = top_coord;
		break;
		case "Roteiro" :			
			left_roteiro = left_coord;
			top_roteiro = top_coord;
		break;
	}
}
//Fim de Altera Painel Auxiliar


//Manipula Painel Link
function linkURL(event) {	
	var evento = event || window.event;
	var srcElem = evento.target || evento.srcElement;	
	switch (srcElem.id) {
		case "link_url" :			
			document.getElementById("link_url").select();
		break;
		case "btPainelLink" :			
			document.getElementById("painel_link").style.visibility = "hidden";
		break;
		case "abre_link" :			
			var link_url = document.getElementById("link_url").value;
			window.open(link_url);
		break;
	}
}
//Fim de Manipula Painel Link


//Verificação de Campos
function verifcampo(camp){
	if (!pg_carreg) {
		camp.value = "";		
		var txt_alert = "P" + unescape("\u00E1") + "gina Carregando...\n\nAguardar o carregamento total da p" + unescape("\u00E1") + "gina e tentar novamente";		
		alert (txt_alert);
		return;
	}
	
	var sinal = "";
	var campovalor = "";
	var coordFormatID = document.configura.coord_format.selectedIndex;
	
	if (camp.name=="kmporhora" || camp.name=="kmporlitro" || camp.name=="valorporlitro") {		
		obj_format.setValue(camp.value, true, decSep, milSep, 0, "2");		 
		camp.value = obj_format.getValueFormat();				
		if (camp.value=="") {return;}
		preenchePainel2();
		return;
	}
			
	if (camp.name=="lat_index") {
		sinal = (document.mapa.lat_index.selectedIndex==0)? "" : "-";
		lat_dec = parseFloat((sinal + Math.abs(lat_dec)));
		if (coordFormatID==0) {
			campovalor = obj_format.setValue(document.mapa.lat_1.value, false, decSep, false, tipo, cdec);		
			obj_format.setValue(Math.abs(campovalor));
			document.mapa.lat_1.value = sinal + obj_format.getValueFormat();			
		}
		else {
			if ((document.mapa.lat_1.value).indexOf("-")!=-1) {
				document.mapa.lat_1.value = (document.mapa.lat_1.value).replace(/-/, "");
			}
			else {
				document.mapa.lat_1.value = "-" + document.mapa.lat_1.value;
			}
		}
		return;
	}	
	if (camp.name=="long_index") {
		sinal = (document.mapa.long_index.selectedIndex==0)? "" : "-";
		long_dec = parseFloat((sinal + Math.abs(long_dec)));
		if (coordFormatID==0) {
			campovalor = obj_format.setValue(document.mapa.long_1.value, false, decSep, false, tipo, cdec);
			obj_format.setValue(Math.abs(campovalor));
			document.mapa.long_1.value = sinal + obj_format.getValueFormat();			
		}
		else {
			if ((document.mapa.long_1.value).indexOf("-")!=-1) {
				document.mapa.long_1.value = (document.mapa.long_1.value).replace(/-/, "");
			}
			else {
				document.mapa.long_1.value = "-" + document.mapa.long_1.value;
			}
		}
		return;
	}
	
	campovalor = camp.value;
	if (coordFormatID==0) {
		campovalor = obj_format.setValue(camp.value, true);	
	}
	else {		
		var val_g = "";
		var val_m = "";
		var val_s = "";
		var sep_g_id, sep_m_id, sep_s_id;		
		var coord_sfx = campovalor.match(/[WOELNS]/gi);
		if (coord_sfx!=null && campovalor.indexOf(coord_sfx)==(campovalor.length-1)) {			
			if (camp.name=="lat_1") {
				var negativo = (coord_sfx[0]=="S" || coord_sfx[0]=="s")? true : false;
			}
			if (camp.name=="long_1") {
				var negativo = (coord_sfx[0]=="W" || coord_sfx[0]=="w" || coord_sfx[0]=="O" || coord_sfx[0]=="o")? true : false;
			}			
			
			if (negativo) {
				campovalor = (campovalor.charAt(0)=="-")? campovalor : "-" + campovalor;
			}
			else {
				campovalor = (campovalor.charAt(0)=="-")? campovalor.replace(/-/, "") : campovalor;
			}		
		}		
		campovalor = campovalor.replace(/[WOELNS]/gi, "");		
		var valida_stg = campovalor.match(/[^\s\d\u00B0\u0027\u0022\u00B4\u02B9\u02BA\u2032\u2033\,\.:-]/g);		
		if (valida_stg!=null) {
			camp.value = "";
			alert("Entrada Inv" + unescape("\u00E1") + "lida");
			return;
		}
		else {
			var campovalor_stg = campovalor.replace(/[^\d\u00B0\u0027\u0022\u00B4\u02B9\u02BA\u2032\u2033\,\.:-]/g, "");
			campovalor_stg = campovalor.replace(/\,/, ".");			
			var separadores = campovalor_stg.replace(/[^\u00B0\u0027\u0022\u00B4\u02B9\u02BA\u2032\u2033:]/g, "");
			if (separadores.indexOf(":")!=-1) {
				var valida_sep = separadores.match(/[^:]/g);
				if (valida_sep!=null) {
					camp.value = "";
					alert("Entrada Inv" + unescape("\u00E1") + "lida");
					return;
				}
				
				var cont = 1;
				for (var i=0; i<campovalor_stg.length; i++) {
					if (cont==1) {
						if (campovalor_stg.charAt(i)!=":") {
							val_g += campovalor_stg.charAt(i);
						}
						else {
							cont++;
							continue;
						}
					}					
					if (cont==2) {
						if (campovalor_stg.charAt(i)!=":") {
							val_m += campovalor_stg.charAt(i);
						}
						else {
							cont++;
							continue;
						}
					}
					if (cont==3) {
						if (campovalor_stg.charAt(i)!=":") {
							val_s += campovalor_stg.charAt(i);
						}
					}
				}				
			}
			else {
				sep_g_id = campovalor_stg.search(/\u00B0/);
				sep_m_id = campovalor_stg.search(/[\u0027\u00B4\u02B9\u2032]/g);
				sep_s_id = campovalor_stg.search(/[\u0022\u02BA\u2033]/g);
				sep_m_id = (sep_m_id!=-1)? sep_m_id : 1000;
				sep_s_id = (sep_s_id!=-1)? sep_s_id : 10000;								
				if (sep_g_id>sep_m_id || sep_g_id>sep_s_id || sep_m_id>sep_s_id) {
					camp.value = "";
					alert("Entrada Inv" + unescape("\u00E1") + "lida");
					return;
				}
				val_g = (sep_g_id!=-1)? campovalor_stg.slice(0, sep_g_id) : "";
				val_m = (sep_m_id!=1000)? campovalor_stg.slice(sep_g_id+1, sep_m_id) : "";
				val_s = (sep_s_id!=10000)? campovalor_stg.slice(sep_m_id+1, sep_s_id) : "";												
			}			
		}
		
		val_g = (val_g!="")? parseInt(val_g) : 0;
		val_m = (val_m!="")? parseInt(val_m) : 0;
		val_s = (val_s!="")? parseFloat(val_s) : 0;
		if (val_m>60 || val_s>60 || val_m<0 || val_s<0 || (val_m==60 && val_s!=0)) {
			camp.value = "";
			alert("Entrada Inv" + unescape("\u00E1") + "lida");
			return;
		}		
		var val_s_dec = val_s/60;
		var val_m_dec = (val_m + val_s_dec)/60;		
		var val_g_dec = Math.abs(val_g) + val_m_dec;
		val_g_dec = (val_g<0)? (-1*val_g_dec) : val_g_dec;
		campovalor = obj_format.setValue(val_g_dec, false);			
	}	
	
	if (camp.name=="lat_1") {
		if (campovalor<-90 || campovalor>90) {
			camp.value = "";
			alert ("A Latitude deve ser um valor entre -90 e +90 graus");
			return;
		}
		else {
			document.mapa.lat_index.selectedIndex = (campovalor>=0)? 0 : 1;
			lat_dec = campovalor;
		}
	}	
	if (camp.name=="long_1") {
		if (campovalor<-180 || campovalor>180) {
			camp.value = "";
			alert ("A Longitude deve ser um valor entre -180 e +180 graus");
			return;
		}
		else {
			document.mapa.long_index.selectedIndex = (campovalor>=0)? 0 : 1;
			long_dec = campovalor;
		}
	}
		
	if (coordFormatID==0) {	
		camp.value = obj_format.getValueFormat();
	}
	else {
		obj_format.setValue(val_s, false);
		var val_g_txt = (val_g!=0)? val_g : "0";
		var val_m_txt = (val_m!=0)? (val_m + "\'") : ((val_s!=0)? "0\'" : "");		
		var val_s_txt = (val_s!=0)? (obj_format.getValueFormat() + "\"") : "";
		camp.value = val_g_txt + unescape("\u00B0") + " " + val_m_txt + " " + val_s_txt;
	}
}
//Fim da Verificação de Campos


//Ativa Cálculo de Distâncias
function ativaCalcDist(cbx) {	
	if (painel_id==1 && cbx.id=="calc_dist_op2" && !cbx.checked && (pt_1!=null || pt_2!=null)) {
		var apagar = confirm("Deseja apagar o(s) ponto(s) definido(s)?");				
		if (apagar) {
			pt_1 = null;
			pt_2 = null;
			document.getElementById("pt1Txt").innerHTML = "&nbsp;";
			document.getElementById("pt2Txt").innerHTML = "&nbsp;";			
		}
	}
	
	var marca_border;
	if (!document.mapa.calc_dist_op.checked && !document.mapa.calc_dist_op2.checked) {
		document.getElementById("calc_op_txt").style.color = "#b4b4b4";	
		document.getElementById("dist").style.backgroundColor = "#dddddd";
		document.getElementById("calc_op_txt_acum").style.color = "#b4b4b4";	
		document.getElementById("dist_acum").style.backgroundColor = "#dddddd";
		document.getElementById("dist_acum").style.color = "#8f8f8f";
		document.mapa.dist.value = "";
		pt_ant = "";
		if (document.configura.apaga_dist_acum.checked) {
			dist_m_acum = 0;
			document.mapa.dist_acum.value = "";
		}
		marca_border = document.getElementById("marca_coord").style.borderStyle;
		if (marca_border.indexOf("inset")!=-1) {
			document.getElementById("marca_coord").style.borderStyle = "";
			document.getElementById("marca_coord").style.backgroundColor = "#ffffff";
			altMap (document.getElementById("marca_coord"));
		}		
	}
	else {
		document.getElementById("calc_op_txt").style.color = "#000000";
		document.getElementById("dist").style.backgroundColor = "";
		if (document.configura.calc_dist_acum.checked) {
			document.getElementById("calc_op_txt_acum").style.color = "#000000";
			document.getElementById("dist_acum").style.backgroundColor = "";
			document.getElementById("dist_acum").style.color = "#000000";
		}
		marca_border = document.getElementById("marca_coord").style.borderStyle;
		if (marca_border.indexOf("inset")==-1) {
			document.getElementById("marca_coord").style.paddingTop = "3px";	
			document.getElementById("marca_coord").style.paddingLeft = "1px";
			document.getElementById("marca_coord").style.marginBottom = "-1px";			
			if (is.fox) {
				document.getElementById("marca_coord").style.paddingRight = "3px";
				document.getElementById("marca_coord").style.paddingBottom = "0px";
			}
			document.getElementById("marca_coord").style.borderWidth = "1px";
			document.getElementById("marca_coord").style.borderStyle = "inset";
			document.getElementById("marca_coord").style.backgroundColor = "#eeeeee";			
			altMap (document.getElementById("marca_coord"));
			document.getElementById("marcaCoord").title = "Ativar Marca" + unescape("\u00E7") + unescape("\u00E3") + "o de Coordenadas";
		}
	}
	if (cbx.id=="calc_dist_op") {
		 document.mapa.calc_dist_op2.checked = false;
	}
	else if (cbx.id=="calc_dist_op2") {
		 document.mapa.calc_dist_op.checked = false;
	}
	if (document.mapa.calc_dist_op2.checked) {
		document.mapa.def_ponto1.disabled = false;
		document.mapa.def_ponto2.disabled = false;
		document.getElementById("pt1Txt").style.color = (pt_1!=null)? "#000000" : "#dddddd";
		document.getElementById("pt2Txt").style.color = (pt_2!=null)? "#000000" : "#dddddd";
	}
	else {
		document.mapa.def_ponto1.disabled = true;
		document.mapa.def_ponto2.disabled = true;
		document.getElementById("pt1Txt").style.color = "#dddddd";
		document.getElementById("pt2Txt").style.color = "#dddddd";
	}
}
//Fim de Ativa Cálculo de Distâncias


//Calcula Distância Entre Coordenadas
function distCoord(bt) {
	var btId = bt.id;
	if (btId=="def_ponto1") {
		pt_1 = map.getCenter();
		document.getElementById("pt1Txt").innerHTML = "Lat: " + document.mapa.lat_1.value + "<br />Long: " + document.mapa.long_1.value;
		document.getElementById("pt1Txt").style.color = "#000000";
		document.getElementById("pt2Txt").innerHTML = "&nbsp;";
		document.getElementById("pt2Txt").style.color = "#dddddd";
		document.mapa.dist.value = "0";
		pt_2 = null;		
	}
	if (btId=="def_ponto2") {
		if (pt_1==null) {
			alert ("Definir o Ponto 1");
			return;
		}		
		pt_2 = map.getCenter();
		document.getElementById("pt2Txt").innerHTML = "Lat: " + document.mapa.lat_1.value + "<br />Long: " + document.mapa.long_1.value;
		document.getElementById("pt2Txt").style.color = "#000000";
				
		var ponto_1 = new GLatLng(pt_1.y, pt_1.x);
		var ponto_2 = new GLatLng(pt_2.y, pt_2.x);		
		var dist_unid_elem = document.getElementById("dist_unid");
		var fator_conv = parseFloat(dist_unid_elem.options[dist_unid_elem.selectedIndex].value);		
		dist_m = ponto_2.distanceFrom(ponto_1);
		dist_m = obj_format.setValue(dist_m, false, decSep, milSep, tipo, "0");		
		if (dist_unid_elem.selectedIndex!=0) {
			var dist = dist_m * fator_conv;
			obj_format.setValue(dist, false, decSep, milSep, tipo, "3");
		}		
		document.mapa.dist.value = obj_format.getValueFormat() + unid_dist;
		
		if (document.configura.calc_dist_acum.checked) {
			dist_m_acum += dist_m;
			obj_format.setValue(dist_m_acum, false, decSep, milSep, tipo, "0");			
			if (dist_unid_elem.selectedIndex!=0) {
				var dist_acum = dist_m_acum * fator_conv;
				obj_format.setValue(dist_acum, false, decSep, milSep, tipo, "3");
			}			
			document.mapa.dist_acum.value = obj_format.getValueFormat() + unid_dist;
		}
		
		if (document.configura.linha_dist.checked) {
			var polyline = new GPolyline([ponto_1, ponto_2], cor_lnh, larg_lnh, opac_lnh);
			map.addOverlay(polyline);
			hasOverlay = true;
		}		
	}
}
//Fim de Calcula Distância Entre Coordenadas


//Localiza Placemarks
function localizaPlace(lt, lg, pm) {
	document.getElementById("placeMarks").style.display = "none";
	document.getElementById("end_geocoder").value = unescape(pm);
	pilhaMapas[pilha_index][4] = (campoGeocodeIniciado)? document.getElementById("end_geocoder").value : "";
	var pt = new GLatLng(lt, lg);	
	map.setCenter(pt);		
	if (painel_id==1) {preenchePainel (painel_id);}
	var marker = new GMarker(pt);
	map.addOverlay(marker);
	document.mapa.zoom_level.selectedIndex = map.getZoom();
	hasOverlay = true;							
	GEvent.addListener(marker, "dblclick", function() {							
		map.removeOverlay(marker);
	});
}
//Fim de Localiza Placemarks


//Janela de Configurações
var coord_format_ant = 1;
function fechaConfig() {
	document.getElementById("config").style.display = "none";
	var bt = document.getElementById("abre_config");
	bt.style.paddingTop = "4px";
	bt.style.paddingLeft = "2px";
	bt.style.marginBottom = "0px";
	if (is.fox) {bt.style.paddingBottom = "1px";}
	bt.style.borderWidth = "0px";
	bt.style.borderStyle = "";
	bt.style.backgroundColor = "#ffffff";
}

function alterFormat(form, elem) {
	if (elem.id=="nova_janela") {
		if (document.configura.nova_janela.checked && mapaExpandido) {					
			abreJanMapa();
		}		
		if (!form.nova_janela.checked) {
			document.getElementById("div_jan_alt_mapa").style.color = "#9e9e9e";
			form.jan_alt_mapa.disabled = true;
		}
		else {
			document.getElementById("div_jan_alt_mapa").style.color = "#000000";
			form.jan_alt_mapa.disabled = false;
		}
		return;			
	}
	if (elem.id=="ativa_format") {
		if (form.ativa_format.checked) {
			form.coord_format.selectedIndex = 1;
			document.getElementById("config_format").style.color = "#9e9e9e";
			form.coord_format.disabled = true;
			form.opdec.disabled = true;
			form.dec.disabled = true;
			form.dec_sep.disabled = true;
			form.mil_sep.disabled = true;		
		}
		else {
			form.coord_format.selectedIndex = coord_format_ant;
			document.getElementById("config_format").style.color = "#000000";
			form.coord_format.disabled = false;
			form.opdec.disabled = false;
			form.dec.disabled = false;
			form.dec_sep.disabled = false;
			form.mil_sep.disabled = false;
		}
		preenchePainel(painel_id);
		preencheRodape();
		alterFormat(form, form.dist_unid);
		if (painel_id==2) {preenchePainel2();}
		return;	
	}	
	if (elem.id=="coord_format") {coord_format_ant = form.coord_format.selectedIndex;}
	if (elem.id=="restaura") {
			form.coord_format[0].checked = true;
			form.coord_format[1].checked = false;
			document.getElementById("config_format").style.color = "#000000";
			form.coord_format[0].disabled = false;
			form.coord_format[1].disabled = false;
			form.opdec.disabled = false;
			form.dec.disabled = false;
			form.dec_sep.disabled = false;
			form.mil_sep.disabled = false;
			coord_format_ant = 1;
			cor_lnh = "#FF0000"
			opac_lnh = 0.5;
			larg_lnh = 5;
			document.getElementById("cor_lnh_img").style.visibility = "visible";
			document.getElementById("cor_lnh_a").style.cursor = "hand";
			document.getElementById("cor_lnh_a").onclick = function() {return true;};
			document.getElementById("format_lnh").style.color = "#000000";
			document.getElementById("cor_lnh").style.backgroundColor = cor_lnh;
			document.getElementById("cor_lnh").style.color = cor_lnh;
			form.largura_lnh.disabled = false;
			form.opacidade_lnh.disabled = false;
			document.getElementById("kml_url").disabled = true;
			document.getElementById("carregar_url").disabled = true;
			if (kml_overlay!=null) {
			 map.removeOverlay(kml_overlay);
			 kml_overlay = null;
			}			
			mira();			
			if (painel_id==2 && (window.preenchePainel2)) {preenchePainel2();}			
	}
	if (elem.id=="dist_unid") {							
		if (painel_id==1) {
			switch (elem.selectedIndex) {
				case 0 :			
					unid_dist = " m";
				break;
				case 1 :			
					unid_dist = " km";
				break;
				case 2 :			
					unid_dist = " mi";
				break;
				case 3 :			
					unid_dist = " miN";
				break;
			}
			
			var fator_conv = parseFloat(form.dist_unid.options[form.dist_unid.selectedIndex].value);
			if (document.mapa.dist.value!="") {
				obj_format.setValue(dist_m, false, decSep, milSep, tipo, "0");
				if (form.dist_unid.selectedIndex!=0) {
					var dist = dist_m * fator_conv;
					obj_format.setValue(dist, false, decSep, milSep, tipo, "3");
				}
				document.mapa.dist.value = obj_format.getValueFormat() + unid_dist;
			}
			if (document.mapa.dist_acum.value!="") {
				obj_format.setValue(dist_m_acum, false, decSep, milSep, tipo, "0");											
				if (form.dist_unid.selectedIndex!=0) {
					var dist_acum = dist_m_acum * fator_conv;
					obj_format.setValue(dist_acum, false, decSep, milSep, tipo, "3");
				}											
				document.mapa.dist_acum.value = obj_format.getValueFormat() + unid_dist;
			}		
		}
		if (painel_id==2) {preenchePainel2();}		
	}
	if (elem.id=="dec_sep" || elem.id=="mil_sep") {
		decSep = form.dec_sep.options[form.dec_sep.selectedIndex].value;
		milSep = form.mil_sep.checked;
		alterFormat(form, form.dist_unid);
		return;
	}
	if (elem.id=="linha_dist") {		
		if (!form.linha_dist.checked) {
			document.getElementById("cor_lnh_img").style.visibility = "hidden";
			document.getElementById("cor_lnh_a").style.cursor = "arrow";
			document.getElementById("cor_lnh_a").onclick = function() {return false;};
			document.getElementById("format_lnh").style.color = "#9e9e9e";
			document.getElementById("cor_lnh").style.backgroundColor = "#9e9e9e";
			document.getElementById("cor_lnh").style.color = "#9e9e9e";
			form.largura_lnh.disabled = true;
			form.opacidade_lnh.disabled = true;
		}
		else {
			document.getElementById("cor_lnh_img").style.visibility = "visible";
			document.getElementById("cor_lnh_a").style.cursor = "hand";
			document.getElementById("cor_lnh_a").onclick = function() {return true;};
			document.getElementById("format_lnh").style.color = "#000000";
			document.getElementById("cor_lnh").style.backgroundColor = cor_lnh;
			document.getElementById("cor_lnh").style.color = cor_lnh;
			form.largura_lnh.disabled = false;
			form.opacidade_lnh.disabled = false;
		}
		return;	
	}		
	preenchePainel(painel_id);
	preencheRodape();	
}
//Fim de Janela de Configurações


//Abre Janela do Google Maps/GeoHack
function abreJanGoogle() {
	switch (document.mapa.op_abre_site.selectedIndex) {
		case 0 :
			var url_stg = "http://www.google.com/maps?f=q";
			//url_stg += escape(document.mapa.end_geocoder.value);
			url_stg += "&hl=en&geocode=&q=&sll=&sspn=&ie=UTF8&cd=1&ll=";
			url_stg += lat_1 + "," + long_1;
			url_stg += "&spn=&z=";
			url_stg += document.mapa.zoom_level.selectedIndex;
			url_stg += "&om=1&t=";
			url_stg += map.getCurrentMapType().getUrlArg();
		break;
		case 1 :			
			var z_lv = 17 - document.mapa.zoom_level.selectedIndex;
			z_lv = (z_lv<0)? 0 : z_lv;
			var map_tp = ((map.getCurrentMapType().getUrlArg())=="m")? "0" : ((map.getCurrentMapType().getUrlArg())=="k")? "1" : "2";
			var url_stg = "http://www.panoramio.com/map/#lt=";		
			url_stg += lat_1 + "&ln=" + long_1;
			url_stg += "&z=" + z_lv;
			url_stg += "&k=" + map_tp;
			url_stg += "&a=1&tab=1";
		break;
		case 2 :
			var url_stg = "http://tools.wikimedia.de/~magnus/geo/geohack.php?pagename=&params=";		
			url_stg += lat_1 + "_N_" + long_1 + "_E";
		break;
		case 3 :			
			var url_stg = "http://www.geonames.org/maps/google_";		
			url_stg += lat_1 + "_" + long_1 + ".html";
		break;
		case 4 :			
			var url_stg = "http://wikimapia.org/maps?ll=" + lat_1 + "," + long_1 + "&spn=0.3,0.3";
		break;
		case 5 :
			var z_lv2 = 17 - document.mapa.zoom_level.selectedIndex;
			z_lv2 = (z_lv2<0)? 0 : z_lv2;			
			var url_stg = "http://www.flickr.com/map/?&fLat=" + lat_1 + "&fLon=" + long_1;
			url_stg += "&zl=" + z_lv2;
		break;
	}	
	var jan_site = window.open(url_stg,"janSite","toolbar=0,location=1,directories=1,status=0,menubar=1,scrollbars=1,resizable=1");
}
//Fim de Abre Janela do Google Maps


//Janela Aux. de Coordenadas
function janCoord() {	
	if (GBrowserIsCompatible()) {		
		GEvent.addListener(map, "click", function(marker, point) {		
			var coord = "";
			var coordJan = window.open("",'coordWin','toolbar=0,location=no,directories=0,status=0,menubar=0,scrollbars=0,resizable=0,width=400,height=100');
			coord += point + "<br />";
			coordJan.document.write(coord);			
		});	
	}
}
//Fim Janela Aux. de Coordenadas


//Abre Nova Janela Mapa
function abreJanMapa() {
	var mapaWidth = window.screen.availWidth - 23;
	var mapaHeight = window.screen.availHeight - 70;	
	var janConfig = "toolbar=0,location=no,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width=" + mapaWidth + ",height=" + mapaHeight + ",left=5,top=5"
	var janMapa = window.open("jan_mapa.html","jan_webcalc_mapas", janConfig);
}

function janUnload() { //Disparada pelo evento "onunload" da Janela Mapa
	if (document.configura.jan_alt_mapa.checked) {		
		var centro = new GLatLng(jan_center_y, jan_center_x);
		map.setCenter(centro);
		map.setZoom(jan_zoom);		
		if (jan_map_type=="m") {
			map.setMapType(G_NORMAL_MAP);			
		}
		else if (jan_map_type=="k") {
			map.setMapType(G_SATELLITE_MAP);			
		}
		else if (jan_map_type=="h") {
			map.setMapType(G_HYBRID_MAP);
		}
		else if (jan_map_type=="p") {
			map.setMapType(G_PHYSICAL_MAP);
		}			
	}	
}
//Fim de Abre Nova Janela Mapa


//Abre Nova Janela Google Earth
function abreJanGE() {
	var mapaWidth1 = window.screen.availWidth - 23;
	var mapaHeight1 = window.screen.availHeight - 70;	
	var janConfig1 = "toolbar=0,location=no,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width=" + mapaWidth1 + ",height=" + mapaHeight1 + ",left=5,top=5"
	var janMapaGE = window.open("jan_mapa_ge.html","jan_webcalc_mapas_ge", janConfig1);
}
//Fim de Abre Nova Janela Google Earth


//Abre Janela da Paleta de Cores
function abreJanPaleta() {
	var winimp = window.open("paleta_cores.html","palheta_cores","toolbar=0,location=no,directories=0,status=0,menubar=0,scrollbars=0,resizable=0,width=306,height=360,left=10,top=10");
}
//Fim de Abre Janela da Paleta de Cores


//Adicionar KML
var kml_overlay = null;
function addKML(src, isLink) {	
	var src_id = src.id;
	var url_stg = document.configura.kml_url.value;
	switch (src_id) {
		case "ativa_kml" :
			if (document.getElementById("kml_url").disabled) {
				 document.getElementById("kml_url").disabled = false;
				 document.getElementById("carregar_url").disabled = false;
				 if (document.configura.kml_url.value=="") {
					 document.getElementById("kml_url").style.color = "#909090";
					 document.configura.kml_url.value = "Digite o URL do Arquivo KML ou GeoRSS";
				 }
				 if (kml_overlay!=null) {map.addOverlay(kml_overlay);}
			}
			else {
				 document.getElementById("kml_url").disabled = true;
				 document.getElementById("carregar_url").disabled = true;
				 if (kml_overlay!=null) {map.removeOverlay(kml_overlay);}
			}			
		break;
		case "kml_url" :			
			if ((document.configura.kml_url.value).indexOf("Digite o URL do Arquivo")!=-1) {
			 document.getElementById("kml_url").style.color = "#000000";
			 document.configura.kml_url.value = "";
			}
		break;
		case "carregar_url" :
			if (!isLink) {
				MudaBtVz(document.getElementById("abre_config"), 4);
				altMap(document.getElementById("abre_config"));
				MudaBtVz(document.getElementById("abre_config"), 3);
				MudaBtVz(document.getElementById("abre_config"), 2);
			}		
			document.getElementById("carregando").style.top = document.getElementById("mira").style.top;
			document.getElementById("carregando").style.left = document.getElementById("mira").style.left;
			document.getElementById("carregando").style.visibility = "visible";			
			if (document.getElementById("ativa_kml").checked) {
				if (kml_overlay!=null) {map.removeOverlay(kml_overlay);}
				kml_overlay = new GGeoXml(url_stg, function () {
					kml_overlay.gotoDefaultViewport(map);
					document.getElementById("carregando").style.visibility = "hidden";
				});
				map.addOverlay(kml_overlay);
			}			
		break;
	}
}
//Fim de Adicionar KML


//Manipula Mira
function mira() {	
	var divMira = document.getElementById("mira");
	if (!mapaExpandido) {
		divMira.style.top = "381px";
		divMira.style.left = "243px";
		if (is.fox) {divMira.style.top = "410px";}
	}
	if (document.configura.ativa_mira.checked) {divMira.style.visibility = "visible";}
	else {divMira.style.visibility = "hidden";}	
}
//Fim de Manipula Mira



//Pendências:
//- Bugs:
//        - Botão de ativação de marcação de coordenadas não desativa a marcação (IE, Fox e Chrome)
//        - Caixas de ativação de cálculos de distâncias "bagunçam" a legenda do botão de ativação de marcação de coordenadas (Fox e Chrome)
//        - Problema em arrastar janelas de roteiro e destinos interm. resulvido no Firefox 1.0.3 (Mas permanece o problema no Chrome 0.2.149.27,versão BETA)
//
//- Geral (Mapa):
//        - Criar objeto na pag. menu para armazenar valores campos config, mantendo persistencia
//        - Acrescentar opcao de escolha do tipo de "crosshair" (inclusive sem "crosshair")
//        - Verificar se e possivel carregar arquivo KML local????
//        - Revisar geracao de link para os mapas -> Criar "array" de marcadores acrescentados via link, para repassar para mapa ampliado em nova janela
//        -  Incluir opcao de click no marcador abrir janela com coordenadas (formato decimal, formato config e formato Garmin)
//        - Incluir opcao de formato UTM de coordenadas
//        - Solicitar confirmacao ao clicar no botao "Restaurar Padrao", com valores de distancias != "" (O mesmo para as caixas de selecao "Distâncias Marcas" e "Distâncias Coordenadas"
//        - Limitar tamanho da matriz da pilha (100??)
//        - Acrescentar botoes "Traffic" e "Street View"
//        - Verificar como acrescentar botao GTrafficOverlay() no mapa (Ex: var trafficInfo = new GTrafficOverlay(); ??)
//        - Verificar opcao de AdSense no mapa (API Google ??)

//- Painel Roteiros":
//                   - Incluir opcoes: "Seguir Rodovias/Ruas" ou "Percurso Direto", para cálculo de distâncias em linha reta entre pontos (Ex: Aeroportos)
//                   - Opcao de exportar para excel o roteiro impresso
//                   - Opcao de mostrar os "steps" como marcadores e clicando-se abre infoWindow com descricao e minimapa
//                   - Salvar roteiro como "tracklog" em KML e/ou GPX: Explorar o objeto "gdir.getPolyline()" (ver exemplo abaixo)
//                       var polyline = "";
//                       for (var i=0; i<gdir.getPolyline().getVertexCount(); i++) {
//	                        polyline += gdir.getPolyline().getVertex(i) + "<br />"
//                       }
//                       transformar "polyline" em formato KML, enviar para script PHP e retornar arquivo para download (com opcao para formato GPX)
//
//                   - Incluir recurso de clicar em mini-mapa para definir coordenada "de" e/ou "para" e/ou dest. interm.
//                   - Opcao de roteiro entre coordenadas (Inclusive clicando no mapa (Ex: BR-101 @-11.624010, -37.885720)
//                   - Opcao de clicar no mapa para destinos intermediarios (Ex: BR-101 @-11.624010, -37.885720)
//                   - Incluir temporizador para interromper conexao com servidor apos 20seg (?) e avisar
//                   - Gradacao de cor apos atualizacao da lista do roteiro

//- Painel "Coordenadas":
//                   - Revisar codigo de verificacao de entrada de coordenadas permitindo varias opcoes de formato, modando automaticamente de acordo com a entrada do usuario (Ex: 20g02m30s ou -12d15'40s ou 40°10m12" ou -10 12 20 ou -12g30,4m)
//                   - Página de impressão
//                   - Histórico dos cálculos de distâncias
//                   - Opcao de apagar ultima distâncias calculada (Voltar)

//- Google Earth:
//                   - Verificar possibilidade de implementação de funcionalidades da API do Google Earth na janela atual (Utiliza a API do Google Maps) 
//                   - Implementar janela do Google Earth usado a API adequada, com as várias funcionalidades disponibilizadas 

//- Funcionalidades: (Aeroportos, GeoNames, Municipios brasileiros, Wikipedia(?), 1000 lugares mundo, 1000 lugares BR, Waypoints e Tracklogs (Salvar em KML ou GPX), POI em livros famosos, rotas das grandes navegacoes, locais de fatos historicos relevantes, etc)
