function initialize(id,agency) 
{
	if(typeof(agency) != 'undefined' && agency != 0)
	{
		$("#agency").val(agency);
	}
	if($('#agency').val() == '')
	{
		$('#hours').attr('checked',true);
		$('#date').attr('disabled',true);
	}
	//setup event handlers
	$("#traffic").click(get_data);
	//$("#graph").click(get_data);
	$("#hours").click(function() {
		$('#date').val('');
		get_data();
	});
	$("#table_option").change(function() {
		get_data();
	});
	$("#agency").change(function() {
		get_data();
		//var agency_name = $('#agency :selected').text();
		//agency_name = agency_name.replace(/(\s+)/g,"");
		//agency_name = agency_name.replace(/((\'s)|(Police))Department/,"");
    	//pageTracker._trackPageview('/agency/' + $('#agency :selected').text());
	});

	get_data(id);
}

function get_data(id)
{
	//agency = typeof(agency) != 'undefined' ? a : '';
	id = (typeof(id) != 'undefined' && typeof(id) != 'object') ? id : 0;
	var agency = $('#agency').val();
	var traffic = $('#traffic').is(":checked");
	//var graph = $('#graph').is(":checked");
	var graph = 0;

	if(typeof(agency) == 'undefined')
	{
		agency = $('#agency').val();
	}
	if(agency == '')
	{
		$('#hours').attr('checked',true);
		$('#date').attr('disabled',true);
	}
	else
	{
		$('#date').attr('disabled',false);
	}
	
	//setup the ajax call
	var url = "/get.php?agency="+agency;
	if(id == 0)
	{
		if(agency == '' || $('#hours').is(":checked"))
		{
			url += "&hours=" + $('#hours').val();
		}
		if($('#date').is(":checked"))
		{
			url += "&date=" + $('#date').val();
		}
		if(traffic)
		{
			url += "&traffic=1";
		}
		if(graph)
		{
			url += '&graph=1'
		}
	}
	else
	{
		url += "&id=" + id;
	}
	//alert(url);
	$.getJSON(url,function(data){
		$('#calls_head').html('');
		$('#calls_body').html('');
		if(graph)
		{
			draw_graph(data);
		}
		else
		{
			var table_row = '<tr><th>Date/Time</th><th>Description</th><th>Location</th><th>City</th></tr>';
	 		$('#calls_head').append(table_row);
			add_map_points(data);
		}
	});
}

function draw_graph(data)
{
	//console.log(data);
	var title = 'Call+Descriptions';
	var url = 'http://chart.apis.google.com/chart?';
	url += 'chs=700x400';
	url += '&amp;chd=t:' + data.totals;
	url += '&amp;chds=' + data.range;
	url += '&amp;chxl=0:|0|' + data.labels + '|';
	url += '&amp;chxp=0,0,' + data.positions;
 	url += '&amp;chxt=y';
	//url += '&amp;chdl=|' + data.legend;
	url += '&amp;cht=bvs&amp;chts=000000,20&amp;chco=0000FF';
	url += '&amp;chtt=' + title;
	$('#map_canvas').html('<img src=' + url + ' alt=' + title + '/>');
}

function add_map_points(data) 
{
	if (GBrowserIsCompatible()) 
	{
		geoXml = new GGeoXml("http://www.occrimemap.com/OCBorderRed.kml");
		map = new GMap2(document.getElementById("map_canvas"));
		geocoder = new GClientGeocoder();
		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());
		//default to a full view of orange county
		map.setCenter(new GLatLng(33.681725, -117.775496), 10);
		map.openInfoWindow(map.getCenter(),
        document.createTextNode("OCCrimeMap.com"));
		map.addOverlay(geoXml);
	}
	var table_option = $('#table_option').val();
	var total = data.count;	
	var id = 0;
	var mapped_total = 0;
	var unmapped_total = 0;
	map.clearOverlays();
	map.addOverlay(geoXml);
	var row_class = 'even';
	//console.log(data);
	if(data.center != 0)
	{
		map.setCenter(new GLatLng(data.center.latitude, data.center.longitude), 10);
	}
	//default to a full view of orange county
	else
	{
		map.setCenter(new GLatLng(33.681725, -117.775496), 10);
	}
	$.each(data.calls, function(key,call){
		if(call.latitude != 0) {
			var address = call.location + ' ' + call.city + ', CA';
			var point = new GLatLng(call.latitude,call.longitude);
			//if its a single call, center the map around it
			if(total == 1) {
				map.setCenter(point);
				map.setZoom(16)
				id = call.id;
			}
			var info = '<img src="/images/magnifier.png" class="icon"/>&nbsp;<b>' + call.dept_name + '</b>';
			info += '<br />' + address;
			info += '<br />' + call.description;
			info += ' - ' + call.complaint;
			info += '<br />' + call.dispatch_time;
			show_address(point,info,id);
			if(table_option == 'All' || table_option == 'Mapped')
			{
				(row_class == 'even') ? row_class='odd' : row_class='even';
				add_row(call,row_class);
			}
			mapped_total++;
		}
		else if(table_option == 'All' || table_option == 'Unmapped')
		{
			(row_class == 'even') ? row_class='odd' : row_class='even';
			add_row(call,row_class);
		}
	});
	//unmapped_total = total - mapped_total;
	var stats = mapped_total + '/' + total;
	$('#stats').html(stats);

}

function show_address(point,info,id) 
{
	var icon = new GIcon(G_DEFAULT_ICON);
	icon.image = "/images/map_pins/pin_red.png";
	icon.size = new GSize(36, 36);
	var marker = new GMarker(point);//,icon);
	map.addOverlay(marker);
	GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowHtml(info);
	});
	if(typeof(id) != 'undefined' && id != 0) 
	{
		marker.openInfoWindowHtml(info);	
	}
}

function add_row(call,row_class) 
{
	var link = '';
	if(call.latitude != 0 && call.longitude != 0)
	{
		link = '<a href="/call/' + call.id + '"><img src="/images/magnifier.png" /></a>&nbsp;';
	}
	var table_row = '<tr class="' + row_class + ' call_row">';
	table_row += '<td>' + link + call.dispatch_time + '</td>';
	table_row += '<td>' + call.description + '</td>';
	table_row += '<td>' + call.location + '</td>';
	table_row += '<td>' + call.city + '</td>';
	table_row += "</tr>\n";
	 $('#calls_body').append(table_row);
}
