﻿var map = null;

var gMap = {

	_mapDiv: null,
	_loadRetry: 0,
	_oldLoad: null,
	_gPoint: null,
	IsLoaded: false,

	Load: function(divName, mapWidth, mapHeight) {
		// Global for this function 
		var refDocument = $(document);

		// Dispose map if exists 
		if (map != null) this._GDisposeMap();

		if (refDocument != null) {
			var e = $("#" + divName);
			if (e != null) {
				// Set our global variable 
				this._mapDiv = divName;
				// Load and Set Dispose 
				gMap._GDelayGetMap();
				$(window).unload(function() { gMap._GDisposeMap(); });

				//remove those buttons
				map.removeMapType(G_HYBRID_MAP);
				map.removeMapType(G_SATELLITE_MAP);
				map.removeMapType(G_NORMAL_MAP);
			} else {
				// Element does not exist 
			}
		} else {
			// Document reference does not exist 
		}
	},

	// ############################################################# 
	//  DELAY GET MAP 
	// ############################################################# 

	_GDelayGetMap: function() {
		try {
			if (GBrowserIsCompatible()) {
				map = new GMap2(document.getElementById(gMap._mapDiv));
				map.addControl(new GSmallZoomControl());
				map.addControl(new GMapTypeControl());

				GEvent.addListener(map, "click", gMap._GSetPushPin);
				gMap.IsLoaded = true;
			}
		} catch (err) {
			if (gMap._loadRetry < 30) {
				gMap._loadRetry++;
				setTimeout('gMap._GDelayGetMap()', 1000);
			} else {
				alert('Map loading timed out. Please refresh the page or try again later.');
			}

			gMap.IsLoaded = false;
		}
	},

	// ############################################################# 
	//  DISPOSE 
	// ############################################################# 

	_GDisposeMap: function() {
		if (map != null) {
			// Reset variables 
			GUnload();
			map = null;
			mapDiv = null;
			_oldLoad = null;
			_loadRetry = 0;
			IsLoaded = false;
		};
	},

	// ############################################################# 
	//  SET PUSHPIN 
	// #############################################################

	_GSetPushPin: function(marker, point) {
		if (marker == "GetPin") {
			map.clearOverlays();

			var gPoint = new GMarker(point, { draggable: false });

			map.addOverlay(gPoint);
			gMap._gPoint = gPoint;
		}
		//        if (marker) {
		//            map.removeOverlay(marker);
		//            _gPoint = null;
		//        } else {
		//            map.clearOverlays();

		//            var gPoint = new GMarker(point, { draggable: false });

		////            GEvent.addListener(gPoint, "mouseover", function() {
		////                gPoint.openInfoWindowHtml("No <strong>information</strong> at this <span style='color:red'>time</span>");
		////            });

		////            GEvent.addListener(gPoint, "dragstart", function() {
		////                gPoint.closeInfoWindow();
		////            });

		//            map.addOverlay(gPoint);
		//            gMap._gPoint = gPoint;
		//        }
	},

	getLatLon: function() {
		var ll = null;

		if (gMap._gPoint != null) {
			var loc = gMap._gPoint.getPoint();
			ll = { latitude: loc.lat(), longitude: loc.lng() };
		}

		return ll;
	},

	findLocation: function(s) {
		var locNotFound = 'Could not find location. Please try again.';
		var geocoder = new GClientGeocoder();

		geocoder.getLatLng(s,
			function(point) {
				if (!point) {
					//alert(locNotFound);
				} else {
					map.setCenter(point, 13);
					map.clearOverlays();

					gMap._GSetPushPin("GetPin", point);
				}
			});
	}
}