var chart;
$(document).ready(function() {
	var updater_impressions;
	var updater_reach;
	var chart_reach;
	var beta = $('#measure input[name=beta]').length;

	function update_impressions(graph) {
		if (!graph.status) {
			updater_impressions.stop();
			if (graph.impressions) {
				if (graph.impressions.series[0].data.length > 1) {
					$.each(graph.impressions.series, function(i, v) {
						v.pointStart *= 1000;
						v.pointStart -= 18000000;
					});
					chart_reach.series[0].options.pointInterval = graph.impressions.series[0].pointInterval;
					chart_reach.series[0].options.pointStart = graph.impressions.series[0].pointStart;
					chart_reach.series[0].name = graph.impressions.series[0].name;
					chart_reach.series[0].setData(graph.impressions.series[0].data);
					var extremes = chart_reach.xAxis[0].getExtremes();
					chart_reach.xAxis[0].setExtremes(extremes.dataMin - graph.impressions.series[0].pointInterval / 2, extremes.dataMax + graph.impressions.series[0].pointInterval / 2);
				}
			}
			if (graph.impressions_total) {
			    $('#reach-impressions').html(graph.impressions_total);
			}
		} else if (graph.status == 'unavailable') {
			updater_impressions.stop();
		}
	}

	function update_reach(graph) {
		if (!graph.status) {
			updater_reach.stop();
			if (graph.reach) {
				if (graph.reach.series[0].data.length > 1) {
					$.each(graph.reach.series, function(i, v) {
						v.pointStart *= 1000;
						v.pointStart -= 18000000;
					});
					chart_reach.series[1].options.pointInterval = graph.reach.series[0].pointInterval;
					chart_reach.series[1].options.pointStart = graph.reach.series[0].pointStart;
					chart_reach.series[1].name = graph.reach.series[0].name;
					chart_reach.series[1].setData(graph.reach.series[0].data);
					var extremes = chart_reach.xAxis[0].getExtremes();
					chart_reach.xAxis[0].setExtremes(extremes.dataMin - graph.reach.series[0].pointInterval / 2, extremes.dataMax + graph.reach.series[0].pointInterval / 2);
				}
			}
			chart_reach.hideLoading();
			if (graph.reach_total) {
			    if ($('#reach-impressions').html()) {
			        $('#reach-impressions').html('This page received <strong>' + number_format($('#reach-impressions').html()) + '</strong> impressions with up to <strong>' + number_format(graph.reach_total) + '</strong> people on Twitter');
			        $('#reach-impressions').show();
			    }
			}
		} else if (graph.status == 'unavailable') {
			updater_reach.stop();
			$('#reach-area').remove();
		}
	}

	if ($('#measure input[name=p]').length) {
		mpmetrics.track($('#measure input[name=p]').val());
	}

	if ($('#graph-data').length) {
		var graph = $('#graph-data input[name=graph]').val();
		graph = jQuery.parseJSON(graph);
		var options = get_chart_options('/ajax', {
			renderTo: 'graph-area',
			defaultSeriesType: 'line',
			title: graph.title.text,
			subtitle: graph.subtitle.text,
			xAxis: {
				title: graph.xAxis.title
			},
			yAxis: {
				title: graph.yAxis.title
			},
			window: graph.window,
			interval: graph.interval,
			series: graph.series
		});
		chart = new Highcharts.Chart(options);
	}

	if ($('#reach-data').length && beta) {
		var graph = $('#reach-data input[name=graph]').val();
		graph = jQuery.parseJSON(graph);
		var options = get_minichart_options('/ajax', {
			renderTo: 'reach-area',
			defaultSeriesType: 'area',
			title: graph.title.text,
			subtitle: graph.subtitle.text,
			xAxis: {
				title: graph.xAxis.title
			},
			yAxis: {
				title: graph.yAxis.title
			},
			series: graph.series
		});
		chart = new Highcharts.Chart(options);
	} else if ($('#reach-area').length && beta) {
	    var options = get_minichart_options('/ajax', {
			renderTo: 'reach-area',
			defaultSeriesType: 'column',
			title: 'Twitter Reach & Impressions',
			subtitle: 'The number of unique people this URL reached with impressions',
			xAxis: {
				title: ''
			},
			yAxis: {
				title: ''
			},
			series: [{
				name: '',
		        data: []
		    }, {
				name: '',
				data: []
			}]
		});

		chart_reach = new Highcharts.Chart(options);
		chart_reach.showLoading();

		updater_impressions = $.PeriodicalUpdater('http://www.backtype.com/ajax/twitter/impressions_stats', {
			data: {
				q: $('#measure input[name=q]').val(),
				since: $('#measure input[name=since]').val(),
				until: $('#measure input[name=until]').val(),
				check: 1
			},
			maxTimeout: 10000,
			multiplier: 5,
			maxCalls: 100
		}, update_impressions);

		updater_reach = $.PeriodicalUpdater('http://www.backtype.com/ajax/twitter/reach_stats', {
			data: {
				q: $('#measure input[name=q]').val(),
				since: $('#measure input[name=since]').val(),
				until: $('#measure input[name=until]').val(),
				check: 1
			},
			maxTimeout: 10000,
			multiplier: 5,
			maxCalls: 100
		}, update_reach);
	}

	if ($('#followers-data').length) {
		var graph = $('#followers-data input[name=graph]').val();
		graph = jQuery.parseJSON(graph);
		var options = get_minichart_options('/ajax', {
			renderTo: 'followers-area',
			defaultSeriesType: 'area',
			title: graph.title.text,
			subtitle: graph.subtitle.text,
			xAxis: {
				title: graph.xAxis.title
			},
			yAxis: {
				title: graph.yAxis.title
			},
			series: graph.series
		});
		chart = new Highcharts.Chart(options);
	}

	if ($('#follows-unfollows-data').length) {
		var graph = $('#follows-unfollows-data input[name=graph]').val();
		graph = jQuery.parseJSON(graph);
		var options = get_column_options('/ajax', {
			renderTo: 'follows-unfollows-area',
			title: graph.title.text,
			subtitle: graph.subtitle.text,
			xAxis: {
				categories: graph.xAxis.categories
			},
			series: graph.series
		});
		chart = new Highcharts.Chart(options);
	}

    if ($('#fans-data').length) {
		var graph = $('#fans-data input[name=graph]').val();
		graph = jQuery.parseJSON(graph);
		var options = get_minichart_options('/ajax', {
			renderTo: 'fans-area',
			defaultSeriesType: 'area',
			title: graph.title.text,
			subtitle: graph.subtitle.text,
			xAxis: {
				title: graph.xAxis.title
			},
			yAxis: {
				title: graph.yAxis.title
			},
			series: graph.series
		});
		chart = new Highcharts.Chart(options);
	}

    if ($('#engagement-data').length) {
		var graph = $('#engagement-data input[name=graph]').val();
		graph = jQuery.parseJSON(graph);
		var options = get_minichart_options('/ajax', {
			renderTo: 'engagement-area',
			defaultSeriesType: 'area',
			title: graph.title.text,
			subtitle: graph.subtitle.text,
			xAxis: {
				title: graph.xAxis.title
			},
			yAxis: {
				title: graph.yAxis.title
			},
			series: graph.series
		});
		chart = new Highcharts.Chart(options);
	}

	if ($('#multigraph-data').length) {
		var graph = $('#multigraph-data input[name=graph]').val();
		graph = jQuery.parseJSON(graph);
		var options = get_multichart_options('/ajax', {
			renderTo: 'graph-area',
			defaultSeriesType: 'line',
			title: graph.title.text,
			subtitle: graph.subtitle.text,
			xAxis: {
				title: graph.xAxis.title
			},
			window: graph.window,
			interval: graph.interval,
			series: graph.series
		});
		chart = new Highcharts.Chart(options);
		var extremes = chart.xAxis[0].getExtremes();
		chart.xAxis[0].setExtremes(extremes.dataMin - graph.series[0].pointInterval / 2, extremes.dataMax + graph.series[0].pointInterval / 2);
	}

	$('.c').each(function(i, o) {
		$(o).show();
	});

	$('.sparkline-bar').each(function(i, o) {
		$(o).sparkline('html', get_spark_options());
	});
	$('.sparkline-minibar').each(function(i, o) {
		$(o).sparkline('html', get_spark_options('mini'));
	});
	$('.sparkline-bullet').each(function(i, o) {
		$(o).sparkline('html', get_spark_options('bullet'));
	});

	$('.graph-pie').each(function(i, o) {
		var options = get_pie_options(i, o);
		chart = new Highcharts.Chart(options);
	});

	if (chart) {
		$('.i').each(function(i) {
			var o = $(this);
			o.mouseover(function() {
				var obj = $('.i').get(i);
				chart.xAxis[0].addPlotBand({
					id: 'influence-band',
					value: obj.id.substring(1) * 1000,
					color: '#CCCCCC',
					width: 4
				});
			});
			o.mouseout(function() {
				chart.xAxis[0].removePlotBand('influence-band');
			});
		});
	}
});
