/*
 * Assists Google Map rendering
 */
function MapLocator() {	
	
	this.map = null;
	
	// Latitude and longitude value of the selected location
	this.currentLatLng = null;
	
	//Object for the visible rectangular region of the map view in geographical coordinates.
	this.bounds = null;
	
	// Zip circle border color.
	var CCOLOR = "#413C3A";
	
	// Zip circle border width.
	var CWIDTH = 3;
	
	this.firstLocationsLatLng = null;
	
	this.loadMap = function() {
		return new Error("Error: Invoke the method in subclass.");
	};
	
	/* 
	 * This method can be overridden if the map creation process needs to be customized
	 */
	this.createMap = function() {
		return this.createGoogleMap();
	};

	this.createGoogleMap = function() {		
		return new GMap2(document.getElementById("map"));
	};

	this.setMapUI = function(mapObj) {
		var customUI = mapObj.getDefaultUI();
		customUI.controls.maptypecontrol = false;
		customUI.controls.scalecontrol = false;
		return customUI;
	};
	
	this.setMapDefaults = function() {
		this.map = this.createMap();		
		this.map.setCenter(this.firstLocationsLatLng, this.map.getCurrentMapType().getMaximumResolution());
		this.map.setUI(this.setMapUI(this.map));	
	};
	
	this.addDealershipOverlay = function(overlayId) {
		this.map.addControl(getDealershipOverlayControl(overlayId));
	};
		
	var getDealershipOverlayControl = function(overlayId) {
		// DealershipOverlayControl - GControl that displays Nearest Dealership
		function DealershipOverlayControl() {
		}
	
		DealershipOverlayControl.prototype = new GControl(false, true);
		DealershipOverlayControl.prototype.initialize = function(mapObj) {
			var container = document.getElementById(overlayId);
			mapObj.getContainer().appendChild(container);
			return container;
		};

		/*
		 * Position the "Nearest dealership widget" in the map's top right
		 * corner with the 10px space from right and 5px space from top.
		 */
		DealershipOverlayControl.prototype.getDefaultPosition = function() {
			return new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(10, 5));
		};
		 
		return new DealershipOverlayControl();
	};
	
	this.zoomAndPanMap = function() {
		this.map.setZoom(this.map.getBoundsZoomLevel(this.bounds) - 1);
		this.map.setCenter(this.bounds.getCenter());

		// -135 is the space between center of the map and left edge of the map.
		this.map.panBy(new GSize(-135, 0));
	};
	
	/*
	 * Draw circle for the entered zip code. radius parameter is the distance
	 * selected by the user.
	 */
	this.drawCircle = function(center, radius, nodes) {
		var pInt, i;
		var points = [];

		// Converting kilometers into miles.
		var latConv = (center.distanceFrom(new GLatLng(center.lat() + 0.1, center.lng())) / 100) * .62;
		var lngConv = (center.distanceFrom(new GLatLng(center.lat(), center.lng() + 0.1)) / 100) * .62;
		var step = 360 / nodes;
		for (i = 0; i <= 360; i += step) {
			pInt = new GLatLng(center.lat()	+ 
						(radius / latConv * Math.cos(i * Math.PI / 180)),	center.lng() + 
						(radius / lngConv * Math.sin(i * Math.PI / 180)));
			points.push(pInt);
		}
		var poly = new GPolygon(points, this.CCOLOR, this.CWIDTH);
		this.map.addOverlay(poly);
	};

	/*
	 * Build and return popup pixelData object
	 */
	this.getPopupPixelDataObj = function(widgetName, popupName) {
		var popupPixelData = {
			widgetName : widgetName,
			popupName : popupName
		};
		return popupPixelData;
	};
}
