﻿
var map;
var gdir;
var geocoder = null;
var addressMarker;


function initialize(tipo)
{
	if (GBrowserIsCompatible())
	{
		map = new GMap2(document.getElementById("map_canvas"));

		//
		gdir = new GDirections(map, document.getElementById("directions"));
		GEvent.addListener(gdir, "load", onGDirectionsLoad);
		GEvent.addListener(gdir, "error", handleErrors);

		if(tipo == 'direccoes'){
			setDirections("R. do Campo Alegre, Porto 4150, Portugal","R. das Condominhas, Porto 4150, Portugal", "pt_PT");
		}

		//
		map.setCenter(new GLatLng(41.151099741761406,-8.650617599487305), 15);
		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());


		// Create a base icon for all of our markers that specifies the
		// shadow, icon dimensions, etc.
		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);

        // Creates a marker whose info window displays the letter corresponding
        // to the given index.
		function createMarker(point, letra)
		{
			// Create a lettered icon for this point using our icon class
			var letter = String.fromCharCode(letra.charCodeAt(0));
			var letteredIcon = new GIcon(baseIcon);
			letteredIcon.image = "http://www.google.com/mapfiles/marker" + letter + ".png";

			// Set up our GMarkerOptions object
			markerOptions = { icon:letteredIcon,draggable:false,title:"Grupo Folcloríco de Lordelo do Ouro" };
			var marker = new GMarker(point, markerOptions);

			//teste

			GEvent.addListener(marker, "click", function() {
				marker.openInfoWindowHtml("<span style=\"font-size: 8pt; \"><br \/>Sede do Grupo Folclorco de Lordelo do Ouro<br \/>Porto<br \/>Portugal</span>");
			});
			/*
			GEvent.addListener(marker, "click", function() {
			alert(marker.getLatLng());
			document.getElementById('coord').innerHTML=marker.getLatLng();
			});*/

			return marker;
		}


		var latlng = new GLatLng(41.151099741761406,-8.650617599487305);
		map.addOverlay(createMarker(latlng, "L"));

	}
}

function setDirections(fromAddress, toAddress, locale)
{
	gdir.load("from: " + fromAddress + " to: " + toAddress, { "locale": locale });
}

function handleErrors()
{
	var lang = document.getElementById('locale').selectedIndex;
	/*alert('>'+lang );*/

	var trad = new Array();
	for(var i=0; i<2; i++)
	{
		trad[i] = new Array();
	}
	trad[0][0] = "Não foi possivel encontrar a localização inserida. Por favor refine a pesquisa.";
	trad[1][0] = "No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.";

	if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
		alert(trad[lang][0]);
	else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
		alert(trad[lang][0]);
	else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
		alert(trad[lang][0]);
	//   else if (gdir.getStatus().code == G_UNAVAILABLE_ADDRESS)  <--- Doc bug... this is either not defined, or Doc is wrong
	//     alert("The geocode for the given address or the route for the given directions query cannot be returned due to legal or contractual reasons.\n Error code: " + gdir.getStatus().code);

	else if (gdir.getStatus().code == G_GEO_BAD_KEY)
		alert(trad[lang][0]);

	else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
		alert(trad[lang][0]);
	else alert(trad[lang][0]);
}

function onGDirectionsLoad(){
     // Use this function to access information about the latest load()
     // results.

     // e.g.
     //document.getElementById("getStatus").innerHTML = gdir.getStatus().code;
  // and yada yada yada...
}

function traduzir(lang)
{
	var origem = document.getElementById('origem');
	var destino = document.getElementById('destino');
	var idioma = document.getElementById('idioma');
	var obter_direccoes = document.getElementById('obter_direccoes');
	//var direccoes = document.getElementById('direccoes');
	//var mapa = document.getElementById('mapa');

	if(lang == 'pt')
	{
		origem.innerHTML = "Partida";
		destino.innerHTML = "Destino";
		idioma.innerHTML = "Idioma";
		obter_direccoes.value = "Obter Direcções";
		//direccoes.innerHTML = "Direcções";
		//mapa.innerHTML = "Mapa";
	}
	else
	{
		origem.innerHTML = "From";
		destino.innerHTML = "To";
		idioma.innerHTML = "Language";
		obter_direccoes.value = "Get Directions";
		//direccoes.innerHTML = "Directions";
		//mapa.innerHTML = "Map";

	}

}
