﻿Type.registerNamespace('Apache.Libraries.AjaxExtenders');

Apache.Libraries.AjaxExtenders.GoogleMapsPanelBehavior = function(element) {
	Apache.Libraries.AjaxExtenders.GoogleMapsPanelBehavior.initializeBase(this, [element]);

	// property variables.
	this._latitude = null;
	this._longitude = null;
	this._zoomLevel = null;
	this._jsonUrl = null;
}

Apache.Libraries.AjaxExtenders.GoogleMapsPanelBehavior.prototype = {

	/// <summary>
	/// Initialise the behavior.
	/// </summary>
	initialize: function() {
		Apache.Libraries.AjaxExtenders.GoogleMapsPanelBehavior.callBaseMethod(this, 'initialize');

		// ensure all mandatory properties have been set.
		if (!this._latitude) {
			throw Error.argument('Latitude', 'Please supply a value for the Latitude.');
		}
		if (!this._longitude) {
			throw Error.argument('Longitude', 'Please supply a value for the Longitude.');
		}
		if (!this._zoomLevel) {
			throw Error.argument('ZoomLevel', 'Please supply a value for the Zoom Level.');
		}
		if (!this._jsonUrl) {
			throw Error.argument('JsonUrl', 'Please supply a value for the JSON URL.');
		}

		// initialise the map.
		this.initialiseMap();
	},

	/// <summary>
	/// Dispose the behavior.
	/// </summary>
	dispose: function() {
		Apache.Libraries.AjaxExtenders.GoogleMapsPanelBehavior.callBaseMethod(this, 'dispose');
	},

	/// <summary>
	/// Conditionally initialises the map.
	/// </summary>
	initialiseMap: function(eventObj) {

		// If Google Maps is incompatiable, alert the user and stop executing function
		if (!GBrowserIsCompatible()) {
			alert("Sorry, your browser doesn't support Google Maps.");
			return;
		}

		// Instantiate new map in #map_canvas div
		var map = new GMap2(this.get_element());

		// Define map controls and set centre point and zoom level
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		map.setCenter(new GLatLng(parseFloat(this._latitude), parseFloat(this._longitude)), parseInt(this._zoomLevel, 10));

		// Determine z-index of markers
		function importanceOrder(marker, b) {
			return GOverlay.getZIndex(marker.getPoint().lat()) + (marker.importance * 1000000);
		}

		// Grove & Dean Logo icon
		var logoIcon = new GIcon();
		logoIcon.image = 'images/map/logo.png';
		logoIcon.iconSize = new GSize(57, 39);
		logoIcon.iconAnchor = new GPoint(27, 17);

		// Car Park Icon
		var carParkIcon = new GIcon(logoIcon);
		carParkIcon.image = 'images/map/parking.gif';
		carParkIcon.iconSize = new GSize(18, 18);
		carParkIcon.iconAnchor = new GPoint(9, 9);

		// Train Station Icon
		var trainStationIcon = new GIcon(carParkIcon);
		trainStationIcon.image = 'images/map/train-station.gif';

		logoMarkerOptions = { icon: logoIcon, zIndexProcess: importanceOrder };
		carParkMarkerOptions = { icon: carParkIcon, zIndexProcess: importanceOrder };
		trainStationMarkerOptions = { icon: trainStationIcon, zIndexProcess: importanceOrder };

		// Process JSON data
		function ProcessJson(jsonStr) {
			var jsonData = eval('(' + jsonStr + ')');

			// Configure, position and plot the markers based on JSON data
			for (var j = 0; j < jsonData.markers.length; j++) {
				var markerJson = jsonData.markers[j];
				var point = new GLatLng(markerJson.latitude, markerJson.longitude);
				var markerObj = eval('new GMarker(point, ' + markerJson.marker + 'MarkerOptions)');

				// Assign zIndex, layering the marker above all others if it's the G&D logo
				markerObj.importance = (markerJson.marker === 'logo') ? 2 : 1;

				// Plot marker
				map.addOverlay(markerObj);
			}
		}

		// Fetch JSON data and process
		GDownloadUrl(this._jsonUrl, ProcessJson);
	},

	// property accessors.
	get_Latitude: function() {
		return this._latitude;
	},
	set_Latitude: function(value) {
		this._latitude = value;
	},

	get_Longitude: function() {
		return this._longitude;
	},
	set_Longitude: function(value) {
		this._longitude = value;
	},

	get_ZoomLevel: function() {
		return this._zoomLevel;
	},
	set_ZoomLevel: function(value) {
		this._zoomLevel = value;
	},

	get_JsonUrl: function() {
		return this._jsonUrl;
	},
	set_JsonUrl: function(value) {
		this._jsonUrl = value;
	}
}

Apache.Libraries.AjaxExtenders.GoogleMapsPanelBehavior.registerClass('Apache.Libraries.AjaxExtenders.GoogleMapsPanelBehavior', AjaxControlToolkit.BehaviorBase);
if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();