var locations;
var count = 0;
var totalLat = 0;
var totalLong = 0;

function setLocations() {
	// New coordinates can be retrieved from this site: http://itouchmap.com/latlong.html
	locations = [
		new Location("Flint","2199 S. Linden Rd. <br />Flint, MI 48532",42.99606,-83.771431,"(810) 733-7331"),
		new Location("Linwood","925 S Huron Rd. <br />Linwood, MI 48634",43.7121035,-83.9692176,"(989) 697-7008"),
		new Location("Midland","4906 N. Saginaw Rd.<br />Midland, MI 48640",43.641171,-84.287814,"(989) 835-4881"),
		new Location("Mt. Pleasant","4858 E. Pickard St<br />Mt Pleasant, MI 48858",43.6117323,-84.7509174,"(989) 773-2145"),
		new Location("Owosso","2085 E. M-21<br />Owosso, MI 48867",42.998272,-84.126455,"(989) 729-0044"),
		new Location("Saginaw (Bay Rd.)","6410 Bay Rd.<br />Saginaw, MI 48604",43.500343,-83.973242,"(989) 793-3390"),
		new Location("Saginaw (State St.)","6225 State St.<br />Saginaw, MI 48603",43.437060,-84.040371,"(989) 799-7030")
	];
	
	$(locations).each(function(){
		totalLat += this.latitude;
		totalLong += this.longitude;
	});
}

function Location(t,a,lt,ln,p) {
	this.title = t;
	this.address = a;
	this.latitude = lt;
	this.longitude = ln;
	this.phone = p;
	this.letter = String.fromCharCode(65+count);
	this.point = new google.maps.LatLng(this.latitude,this.longitude);
	this.windowHtml = "<a href=\"LocationDetails/"+this.title.replace(/\s/g,'-').replace(/[^\w\d\-]/g,'')+"\"><strong>"+this.title+" Store</strong></a><br />"+this.address+"<br />"+this.phone+"<br /><a target=\"_blank\" href=\"http://maps.google.com/maps?saddr="+this.address.replace(/(<([^>]+)>)/ig," ")+"\">Get Directions</a>";
	this.getIcon = function() {
		var icon = new google.maps.Icon();
		icon.iconSize = new google.maps.Size(20,34);
		icon.shadowSize = new google.maps.Size(37,34);
		icon.iconAnchor = new google.maps.Point(9,34);
		icon.infoWindowAnchor = new google.maps.Point(9,2);
		icon.image = "http://www.google.com/mapfiles/marker"+this.letter+".png";
		icon.shadow = "http://www.google.com/mapfiles/shadow50.png";
		
		return icon;
	};
	this.getMarker = function() {
		if (!this.marker) {
			var marker = new google.maps.Marker(this.point,this.getIcon());
			var html = this.windowHtml;
			var letter = this.letter;
			google.maps.Event.addListener(marker,"click",function(){ 
				$(".location").removeClass("selected");
				$(".location."+letter).addClass("selected");
				marker.openInfoWindowHtml(html);
			});
			this.marker = marker;
		}
		return this.marker;
	};
	this.element = $("#locations ."+this.letter);
	count++;
}

google.load("maps", 2);

google.setOnLoadCallback(function(){
	var map = new google.maps.Map2(document.getElementById("mapHolder"));
	setLocations();
	
	var center = new google.maps.LatLng((totalLat / locations.length)-.1,(totalLong / locations.length)-.2);
   	map.setCenter(center, 9);
   	map.addControl(new google.maps.LargeMapControl());
   	
   	$(locations).each(function() {
		map.addOverlay(this.getMarker());
		
		var marker = this.marker;
		var html = this.windowHtml;
		$(this.element).find(".icon a").click(function() {
			$(".location").removeClass("selected");
			$(this).parents(".location").addClass("selected");
			marker.openInfoWindow(html);
			return false;
		});
	});
	
	var params = location.search.substr(1).split("&");
	$(params).each(function(){
		var selected;
		var pair = this.split("=");
		if (pair[0] == "location") {
			switch (pair[1]) {
				case 'linwood':
					selected = 0;
					break;
				case 'midland':
					selected = 1;
					break;
				case 'mt-pleasant':
					selected = 2;
					break;
				case 'owosso':
					selected = 3;
					break;
				case 'saginaw-bay':
					selected = 4;
					break;
				case 'saginaw-state':
					selected = 5;
					break;
			}
			if (selected !== null) {
				selected = locations[selected];
				selected.marker.openInfoWindow(selected.windowHtml);
				$(selected.element).addClass("selected");
			}
		}
	});
});
