/*

		GoogleMaps.js
		
		Mootools-based class to provide Google Maps functionality
		
		Written by:		Ken Kolodziej
		Created on:		September 1, 2010
		
*/


var GoogleMaps = new Class({
	options: {
		mapContainerID: '',
		points: [],
		defaultZoom: 10,
		keyboardShortcuts: false,
		mapType: 'ROADMAP',
		width: 0,
		height: 0
	},
	initialize: function(options) {
		this.setOptions(options);
		
		this.dirty = false;
		
		this.geoPoints = [];
	},
	injectMap: function() {
		// Get bounds
		var bounds = new google.maps.LatLngBounds();
		var theZoom = this.options.defaultZoom;
		
		if(this.options.points.length == 1)
			theZoom = 12;
		
		for(var i = 0; i < this.options.points.length; i++) {
			var pos = null;
			
			if(this.options.points[i].latitude != null && this.options.points[i].longitude != null
			   && this.options.points[i].latitude != 0 && this.options.points[i].longitude != 0)
				pos = new google.maps.LatLng(this.options.points[i].latitude, this.options.points[i].longitude);
			
			// Extend boundary with coords
			bounds.extend(pos);
			
			this.options.points[i].pos = pos;
		}
		
		// Set options
		var mapOpts = {
			//zoom: this.options.defaultZoom,
			mapTypeId: google.maps.MapTypeId[this.options.mapType],
			center: bounds.getCenter(),
			keyboardShortcuts: this.options.keyboardShortcuts,
			zoom: theZoom
			// zoom: this.options.defaultZoom
		}
		
		var map = new google.maps.Map(document.getElementById(this.options.mapContainerID), mapOpts);
		
		for(var i = 0; i < this.options.points.length; i++) {
			var marker = new google.maps.Marker({
				map: map,
				position: this.options.points[i].pos,
				title: this.options.points[i].label
			});
			
			// Create associated InfoWindow
			this.createInfoWindow(map, marker, this.options.points[i].labelText);
			
			// To add the marker to the map, call setMap();
			marker.setMap(map);
		}
	},
	createInfoWindow: function(map, marker, content) {
		var infowindow = new google.maps.InfoWindow({
			content: content
		});
		
		google.maps.event.addListener(marker, 'click', function() {
			infowindow.open(map, marker);
		});
	}
});
GoogleMaps.implement(new Options, new Events);
