$(document).ready(function(){
	var ctx = $("#ctx").val();
	var flow=[];
	var tipLabel=new Array("PV","UV","Impression","Click","Income");
	var pv=tipLabel[0];
	$.ajax({
		type : 'post',
		url : ctx+'/operator/main/dashboard/list',
		async : false,
		cache : false,
		dataType : 'json',
		success : function(data){
			var pvArray = [];
			var uvArray = [];
			var impressionArray = [];
			var clickArray = [];
			var incomeArray = [];
			$.each(data,function(n,value) {
				pvArray[6-n] = value.pv;
				uvArray[6-n] = value.uv;
				impressionArray[6-n] = value.impression;
				clickArray[6-n] = value.click;
				incomeArray[6-n] = value.income;
			});
			flow[0] = pvArray;
			flow[1] = uvArray;
		 	flow[2] = impressionArray;
			flow[3] = clickArray;
			flow[4] = incomeArray;
		},
		error : function (XMLHttpRequest, textStatus, errorThrown) {
			// 通常 textStatus 和 errorThrown 之中
			// 只有一个会包含信息
			alert('error');
			this; // 调用本次AJAX请求时传递的options参数
		}
	});
		
	//设置日期，当前日期的前七天
	var myDate = new Date(); //获取今天日期
	var myYear = myDate.getFullYear();//获取年份
	myDate.setDate(myDate.getDate() - 7);
	var labels = [];
	var dateTemp;
	var flag = 1;
	for (var i = 0; i < 7; i++) {
		dateTemp = myDate.getFullYear() + "/" + (myDate.getMonth() + 1) + "/" + myDate.getDate();
		labels.push(dateTemp);
		myDate.setDate(myDate.getDate() + flag);
	}		
	drawDiagram(myDate,flow[0],pv);
		
	$('#data_number td').click(function(){
		$('#data_number td').css({"background-color":"#ffffff","color":"black"});
		$(this).css({"background-color":"#36aeea","color":"#ffffff"});
		var n = $(this).index();
		pv=tipLabel[n];
		var data = flow[n];
		$('#baseTitle').text('近7天全部广告位' + pv);
		drawDiagram(myDate,data,pv);
	});
		
	function drawDiagram(myDate,data,pv){		
		$('#diagram').highcharts({ 
			chart: { type: 'line' },
			title: { text: '' }, //标题
			//subtitle: { text: 'Source: WorldClimate.com' }, //副标题
			xAxis: { categories: labels }, 
			yAxis: { 
				title: { text: '' }, 
				plotLines: [{ value: 0, width: 1, color: '#808080' }]
			}, 
			tooltip: { crosshairs: true, shared: true }, 
			legend: { 
				enabled:false,
			},
			credits:{
				enabled:false,
			},
			
			series: [{ 
				name: pv, 
				data: data,
				color:'#36aeea',
				marker:{
					symbol: 'circle',
					fillColor:'#ffffff',
					lineColor:'#87b9f6',
					lineWidth: 2,
					radius: 5,
				}
			}]
		}); 
	}
});