// map script used in about/location.php and map.php

	//<![CDATA[

    var iconDarkBlue = new GIcon();  
    iconDarkBlue.image = 'http://dunster.org.uk/images/darkblue_marker.png'; 
    iconDarkBlue.shadow = 'http://dunster.org.uk/images/marker_shadow.png'; 
    iconDarkBlue.iconSize = new GSize(12, 20); 
    iconDarkBlue.shadowSize = new GSize(22, 20); 
    iconDarkBlue.iconAnchor = new GPoint(6, 20); 
    iconDarkBlue.infoWindowAnchor = new GPoint(5, 1); 
 
    var iconRed = new GIcon();  
    iconRed.image = 'http://dunster.org.uk/images/red_marker.png'; 
    iconRed.shadow = 'http://dunster.org.uk/images/marker_shadow.png'; 
    iconRed.iconSize = new GSize(12, 20); 
    iconRed.shadowSize = new GSize(22, 20); 
    iconRed.iconAnchor = new GPoint(6, 20); 
    iconRed.infoWindowAnchor = new GPoint(5, 1); 
 
    var iconYellow = new GIcon();  
    iconYellow.image = 'http://dunster.org.uk/images/yellow_marker.png'; 
    iconYellow.shadow = 'http://dunster.org.uk/images/marker_shadow.png'; 
    iconYellow.iconSize = new GSize(12, 20); 
    iconYellow.shadowSize = new GSize(22, 20); 
    iconYellow.iconAnchor = new GPoint(6, 20); 
    iconYellow.infoWindowAnchor = new GPoint(5, 1); 
 
    var iconPink = new GIcon();  
    iconPink.image = 'http://dunster.org.uk/images/pink_marker.png'; 
    iconPink.shadow = 'http://dunster.org.uk/images/marker_shadow.png'; 
    iconPink.iconSize = new GSize(12, 20); 
    iconPink.shadowSize = new GSize(22, 20); 
    iconPink.iconAnchor = new GPoint(6, 20); 
    iconPink.infoWindowAnchor = new GPoint(5, 1); 
 
    var iconBlack = new GIcon();  
    iconBlack.image = 'http://dunster.org.uk/images/black_marker.png'; 
    iconBlack.shadow = 'http://dunster.org.uk/images/marker_shadow.png'; 
    iconBlack.iconSize = new GSize(12, 20); 
    iconBlack.shadowSize = new GSize(22, 20); 
    iconBlack.iconAnchor = new GPoint(6, 20); 
    iconBlack.infoWindowAnchor = new GPoint(5, 1); 
 
    var iconGreen = new GIcon();  
    iconGreen.image = 'http://dunster.org.uk/images/green_marker.png'; 
    iconGreen.shadow = 'http://dunster.org.uk/images/marker_shadow.png'; 
    iconGreen.iconSize = new GSize(12, 20); 
    iconGreen.shadowSize = new GSize(22, 20); 
    iconGreen.iconAnchor = new GPoint(6, 20); 
    iconGreen.infoWindowAnchor = new GPoint(5, 1); 
 
    var iconLightBlue = new GIcon();  
    iconLightBlue.image = 'http://dunster.org.uk/images/lightblue_marker.png'; 
    iconLightBlue.shadow = 'http://dunster.org.uk/images/marker_shadow.png'; 
    iconLightBlue.iconSize = new GSize(12, 20); 
    iconLightBlue.shadowSize = new GSize(22, 20); 
    iconLightBlue.iconAnchor = new GPoint(6, 20); 
    iconLightBlue.infoWindowAnchor = new GPoint(5, 1); 
 
    var iconSilver = new GIcon();  
    iconSilver.image = 'http://dunster.org.uk/images/silver_marker.png'; 
    iconSilver.shadow = 'http://dunster.org.uk/images/marker_shadow.png'; 
    iconSilver.iconSize = new GSize(12, 20); 
    iconSilver.shadowSize = new GSize(22, 20); 
    iconSilver.iconAnchor = new GPoint(6, 20); 
    iconSilver.infoWindowAnchor = new GPoint(5, 1); 
 
    var customIcons = []; 

    // individual marker 
    customIcons["00"] = iconPink; 

		// colour depends on PageID of the Business_Classes
    customIcons["1"] = iconBlack; 			// Events Diary
    customIcons["2"] = iconBlack; 			// Meetings Page
    customIcons["3"] = iconDarkBlue; 		// Business Directory
    customIcons["4"] = iconRed; 				// Places to Eat and Drink
    customIcons["5"] = iconBlack; 			// Parish Council Home Page
    customIcons["6"] = iconGreen; 			// Accommodation Directory
    customIcons["7"] = iconYellow; 			// Community Home Page
    customIcons["8"] = iconYellow; 			// Local Information Home Page
    customIcons["9"] = iconSilver; 			// Out and About Home Page
    customIcons["10"] = iconBlack; 			// Classified Ads
    customIcons["11"] = iconSilver; 		// Things to See and Do
    customIcons["12"] = iconYellow; 		// Clubs and Societies Home Page
    customIcons["13"] = iconYellow; 		// Churches and Chapels Home Page
    customIcons["14"] = iconBlack; 			// not used
    customIcons["15"] = iconBlack; 			// Local Government

    //customIcons["00"] = iconLightBlue; 	// spare


function loadMap(latitude, longitude, zoom, member, webaddress, category) {

    // Check to see if this browser can run the Google API
    if (GBrowserIsCompatible()) {
			if (member != "") {
				if (member == "showall") {
					showall(latitude, longitude, zoom, category);
				} else {
					// map showing specific directory entry
					showmember(latitude, longitude, zoom, member, webaddress);
				}
			} else {
				// blank map
				showmap(latitude, longitude, zoom);
			}
    }
    // display a warning if the browser was not compatible
    else {
      alert("Sorry, the Google Maps API is not compatible with this browser");
    }
}

function showmember(latitude, longitude, zoom, member, webaddress) {
		// Display the map
		var map = new GMap2(document.getElementById("mapzone"));
		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());
		map.setCenter(new GLatLng(latitude, longitude), zoom);
		
		var point = new GLatLng(latitude, longitude);
		var marker = new GMarker(point, customIcons["00"]); 

		if (member != "") {
			var html = "&nbsp;</br>" + member;
			if (webaddress != "") {
				// add link to website or page
				if (webaddress.substring(0, 7) == "http://") {
					// link to external website
					var weblink = webaddress.replace("http://","");
					html += "</br><a href=\"" + webaddress + "\" target=\"_blank\">" + weblink + "</a>";
				} else {
					// link to page
					html += "</br><a href=\"" + webaddress + "\">more information...</a>";
				}
			}
			map.openInfoWindowHtml(map.getCenter(), html);
		}

		marker.html = html;
		map.addOverlay(marker);

		GEvent.addListener(map, "click", function(overlay, point) {
			if (overlay) {
					 if (overlay.html) {
						overlay.openInfoWindowHtml(overlay.html);
					 }
			}
		});      
}

function showmap(latitude, longitude, zoom) {
		// Display a blank map
		var map = new GMap2(document.getElementById("mapzone"));
		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());
		map.setCenter(new GLatLng(latitude, longitude), zoom);
}

function showall(latitude, longitude, zoom, category) {
		if (GBrowserIsCompatible()) { 
			var map = new GMap2(document.getElementById("mapzone")); 
			map.addControl(new GSmallMapControl()); 
			map.addControl(new GMapTypeControl()); 
			map.setCenter(new GLatLng(latitude, longitude), zoom);

			GDownloadUrl("/include/directory_xml.php?category="+category, function(data) { 
				var xml = GXml.parse(data); 
				var markers = xml.documentElement.getElementsByTagName("marker"); 
				for (var i = 0; i < markers.length; i++) { 
					var name = markers[i].getAttribute("name"); 
					var pageID = markers[i].getAttribute("pageID"); 
					var logo = markers[i].getAttribute("logo"); 
					var address = markers[i].getAttribute("address"); 
					var point = new GLatLng(parseFloat(markers[i].getAttribute("gridlat")), 
																	parseFloat(markers[i].getAttribute("gridlong"))); 
					var website = markers[i].getAttribute("website"); 
					var marker = createMarker(point, name, logo, address, pageID, website); 
					map.addOverlay(marker); 
				} 
			}); 
		} 
} 
 
function createMarker(point, name, logo, address, pageID, website) { 

		var marker = new GMarker(point, customIcons[pageID]); 

		var html = '<div id="infowindow">';
		if (website != "") {
			// external website or internal link
			if (website.substring(0, 7) == "http://") {
				// link to external website
				//var weblink = website.replace("http://","");
				html += '<a href="' + website + '" target="_blank"><img src="../funding/logos/' + logo + '.jpg" alt="' + name + '" width="150" height="100" /></a>';
				html += "<b>" + name + "</b><p>" + address + "</p>";
				//html += '<p><a href="' + website + '" target="_blank">' + weblink + '</a></p></div>'; 
				html += '<p><a href="' + website + '" target="_blank">website</a></p></div>'; 
			} else {
				// link to page
				html += '<a href="' + website + '"><img src="../funding/logos/' + logo + '.jpg" alt="' + name + '" width="150" height="100" /></a>';
				html += "<b>" + name + "</b><p>" + address + "</p>";
				html += '<p><a href="' + website + '">more information...</a></p></div>'; 
			}
		} else {
			// simple entry with no link
			html += '<img src="../funding/logos/' + logo + '.jpg" alt="' + name + '" width="150" height="100" />';
			html += '<b>' + name + '</b><p>' + address + '</p></div>'; 
		}
		GEvent.addListener(marker, 'click', function() { 
			marker.openInfoWindowHtml(html); 
		}); 
		return marker; 
} 

function loadGridMap(latitude, longitude, zoom) {
    // Check to see if this browser can run the Google API
    if (GBrowserIsCompatible()) {
			// display map
			var map = new GMap(document.getElementById("mapzone"));
			map.addControl(new GSmallMapControl());
			map.addControl(new GMapTypeControl());
			map.setCenter(new GLatLng(latitude, longitude), zoom);
	
			// Add listener in case we click on the map		
			GEvent.addListener(map, 'click', function(overlay, point) {
			if (overlay) {
				// clicked on overlay so remove it
				if (overlay.content_html) {
				overlay.openInfoWindowHtml(overlay.content_html);
				} else {
				map.removeOverlay(overlay);}
			} else if (point) {
				//didn't click on overlay so add marker at point and display position
				map.addOverlay(new GMarker(point));
				document.business.latitude.value = point.y;
				document.business.longitude.value = point.x;
					}
			});
			}
    // display a warning if the browser was not compatible
    else {
      alert("Sorry, the Google Maps API is not compatible with this browser");
    }
}
	//]]>

