// TODO: Add class="selected" to checkboxes in the filters list that get or load selected.  Add "first" and/or "last" to each one if necessary to add the border to the top and bottom respectively, as well.
var tidesProjDir = {
	init: function() { 
		this.projMap.initMap();
		this.initListToggle();
		this.initFilterToggle();
	},
	initListToggle : function() {
		if($('projListWrapper')) {
			if($('projListWrapper').getHeight() > 400) {
				$('projListWrapper').addClassName('isMin');
				$('maxList').show();
			}
			$('minList').observe('click',function() {
				if($('projListWrapper').hasClassName('isMax')) {
					$('projListWrapper').addClassName('isMin');
					$('projListWrapper').removeClassName('isMax');
					$('minList').hide();
					$('maxList').show();
				}
			})
			$('maxList').observe('click',function() {
				if($('projListWrapper').hasClassName('isMin')) {
					$('projListWrapper').addClassName('isMax');
					$('projListWrapper').removeClassName('isMin');
					$('maxList').hide();
					$('minList').show();
				}			
			})					
		}
	},
	initFilterToggle : function() {
		
		if($('areaFilters').hasClassName('areaFiltersShow') == false) {
			$('areaFilters').hide();
			$('filterSubmit').hide();			
		}		
		
		$('minFilterList').observe('click',function() {			
			$('areaFilters').hide();
			$('maxFilterList').show();
			$('minFilterList').hide();
			$('filterSubmit').hide();			
		})
		$('maxFilterList').observe('click',function() {
			$('areaFilters').show();
			$('maxFilterList').hide();
			$('minFilterList').show();
			$('filterSubmit').show();			
		})		
	},
	// the map object
	projMap : {
		assetDir: 'http://tidescenter.org/typo3conf/ext/projdir/assets/img/',
		pins: new Array(),
		initMap: function() {
			if ($('mapDiv') && GBrowserIsCompatible()) {
				// create map object
			    this.map = new GMap2(document.getElementById("mapDiv"));
				var center = new GLatLng(37.0902, -95.7129);			
				this.map.setCenter(center, 4);
				this.map.addControl(new GLargeMapControl());
				this.map.addControl(new GMapTypeControl());

				// create our pin icons
				this.bluePin = new GIcon(G_DEFAULT_ICON);
				this.greenPin = new GIcon(G_DEFAULT_ICON);
				this.redPin = new GIcon(G_DEFAULT_ICON);
				var iconSize = new GSize(37,35);
				var shadowImg = this.assetDir + 'map_pin_shadow.png';

				this.redPin.image = this.assetDir + 'map_pin_red.png';
				this.greenPin.image = this.assetDir + 'map_pin_green.png';
				this.bluePin.image = this.assetDir + 'map_pin_blue.png';

				this.redPin.shadow = shadowImg;
				this.greenPin.shadow = shadowImg;
				this.bluePin.shadow = shadowImg;

				this.redPin.iconSize = iconSize;
				this.greenPin.iconSize = iconSize;
				this.bluePin.iconSize = iconSize;

				this.displayPins();				
			}		
		},
		displayPins: function() {
			this.pins.each(function(pin,index) {
				var color = pin.get('color');
				var lat = pin.get('lat');
				var lng = pin.get('lng');
				var txt = pin.get('txt');
				txt.set('toLink','http://maps.google.com/maps?f=d&hl=en&geocode=&daddr=' + lat + ',' + lng);
				txt.set('fromLink','http://maps.google.com/maps?f=d&hl=en&geocode=&saddr=' + lat + ',' + lng);
				if ($('mapDiv') && GBrowserIsCompatible()) {
					if(color == 'red') {
						markerOptions = { icon:tidesProjDir.projMap.redPin};
					}
					if(color == 'blue') {
						markerOptions = { icon:tidesProjDir.projMap.bluePin};			
					}
					if(color == 'green') {
						markerOptions = { icon:tidesProjDir.projMap.greenPin};			
					}
					var point = new GLatLng(lat,lng);
					var marker = new GMarker(point,markerOptions);
					var template = new Template('<br />#{address}<br /><br />Get directions: <a target="blank" href="#{toLink}">To here</a> <a target="blank" href="#{fromLink}">From here</a>');
					
					GEvent.addListener(marker, "click", function() {						
						marker.openInfoWindowHtml(template.evaluate(txt)); // TODO: max width not working. why?
					});
					tidesProjDir.projMap.map.addOverlay(marker);
				}				
			});
		},
		addPin: function(color,lat,lng,txt) {
			var hash = $H({'color':color,'lat':lat,'lng':lng,'txt':txt});
			this.pins.push(hash);
		}				
	}
};

Element.observe(document, 'dom:loaded', function(){
	tidesProjDir.init();
});
