(function(){
	
	var map
	var geocoder = new google.maps.Geocoder();
	var currentInfo;
	
	
	var fxScroll;
	
	
	// Display the correct info window and hide the previous one
	var displayInfo = function(marker, infoWindow){
		infoWindow.open(map, marker);
		if (currentInfo && currentInfo !== infoWindow) currentInfo.close();
		currentInfo = infoWindow;
	};


	var addPoint = function(name, address, icon){
		if (geocoder) {
			geocoder.geocode({
				'address': address + ' Seattle, WA'
			}, function(results, status){
				if (status == google.maps.GeocoderStatus.OK) {
					if (status != google.maps.GeocoderStatus.ZERO_RESULTS){
						
						// create icon to use with the marker
						var image = new google.maps.MarkerImage(icon,
							new google.maps.Size(21, 25), // Size
							new google.maps.Point(0, 0), // Origin
							new google.maps.Point(21, 25) // Anchor
						);
						
						// Create a new marker at the correct location
						var marker = new google.maps.Marker({
							map: map, 
							position: results[0].geometry.location,
							icon: image
						});
						
						// Create an info window for the marker with the name and address
						var infoWindow = new google.maps.InfoWindow({
							size: new google.maps.Size(200, 25),
							content: '<div class="infoWindow"><strong>' + name.get('html') + '</strong><address>' + address + '</address></div>'
						});
						
						// Add a click listener to the marker to open the info window and close the previous one
						google.maps.event.addListener(marker, 'click', function(){
							displayInfo(marker, infoWindow);
						});
						
						// Add the same event to the list item's name
						name.getPrevious().addEvent('click', function(){
							fxScroll.toTop();
							displayInfo(marker, infoWindow);
						});
						
					}
					else{
						alert("No results found");
					}
				}
				/*else{
					alert("Geocode was not successful for the following reason: " + status);
				}*/
			});
		}
	};
	
	
	// force ie6 sidebar to be correct height. called every time I change the display of an item.
	var fixSidebar = function(){
		if (Browser.Engine.trident4) {
			var right = $('content-right');
			right.setStyle('height', right.getParent().getSize().y);
		}
	};
	
	
	
	
	// load on domready
	window.addEvent('domready', function(){
		
		/*
		initialise the google map
		*/
		map = new google.maps.Map(document.getElementById("map_canvas"), {
			zoom: 15,
			center: new google.maps.LatLng(47.60868028567026, -122.34042406082153),
			mapTypeId: google.maps.MapTypeId.ROADMAP,
			mapTypeControl: false
		});
		
		fxScroll = new Fx.Scroll(window);
		
		/*
		This loops through all the list item offers and plots their locations on the map using the function above
		*/
		var container = $pick($('offers'), $('attractions'), $('happyhour'));
		container.getElements('div li').each(function(li){
			var name = li.getElement('h3');
			var address = li.getElement('address').get('html');
			var icon = li.getElement('img').get('src');
			addPoint(name, address, icon);
		});
		
		/*
		fix the sidebar height for ie6
		*/
		fixSidebar();
		
		
		/*
		If we're on the deals page set the print offers button to bring up the print dialoge
		*/
		var offers = $('offers');
		
		if (offers){
			// loop through the list items
			offers.getElements('li').each(function(li){
				// add the noprint class
				li.addClass('noprint');
				
				// find the checkbox
				var input = li.getElement('input');
				
				// turn the checkbox off in case the page has been reposted
				input.erase('checked');
				
				// add the click event to toggle the noprint class
				input.addEvent('click', function(){
					li.toggleClass('noprint');
				});
			});
			
			// bring up the print dialouge when the submit button is clicked
			var printOffers = $('print-offers');
			if (printOffers){
				printOffers.addEvent('click', function(){
					window.print();
					return false;
				});
			}
		}
		
	});

})();