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').attr('src');
	    points[count]['org'] = $(this).find('span.org').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.phone').text();
	    points[count]['email'] = $(this).find('span.email').text();
	    points[count]['web'] = $(this).find('span.web a').attr('href');
	    count++; // increase the count variable for the next iteration
	});	
	
	if (GBrowserIsCompatible()) {
	    map = new google.maps.Map2(document.getElementById("googlemaps"));
		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 = "pois/flag_shadow.png";
	    icon.iconSize = new GSize(33.0, 44.0);
	    icon.shadowSize = new GSize(56.0, 44.0);
	    icon.iconAnchor = new GPoint(31.0, 43.0);
	    icon.infoWindowAnchor = new GPoint(21.0, 43.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="address"><div>' + points[i]['street-address'] + '</div><div>' + points[i]['locality'] + '</div><div>' + points[i]['country-name'] + '</div><div>' + points[i]['postal-code'] + '</div></div><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
		} else {
		    marker.bindInfoWindowHtml('<div class="info_window"><h3>' + points[i]['org'] + '</h3></div>');                          // place the pointer on the map object
		}
	}

	$('span.org a').click(function() {
	    var lat = $(this).parent().parent().next().next().find('dd.latitude').text();
	    var lng = $(this).parent().parent().next().next().find('dd.longitude').text();
	    var org = $(this).parent().parent().find('span.org').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().next().find('span.phone').text();
	    var email = $(this).parent().parent().next().find('span.email').text();
	    var web = $(this).parent().parent().next().find('span.web a').attr('href');
	    var image = $(this).parent().parent().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="address"><div>' + streetAdress + '</div><div>' + locality + '</div><div>' + countryName + '</div><div>' + postalCode + '</div></div><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.openInfoWindow(new google.maps.LatLng(lat, lng), '<div class="info_window"><h3>' + org + '</h3></div>');  
	        //map.setCenter(new google.maps.LatLng(lat, lng), 9);
	    }
	    return true;
	});
}

google.setOnLoadCallback(load);