GY.map = (function(parent, $) {
	
	var self = parent.map = parent.map || {};
	
	self.init = function(target, options) {
		this.options = $.extend({
			imageMarkerPath: '_img/marker.png',
			lat: 47.667477,
			lng: -122.385078,
			template: '<div><p>{{address}}<br /><strong>{{title}}</strong><br /><a href="{{url}}">View the Yodel</a></p><p><img src="{{thumb}}" width="100" height="100" /></p></div>',
			zoom: 3 
		}, options || {});
		
		this.target = document.getElementById(target);
		this.gmarkers = []; // holds all markers
		this.mapOptions = {
			maxZoom: 11,
			minZoom: 3,
			zoom: this.options.zoom,
		    center: new google.maps.LatLng(this.options.lat, this.options.lng),
		 	navigationControl: true,
			navigationControlOptions: { 
				style: google.maps.NavigationControlStyle.ZOOM_PAN,
				position: google.maps.ControlPosition.LEFT_CENTER
			},
		    scaleControl: true,
			scrollwheel: false,
			streetViewControl: false,
			mapTypeControl: false,
			mapTypeId: google.maps.MapTypeId.TERRAIN
		};
		this.map = new google.maps.Map(this.target, this.mapOptions); 
		
		this.gBounds = new google.maps.LatLngBounds();
		
		for (var i = 0, l = window.locales.length; i < l; i++) {
		    var locale = window.locales[i];
			this.addToMap(locale);
	  	}
		
		//google.maps.event.trigger(this.map, 'resize'); 
		
	};
	
	self.addToMap = function (locale) {
		
		var self = this; 
		var myLatLng = new google.maps.LatLng(locale.lat, locale.lng);
		var marker = new google.maps.Marker({
			position: myLatLng,
			map: this.map,
			animation: google.maps.Animation.DROP
	    });
		
		// extend the point to the others and tell google maps to fit it all within one window
		this.gBounds.extend(myLatLng);
		//this.map.fitBounds(this.gBounds);
		
		// push all markers into the gmarkers array - so we can reference it from outside the class
		this.gmarkers.push(marker);
		
		var infowindow = new google.maps.InfoWindow({
		    //content: template.evaluate(locale)
			content: $.mustache(self.options.template, locale)
		});
		
		activewindow = null;
		google.maps.event.addListener(marker, 'click', function(e) {
			
			if(activewindow) {
				activewindow.close();
			}
			activewindow = infowindow;
			infowindow.open(self.map, marker);
			
			self.map.panBy(0, -200);
			// if (self.map.getZoom() < 18) {
			// 	self.map.setZoom(18);
			// }
			//self.map.setCenter(new google.maps.LatLng(locale.lat, locale.lng));
		});
	};
	
	return self;
	
})(GY || {}, jQuery);
