﻿
    //<![CDATA[

	var new_coordinates = "";
	var old_coordinates = "";
	var map = null;
	var school_count = 0;
	var default_zoom_level = 11;
	var zoom_threshold = 16;
	var display_threshold = 200;
	
	var default_max_latitude;
	var default_min_latitude;
	var default_max_longitude;
	var default_min_longitude;

	var base_url = "http://localhost:1515";

	function load(default_latitude, default_longitude) {
      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);
		
        var bounds = map.getBounds();
        var southWest = bounds.getSouthWest();
        var northEast = bounds.getNorthEast();

        default_max_latitude = northEast.lat();
        default_min_latitude = southWest.lat();
        default_max_longitude = northEast.lng();
        default_min_longitude = southWest.lng();
        
		GEvent.addListener(map, "zoomend", 
			function() {
		  		map.clearOverlays();
				showHideMaxDiv(0);
				old_coordinates = "";
				new_coordinates = "";
			}
		);
		
		GEvent.addListener(map, "dragend", 
			function() {
		  		map.clearOverlays();
				showHideMaxDiv(0);
				old_coordinates = "";
				new_coordinates = "";
			}
		);
		
		GEvent.addListener(map, "moveend", 
			function() {
				showHideMaxDiv(0);
				//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(default_max_latitude, default_min_latitude, default_max_longitude, default_min_longitude);
      }
    }
	
	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 loadOverlays(max_latitude, min_latitude, max_longitude, min_longitude) {
		//check to see if the map has been re-sized, ie search tab show / hide. If res-sized than map will 
		//recalculate its boundarties
		map.checkResize();
		old_coordinates = new_coordinates;
		new_coordinates = "";
		
		var xml = null;
		var schools = null;
		var point = null;
		var icon = null;
		//get list of which category types to search for. These are selected in search menu  
		//var categories = getCategories();
		//GLog.write(categories);
		//create url to reteieve property data including map boundary and categories
		var url = "GMaps-XML.ashx?maxlat=" + max_latitude + "&minlat=" + min_latitude + "&maxlng=" + max_longitude + "&minlng=" + min_longitude;
		//alert (url)
		//GLog.writeUrl(url);
		GDownloadUrl(url,
			function(data, responseCode) {
			    xml = GXml.parse(data);
			    schools = xml.documentElement.getElementsByTagName("school");
			    school_count = schools.length;

			    if (schools.length <= display_threshold) {

			        //iterate through xml elements
			        for (var i = 0; i < schools.length; i++) {
			            latitude = parseFloat(schools[i].getAttribute("lat"));
			            longitude = parseFloat(schools[i].getAttribute("long"));

			            //add all co-ordinates to a tokenised string. This will be used when map next loads
			            //when calculating whether to add an overlay
			            new_coordinates += longitude + ":" + latitude + "|";

			            //check if an overlay is to be added, by seeing if overlay still exists from last map load
			            if (addOverlay(old_coordinates, longitude, latitude)) {

			                //create point on map to create overlay
			                point = new GLatLng(longitude, latitude);

			                //generate map icon for overlay
			                icon = new GIcon();
			                icon.image = schools[i].getAttribute("icon");
//			                icon.iconSize = new GSize(23, 33);
			                icon.iconAnchor = new GPoint(15, 15);
			                icon.infoWindowAnchor = new GPoint(11, 15);

			                html = schools[i].getAttribute("content");
			                
			                //create overlay marker with point, icon, and popup html
			                marker = createMarker(point, html, icon, schools[i].getAttribute("name"));
			                //add overlay
			                map.addOverlay(marker);
			            }

			        }
		        
			    } else {
			        // if zoom level is above threshold, then clear all overlays and display message
			        map.clearOverlays();
			        showHideMaxDiv(1);
			    }


			}
		);
	}
	
	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 zoomIn() {
	//this zooms the map in one level, and is called from the popup displayed when the property 
	//threshold has been reached
		showHideMaxDiv(0);
		map.zoomIn();
	}

	
	function showHideMaxDiv(show) {
		var maxElement = document.getElementById("maxDiv");
		if (show) {
			maxElement.style.visibility = "visible";
		} else {
			maxElement.style.visibility = "hidden";
		}
	}

    //]]>