$(document).ready( function() {
	loadGoogleMap();
});

var communitymap = null;
var markers = new Array();
var busy = false;

var icon_studio = new GIcon();
icon_studio.image = "/wp-content/plugins/google-map/images/marker_blue.png";
icon_studio.iconSize = new GSize(18,32);
icon_studio.iconAnchor = new GPoint(9,32);
icon_studio.infoWindowAnchor = new GPoint(9,0);

var icon_store = new GIcon();
icon_store.image = "/wp-content/plugins/google-map/images/marker_orange.png";
icon_store.iconSize = new GSize(18,32);
icon_store.iconAnchor = new GPoint(9,32);
icon_store.infoWindowAnchor = new GPoint(9,0);

var icon_venue = new GIcon();
icon_venue.image = "/wp-content/plugins/google-map/images/marker_red.png";
icon_venue.iconSize = new GSize(18, 32);
icon_venue.iconAnchor = new GPoint(9, 32);
icon_venue.infoWindowAnchor = new GPoint(9,0);


function loadGoogleMap() {
	var gmap = document.getElementById("google-map");
	if(gmap && GBrowserIsCompatible()) {
		communitymap = new GMap2(gmap);
		
		var customUI = communitymap.getDefaultUI();
		customUI.controls.scalecontrol = false;
		
		communitymap.setCenter(new GLatLng(53,1.2),5,G_NORMAL_MAP);
		communitymap.addMapType(G_PHYSICAL_MAP);
		communitymap.setUI(customUI);

		GEvent.addListener(communitymap, "zoomend", function() {
			if(!busy) {
				loadMarkersInBounds();
			}
		});
		GEvent.addListener(communitymap, "dragend", function() {
			if(!busy) {
				loadMarkersInBounds();
			}
		});
	}
}

/* ---------- Generic Functions ---------- */

function createMarker(point,content,icon) {
	var marker = new GMarker(point, icon);
	GEvent.addListener(marker, "click", function() {
		//communitymap.panTo(point);
		marker.openInfoWindowHtml(content);
	});
	communitymap.addOverlay(marker);
}

/* ---------- Map Search Functions ---------- */

function doMapPostCodeSearch(postcode) {
	var localSearch = new GlocalSearch();
	busy = true;
	
	if(postcode=="Enter Postcode" || postcode=="") {
		alert("Please enter your PostCode");
		return;
	}
	
	localSearch.setSearchCompleteCallback(null, function() {
		if (localSearch.results[0]) {
			var lat = localSearch.results[0].lat;
			var lng = localSearch.results[0].lng;
			var centre = new GLatLng(lat,lng);
			communitymap.clearOverlays();
			communitymap.setCenter(centre,13);
			loadMarkersInBounds();
		} else {
			alert("Sorry, we couldn't find your PostCode");	
		}
		busy = false;
	});
	
	localSearch.execute(postcode);
}


// Display markers within the current map bounds
function loadMarkersInBounds() {
	var bounds = communitymap.getBounds();
	
	communitymap.clearOverlays();
	
	/*
		0 latitude
		1 longitude
		2 name
		3 address
		4 telephone
		5 website
		6 email
		7 type
	*/
	
	for(i=0;i<markers.length;i++) {
		var point = new GLatLng(markers[i][0],markers[i][1]);
		var content = "";
		
		if (bounds.contains(point) == true) {
			
			// Decode the HTML elements
			address = markers[i][3].replace(/&amp;/g,'&').replace(/&lt;/g,'<').replace(/&gt;/g,'>');
			
			// Generate the overlay content
			content = "<p><strong>"+markers[i][2]+"</strong></p>";
			content += "<p>"+address+"</p>";
			content += "<p>Tel: "+markers[i][4]+"</p>";
			content += "<p>Email: <a href=\"mailto:"+markers[i][6]+"\">"+markers[i][6]+"</a></p>";
			content += "<p><a href=\""+markers[i][5]+"\" target=\"_blank\">Visit Website</a></p>";
			
			// Add The Marker
			createMarker(point,content,{icon:markers[i][7]});
		}
	}
}
