	var map=null;
	var hood_id=null;
	var hood_polygon=null;
	var hood_edit_polygon=null;
	var polylines = new Array();
	var polygons=Array();
	var add_click_event_handle="";
	var startZoom = 12;
	
	function mapinit(includenearby) {
		map = new GMap(document.getElementById("map"));
		map.addMapType(G_PHYSICAL_MAP);
		
		map.addControl(new GSmallMapControl());	
		map.setCenter(new GLatLng(centerLatitude, centerLongitude), startZoom);
		
		if($("hood") != null){	
			var hood = $('hood').value;
			hood_get_and_draw_nearby(hood);
		}
	}
	
	function hood_load() {
	  var hood = $('hood').value;
	  hood_get_and_draw(hood);
	}
	
	/* draw a neighborhood polygon
	----------------------------------------------------------------*/
	function hood_get_and_draw_nearby(id) {
	  hood_id=id;
	  var post_args = "action=get_polygons_plus_nearby&id=" + id;
	  var myXHR = new Request( { url:'/include/poly_map/php/controller_neighborhood.php?' + post_args ,onSuccess:function(resp){
	  var return_obj = JSON.decode(resp);
		if (return_obj["success"]==true){
			var hoods = return_obj["results"];
			var bounds = new GLatLngBounds();
			for(id in hoods) {
				var hood = hoods[id];
				var polygon = create_hood_polygon(id, hoods[id]);
				
				if(polygon.getBounds())
					bounds.extend( polygon.getBounds().getSouthWest() );
				
				if(polygon.getBounds())
					bounds.extend( polygon.getBounds().getNorthEast() );
			
				polygons.push(polygon);
			}
		
		/*
			  var center = bounds.getCenter();
			  var zoom = map.getBoundsZoomLevel(bounds);
		*/	 
			  var center='';
			  if(polygon.getBounds())
				center = hood_polygon.getBounds().getCenter();
				 var zoom = map.getBoundsZoomLevel(hood_polygon.getBounds());
				 map.setCenter( center, zoom );
				} else{
					alert(return_obj["message"]);
				}
		}}).send(post_args);
	}
	
	/* draw a neighborhood polygon
	----------------------------------------------------------------*/
	function hood_get_and_draw(id) {
	  hood_id=id;
	  var post_args = "action=get_polygons&id=" + id + "&includenearby=" + includenearby;
	  var myXHR = new Request( { url:'/include/poly_map/php/controller_neighborhood.php',onSuccess:function(resp){
	  var return_obj = JSON.decode(resp);
	
		if (return_obj["success"]==true){
		  for (var i = 0; i < return_obj["results"].length; i++ ) {
			var vertices = new Array();
			for (var j = 0; j < return_obj["results"][i].length; j++ ) {
			  lat = parseFloat(return_obj["results"][i][j].lat);
			  lng = parseFloat(return_obj["results"][i][j].lng);
			  vertices.push( new GLatLng(lat, lng) );
			}
			vertices.push(vertices[0]);
			polylines.push(vertices);
		  }
		  hood_polygon = hood_polygon_create(id, polylines, "#00FF00", 0.2);
		  hood_zoom_to();
		} else{
		  alert(return_obj["message"]);
		}
		}}).send(post_args);
	}
	
	/* draw a neighborhood polygon
	----------------------------------------------------------------*/
	function hood_clear() {
	  if (hood_polygon) {
	
		map.removeOverlay(hood_polygon);
		hood_polygon=null;
	  }
	}
	
	/* draw a neighborhood polygon
	----------------------------------------------------------------*/
	function hood_polygon_create(id, polylines, color_val, opacity) {
		polylineEncoder = new PolylineEncoder();
		var poly = polylineEncoder.dpEncodeToGPolygon(polylines,"#000000", 2,0.5,color_val, opacity, true, true);
		poly.hood = id;
		map.addOverlay(poly);
		return poly;
	}
	
	/* zoom to hood
	----------------------------------------------------------------*/
	function hood_zoom_to() {
	  if(hood_polygon) {	
		map.setZoom(map.getBoundsZoomLevel(hood_polygon.getBounds()));
		map.setCenter(hood_polygon.getBounds().getCenter());
	  }
	}
		
	/* enter edit mode
	----------------------------------------------------------------*/
	function hood_edit_start(id) {
	  if (hood_polygon) {
		if (hood_edit_polygon) {
		  map.removeOverlay(hood_edit_polygon);
		}
		hood_edit_polygon = hood_polygon_create(id, polylines, "#FF0000", 0.5);
		hood_edit_polygon.enableEditing();
	  }
	
	  $('sidebar-header-edit').style.display  = "block";
	  $('sidebar-header-views').style.display = "none";
	}
	
	/* cancel edit mode
	----------------------------------------------------------------*/
	function hood_edit_cancel(redraw) {
	  $('sidebar-header-views').style.display = "block";
	  $('sidebar-header-edit').style.display  = "none";
	
	  if (hood_edit_polygon) {
		hood_edit_polygon.disableEditing();
		map.removeOverlay(hood_edit_polygon);
		delete hood_edit_polygon;
		hood_edit_polygon = null;
	  }
	  if(redraw) hood_get_and_draw(hood_id);
	}
	
	
	/* restore the original polygon and clear out the working objects
	----------------------------------------------------------------*/
	function hood_edit_save() {
	  $('sidebar-header-views').style.display = "block";
	  $('sidebar-header-edit').style.display  = "none";
	
	  hood_edit_polygon.disableEditing();
	
	  var data = "";
	  var id   = hood_id;
	  polylines    = Array();
	  var vertices = new Array();
	  for (var i = 0; i < hood_edit_polygon.getVertexCount(); ++i) {
		var pnt = hood_edit_polygon.getVertex(i);
		vertices.push( pnt );
		data += '0,' + pnt.lat() + ',' + pnt.lng() + '|';
	  }
	
	  polylines.push(vertices);

	  hood_polygon = hood_polygon_create(id, polylines, "#00FF00", 0.2);
	  hood_zoom_to();
	
	  post = 'data=' + data +'&id=' + id;
	  var myXHR = new Request( { url:'/include/poly_map/php/update_arrays.php' }).send(post);
	  
	 // map.removeOverlay(hood_polygon);
	 // delete hood_polygon;
	
	  hood_edit_cancel(false);	  
	}
	
	/* clear all vertices from the working polyline and put map into 'add' mode
	----------------------------------------------------------------*/
	function hood_edit_clear() {
	 if (hood_edit_polygon) {
		hood_edit_polygon.disableEditing();
		map.removeOverlay(hood_edit_polygon);
		delete hood_edit_polygon;
		hood_edit_polygon = null;
	  }
	
	  var empty = Array();
	  hood_edit_polygon = new GPolygon(empty, "#000000", 2, 0.5, "#FF0000", 0.5);
	//  var poly = polylineEncoder.dpEncodeToGPolygon(polylines,"#000000", 2,0.5,color_val, opacity, true, true);
	  hood_edit_polygon.hood = hood_id;
	  map.addOverlay(hood_edit_polygon);
	  hood_edit_polygon.enableDrawing();
	}
	
	function create_hood_polygon(id, hood) {
	  var shape = hood['shape'];
	  var plines = new Array();
	  for (var i = 0; i < shape.length; i++ ) {
		var vertices = new Array();
	
		for (var j = 0; j < shape[i].length; j++ ) {
		  lat = parseFloat(shape[i][j].lat);
		  lng = parseFloat(shape[i][j].lng);
		  vertices.push( new GLatLng(lat, lng) );
		}
		vertices.push(vertices[0]);
		plines.push(vertices);
		
	  }
	
	  if (id == hood_id) {
		var opacity = 0.4;
		var color = heatToColor(1.0);
	  } else {
		var opacity = 0.2;
		var color = heatToColor( (id % 12) / 16 );
	  }
	
	  var polygon = hood_polygon_create(id, plines, color, opacity);
	  var content='<div id="hood-info-window" style="width:225px;"><a style="font-size:12px;" href="/converter.php?hood='+id+'&type=hood">' + hood['name'] + '</a><br><br>';
	
	  if(hood['rating'] >= 0 ){
		content+='<div class="thumbsup_diggs">' + hood['rating'] + '</div>';
	  }else{
		content+='<div class="thumbsdown_diggs">' + hood['rating'] + '</div>';
	  }
	
	  if (hood['comment_image']) {
		content+= '<div id="hoodpic">' + hood['comment_image'] + '</div>';
	  }
	
	  content+= hood['comment_body'];
	  content+='<div class="hood_lst_rentals"><a href="javascript:search_this_hood('+id+')">' + hood['rentals'] + ' Available Rentals &gt;&gt;</a></div>';
	  content+='</div>';
	  polygon.content = content;
	  GEvent.addListener(polygon, 'click', function(point) {
		if (point) {
		  map.openInfoWindowHtml(point, polygon.content);  // open info window where user clicked
		} else {
		   if(polygon.getBounds())
		  map.openInfoWindowHtml(polygon.getBounds().getCenter(), polygon.content);  // open info window at the center of polygon
		}
	  });
	
	  if (id == hood_id) {
		 hood_polygon = polygon;
		 polylines = plines;
	  }
	  return polygon;
	}
