/******************************************
 * The Google Map Class
 *
 * Adds a Google Map to the page with support for custom markers.
 ******************************************/

if (typeof crumbs == "undefined" || !crumbs) {
    var crumbs = {};
}

crumbs.GMap = Class.create({
	/**
	 * Constructor
	 *
	 * @param string mapId: the id of the map's html container
	 * @param string skinUrl: the path to the skin directory
	 * @param float centerLat: the latitude to center the map on
	 * @param float centerLong: the longitude to center the map on
	 * @param int zoom: zoom level
	 */
	initialize: function(mapId, skinUrl, centerLat, centerLong, zoom) {
		this.id = mapId;
		this.url = skinUrl;
		this._setupIcons();
		if ($(this.id)) {
			this.map = new google.maps.Map2($(this.id));
			this.map.addControl(new google.maps.LargeMapControl());
			var loadZoom = zoom ? zoom : 8;
			this.setCenter(centerLat, centerLong, loadZoom);
			//this.setCenter(38.2, -95, loadZoom);  // Center of the US
			this.modal = screenManager.getLocationModal();

			Event.observe(window, 'unload', google.maps.Unload);  // Helps IE avoid memory leaks
		}
	},

	/**
	 * Positions the center of the map.
	 *
	 * @param int latitude
	 * @param int longitude
	 * @param int zoom
	 */
	setCenter: function (latitude, longitude, zoom) {
		this.map.setCenter(new google.maps.LatLng(latitude, longitude), zoom);
	},

	/**
	 * Adds a location marker to the map;
	 *
	 * @param array locationArray
	 */
	addLocation: function (locationArray) {
		var container = document.createElement("div");
		container = $(container);
		container.addClassName(locationArray['containerClass']);

		var a = document.createElement("a");
		a = $(a);
		a.innerHTML = locationArray['title'];
		Event.observe(a, "click", this.modal.load.bindAsEventListener(this.modal, locationArray['url']));
		container.appendChild(a);

		this._addMarker(locationArray['type'], locationArray['latitude'], locationArray['longitude'], container);
	},

	/**
	 * Adds an array of location markers to the map.
	 *
	 * @param array locationsArray
	 */
	addLocations: function (locationsArray) {
		for (var i = 0; i < locationsArray.length; i++) {
			this.addLocation(locationsArray[i]);
		}
	},

	/**
	 * Adds a White Star marker to the coordinates passed.
	 *
	 * @param int latitude
	 * @param int longitude
	 * @param string html
	 */
	addWhiteStarMarker: function (latitude, longitude, html) {
		this._addMarker("whiteStar", latitude, longitude, html);
	},
	
	/**
	 * Adds a Red Star marker to the coordinates passed.
	 *
	 * @param int latitude
	 * @param int longitude
	 * @param string html
	 */
	addRedStarMarker: function (latitude, longitude, html) {
		this._addMarker("redStar", latitude, longitude, html);
	},

	/**
	 * Adds a Yellow Star marker to the coordinates passed.
	 *
	 * @param int latitude
	 * @param int longitude
	 * @param string html
	 */
	addYellowStarMarker: function (latitude, longitude, html) {
		this._addMarker("yellowStar", latitude, longitude, html);
	},

	/**
	 * Adds a Cupcake marker to the coordinates passed.
	 *
	 * @param int latitude
	 * @param int longitude
	 * @param string html
	 */
	addCupcakeMarker: function (latitude, longitude, html) {
		this._addMarker("cupcake", latitude, longitude, html);
	},

	_addMarker: function (type, latitude, longitude, html) {
		switch (type) {
			case "yellowStar":
				markerOpts = { icon: this.yellowStar };
				break;
			case "redStar":
				markerOpts = { icon: this.redStar };
				break;
			case "whiteStar":
				markerOpts = { icon: this.whiteStar };
				break;
			case "cupcake":
			default:
				markerOpts = { icon: this.cupcake };
		}
		var point = new google.maps.LatLng(latitude, longitude);
		var marker = new google.maps.Marker(point, markerOpts);
		marker.map = this.map;
		this.map.addOverlay(marker);

		var ewindow = new EWindow(this.map, E_CRUMBS); 
		this.map.addOverlay(ewindow);

		google.maps.Event.addListener(marker, "click", function () {
			ewindow.openOnMarker(marker, html);
			this.map.panTo(new google.maps.LatLng(marker.getPoint().lat(), marker.getPoint().lng()));
			//marker.openInfoWindowHtml(html);
		});
		google.maps.Event.addListener(this.map, "click", function () {
			ewindow.hide();
			//marker.closeInfoWindow();
		});
	},

	_setupIcons: function () {
		this.whiteStar = new google.maps.Icon();
		this.whiteStar.image = this.url + "images/icons/white_star.png";
		this.whiteStar.iconSize = new GSize(20, 18);
		this.whiteStar.iconAnchor = new GPoint(10, 10);
		this.whiteStar.infoWindowAnchor = new GPoint(5, 1);

		this.redStar = new google.maps.Icon();
		this.redStar.image = this.url + "images/icons/red_star.png";
		this.redStar.iconSize = new GSize(20, 18);
		this.redStar.iconAnchor = new GPoint(10, 10);
		this.redStar.infoWindowAnchor = new GPoint(5, 1);

		this.yellowStar = new google.maps.Icon();
		this.yellowStar.image = this.url + "images/icons/yellow_star.png";
		this.yellowStar.iconSize = new GSize(20, 18);
		this.yellowStar.iconAnchor = new GPoint(10, 10);
		this.yellowStar.infoWindowAnchor = new GPoint(5, 1);

		this.cupcake = new google.maps.Icon();
		this.cupcake.image = this.url + "images/icons/customer.png";
		this.cupcake.iconSize = new GSize(20, 18);
		this.cupcake.iconAnchor = new GPoint(10, 10);
		this.cupcake.infoWindowAnchor = new GPoint(5, 1);
	}
});
