var googleMap = (function () {
	'use strict';
	var obj, icon, shadow, shape, boundary;
	shadow = new google.maps.MarkerImage('/files/img/google/shadow.png',
				new google.maps.Size(34, 19),
				new google.maps.Point(0, 0),
				new google.maps.Point(0, 19));
	shape = {
		coord: [17, 39, 3, 25, 1, 15, 9, 4, 17, 2, 24, 4, 32, 14, 31, 26, 17, 39],
		type: 'poly'
	};
	icon = {
		"pin": {
			"icon": new google.maps.MarkerImage('/files/img/google/pin.png',
				new google.maps.Size(34, 41),   // This marker is 20 pixels wide by 32 pixels tall.
				new google.maps.Point(0, 0),    // The origin for this image is 0,0.
				new google.maps.Point(17, 41)), // The anchor for this image is the base of the flagpole at 0,32.
			"shadow": shadow,
			"shape": shape
		},
		"beach": {
			"icon": new google.maps.MarkerImage('/files/img/google/pin-beach.png',
				new google.maps.Size(34, 41),
				new google.maps.Point(0, 0),
				new google.maps.Point(17, 41)),
			"shadow": shadow,
			"shape": shape
		},
		"beachaccess": {
			"icon": new google.maps.MarkerImage('/files/img/google/pin-beachaccess.png',
				new google.maps.Size(34, 41),
				new google.maps.Point(0, 0),
				new google.maps.Point(17, 41)),
			"shadow": shadow,
			"shape": shape
		},
		"lifeguard": {
			"icon": new google.maps.MarkerImage('/files/img/google/pin-lifeguard.png',
				new google.maps.Size(34, 41),
				new google.maps.Point(0, 0),
				new google.maps.Point(17, 41)),
			"shadow": shadow,
			"shape": shape
		},
		"pointofinterest": {
			"icon": new google.maps.MarkerImage('/files/img/google/pin-pointofinterest.png',
				new google.maps.Size(34, 41),
				new google.maps.Point(0, 0),
				new google.maps.Point(17, 41)),
			"shadow": shadow,
			"shape": shape
		}
	};
	obj = {
		map: null,
		marker: null,
		markers: [],
		iterator: 1,
		overlays: [],
		mapCenter: {lat: 35.409284293261216, lng: -80.58103697872161},
		zoom: 12,
		canvas: 'mapCanvas',
		initialize: function (lat, lng) {
			var latLng, that = this, canvas = document.getElementById(this.canvas);
			if (!isNaN(lat) && !isNaN(lng)) {
				latLng = new google.maps.LatLng(lat, lng);
			} else {
				latLng = new google.maps.LatLng(this.mapCenter.lat, this.mapCenter.lng);
			}
			this.map = new google.maps.Map(canvas, {
				"zoom": this.zoom,
				"center": latLng,
				"mapTypeId": google.maps.MapTypeId.ROADMAP
			});
		},
		addMarkerToCenter: function () {
			if (!this.marker) {
				this.createMarker(this.map.getCenter());
			} else {
				this.map.panTo(this.marker.getPosition());
			}
		},
		setAnchorMarker: function (data) {
			var myCon = icon.pin;
			if (isNaN(data.lat) || isNaN(data.lng)) {
				return;
			}
			if (icon[data.icon]) {
				myCon = icon[data.icon];
			}
			this.marker = new google.maps.Marker({
				position: new google.maps.LatLng(data.lat, data.lng),
				title: data.title,
				map: this.map,
				animation: google.maps.Animation.DROP,
				shadow: myCon.shadow,
				icon: myCon.icon,
				shape: myCon.shape
			});
		},
		createMarker: function (data) {
			var that = this;
			if (isNaN(data.lat) || isNaN(data.lng)) {
				return;
			}
			setTimeout(function () {
				var myCon, infowin, newMarker, myTemplate;
				myCon = icon.pin;
				if (icon[data.icon]) {
					myCon = icon[data.icon];
				}
				newMarker = new google.maps.Marker({
					position: new google.maps.LatLng(data.lat, data.lng),
					title: data.title,
					map: that.map,
					animation: google.maps.Animation.DROP,
					shadow: myCon.shadow,
					icon: myCon.icon,
					shape: myCon.shape
				});
				that.markers.push(newMarker);

				if (data.title || data.summary) {
					if (data.url) {
						myTemplate = new Template('<div class="google-info-box"><h3><a href="#{url}">#{title}</a></h3><p>#{desc}</p></div>');
					} else {
						myTemplate = new Template('<div class="google-info-box"><h3>#{title}</h3><p>#{desc}</p></div>');
					}
					infowin = new google.maps.InfoWindow({
						content: myTemplate.evaluate({"title": data.title, "url": data.url, "desc": data.summary}),
						maxWidth: 200,
						pixelOffset: {width: 5, height: 40}
					});
					google.maps.event.addListener(newMarker, 'click', function () {
						infowin.close();
						infowin.open(that.map, newMarker);
					});
				}
			}, this.iterator * 150);
		},
		clearMarkers: function () {
			var i;
			if (this.markers) {
				for (i in this.markers) {
					if (this.markers.hasOwnProperty(i)) {
						this.markers[i].setMap(null);
					}
				}
			}
			this.markers = [];
		},
		setMarkers: function (arrayObj) {
			var i, that = this;
			this.iterator = 1;
			if (arrayObj instanceof Array) {
				for (i in arrayObj) {
					this.iterator++;
					this.createMarker(arrayObj[i]);
				}
			} else {
				this.createMarker(arrayObj);
			}
		},
		createOverlay: function (obj) {
			var poly = new google.maps.Polygon({
				paths: this.makeCoordinates(obj.points),
				strokeColor: obj.strokeColor,
				strokeOpacity: obj.strokeOpacity,
				strokeWeight: obj.strokeWeight,
				fillColor: obj.fillColor,
				fillOpacity: obj.fillOpacity
			});
			this.overlays.push(poly);
			poly.setMap(this.map);
		},
		setOverlays: function (obj) {
			var i, that = this;
			if (obj instanceof Array) {
				for (i in obj) {
					this.createOverlay(obj[i]);
				}
			} else {
				this.createOverlay(obj);
			}
		},
		clearOverlays: function () {
			var i;
			if (this.overlays) {
				for (i in this.overlays) {
					if (this.overlays.hasOwnProperty(i)) {
						this.overlays[i].setMap(null);
					}
				}
			}
			this.overlays = [];
		},
		makeCoordinates: function (points) {
			var tmp = [];
			points.each(function (point) {
				tmp.push(new google.maps.LatLng(point[0], point[1]));
			});
			return tmp;
		}
	};
	return obj;
})();
