// This Javascript is based on code provided by the
// Community Church Javascript Team
// http://www.bisphamchurch.org.uk/   
// http://econym.org.uk/gmap/
// http://code.google.com/intl/es-ES/apis/maps/documentation/javascript/v2/reference.html#GMarker

var map;
var countries = {};
//copy of continents table
var continents = {
    1: {
        "name": "Azie",
        "lat": 34.047863,
        "lng": 100.6196553 
    },
    2: {
        "name": "Noord-Amerika",
        "lat": 54.5259614,
        "lng": -105.2551187
    },
    3: {
        "name": "Europa",
        "lat": 54.5259614,
        "lng": 15.2551187
    },
    4: {
        "name": "Afrika",
        "lat": -8.783195,
        "lng": 34.508523
    },
    //overwrite here for esthetics
    4: {
        "name": "Afrika",
        "lat": 12.211180191503997,
        "lng": 17.9296875
    },
    5: {
        "name": "Oceanie",
        "lat": -22.7359095,
        "lng": 140.0187653
    },
    6: {
        "name": "Zuid-Amerika",
        "lat": -8.783195,  
        "lng": -55.491477
    }
};


if (GBrowserIsCompatible()) { 
    
    // ====== Create a Client Geocoder ======
    var geo = new GClientGeocoder(); 
    
    // ====== Array for decoding the failure codes ======
    var reasons=[];
    reasons[G_GEO_SUCCESS]            = "Success";
    reasons[G_GEO_MISSING_ADDRESS]    = "Missing Address: The address was either missing or had no value.";
    reasons[G_GEO_UNKNOWN_ADDRESS]    = "Unknown Address:  No corresponding geographic location could be found for the specified address.";
    reasons[G_GEO_UNAVAILABLE_ADDRESS]= "Unavailable Address:  The geocode for the given address cannot be returned due to legal or contractual reasons.";
    reasons[G_GEO_BAD_KEY]            = "Bad Key: The API key is either invalid or does not match the domain for which it was given";
    reasons[G_GEO_TOO_MANY_QUERIES]   = "Too Many Queries: The daily geocoding quota for this site has been exceeded.";
    reasons[G_GEO_SERVER_ERROR]       = "Server error: The geocoding request could not be successfully processed.";
} else {
    alert("Sorry, the Google Maps API is not compatible with this browser");
}
function showPoint(overlay,point){
    alert (point.toString());
}
function showMap() {
    map = new GMap2(document.getElementById("map"));
    map.addControl(new GLargeMapControl());
    map.addControl(new GMapTypeControl());
    map.setCenter(new GLatLng(20,0),2);
    //GEvent.addListener(map, "click", showPoint);
}

// ====== Geocoding ======
function showAddress(search, center, clear, mark, show) {
    center = (center) ? true : false;
    clear = (clear) ? true : false;
    mark = (mark) ? true : false;
    show = (show) ? true : false;
    
    // ====== Perform the Geocoding ======        
    geo.getLocations(search, function (result)
      { 
        // If that was successful
        if (result.Status.code == G_GEO_SUCCESS) {
          //Clear all markers
          if (clear) {
              map.clearOverlays();
          }
          // Loop through the results, placing markers
          for (var i=0; i<result.Placemark.length; i++) {
            var p = result.Placemark[i].Point.coordinates;
            var marker = new GMarker(new GLatLng(p[1],p[0]));
            //document.getElementById("message").innerHTML += "<br>"+(i+1)+": "+ result.Placemark[i].address + marker.getPoint();
            if (mark)
            map.addOverlay(marker);
            break;
          }

          if (center) {
              var f = 0.1;
              // centre the map on the first result
              var p = result.Placemark[0].Point.coordinates;
              // ===== Look for the bounding box of the first result =====
              var N = result.Placemark[0].ExtendedData.LatLonBox.north;
              var S = result.Placemark[0].ExtendedData.LatLonBox.south;
              var E = result.Placemark[0].ExtendedData.LatLonBox.east;
              var W = result.Placemark[0].ExtendedData.LatLonBox.west;
              var bounds = new GLatLngBounds(new GLatLng(S,W), new GLatLng(N,E));
              // Choose a zoom level that fits
              var zoom = map.getBoundsZoomLevel(bounds);
              zoom = 3;
              map.setCenter(bounds.getCenter(),zoom);
          }
          //var points=[new GLatLng(N,W),new GLatLng(N,E),new GLatLng(S,E),new GLatLng(S,W),new GLatLng(N,W)];
          //map.addOverlay(new GPolyline(points));
          
          if (show) showCountryMarkers();
          
        }
        // ====== Decode the error status ======
        else {
          var reason="Code "+result.Status.code;
          if (reasons[result.Status.code]) {
            reason = reasons[result.Status.code]
          } 
          alert('Could not find "'+search+ '" ' + reason);
        }
      }
    );
}
function showCountryMarkers() {
    for (var i in countries) {
        var country = countries[i];
        map.addOverlay(country.marker);
    }
}
function addCountryMarker(data) {
    var data = eval(data);
    if (!data) return;
    countries[data.id] = data;

    var country = countries[data.id];
    country.marker = new GMarker(new GLatLng(country.lat, country.lng), {"title":"Rondreis " + country.name});
    country.anchor = document.getElementById(data.id);
    country.marker.country = country; //save reverse pointer
    GEvent.addListener(country.marker, "click", function() {
        var href = '/rondreis-' + this.country.name;
        document.location = href.toLowerCase();
    });
    GEvent.addListener(country.marker, "mouseover", function() {
        this.country.anchor.style.textDecoration = 'underline';
    });
    GEvent.addListener(country.marker, "mouseout", function() {
        this.country.anchor.style.textDecoration = 'none';
    });
}
function highlightCountry(oANCHOR) {
    var country = countries[oANCHOR.id];
    if (country) {
        country.marker.setImage('/images/marker_blue.png');
    }
}
function clearCountryHighlight(oANCHOR) {
    var country = countries[oANCHOR.id];
    if (country) {
        country.marker.setImage('/images/marker_red.png');
    }
}
function clearContinentHighlights() {
    for (var i in continents) {
        var continent = continents[i];
        continent.marker.setImage('/images/marker_red.png');
    }
}
function clearAnchorHighlights() {
    for (var i in continents) {
        var continent = continents[i];
        continent.anchor.style.textDecoration = 'none';
    }
}
function highlightContinent( oANCHOR ) {
    clearContinentHighlights();
    for (var i in continents) {
        var continent = continents[i];
        if (i == oANCHOR.id) {
            continent.marker.setImage('/images/marker_blue.png');
            break;
        }
    }
}
function showContinentMarkers() {
    for(var i in continents) {
        var continent = continents[i];
        continent.marker = new GMarker(new GLatLng(continent.lat,continent.lng), {"title":"Rondreizen " + continent.name});
        continent.anchor = document.getElementById(i);
        continent.marker.continent = continent; //save reverse pointer
        GEvent.addListener(continent.marker, "click", function() {
            var href = '/rondreizen/' + this.continent.name;
            document.location = href.toLowerCase();
        });
        GEvent.addListener(continent.marker, "mouseover", function() {
            this.continent.anchor.style.textDecoration = 'underline';
        });
        GEvent.addListener(continent.marker, "mouseout", function() {
            this.continent.anchor.style.textDecoration = 'none';
        });
        map.addOverlay(continent.marker);
    }
}
