function MapController(admin)
{
	this.admin = (admin ? admin : false);

	this.map = null;
    this.zoomMap = {".8":15, "1.6":14, "3.2":13, "8":11, "16":12, "999":8};
	this.addressCircle = null;
	this.addressMarker = null;
}
MapController.prototype.initialize = function(map_div, latitude, longitude, zoom)
{ 
    if (!GBrowserIsCompatible()) alert("Sorry your browser is not compatible with Google Maps!");


    MapController.defaultIcon = new GIcon();
	MapController.defaultIcon.iconSize = new GSize(16,28);
	MapController.defaultIcon.shadow = "/Kcmap-portlet/js/pin_shadow.png"
	MapController.defaultIcon.shadowSize = new GSize(40,28);
	MapController.defaultIcon.iconAnchor = new GPoint(8,28);
	MapController.defaultIcon.infoWindowAnchor = new GPoint(8,3);
    this.map = new GMap2(document.getElementById(map_div));
	this.map.addControl(new GScaleControl());
	this.map.addControl(new GMapTypeControl());
	this.map.enableContinuousZoom();
    this.map.setCenter(new GLatLng(latitude, longitude), zoom);

      // ====== Restricting the range of Zoom Levels ===== by Mike Williams
      // Get the list of map types      
      var mt = this.map.getMapTypes();
      // Overwrite the getMinimumResolution() methods
      for (var i=0; i<mt.length; i++) {
        mt[i].getMinimumResolution = function() {return 8;}
      }
	this.map.addControl(new GLargeMapControl());

	this.geocoder = new GClientGeocoder();
}

MapController.prototype.clearMap = function() {
	this.map.clearOverlays();
}

MapController.prototype.centreOnAddress = function(lat, lng, radius) {
	var point = null;
	if(lat && lng)
		point = new GLatLng(lat, lng);
	else
		point = this.map.getCenter();

	this.map.setCenter(point, this.zoomMap[radius]);
	
}

MapController.prototype.drawAddressCircle = function(lat, lng, circleRadius,zip) {
	
			if(this.addressMarker){
				this.map.removeOverlay(this.addressMarker);
				this.addressMarker = null;
			}

			if (this.addressCircle) {
				this.map.removeOverlay(this.addressCircle);
				this.addressCircle = null;
			}


			if(this.circleRadius == 0){
				return 0;
			}
            if(zip != ''){
			this.addressMarker = new GMarker(new GLatLng(lat,lng));
			this.addressMarker.bindInfoWindowHtml("<div>"+zip+"</div>");
			this.map.addOverlay(this.addressMarker);
			}
			var center = this.map.getCenter(); 

			var bounds = new GLatLngBounds();			
			var circlePoints = Array();

			with (Math) {
				
				var d = circleRadius/6378.8;	// radians
				var lat1 = (PI/180)* center.lat(); // radians
				var lng1 = (PI/180)* center.lng(); // radians
				

				for (var a = 0 ; a < 361 ; a++ ) {
					var tc = (PI/180)*a;
					var y = asin(sin(lat1)*cos(d)+cos(lat1)*sin(d)*cos(tc));
					var dlng = atan2(sin(tc)*sin(d)*cos(lat1),cos(d)-sin(lat1)*sin(y));
					var x = ((lng1-dlng+PI) % (2*PI)) - PI ; // MOD function
					var point = new GLatLng(parseFloat(y*(180/PI)),parseFloat(x*(180/PI)));
					circlePoints.push(point);
					bounds.extend(point);
				}

				
				if (d < 1.5678565720686044) {
					this.addressCircle = new GPolygon(circlePoints, '#000000', 2, 1, '#000000', 0.25);	
				}
				else {
					this.addressCircle = new GPolygon(circlePoints, '#000000', 2, 1);	
				}

				
				this.map.addOverlay(this.addressCircle); 
			}
	
}


MapController.prototype.addHostMarker = function(point, html, datetime,completed, full) {
	var marker = null;
	var icon = MapController.defaultIcon;
	if (!completed) {
		// if meeting is full, make it red, otherwise green
		icon.image = (full ? "/Kcmap-portlet/js/marker_red.png" : "/Kcmap-portlet/js/marker_green.png");
		marker = new GMarker(point, {icon:icon, title:datetime});
	}
	else  {
		icon.image = "/Kcmap-portlet/js/marker_blue.png";
		marker = new GMarker(point, icon);
	}
	this.map.addOverlay(marker);
	marker.bindInfoWindowHtml(html);
	return marker;
}

MapController.prototype.addPrintMarker = function(point, host) {
	var marker = null;
	var icon = MapController.defaultIcon;
	icon.image = (host ? "/Kcmap-portlet/js/marker_green.png" : "/Kcmap-portlet/js/marker_pink.png");
	marker = new GMarker(point, icon);
	this.map.addOverlay(marker);
	return marker;
}


MapController.prototype.addGuestMarker = function(point, html, assigned) {
	var marker = null;
	var icon = MapController.defaultIcon;
	icon.image = (assigned ? "/Kcmap-portlet/js/marker_aqua.png" : "/Kcmap-portlet/js/marker_pink.png");
	marker = new GMarker(point, icon);
	this.map.addOverlay(marker);
	marker.bindInfoWindowHtml(html);
	return marker;
}

MapController.prototype.updateGuestMarker = function(marker, checked) {
	this.map.closeInfoWindow();
	// change marker colour
	marker.setImage(checked ? "/Kcmap-portlet/js/marker_aqua.png" : "/Kcmap-portlet/js/marker_pink.png");
}

MapController.prototype.selectMarker = function(marker) {
	GEvent.trigger(marker, "click");
}

MapController.prototype.hideMarker = function(marker) {
	marker.hide();
}

MapController.getGeocodeErrorMsg = function(code) {
	switch(code) {
		case G_GEO_MISSING_ADDRESS:		return "Sorry, the address was either missing or had no value.";
                                      
 		case G_GEO_UNKNOWN_ADDRESS:		return "Sorry, no corresponding geographic location could be found for the specified address. This may be due to the fact that the address is relatively new, or it may be incorrect.";
                                      
// 		case G_GEO_UNAVAILABLE_ADDRESS:	return "Unavailable Address:  The geocode for the given address cannot be returned due to legal or contractual reasons.";
                                      
 		case G_GEO_BAD_KEY:				return "Sorry, the API key is either invalid or does not match the domain for which it was given";
                                      
 		case G_GEO_TOO_MANY_QUERIES:	return "Sorry, the daily geocoding quota for this site has been exceeded.";
                                      
		case G_GEO_SERVER_ERROR:		return "Sorry, the geocoding request could not be successfully processed.";

		case G_GEO_MISSING_QUERY:		return "Sorry, no query was specified in the input.";

		case G_GEO_BAD_REQUEST:			return "Sorry, the directions request could not be understood.";
	    
    	case G_GEO_UNKNOWN_DIRECTIONS:	return "Sorry, we could not compute directions between the two points, either because there is no route available or we do not have data for routing in that region.";
		
		default:						return "Geocoding error: " + code;
	}	    
}




