var map;
var locations;

jQuery(document).ready(function() {
    if (GBrowserIsCompatible()) {
	    map = new GMap2(document.getElementById("map"));
        map.addControl(new GLargeMapControl());
        map.setCenter(new GLatLng(54.9, -4.0), 6);
		
		if (window.location.href.match("/events")) {
			jQuery.get('/googlemap/engageeventlocations.php', processEngageEventLocations);
			jQuery.get('/googlemap/cffeventlookup.php', processCFFEventLocations);
			jQuery.get('/googlemap/othereventlocations.php', processOtherEventLocations);
		} else if(window.location.href.match("/encounter")) {
        	jQuery.get('/googlemap/encounterlocations.php', processEncounterLocations );
		}
    }
});

function importanceOrder (marker,b) {
   return GOverlay.getZIndex(marker.getPoint().lat()) + marker.importance*1000000;
}


function processEncounterLocations(content) {
    eval("locations = "+content);
    locations.forEach(function(element, index, array) {
        var encounterPopup = element.name;
        encounterPopup = encounterPopup.replace('&rsquo;','\'');
        
        var marker = new GMarker(new GLatLng(element.latitude, element.longitude), {title: encounterPopup});
        map.addOverlay(marker);
        GEvent.addListener(marker, 'click', function() {
            marker.openInfoWindowHtml('<div class="bubble"><h1><a href="' + element.url + '">' + element.name + '</a></h1><p>' + element.extrainfo + '</p><p class="readmore"><a href="' + element.url + '">Read more...</a></p></div>');       
        });
    });
}

function processCFFEventLocations(content) {
	var cffIcon = new GIcon(G_DEFAULT_ICON, '/images/cffmarker.png');
	processEventLocations(content, cffIcon, 1);
}

function processEngageEventLocations(content) {
	var engageIcon = new GIcon(G_DEFAULT_ICON, '/images/engagemarker.png');
	processEventLocations(content, engageIcon, 2);
}

function processOtherEventLocations(content) {
	processEventLocations(content, G_DEFAULT_ICON, 0);
}

/**
 * Process the event locations specified in content, using the given markerIcon.
 * Importance defines the z-index layer of the point (the higher the importance, the more likely they'll be shown on top.
 */
function processEventLocations(content, markerIcon, importance) {
    eval("locations = "+content);
    locations.forEach(function(element, index, array) {
        var eventPopup = element.name + ' - ' + element.location;
        eventPopup = eventPopup.replace('&rsquo;','\'');
        
		var marker = new GMarker(new GLatLng(element.latitude, element.longitude), {title: eventPopup, icon: markerIcon, zIndexProcess:importanceOrder});
		marker.importance = importance
		
		// Only display the pin if the event is in the future
		var eventDate = Date.parse(element.eventdate);
        
		if (Date.today().compareTo(eventDate) <=0) {
        	map.addOverlay(marker);
        	GEvent.addListener(marker, 'click', function() {
            	marker.openInfoWindowHtml('<div class="bubble"><h1><a href="' + element.url + '">' + element.name + ' - ' + element.location + '</a></h1><p class="date">' + eventDate.toString("d MMMM yyyy") + '</p><p>' + element.extrainfo + '</p><p class="readmore"><a href="' + element.url + '">Read more...</a></p></div>');       
        	});
		};
    });
}