﻿$(document).ready(function() {
    setEmailLink();
    createGoogleMaps();
});


function setEmailLink() {
    $(document).find('a#emailLink').each(function() {

        var parts = $(this).attr('href').split('_');

        if (parts.length == 3) {

            var a = parts[0];
            var d = parts[1];
            var t = parts[2];

            if (a.length > 0 && d.length > 0 && t.length > 0) {
                $(this).text(a + '@' + d + '.' + t);
                $(this).attr('href', function() {
                    return 'mailto:' + a + '@' + d + '.' + t;
                });
            }
        }

    });
}

function createGoogleMaps() {
    if ($('#Gmap').length != 0){
      var m = $('#Gmap');
      var parts = m.attr('title').split('_');

      if (parts.length==6) {
          load(parts[0], parts[1], parts[2], parts[3], parts[4], parts[5]);
      }
    }    
}

var map = null;
var geocoder = null;

function load(street, streetnum, city, country, zipcode, phone) {

    if (GBrowserIsCompatible()) {
        geocoder = new GClientGeocoder();
        showAddress(street + ' ' + streetnum + ', ' + city + ', ' + country, street, streetnum, city, country, zipcode, phone);
    }
}

function showAddress(address, street, streetnum, city, country, zipcode, phone) {

    var sURLCountry = 'nl';
    var map = new GMap2(document.getElementById("Gmap"));
    var icon = new GIcon();

    switch (country.toLowerCase()) {
        case 'nederland':
            sURLCountry = 'nl';
            break;
        case ('belgie' || 'belgië'):
            sURLCountry = 'be';
            break;
        default:
            sURLCountry = 'nl';
    }

    var sInput = '<strong>Boekhandel De Slegte</strong><br />' + street + ' ' + streetnum + '<br />' + zipcode + ' ' + city + '<br />' + phone + '<br /><a target=_blank href=http://maps.google.nl/maps?f=q&source=s_q&hl=' + sURLCountry + '&q=' + street.replace(" ", "+") + '+' + streetnum + '+' + city + '+' + zipcode.replace(" ", "+") + '&ie=UTF8&t=h&z=18&iwloc=r1&iwstate1=dir>Routebeschrijving hier naartoe</a>';
    
    map.addControl(new GSmallMapControl());
    map.addControl(new GMapTypeControl());

    // Create our "tiny" marker icon
    var icon = new GIcon();
    icon.image = "/static/images/icons/deslegte_icon_gmaps.jpg";
    icon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
    icon.iconSize = new GSize(30, 19);
    icon.shadowSize = new GSize(40, 29);
    icon.iconAnchor = new GPoint(20, 19);
    icon.infoWindowAnchor = new GPoint(5, 1);
    //************************************//

    geocoder.getLatLng(
                address,
                function(point) {
                    if (!point) {
                        alert(address + " not found");
                    } else {
                        map.setCenter(point, 14);
                        map.setMapType(G_NORMAL_MAP); //opties: G_NORMAL_MAP, G_SATELLITE_MAP, G_HYBRID_MAP
                        var marker = new GMarker(point, icon);
                        map.addOverlay(marker);
                        marker.openInfoWindowHtml(sInput);
                    }
                }
              );
}

/*window.onload = load;*/
if (document.GUnload) {
    window.onunload = GUnload;
}
