<?php

/**
* @desc This report displays in what countries your facebook application/page is viewed 
*/
$reports["_FACEBOOK_CITIES"] = Array(
    "ClassName" => "FacebookPageCities", 
    "Category" => "_FACEBOOK", 
    "icon" => "images/icons/32x32/fb_location.png",
	"Options" => "daterangeField,displaymode,limit",
	"Filename" => "facebook_page_cities",
	"Distribution" => "Premium",
	"Order" => 15,
	"ReportVersion" => 4.0,
	"MinimumVersion" => 3.0,
	"EmailAlerts" => false,
	"Update" => false
);

class FacebookPageCities extends FacebookCore {
	
	function Settings() {
		parent::Settings();
		$this->DisplayModes = "pie,areachart";
		$this->DefaultDisplay = "areachart";
		if(empty($this->displaymode)){
			$this->displaymode = $this->DefaultDisplay;
		}
		$this->addlabel = _FACEBOOK_CITIES_ADDLABEL;
		$this->help = _FACEBOOK_CITIES_HELP;
		$this->metric = "cities";
	}	
    
	function DisplayPie(){
		?><script type="text/javascript">
		function lg_fb_<?php echo $this->graphID; ?>(container_id, result){
			if(result.Status == undefined || result.Status != "Success"){
				return;
			}

			var data = result.data;
			if(!data || data.values === undefined){
				console.log("No data");
				return;
			}	

			var graph_data = [];

			var max = data.values.length - 1;
			data = data.values[max].value;

			var i = 0;
			$.each(data, function(k,v){
				if(i < <?php echo $this->limit; ?>){
					graph_data[i] = [k ,v];
					i++;
				}
			});
			$("#"+container_id).attr("data", JSON.stringify(graph_data) );

			lg.Pie(container_id);
		}
		</script><?php
	}

	function DisplayGraph(){
		?><script type="text/javascript">
		function lg_fb_<?php echo $this->graphID; ?>(container_id, result){
			if(result.Status == undefined || result.Status != "Success"){
				return;
			}

			
			var data = result.data;
			if(!data || data.values === undefined){
				console.log("No data");
				return;
			}	

			var limit = <?php echo $this->limit; ?>;

			var dataSet = {};
			var graph_data = [];
			var columns = [];
			var xticks = [];
			var totalColumns = 0;
			var columnsCheck = {};

			for (var i = 0; i < data.values.length; i++) {
				var row = data.values[i];

				xticks.push(d3.time.format('<?php echo GetCustomDateFormat("D3"); ?>')(new Date(row.end_time)));

				var ii = 0;

				$.each(row.value, function(a,b){
					if(ii >= limit){
						return;
					}
					ii ++;

					if(dataSet[ a ] == undefined){
						dataSet[ a ] = {
							key : a,
							values : []
						}
						columns.push({ "Label" : a });
					}			

					dataSet[a].values.push( [i , parseInt(b)] );

					columnsCheck[i] = 0;

					if(totalColumns < dataSet[a].values.length){
						totalColumns = dataSet[a].values.length;
					}			
				});

			}

			function LookForDate(date , values){
				var c = false;
				$.each(values, function(k,v){
					if(parseInt(v[0]) == parseInt(date) ){
						c = v[1];

					}
				});
				return c;
			}

			$.each(dataSet, function(k,v){
				// we are missing columns for this record
				if(v.values.length < totalColumns){
					// lets check all the columns
					var n = [];
					$.each(columnsCheck, function(a,b){
						var c = LookForDate(a, v.values);
						if(c === false){
							n.push([parseInt(a), 0]);
						} else {
							n.push([parseInt(a), c]);
						}
					});
					v.values = n;
				}
				graph_data.push(v);
			});

			$("#"+container_id).attr("data-coldefs", JSON.stringify(columns));
			
			$("#"+container_id).attr("data-chartoptions", JSON.stringify({
				"stacked_graph_style" : "stream", "stacked_showcontrols" : true, "showlegend" : false
			}));

			$("#"+container_id).attr("data-ticks", JSON.stringify(xticks) );
			$("#"+container_id).attr("data-chart", JSON.stringify(graph_data) );

			lg.StackedAreaChart(container_id);
		}
		</script><?php
	}
}
?>