// Behaviors ===================================================================
//==============================================================================
var Neighborhood = Behavior.create(RichmondCrime, {
	initialize: function($super, settings) {
		this.settings = settings;
		if(this.settings.get('onReady')) {
			this.onsubmit();
		}
	},
	onsubmit: function($super, e) {	
		this.clearMap();
		this.startDate = $F('start_dateYear') + '-' + $F('start_dateMonth') + '-' + $F('start_dateDay');
		this.endDate = $F('end_dateYear') + '-' + $F('end_dateMonth') + '-' + $F('end_dateDay');	
	
		//get and autoplot the markers
		var ids = $F('ids');
		this.__getMarkers({				
			url: $F('base_url') + '/incidents/neighborhood/' + ids + '/start_date:' + this.startDate + '/end_date:' + this.endDate + '.json',
			method: 'post',
			appendResults: false,
			successCallback: this.__plotAndList.bind(this)						
		});
		return false;
	},
	__getNeighborhoodPoly : function(id) {
		var polygon_url = $F('base_url') + '/neighborhoods/view/' + id + '.json';
		new Ajax.Request(polygon_url, {
			method:'get',
			onSuccess: function(transport){
				var json = transport.responseText.evalJSON();
				var bounds = new GLatLngBounds();
				json.each(function(n){			
					var polygonLines = [];					
					var points = n.Neighborhood.polygon.split(' ');				
					points.each(function(p) {
						var tmp = p.split(',');
						bounds.extend(new GLatLng(tmp[1], tmp[0]));
						polygonLines.push(new GLatLng(tmp[1], tmp[0]));
					});
					var polygon = new GPolygon(polygonLines, "#0052A5", 1, 1, "#0052A5", 0.2);
					map.addOverlay(polygon);
					this.polys.push(polygon);
					map.setZoom(map.getBoundsZoomLevel(bounds));
					map.setCenter(bounds.getCenter());
				}.bind(this));
			}.bind(this)
		});		
	},
	__plotAndList: function (response) {	
		this.__ajaxPlotMarkers(response);
		this.__getNeighborhoodPoly($F('ids'));			
		//this.centerAndZoom(this.points);		
		var list = this.__buildCrimeList(this.data);
		$('incidentListContainer').appear();		
		$('incidentListContainer').update(list);		
	}	
});

// Events ======================================================================
//==============================================================================
Event.addBehavior({
	'#chartLink:click' : function(e) {
		var startDate = $F('start_dateYear') + '-' + $F('start_dateMonth') + '-' + $F('start_dateDay');
		var endDate = $F('end_dateYear') + '-' + $F('end_dateMonth') + '-' + $F('end_dateDay');		
		var chartUrl = 'http://richmondcrime.org/' + $F('base_url') + '/charts/view/OffenseDescriptionPie/neighborhood:' + $F('ids') + '/start_date:' + startDate + '/end_date:' + endDate + '.json';
		console.log(chartUrl);
		swfobject.embedSWF(
		  $F('base_url') + "/flash/open-flash-chart.swf", "chart", "440", "440",
		  "9.0.0", "expressInstall.swf",
		  {"data-file": chartUrl}
		  );		
		
		return false;
	}
});


//OnReady ======================================================================
//==============================================================================
Event.onReady(function() {
	var n = new Neighborhood($('neighborhoodForm'), $H({
		onReady: true
	}));
});