$(function () {
	
	function locationObj(options) {
		this.id = options["id"];
        this.name = options["name"];
		this.address = options["address"];
		this.crossStreet = options["crossStreet"] || "";
		this.website = options["website"] || "";
		this.lat = options["lat"];
		this.lng = options["lng"];
		
		var point = new GLatLng(this.lat, this.lng);
		var marker = new GMarker(point, markerOptions);
		
		// Public methods
		this.getPoint = function() { return point; }
		this.getMarker = function() {  return marker; }
		this.getInfo = function() {
			if(this.website != "")
				return '<b>' + this.name + '</b><br />' + this.address + '<br />' + '<a href="' + this.website + '" target="_blank">Visit website</a>';
			else
				return '<b>' + this.name + '</b><br />' + this.address;
		}
		
		var location = this;
		GEvent.addListener(marker, "click", function() { openDialog(location) } );
        GEvent.addListener(marker, "infowindowclose", function() { $("#store-list li").removeClass("selected"); });
		function openDialog(location) {
			map.panTo(location.getPoint());
			location.getMarker().openInfoWindowHtml(location.getInfo());
            $("#store-list li").removeClass("selected");
            $("#"+location.id).addClass("selected");
            $("#store-list ul").scrollTo($("#"+location.id), 400 );
		}
	}
	
	// Create marker icon
	var markerIcon = new GIcon();
	markerIcon.image = "http://josiesbakesale.com/images/map/marker.png";
	markerIcon.shadow = "http://josiesbakesale.com/images/map/marker-shadow.png";
	markerIcon.iconSize = new GSize(12, 20);
	markerIcon.shadowSize = new GSize(22, 20);
	markerIcon.iconAnchor = new GPoint(6, 20);
	markerIcon.infoWindowAnchor = new GPoint(5, 1);
	markerOptions = { icon:markerIcon };
	
	if (GBrowserIsCompatible()) {
		var map = new GMap2($("#map")[0]);
		var mt = map.getMapTypes();
		
		map.setCenter(new GLatLng(37.77356423357254, -122.40777969360352), 13);
		
		// Overwrite the getMinimumResolution() and getMaximumResolution() methods
		for(var i=0; i<mt.length; i++) {
			mt[i].getMinimumResolution = function() {return 9;}
			mt[i].getMaximumResolution = function() {return 18;}
		}
		
		// Gather locations and their markers
		var locations = new Array();
		$.getJSON("/javascripts/locations.js", function(data) {
            (data.locations).sort(sortByName);
            $.each(data.locations, function(i, location) {
				locations.push(new locationObj(location));
				map.addOverlay(locations[i].getMarker());
				
				$('<li id="' + locations[i].id + '"><strong>' + locations[i].name + '</strong><br /><span>' + locations[i].address + ' <em>at ' + locations[i].crossStreet + '</em></span></li>').click(function() {
					map.panTo(locations[i].getPoint());
					locations[i].getMarker().openInfoWindowHtml(locations[i].getInfo());
					$("#store-list li").removeClass("selected");
					$(this).addClass("selected");
				}).appendTo("#store-list ul");
			});
		});
	}
    
    function sortByName(a, b) {
        var x = a.name.toLowerCase();
        var y = b.name.toLowerCase();
        return ((x < y) ? -1 : ((x > y) ? 1 : 0));
    }
});

$("body").unload(function () { GUnload(); });