﻿var map = null;

google.load("maps", "2.x");

function load() {

    var count = 0; // keeps a record of which iteration we're at, for addressing the points array
    var points = new Array(); // will an indexed array of related lat and long values

    /**
    *  find all geo microformats and put their lat and long into the points array
    */
    $('li.vcard').each(function() {                                      // for each small.geo
        points[count] = new Array();                           // initialise the sencond level of the array
        points[count]['long'] = $(this).find('dd.longitude').text();   // store the long
        points[count]['lat'] = $(this).find('dd.latitude').text();    // store the lat
        points[count]['image'] = $(this).find('img.mapContactFlag').attr('src');
        points[count]['org'] = $(this).find('span.mapTitle').text()
        points[count]['street-address'] = $(this).find('span.street-address').text();
        points[count]['postal-code'] = $(this).find('span.postal-code').text();
        points[count]['locality'] = $(this).find('span.locality').text();
        points[count]['country-name'] = $(this).find('span.country-name').text();
        points[count]['phone'] = $(this).find('span.mapContactNumber').text();
        points[count]['email'] = $(this).find('span.mapContactEmail').text();
        points[count]['web'] = $(this).find('a.mapContactDetails').attr('href');
        count++; // increase the count variable for the next iteration
    });

    if (GBrowserIsCompatible()) {
        map = new google.maps.Map2(document.getElementById("googleMap"));
        map.setMapType(G_PHYSICAL_MAP);
        map.addControl(new google.maps.MapTypeControl());
        map.addControl(new google.maps.LargeMapControl());
        map.setCenter(new google.maps.LatLng(51.9307182793128, -4.7291748046875), 9);
    }

    var icon = null;
    var point = null;
    var marker = null;

    for (var i = 0; i < points.length; i++) {                         // for each point in the points array
        point = new google.maps.LatLng(points[i]['lat'], points[i]['long']); // crate a pointer for points[0]
        icon = new google.maps.Icon(G_DEFAULT_ICON);
        icon.image = points[i]['image'];
        icon.shadow = "/_images/flag-with-shadow.png";
        icon.iconSize = new GSize(40.0, 45.0);
        icon.shadowSize = new GSize(63.0, 45.0);
        icon.iconAnchor = new GPoint(20.0, 22.0);
        icon.infoWindowAnchor = new GPoint(20.0, 22.0);

        marker = new google.maps.Marker(point, { "icon": icon });
        map.addOverlay(marker);

        //if (points[i]['image'].indexOf("poi_other_places") == -1) {
            marker.bindInfoWindowHtml('<div class="info_window"><h3>' + points[i]['org'] + '</h3><div class="phone">' + points[i]['phone'] + '</div><div>Email: <a href="mailto:' + points[i]['email'] + '">' + points[i]['email'] + '</a></div><div>Web: <a href="' + points[i]['web'] + '">Website</a></div></div>');                          // place the pointer on the map object
        //}
    }

    $('a.mapLink').click(function(e) {
        var lat = $(this).parent().parent().find('dd.latitude').text();
        var lng = $(this).parent().parent().find('dd.longitude').text();
        var org = $(this).parent().parent().find('span.mapTitle').text();
        var streetAdress = $(this).parent().parent().find('span.street-address').text();
        var postalCode = $(this).parent().parent().find('span.postal-code').text();
        var locality = $(this).parent().parent().find('span.locality').text();
        var countryName = $(this).parent().parent().find('span.country-name').text();
        var phone = $(this).parent().parent().find('span.mapContactNumber').text();
        var email = $(this).parent().parent().find('span.mapContactEmail').text();
        var web = $(this).parent().parent().find('a.mapContactDetails').attr('href');
        var image = $(this).parent().parent().find('img').attr('src');

        if (image.indexOf("poi_other_places") == -1) {
            map.openInfoWindow(new google.maps.LatLng(lat, lng), '<div class="info_window"><h3>' + org + '</h3><div class="phone">' + phone + '</div><div>Email: <a href="mailto:' + email + '">' + email + '</a></div><div>Web: <a href="' + web + '">Website</a></div></div>');
        } else {
            map.closeInfoWindow();
            map.setCenter(new google.maps.LatLng(lat, lng), 9);
        }
        return true;
    });
}

google.setOnLoadCallback(load);