// JavaScript Document

var map = null;
var gdir;
var geocoder = null;
var addressMarker;
var points = [];
var mlat=41.171459;
var mlong=-8.616822;
//http://maps.google.com/maps?f=q&hl=en&geocode=&q=ponte+d.+lu%C3%ADs,+porto&sll=37.0625,-95.677068&sspn=29.440076,78.398437&ie=UTF8&ll=41.139445,-8.609333&spn=0.00682,0.01914&z=16
//http://maps.google.com/maps?f=q&hl=en&geocode=&q=R.+de+Aldeia+Nova,+Vila+Nova+de+Gaia,+Porto+4410&sll=41.054664,-8.62268&sspn=0.007734,0.019119&g=R.+de+Aldeia+Nova,+Vila+Nova+de+Gaia,+Porto+4410&ie=UTF8&ll=41.054389,-8.62268&spn=0.007734,0.019119&z=16&iwloc=addr
function createMarker(point, txt) {
	var marker = new GMarker(point);
	GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowHtml(txt);
	});
	return marker;
}

function showMap() {
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map_content"));

		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());
		map.setCenter(new GLatLng(mlat,mlong), 15);
        refs = new Array(
                            new Array('hvp',		mlat, mlong,'<span style="font: 12px bold Verdana, Arial, sans-serif"><strong>Hospital Veterin&aacute;rio do Porto</strong><br />Tv. Silva Porto, 174<br />4250 &ndash; 475 Porto<br />Tlf: 228 348 170</span>'),
                            new Array('arcadeagua',	41.171774, -8.611929,'<span style="font: 12px bold Verdana, Arial, sans-serif"><strong>Jardim de Arca de &Aacute;gua</span>'),
                            new Array('petshop',	41.17033602067049, -8.61729383468628,'<span style="font: 12px bold Verdana, Arial, sans-serif"><strong>PET Shop</strong><br />Rua 9 de Abril, 839<br />4250-352 Porto<br />Telefone 228 348 170</span>')
                        );
		for (i=0;i<refs.length;i++) {
			points[i] = createMarker( new GLatLng(refs[i][1],refs[i][2]), refs[i][3]);
			map.addOverlay(points[i]);
		}
		points[0].openInfoWindowHtml(refs[0][3]);

		// direc??es
        gdir = new GDirections(map, document.getElementById("map_directions"));
        GEvent.addListener(gdir, "load", onGDirectionsLoad);
        GEvent.addListener(gdir, "error", handleErrors);
	}
}

function setDirections(fromAddress, toAddress, locale) {
	if (fromAddress.indexOf('portugal')==-1) {
		fromAddress+=', portugal';
	}
	gdir.load("from: " + fromAddress + " to: " + toAddress,
		{ "locale": locale });
}

function getDir(rua,cidade,pais) {
    if (pais.value==pais.defaultValue || cidade.value==cidade.defaultValue) {
        alert('P.f. introduza, no mínimo, a cidade e o país.');
        return false;
    }
    if (rua.value==rua.defaultValue) {rua.value=""}
	points[0].closeInfoWindow();
	points[0].hide();

    from="";
    if (rua.value!="") from += rua.value +", ";
    from += cidade.value + ", ";
    from += pais.value + ", ";

    setDirections(from,'Tratto @ '+mlat+','+mlong,'pt');

    return false;
}

function onGDirectionsLoad() {
	$('#map_directions h4').remove();
	return true;
}
function handleErrors(){
	/*
   if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
	 alert("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.\nError code: " + gdir.getStatus().code);
   else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
	 alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gdir.getStatus().code);

   else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
	 alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + gdir.getStatus().code);

//   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("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gdir.getStatus().code);

   else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
	 alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);

   else */
   alert("O endereço introduzido não foi encontrado.\n\nP.f. tente de novo.");

}

function latlng() {
	l=map.getCenter();
	$('#divlatlng').html(l.lat()+' - '+l.lng());
}

$(document).ready(function(){
	window.onload = showMap;
	window.onunload = GUnload;
	//setInterval(latlng,1000);
});
