var latitude = 52.65; // for centre of map
var longitude = 0.88; // for centre of map
var startZoom = 9; 

var map;
var gmarkers = [];
var garden_list_html = "";

function createMarker(point,html,id) {
	var marker = new GMarker(point);
	GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowHtml(html);
	});
	gmarkers[id] = marker;
	return marker;
}

function createCustomMarker(point,html,id, iconType) {
	var marker = new GMarker(point, iconType);
	GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowHtml(html);
	});
	gmarkers[id] = marker;
	return marker;
}
	  
function myclick(i){
	GEvent.trigger(gmarkers[i], "click"); 
}

// Create custom icons
// Weather
		var weatherIcon = new GIcon();
		weatherIcon.image = "images/map/weatherIcon.png";
		weatherIcon.iconSize = new GSize(32, 32);
		weatherIcon.iconAnchor = new GPoint(16,16);
		weatherIcon.infoWindowAnchor = new GPoint(16,1);
// Blue
		var blue = new GIcon(G_DEFAULT_ICON);
		blue.image = "images/map/blue.png";
		blue.shadow = "images/map/pin_shadow.png";
		blue.iconSize = new GSize(16, 28);
		blue.shadowSize = new GSize(40, 28);
		blue.iconAnchor = new GPoint(8,28);
		blue.infoWindowAnchor = new GPoint(8,1);
// Yellow
		var yellow = new GIcon(G_DEFAULT_ICON);
		yellow.image = "images/map/yellow.png";		
		yellow.shadow = "images/map/pin_shadow.png";
		yellow.iconSize = new GSize(16, 28);
		yellow.shadowSize = new GSize(40, 28);
		yellow.iconAnchor = new GPoint(8,28);
		yellow.infoWindowAnchor = new GPoint(8,1);	
// Red
		var red = new GIcon();
		red.image = "images/map/adStar.png";		
		red.iconSize = new GSize(16, 16);
		red.iconAnchor = new GPoint(8,8);
		red.infoWindowAnchor = new GPoint(8,1);

function init() {
	
	if (GBrowserIsCompatible()) {
		var map = new GMap2(document.getElementById("map"));
        map.setCenter(new GLatLng(latitude, longitude), startZoom);
		map.setUIToDefault();
	
		
		$.getJSON('getGardenDetails.php', function(data){
			$.each(data, function(entryIndex, entry){
				var open_day_txt;
				if(entry['forNGS'] == ""){
					open_day_txt = "";	
				}else{
					open_day_txt = "NGS Open Days:";	
				}
				var point = new GLatLng(entry['lat'],entry['lng']);
				var infoText = '<div class="infoText"><h2>' + entry["name"] + '</h2>' + entry["address"] + '<br /><br /><strong>' + open_day_txt + '</strong><br />' + entry["forNGS"] +
				'<br /><br /><a href="garden.php?gardenID=' + entry['gardenID'] + '" >View full garden details</a>'
				+
				'<br /><br /><form action="http://maps.google.com/maps" method="get" target="_blank">Get directions from:' +
           		'<input type="text" value="Enter Postcode" SIZE=30 MAXLENGTH=40 name="saddr" id="saddr" value="" />' +
           		' <INPUT value="Go" TYPE="SUBMIT">' +
           		'<input type="hidden" name="daddr" value="' + point.lat() + ',' + point.lng() + 
                '(' + entry["name"] + ')' + 
           		'" />' + '</div>';
				var id = entry['gardenID'];
				garden_list_html = '<div class="gardenListName"><a href="javascript:myclick(' + id + ')">' + entry["name"] +'</a></div>';
				$('#garden_list').append(garden_list_html);
				if(entry["open"]==""){
					markerOptions = {icon:yellow};	
				}else{
					markerOptions = {icon:blue};	
				}
				var marker = createCustomMarker(point, infoText, id, markerOptions);
				map.addOverlay(marker);
			});
		});

// Advertisers
		$.getJSON('getAdDetails.php', function(data){
			$.each(data, function(entryIndex, entry){
				var point = new GLatLng(entry['lat'],entry['lng']);	
				var webText = '';
				if(entry['url'] != "NULL")
				{
					webText = '<a href="http://' + entry['url'] + '" target = "_blank" >Visit Website</a>';
				}
				else {
					webText = '';
				}
					
									
				var infoText = '<div class="infoText"><h2>' + entry["name"] + '</h2>' + entry["postcode"] + '<br /><br />' + webText + '</div>';
				
				var id = entry['id'] + 10000;
				
				markerOptions = {icon:red};	

				var marker = createCustomMarker(point, infoText, id, markerOptions);
				map.addOverlay(marker);
			});
		});

		
// Weather markers
		var point = new GLatLng(52.72,0.405); // Kings Lynn
		var infoText = '<iframe src="http://news.bbc.co.uk/weather/forecast/2479/Next3DaysEmbed.xhtml?target=_parent" allowTransparency="true" width="306" height="435" frameborder="0">You must have a browser that supports iframes to view the BBC weather forecast</iframe>';
		var id = -1;
		markerOptions = {icon:weatherIcon};
		var marker = createCustomMarker(point, infoText, id, markerOptions);
		map.addOverlay(marker);
		
		var point = new GLatLng(52.59247,1.3004); //Cromer
		var infoText = '<iframe src="http://news.bbc.co.uk/weather/forecast/2696/Next3DaysEmbed.xhtml?target=_parent" allowTransparency="true" width="306" height="435" frameborder="0">You must have a browser that supports iframes to view the BBC weather forecast</iframe>';
		var id = -1;
		markerOptions = {icon:weatherIcon};
		var marker = createCustomMarker(point, infoText, id, markerOptions);
		map.addOverlay(marker);	
		
		var point = new GLatLng(52.614723,1.737213); // Great Yarmouth
		var infoText = '<iframe src="http://news.bbc.co.uk/weather/forecast/2332/Next3DaysEmbed.xhtml?target=_parent" allowTransparency="true" width="306" height="435" frameborder="0">You must have a browser that supports iframes to view the BBC weather forecast</iframe>';
		var id = -1;
		markerOptions = {icon:weatherIcon};
		var marker = createCustomMarker(point, infoText, id, markerOptions);
		map.addOverlay(marker);
		
// Set up lists to hide and show
		$('#gardens_btn').click(function(){
			$('#garden_list').slideDown('slow');
			$('#dates_list').hide();
		});
		$('#dates_btn').click(function(){
			$('#dates_list').slideDown('slow');
			$('#garden_list').hide();
		});

	}	
}

window.onload = init;
window.onunload = GUnload;

