var StationMap = {
	bounds: null,
	map: null,
	icon: null,
	stations: {},
	
	init: function(element, noscroll, lat, lng, zoom) {
		noscroll = noscroll || false;
		lat = lat || 0;
		lng = lng || 0;
		zoom = zoom || 4;
		
		this.map = new GMap2(element);
		this.map.setCenter(new GLatLng(lat, lng), zoom);
		this.bounds = new GLatLngBounds();
		this.map.addControl(new GSmallZoomControl());
		
		//this.icon = new GIcon(baseIcon);
		
		this.icon = new GIcon();
		this.icon.shadow = "http://www.google.com/mapfiles/shadow50.png";
		this.icon.iconSize = new GSize(20, 34);
		this.icon.shadowSize = new GSize(37, 34);
		this.icon.iconAnchor = new GPoint(9, 34);
		this.icon.infoWindowAnchor = new GPoint(9, 2);
		this.icon.infoShadowAnchor = new GPoint(18, 25);
		this.icon.image = "/static/img/google_maps_marker.png";
		
		if (!noscroll) {
			// fix so that map will get fixed position when scrolling outside of viewport
			var rightContainer = $("#searchresult_right");
			var mapContainer = $("#map_container");
			var startPosTop = rightContainer.offset().top;
			//var startPosLeft = rightContainer.offset().left;
			var undocked = false;
			$(window).scroll(function() {
				var viewportTop = $(window).scrollTop();
				if (viewportTop > startPosTop-5) {
					mapContainer[0].style.position = "fixed";
					mapContainer[0].style.left = rightContainer.offset().left + "px";
					mapContainer[0].style.top = "5px";
					undocked = true;
				} else if (undocked) {
					mapContainer[0].style.position = "relative";
					mapContainer[0].style.left = "";
					mapContainer[0].style.top = "";
					undocked = false;
				}
			});
		}
		return this.map
	},
	
	addStation: function(id, lat, lng) {
		var latlng = new GLatLng(lat,lng);
		var marker = new GMarker(latlng, this.icon);
		GEvent.addListener(marker, "mouseover", function() {
          StationMap.highlightStation(id, marker)
        });
		GEvent.addListener(marker, "mouseout", function() {
          StationMap.deHighlightStation(id, marker)
        });
		this.map.addOverlay(marker)
		this.bounds.extend(latlng);
		
		this.stations[id] = {marker: marker};
		
		$(".station_id_" + id).mouseover(function() {
			StationMap.highlightMarker(marker)
		});
		$(".station_id_" + id).mouseout(function() {
			StationMap.deHighlightMarker(marker)
		});
	},
	
	highlightStation: function(id, marker) {
		$(".station_id_" + id).addClass("station_highlight");
		marker.setImage("/static/img/google_maps_marker_green.png");
	},
	
	deHighlightStation: function(id, marker) {
		$(".station_id_" + id).removeClass("station_highlight");
		marker.setImage("/static/img/google_maps_marker.png");
	},
	
	highlightMarker: function(marker) {
		marker.setImage("/static/img/google_maps_marker_green.png");
	},
	deHighlightMarker: function(marker) {
		marker.setImage("/static/img/google_maps_marker.png");
	},
	
	setAutoZoom: function() {
		this.map.setZoom(this.map.getBoundsZoomLevel(this.bounds));
		this.map.setCenter(this.bounds.getCenter());
	}
};

