var GMap = (function () {
	var that = this;
	that.map;
	that.markers = new Array();
	that.overlays = new Array();
	that.lines = new Array();
	that.infoWindows = new Array();
	
	that.add_marker = function (geocodeString, options) {
		options = options == undefined ? {} : options;
		geocodeString += "";
		var parts = geocodeString.split(",");
		var geocode = new google.maps.LatLng(parseFloat(parts[0]),parseFloat(parts[1]));
		
		options.position = geocode;
		options.map = that.map;
		
		//if(options.icon) markerOptions.icon = options.icon;
		
		var marker = new google.maps.Marker(options);
		
		if(options.infoWindowContent != undefined) {
			
			if(_.isString(options.infoWindowContent)){
				var temp = "<div class='infoBubble'>" + options.infoWindowContent + "</div>";
			}else{
				var temp = options.infoWindowContent;
			}
			
			var infowindow = new google.maps.InfoWindow({
				content: temp
			});
			
			google.maps.event.addListener(marker, 'click', function () {
				that.close_info_windows();
				infowindow.open(that.map,marker);
			});
			
			if(options.openAtStart != undefined && options.openAtStart == true) {
				infowindow.open(that.map,marker);
			}
			
			that.infoWindows.push(infowindow);
		}
		
		if(options.click != undefined) {
			google.maps.event.addListener(marker, 'click', options.click);
		}
		
		that.markers.push(marker);
		return marker;
	};
	
	that.add_line = function (points,color,opacity,weight,geodesic,options) {
		color = color == undefined || color == null ? "#000000" : color;
		opacity = opacity == undefined || opacity == null ? 1.0 : opacity;
		weight = weight == undefined || weight == null ? 3 : weight;
		geodesic = geodesic == undefined || geodesic == null ? false : geodesic;
		
		
		
		var line = new google.maps.Polyline({
			path: points,
			strokeColor: color,
			strokeOpacity: opacity,
			strokeWeight: weight,
			geodesic: geodesic,
			click: function(){alert("hi")}
		});
		
		line.setMap(that.map);
		
		that.lines.push(line);
		
		return line;
	};
	
	this.add_kml = function (kmlFile, options) {
		var ctaLayer = new google.maps.KmlLayer(kmlFile + "&output=nl", options);
		ctaLayer.setMap(that.map);
		that.lines.push(ctaLayer);
	};
	
	this.add_overlay = function (overlay) {
		that.map.addOverlay(overlay);
	};
	
	this.get_map = function () {
		return that.map;
	};
	
	this.create_info_window = function (options) {
		var infoWindow = new google.maps.InfoWindow(options);
		that.infoWindows.push(infoWindow);
		return infoWindow;
	};
	
	that.clear_overlays = function () {
		for (var i = 0; i < that.markers.length; i++) {
			//that.markers[i].setMap(null);
			that.remove_overlay(that.markers[i]);
		}
		
		for (var i = 0; i < that.lines.length; i++) {
			//that.lines[i].setMap(null);
			that.remove_overlay(that.lines[i]);
		}
		
		for (var i = 0; i < that.overlays.length; i++) {
			//that.lines[i].setMap(null);
			that.remove_overlay(that.overlays[i]);
		}
		
		that.markers = new Array();
		that.lines = new Array();
		that.overlays = new Array();
	};
	
	that.remove_overlay = function (overlay){
		overlay.setMap(null);
	};
	
	that.close_info_windows = function () {
		for (var i = 0; i < that.infoWindows.length; i++) {
			that.infoWindows[i].close();
		}
		
		//that.infoWindows = new Array();
	};
	
	that.fit_bounds = function () {
		var bounds = new google.maps.LatLngBounds();
		
	 	for (var i = 0; i < that.markers.length; i++) {
			bounds.extend(markers[i].position);
		}
		that.map.fitBounds(bounds);
	};
	
	that.load = function () {
		var geocode = new google.maps.LatLng(42.932938,-85.670185);
		var options = {
			zoom: 11,
			center: geocode,
			mapTypeId: google.maps.MapTypeId.ROADMAP
		}
		that.map = new google.maps.Map(document.getElementById("mapCanvas"), options);
	};
	
	return that;
}());
