
/* MAP FUNCTIONS
*************************************************************/
function map() {
	//shortcut for encapsulation
	var self = this;
  
	// INIT VARIABLES
	self.map = null;
	self.map_div = document.getElementById("map");
	self.geocoder = null;
	self.marker_mgr = null;
	self.marker_base = "/_uploads";
	self.polygons = [];
	self.markers = [];
  
	//STARTING POINTS FOR THE MAP
	self.start_lat=51.042203393026064;
	self.start_lng=-114.05593872070312;
	self.current_zoom=14;
	self.current_marker = null;
	self.current_marker_text = "";
	
	self.init = function(new_lat,new_lng,new_zoom){
		if(new_lat != null){
			self.start_lat = new_lat;
		}
		if(new_lng != null){
			self.start_lng = new_lng;
		}
		
		if(new_zoom != null){
			self.current_zoom = new_zoom;
		}
		
		$.getJSON("/get_map_data/",
        function(data){
          self.polygons = data.polygons;
		  self.markers = data.markers;
		  self.setup_map();
        });
		
		return false;
	}
	
	self.setup_map = function(){	
			/*	
		//INITIALIZE THE MAP OBJECT
		var latlng = new google.maps.LatLng(self.start_lat, self.start_lng);
		var mapOptions = {
		  zoom: self.current_zoom,
		  center: latlng,
		  mapTypeId: google.maps.MapTypeId.ROADMAP
		};
		
		self.map = new google.maps.Map(self.map_div, mapOptions);

			*/

		if (GBrowserIsCompatible()) {	
		//INITIALIZE THE MAP OBJECT
			self.map = new GMap2(self.map_div);			
			self.map.addControl(new GSmallMapControl());
			self.map.enableContinuousZoom();
			self.map.enableDoubleClickZoom();				
			self.map.setCenter(new GLatLng(self.start_lat, self.start_lng), self.current_zoom);
			
			//INITIALIZE THE GEOCODER
			self.geocoder = new GClientGeocoder();	
			
			//INITIALIZE THE MARKER MANAGER
			self.marker_mgr = new MarkerManager(self.map);				
			
			//INITIALIZE THE OVERLAY POLYGONS
			self.init_polys();	
			
			//INITIALIZE THE OVERLAY MARKERS
			self.init_markers();
			
			GEvent.addListener(self.map, "moveend", function() {				
				var center = self.map.getCenter();
				var zoom = self.map.getZoom();
				if(zoom != self.current_zoom){
					self.current_zoom = zoom;
					self.map.clearOverlays();
 					self.init_polys();
				}
			});
		}
		return false;
	}
	
	self.init_markers = function(){
		if(self.markers.length > 0){
			$.each(self.markers, function(i,marker){ 
				self.setup_marker(marker);
			});
		}
		return false;
	}
	
	self.init_polys = function(){
		if(self.polygons.length > 0){
			$.each(self.polygons, function(i,polygon){ 
				self.setupPolygon(polygon);
			});
		}
		return false;
	}
	
	self.setupPolygon = function(polyobj){
		var latlng_array = new Array();
		if(polyobj){
			$.each(polyobj.points, function(i,poly){ 
				if(poly.lat){
					var tmppoint = new GLatLng(poly.lat,poly.lng);
					latlng_array.push(tmppoint);
				}
			}); 
			
			if(self.current_zoom <= polyobj.zoom[1]){
				current_alpha = polyobj.fill_opacity;
			} else {
				current_alpha = 0;
			}
			if(self.current_zoom >= polyobj.zoom[0]){
				polygon = new GPolygon(latlng_array, polyobj.line_color, polyobj.line_thickness, polyobj.line_opacity, polyobj.fill_color, current_alpha);
				self.map.addOverlay(polygon);
			}
			
			if(polyobj.marker){
				self.setup_marker(polyobj.marker);
			}
		}
		return true;
	}
	
	self.setup_marker = function(new_marker){
		if(new_marker.lat){
			var point = new GLatLng(new_marker.lat, new_marker.lng);
			var markers = [];
			var text="";
			var icon = "";
			if(new_marker.info.length == 0){
				text = "";
			} else {
				 text='<div style="background-color: #fff; width: 420px; font-size: 11px; font-family: Arial, Helvetica, sans-serif;">' + new_marker.info + '</div>';
			}
			
			if(new_marker.image){
				icon = self.getIcon({ width: new_marker.image.width, height: new_marker.image.height, url: self.marker_base + "/" + new_marker.id + "/" + new_marker.image.name });
				var marker = new GMarker(point, { title: text, icon: icon });
			} else {	
				var marker = new GMarker(point, { title: text });
			}
			markers.push(marker);
			
			self.marker_mgr.addMarkers(markers, new_marker.zoom[0], new_marker.zoom[1]);
			self.marker_mgr.refresh();		
			
			if(text.length > 0){
				GEvent.addListener(marker, "click", function() {
					marker.openInfoWindowHtml(text);
				});
				if(new_marker.lat==self.start_lat && new_marker.lng == self.start_lng){
					self.current_marker = marker;
					self.current_marker_text = text;
					setTimeout(self.open_marker, 100);
				}
			}
		}
		return false;
	}
	
	self.getIcon =    function (image) {
      var icon = null;
      if (image) {
          icon = new GIcon();
          icon.image = image.url;
          icon.iconSize = new GSize(image.width, image.height);
          icon.iconAnchor = new GPoint(image.width >> 1, image.height >> 1);
		  icon.infoWindowAnchor = new GPoint(image.width >> 1, image.height >> 1);
          icon.shadow = image.url;
          icon.shadowSize = new GSize(image.width, image.height);
	  }
      return icon;
    }
	
	self.open_marker = function(){
		self.current_marker.openInfoWindow(self.current_marker_text);
	}
  
}