var LocationsMap = 
{
	_map: null,
	_canvas: null,
	_list: null,
	
	Initialize: function(sDiv,sList)
	{
		LocationsMap._canvas = $(sDiv);
		LocationsMap._list = $(sList);
		if (!LocationsMap._canvas)
		{
			return;
		}
		
		var latlng = new google.maps.LatLng(47.189712,-121.486816);

		var myOptions = {
			zoom: 5,
			center: latlng,
			mapTypeId: google.maps.MapTypeId.ROADMAP
		};

		LocationsMap._map = new google.maps.Map(LocationsMap._canvas, myOptions);

//		var ctaLayer = new google.maps.KmlLayer('http://interfor.com/interfor.kml');
//		ctaLayer.setMap(LocationsMap._map);
		
		new Ajax.Request('/ajax/mapdata_json.php',
			{
				method: 'get',
				onSuccess: function(transport)
				{
					LocationsMap._getLocations_callBack(transport);
				},
				onFailure: function(transport)
				{
				}
			}
		);
	},

	_getLocations_callBack: function(transport)
	{
		var locations = transport.responseJSON;
		
		for (var i = 0; i < locations.length; i++)
		{
			var location = locations[i];
			var image = (location.style == 'mill') ? 'http://maps.google.com/mapfiles/ms/micons/tree.png' : 'http://maps.google.com/mapfiles/ms/micons/rangerstation.png';
			
			var myLatLng = new google.maps.LatLng(location.lat, location.long);

			var marker = new google.maps.Marker({
				position: myLatLng,
				map: LocationsMap._map,
				icon: image
			});

			// Generate left nav markup
			var listItem = new Element('li');
		 	var link = new Element('a',{'href':'#'}).update(location.name);
			LocationsMap._list.insert(listItem);
			listItem.insert(link);
			//listItem.insert(new Element('address').update(
			//	location.street + '<br>' +
			//	location.city + ', ' + location.province + '&nbsp; ' + location.postcode
			//));

			google.maps.event.addListener(marker, 'click', function() {  LocationsMap._showLocation(location,marker,link); });
			// add linkup to link
			Event.observe(link, 'click', LocationsMap._clickLocation.bindAsEventListener(this, location, marker));
		}
	},
	
	_showLocation: function(location,marker,link)
	{
//		var infowindow = new google.maps.InfoWindow({content: location.description});
//		infowindow.open(LocationsMap._map,marker);
	},
	
	_clickLocation: function(e, location, marker)
	{
		var mapList = $('map_list');
		mapList.update('<h3>' + location.name + '</h3>');
		if (location.image != '')
		{
			mapList.insert(new Element('img',{'src':location.image, 'width':'230'}));
		}
		mapList.insert(new Element('address').update(location.street + '<br>' + location.city + ', ' + location.province + '&nbsp; ' + location.postcode));
		
	    latLong = new google.maps.LatLng(location.lat,location.long);
	    LocationsMap._map.setZoom(parseInt(location.zoom));
	    LocationsMap._map.panTo(latLong);

		return false;
	}
	
}

Event.observe(window, 'load', function() { LocationsMap.Initialize('map_canvas','map_list_ul'); });

