
var map;

function GetMarkerLL(latlon, desc, imgUrl)
{
    var markerIcon = new GIcon();
    markerIcon.image = imgUrl;
    markerIcon.iconSize = new GSize(20,20);
    markerIcon.iconAnchor = new GPoint(9,19);
    markerIcon.infoWindowAnchor = new GPoint(9,9);

	var marker = new GMarker(latlon, markerIcon);
	return marker;
}

function AddMarkerLL(latlon, desc, imgUrl)
{
	var markerLL = GetMarkerLL(latlon, desc, imgUrl);
	
	GEvent.addListener(markerLL, 'click', function() {markerLL.openInfoWindowHtml( desc )});
	
	map.addOverlay(markerLL);
}

function AddMarkerLLWithPopup(latlon, desc, imgUrl)
{
	var markerLL = GetMarkerLL(latlon, desc, imgUrl);
	
	GEvent.addListener(markerLL, 'click', function() {markerLL.openInfoWindowHtml( desc )});
	
	map.addOverlay(markerLL);
	
	markerLL.openInfoWindowHtml( desc );
}

function StructGetDescription(strName, strPostcode)
{
	// constructor logic ==>
	this.strName = strName;
	this.strPostcode = strPostcode;
	// <== constructor logic
	
	this.GetName = function()
	{
		return this.strName;
	}
	this.GetPostcode = function()
	{
		return this.strPostcode;
	}
}

function ExecuteCallbackForPostCodeTranslation(postcode, callbackFunction, param1, param2) 
{
	var localSearch = new GlocalSearch(); // create 1 per postcode
  	localSearch.setSearchCompleteCallback
	(	null,
		function() 
    		{
      			if (localSearch.results[0]) 
      			{    
        			var resultLat = localSearch.results[0].lat;
        			var resultLng = localSearch.results[0].lng;
        			var p = new GLatLng(resultLat,resultLng);
        			callbackFunction(p, param1, param2);
      			}
      			else
      			{
        			alert(postcode + ": Postcode not found!");
      			}
    		}
    );  
    
 localSearch.execute(postcode + ", UK");
}

function AddMarker(lat, lon, objGetDescription)
{
	AddMarkerLL(new GLatLng(lat,lon), objGetDescription);
}


function gmaps_latlon_init(latlon, startZoom, centrePostcode)
{
	if ( GBrowserIsCompatible() )
	{
		map = new GMap2(document.getElementById("map"));
		map.addControl(new GSmallMapControl());
		map.setCenter(latlon, startZoom);
		
		var venueDataList = document.getElementById("venueDataList");
		
		var divList = venueDataList.getElementsByTagName("div");
		for (var i=0; i < divList.length; ++i)
		{
			var venueNode = divList[i];
			var postcode = venueNode.getAttribute('postcode');
			if (postcode != null)
			{
				var name = venueNode.getAttribute('name');			
				var town = venueNode.getAttribute('town');			
				var score = venueNode.getAttribute('score');			
				var rdate = venueNode.getAttribute('date');		
				var reviewUrl = "http://www.mungflesh.net/ccc/viewrev.php?eventid=" + venueNode.getAttribute('eventid');
				
				var pinURL = "http://www.mungflesh.net/ccc/images/" + venueNode.getAttribute('pin');			
				//var strInfoText = name + ", " + town + ", " + postcode;
				var strInfoHtml = "<p style=\"font-family: verdana, Arial, sans-serif; font-size: 10px; font-style: normal;\"><b>" + name + "</b><br />" + town + "<br />" + postcode + "</p>";
				if (score != '')
				{
					strInfoHtml = strInfoHtml 	+ "<p style=\"font-family: verdana, Arial, sans-serif; font-size: 10px; font-style: normal;\">CCC Rating: <a href=\"" 
												+ reviewUrl
												+ "\" title=\"click to see review\">"
												+ "<b>"
												+ score 
												+ "%</b> (" 
												+ rdate 
												+ ")"
												+ "</a>"
												+ "</p>";
				}
				else
				{
					strInfoHtml = strInfoHtml + "<p style=\"font-family: verdana, Arial, sans-serif; font-size: 10px; font-style: normal;\">To be reviewed ...</p>";
				}
				
				if (centrePostcode == postcode)
				{
					ExecuteCallbackForPostCodeTranslation(postcode, AddMarkerLLWithPopup, strInfoHtml, pinURL);
				}
				else
				{
					ExecuteCallbackForPostCodeTranslation(postcode, AddMarkerLL, strInfoHtml, pinURL);
				}
			}
		}
	}
}

// default page load method
function gmaps_init()
{
	var centerLat = 51.293546;
	var centerLon = -0.459807;
	var startZoom = 10;
	
	var loc = new GLatLng(centerLat, centerLon);
	
	gmaps_latlon_init(loc, startZoom, null);
}

function gmaps_init_postcode(postcode, startZoom)
{
	ExecuteCallbackForPostCodeTranslation(postcode, gmaps_latlon_init, startZoom, postcode);
}

