//<![CDATA[

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

var popupInfo = "<b>Accedo Interactive Agency</b> <br> Rua Av. Jos&eacute; Gomes Ferreira <br> Edif&iacute;cio Atlas I, Sala 21 e 22 <br> 1495-139 Alg&eacute;s - Portugal <br>";

// Creates a marker at the given point with the given label
function createMarker(point, label){
    var marker = new GMarker(point);
    GEvent.addListener(marker, "click", function(){
        marker.openInfoWindowHtml(label);
    });
    return marker;
}

function setDirections(ruaOrigem, cidadeOrigem, codigoPostalOrigem, ruaDestino, cidadeDestino, codigoPostalDestino, locale){
    gdir.load("from: " + ruaOrigem + ", " + cidadeOrigem + ", " + codigoPostalOrigem + " to: " + ruaDestino + ", " + cidadeDestino + ", " + codigoPostalDestino, {
        "locale": locale
    });
    GEvent.addListener(gdir, "error", handleErrors);
    var moradaDestino = ruaDestino + ", " + cidadeDestino + ", " + codigoPostalDestino;
    var moradaOrigem = ruaOrigem + ", " + cidadeOrigem + ", " + codigoPostalOrigem;
    
    //showAddress(moradaDestino, moradaOrigem);
    map.removeOverlay(marker);
    map.addOverlay(marker);
    
    map.addOverlay(marker);
    
    var infoTabs = marker.openInfoWindowHtml("<b>Accedo InteractiveMkt & Ebusiness</b> <br> Rua Av. Jos&eacute; Gomes Ferreira <br> Edif&iacute;cio Atlas I, Sala 21 e 22 <br> 1495-139 Alg&eacute;s - Portugal <br>");
    
    GEvent.addListener(marker, "click", function(){
        marker.openInfoWindowHtml(popupInfo);
    });
}

function load(){

    if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("map"));
        map.addControl(new GSmallMapControl());
        map.addControl(new GMapTypeControl());
        //GEvent.addListener(gdir, "error", handleErrors);
        gdir = new GDirections(map, document.getElementById("directions"));
        
        // CENTRO DO MAPA
        map.setCenter(new GLatLng(38.714405, -9.230805), 16);
        
        var accedo = new GLatLng(38.714405, -9.230805);
        marker = new GMarker(accedo);
        
        map.addOverlay(marker);
        
        var infoTabs = marker.openInfoWindowHtml(popupInfo);
        
        GEvent.addListener(marker, "click", function(){
           marker.openInfoWindowHtml(popupInfo);
        });
        
    }// FIM IF
} // FIM FUNCAO
function showAddress(addressDestino, addressOrigem){
    alert("1");
    if (geocoder) {
        alert("2");
        geocoder.getLatLng(addressDestino, function(point){
            if (!point) {
                alert(addressDestino + " not found");
            }
            else {
                map.setCenter(point, 15);
                var marker = new GMarker(point);
                
                if (addressDestino == addressOrigem) {
                    map.addOverlay(marker);
                    marker.openInfoWindowHtml(popupInfo);
                    
                }
                else {
                    GEvent.addListener(marker, "click", function(){
                        marker.openInfoWindowHtml(popupInfo);
                    });
                    map.addOverlay(marker);
                    
                }
                
            }
        });
    }
}


function handleErrors(){
    if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS) 
        alert("Não existe informação para o endereço pretendido.\nVerifique os dados inseridos e tente novamente.");
    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("Não existe informação para o endereço pretendido.\nVerifique os dados inseridos e tente novamente.");
    
    map.addOverlay(marker);
    
}

