
    //<![CDATA[

	var map = null;
	var new_coordinates = "";
	var old_coordinates = "";
	var results_html = "";
	var organisation_count = 0;
	var display_threshold = 50;
	var base_url = "http://www.sportessex.com";

    function load() {
      if (GBrowserIsCompatible()) {
	  
		map = new GMap2(document.getElementById("map"));
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		map.setCenter(new GLatLng(default_latitude, default_longitude), default_zoom_level, G_NORMAL_MAP);

		var initBounds = map.getBounds();
		var initSouthWest = initBounds.getSouthWest();
		var initNorthEast = initBounds.getNorthEast();
		max_latitude = initNorthEast.lat();
		min_latitude = initSouthWest.lat();
		max_longitude = initNorthEast.lng();
		min_longitude = initSouthWest.lng();

		//loadOverlays(initNorthEast.lat(), initSouthWest.lat(), initNorthEast.lng(), initSouthWest.lng());


		GEvent.addListener(map, "zoomend", 
			function() {
		  		tidyUp();
			}
		);
		
		GEvent.addListener(map, "dragend", 
			function() {
		  		tidyUp();
			}
		);
		
		GEvent.addListener(map, "moveend", 
			function() {
				//calculate the boundaries of the current map view, and then reload overlays
		  	var bounds = map.getBounds();
				var southWest = bounds.getSouthWest();
				var northEast = bounds.getNorthEast();
				loadOverlays(northEast.lat(), southWest.lat(), northEast.lng(), southWest.lng());
			}
		);

		loadOverlays(max_latitude, min_latitude, max_longitude, min_longitude);
      }
    }

	function loadOverlays(max_latitude, min_latitude, max_longitude, min_longitude) {

		if (document.getElementById("map").style.visibility == "visible") { 
	
		old_coordinates = new_coordinates;
		new_coordinates = "";
		results_html = "";
		
		var xml = null;
		var organisations = null;
		var point = null;
		var icon = null;
		var advanced_categories = getCategories('advanced_organisation_type_id');
		var categories = getCategories('category_id');
		var sports = getCategories('advanced_sport_id');
		var criteria = getCriteria();
		var dates = getDates();
		var members = getMembers();
		var alternate = false;
		var last_postcode = "";
		var this_postcode = "";
		var next_postcode = "";
		var multi_venue = false;
		
		var url = "includes/xml.php?type="+search_type+"&max_latitude="+max_latitude+"&min_latitude="+min_latitude+"&max_longitude="+max_longitude+"&min_longitude="+min_longitude+"&categories="+categories+"&advanced_categories="+advanced_categories+"&sports="+sports+criteria+dates+members;
		//GLog.write(categories);
		//GLog.write(search_type);
		//GLog.writeUrl(url);

		GDownloadUrl(url, 
			function(data, responseCode) {
			  xml = GXml.parse(data);
			  organisations = xml.documentElement.getElementsByTagName("organisation");

			  if (organisations.length == 0) {
			  	// if zoom level is above threshold, then clear all overlays and display message
				map.clearOverlays();
				showHideMinDiv(1);
			  } else if (organisations.length <= display_threshold) {

			  	for (var i = 0; i < organisations.length; i++) {
					latitude = parseFloat(organisations[i].getAttribute("latitude"));
					longitude = parseFloat(organisations[i].getAttribute("longitude"));
					multi_venue = false;
					image = "";
					new_coordinates += latitude+":"+longitude+"|";

					if (addOverlay(old_coordinates, latitude, longitude)) {
					
						organisation_type_id = organisations[i].getAttribute("organisation_type_id");
						org_title = organisations[i].getAttribute("title");
						org_description = organisations[i].getAttribute("description");
						email = organisations[i].getAttribute("email");
						postcode = organisations[i].getAttribute("postcode");
						id = organisations[i].getAttribute("id");
	
						center = map.getCenter();
						zoom = map.getZoom();
						
						form = document.forms['map_search'];
						p = form.postcode_area.options[form.postcode_area.selectedIndex].value;
						o = form.organisation_type_id.options[form.organisation_type_id.selectedIndex].value;
						s = form.sport_id.options[form.sport_id.selectedIndex].value;
						
						extension = "&latitude="+center.lat()+"&longitude="+center.lng()+"&zoom_level="+zoom+"&s="+s+"&o="+o+"&p="+p;
						
						link_url = "facility.php?id="+id+extension;
						if (organisation_type_id == 1) {
							link_url = "club.php?id="+id+extension;
						} else if (organisation_type_id == 3) {
							link_url = "event.php?id="+id+extension;
						} else if (organisation_type_id == 4) {
							link_url = "route.php?id="+id+extension;
						}
						
						last_postcode = this_postcode;
						this_postcode = postcode;
						if (i<organisations.length-1) {
							next_postcode = organisations[i+1].getAttribute("postcode");
						} else {
							next_postcode = "";
						}
						
						if (this_postcode == last_postcode || this_postcode == next_postcode) {
							multi_venue = true;
							image = base_url+"/images/map-buttons/7.gif";
						} else {
							image = base_url+"/images/map-buttons/"+organisation_type_id+".gif";
						}
						
						icon_size = 24;
						icon_anchor = 13;
						icon = new GIcon();
						icon.image = image;
						icon.iconSize = new GSize(icon_size, icon_size);
						icon.iconAnchor = new GPoint(icon_anchor, icon_anchor);
						icon.infoWindowAnchor = new GPoint(icon_anchor, icon_anchor);
					
					    point = new GLatLng(latitude, longitude);
						
						css = "map-res-grey cl";
						if (alternate) {
							css = "map-res-white cl";
						}

						results_html += "<div class=\""+css+"\">";
							results_html += "<img src=\""+image+"\" alt=\"\" />";
							results_html += "<h3 class=\"red underline\"><a href=\""+link_url+"\" class=\"red underline\">"+org_title+"</a></h3>";
							results_html += "<p>"+org_description+"<br />";
							//if (email != '') {
								//results_html += "contact: <a href=\"mailto:"+email+"\" class=\"red\">"+email+"</a>";
							//}
						results_html += "</p></div>";
						
						if (alternate) {
							alternate = false;
						} else {
							alternate = true;
						}
						
						if (multi_venue) {
							org_title = "MULTI-FUNCTION VENUE";
							org_description = "Please view the text results below to see the clubs / facilities at this venue";
						}
	
						html = "<div style=\"width: 260px;\">";
							if (multi_venue) {
								html += "<strong><span class=\"red underline\">"+org_title+"</span></strong><br />";
							} else {
								html += "<strong><a href=\""+link_url+"\" class=\"red underline\">"+org_title+"</a></strong><br />";
							}
							html += "<p>"+org_description+"<br />";
							//if (email != '' && multi_venue == false) {
								//html += "contact: <a href=\"mailto:"+email+"\" class=\"red\">"+email+"</a><br / >";
							//}
							html += "</p>";
						html += "</div>";
	
						marker = createMarker(point, html, icon, org_title);
						map.addOverlay(marker);

						altercontent();
					
					}

				}
			
			  } else {
			  	// if zoom level is above threshold, then clear all overlays and display message
				map.clearOverlays();
				showHideMaxDiv(1);
			  }

			}
		);
		
		current_position = map.getCenter();
		
		}

	}
	
	function altercontent() {
		document.getElementById("results-container").innerHTML = results_html;
	}
	
	function createMarker(point, html, icon, title) {
		//create marker with supplied icone, and listen for clicks on each marker to display supplied html
		var opts = new Object();
		opts.clickable = true;
		opts.icon = icon;
		opts.title = title;
		var marker = new GMarker(point, opts);
		GEvent.addListener(marker, "click", 
			function() {
				//map.setCenter(point);
				marker.openInfoWindowHtml(html);
			}
		);
	  return marker;
	}
	
	function addOverlay(old_coordinates, latitude, longitude) {
		//check if an overlay already exists at a given latitude and longitude
		add_overlay = true;
		var old_array = old_coordinates.split("|");
		var coordinates = new Array();
		
		//iterate through latitude and longitude string of overlays from previous map load
		for (i=0; i<old_array.length; i++) {
			coordinates = old_array[i].split(":");
			if (coordinates[0] != '') {
				if (parseFloat(coordinates[0]) == latitude && parseFloat(coordinates[1]) == longitude) {
					//if an overlay already exists at a given latitude and longitude, no need to create a new one
					add_overlay = false;
				}
			}
		}
		//GLog.write(add_overlay);
		return add_overlay;
	}
	
	function mapSearch(type) {
		showHideOverlayDiv(1);
		search_type = type;
		tidyUp();
		var bounds = map.getBounds();
		var southWest = bounds.getSouthWest();
		var northEast = bounds.getNorthEast();
		loadOverlays(northEast.lat(), southWest.lat(), northEast.lng(), southWest.lng());
	}

	function postcodePan(type) {
		showHideOverlayDiv(1);
		postcode_area = null;
		var form = document.forms['map_search'];
		search_type = type;
		if (type == 1) {
			postcode_area = form.postcode_area.options[form.postcode_area.selectedIndex].value.split("|");
		} else {
			postcode_area = form.advanced_postcode_area.options[form.advanced_postcode_area.selectedIndex].value.split("|");
		}
		if (postcode_area.length == 3) {
			//format the co-ordinates submitted from the drop-down, and pan to them
			//the map will pick up the movement and reload overlays automatically
			var point = new GLatLng(
				parseFloat(postcode_area[0]), parseFloat(postcode_area[1])
			);
			map.setZoom(parseInt(postcode_area[2]));
			map.panTo(point);
		}
	}
	
	function getCriteria() {
		var form = document.forms['map_search'];
		organisation_type_id = form.organisation_type_id.options[form.organisation_type_id.selectedIndex].value;
		sport_id = form.sport_id.options[form.sport_id.selectedIndex].value;
		criteria = "&organisation_type_id="+organisation_type_id+"&sport_id="+sport_id;
		return criteria;
	}
	
	function getMembers() {
		var form = document.forms['map_search'];
		members_men = 0;
		members_women = 0;
		members_youth = 0;
		members_disabled = 0;
		if (form.members_men.checked) {
			members_men = form.members_men.value;
		}
		if (form.members_women.checked) {
			members_women = form.members_women.value;
		}
		if (form.members_youth.checked) {
			members_youth = form.members_youth.value;
		}
		if (form.members_disabled.checked) {
			members_disabled = form.members_disabled.value;
		}
		members = "&members_men="+members_men+"&members_women="+members_women+"&members_youth="+members_youth+"&members_disabled="+members_disabled;
		return members;
	}
	
	function getDates() {
		var form = document.forms['map_search'];
		dates = "";
		if (   (!isEmpty(form.sy) && isNumber(form.sy)) 
			&& (!isEmpty(form.sm) && isNumber(form.sm)) 
			&& (!isEmpty(form.sd) && isNumber(form.sd)) 
			&& (!isEmpty(form.ey) && isNumber(form.ey)) 
			&& (!isEmpty(form.em) && isNumber(form.em)) 
			&& (!isEmpty(form.ed) && isNumber(form.ey))
		) {
			start = form.sy.value+"-"+form.sm.value+"-"+form.sd.value;
			end = form.ey.value+"-"+form.em.value+"-"+form.ed.value;
			dates = "&start="+start+"&end="+end;
		}
		//alert(dates);
		return dates;
	}
	
	function isBlank(s) {
		for(var i = 0; i < s.length; i++) {
			var c = s.charAt(i);
			if ((c!=' ') && (c!='\n') && (c!='\t')) return false;
		}
		return true;
	}		

	function isEmpty(s) {
		if (!(s.value==null || s.value=="" || isBlank(s.value)))return false;
		return true;	
	}	
	
	function isNumber(s) {
		if (isEmpty(s)) {
	 		return false;
	 	}	
	 	for (var i = 0; i < s.value.length; i++) {
	 		var ch = s.value.substring(i, i + 1);
	 		if((ch!=' ') && (ch!='\n') && (ch!='\t')) 
	     		if (ch < "0" || "9" < ch)return false;
	 	}
	 	return true;
	}	
	
	function getCategories(field) {
		var form = document.forms['map_search'];
		var length = form.elements.length;
		var i = 0;
		var j = 0;
		var category = "";

		for(i=0; i<length; i++) {
			if (form.elements[i].name.indexOf(field)!=-1) {
				if (form.elements[i].checked) {
					if (j>0) {
						category += ":";
					}
					category += form.elements[i].value;
					j++;
				} 
			}
		}
		return category;
	}

	function tidyUp() {
		old_coordinates = "";
		new_coordinates = "";
		map.clearOverlays();
		map.checkResize();
		showHideMaxDiv(0);
		showHideMinDiv(0);
	}
	
	function zoomIn() {
		tidyUp();
		map.zoomIn();
	}
	
	function zoomOut() {
		tidyUp();
		map.zoomOut();
	}
	
	function showHideMaxDiv(show) {
		var element = document.getElementById("max_display");
		if (show) {
			element.style.visibility = "visible";
		} else {
			element.style.visibility = "hidden";
		}
	}
	
	function showHideMinDiv(show) {
		var element = document.getElementById("min_display");
		if (show) {
			element.style.visibility = "visible";
		} else {
			element.style.visibility = "hidden";
		}
	}
	
	function showHideOverlayDiv(show) {
		if (show) {
			document.getElementById("map_overlay").style.visibility = "hidden";
			document.getElementById("map").style.visibility = "visible";
			document.getElementById("key_img").style.visibility = "hidden";
			document.getElementById("key_form").style.visibility = "visible";
		} else {
			document.getElementById("map").style.visibility = "hidden";
			document.getElementById("map_overlay").style.visibility = "visible";
			document.getElementById("key_img").style.visibility = "visible";
			document.getElementById("key_form").style.visibility = "hidden";
		}
	}
	
    //]]>
