$(document).ready(function(){
	var ctx = $("#ctx").val();
	/** 实现pv,uv,点击数，曝光数，收入样式 **/
	if($("#chartDivs").attr("value") == "total"){
		initDataFrameOrLender($("#dataFrame0"),$("#linechart0"),"dataFrame0","dataFrame_change",$("#chartDivs div.linechart"));
    }else{
    	initDataFrameOrLender($("#chartLender0 span.chartLender_box"),$("#linechart0"),"chartLender0","chartLender_change",$("#chartDivs div.linechart"));
    }
	
	function initDataFrameOrLender(defaultElemdObj,defaultChartObj,defaultElemdValue,className,chartObjs){
		if($('#dataFrame').val() == ""){
			defaultElemdObj.addClass(className);
			defaultChartObj.show();
			$('#dataFrame').val(defaultElemdValue);
			chartObjs.each(function(i){
				if(i != 0){
					$(this).hide();
				}
			});
		}
	}
	
	$("#content_dataFrame .dataFrame").click(function(){
		 $(this).addClass('dataFrame_change').siblings('.dataFrame_change').removeClass('dataFrame_change');
		 var value = $(this).attr("id");
		 $("#dataFrame").val(value);
		 var index = value.charAt(value.length - 1);
		 $("#linechart"+index).show();
		 $("#chartDivs div.linechart").each(function(i){
			if(i != index){
				$(this).hide();
			}
		});
	});
   
   $("#content_dataFrame .dataFrame").each(function(i){
		var value = $(this).attr("id");
		if($("#dataFrame").val() == value){
			$(this).addClass('dataFrame_change').siblings('.dataFrame_change').removeClass('dataFrame_change');
			$("#linechart"+i).show();
		}else{
			$("#linechart"+i).hide();
		}
   });
	
   $("#content_chartLender .chartLender_name").each(function(i){
		var value = $(this).attr("id");
		if($("#dataFrame").val() == value){
			$(this).find("span.chartLender_box").addClass('chartLender_change').siblings('.chartLender_change').removeClass('chartLender_change');
			$("#linechart"+i).show();
		}else{
			$("#linechart"+i).hide();
		}
	});
   
   $("#content_chartLender .chartLender_name").click(function(){
	   	$(this).find(".chartLender_box").addClass('chartLender_change');
		var value = $(this).attr("id");
		$("#content_chartLender .chartLender_name").each(function(i){
			if($(this).attr("id") != value){
				$(this).find(".chartLender_box").removeClass('chartLender_change');
			}
		});
		$("#dataFrame").val(value);
		var index = value.charAt(value.length-1);
		$("#linechart"+index).show();
		$("#chartDivs div.linechart").each(function(i){
			if(i != index){
				$(this).hide();
			}
		});
	});
   
    /** 实现chart图表 **/
	function getDate(tm){ 
		var tt=new Date(parseInt(tm)).toLocaleDateString();
		return tt; 
	}
	function getele1(){
		try{
		  	var d=document.getElementsByName("hr");
		  	var c=[];
		  	for(i=0;i<=d.length;i++){
		  		c[i]=d[i].childNodes[0].nodeValue;
		  	}
		}catch(e){}
		return c;
	}
	function initChart_homePage(){
		var datePicker = $("#dateRangePicker").val().split(" to ");
		var startDate = datePicker[0];
		var endDate = datePicker[1];
		if(startDate==endDate){
			var m=getele1();
			n=24;
			var color=[];
			for(i=0;i<n;i++){
				color[i]='red';
			}
			n1=n*6;
			var xlabel=[];		
			var j=0;
			for(i=0;i<n1;i+=6){
				xlabel[j]=m[i];
				j++;
			}
			//取y轴
			var uv=[];
			var j=0;
			for(i=2;i<n1;i+=6){
				uv[j]=parseFloat(m[i]);
				j++;
			}
			var pv=[];
			var j=0;
			for(i=1;i<n1;i+=6){
				pv[j]=parseFloat(m[i]);
				j++;
			}							
			var adbaoguang=[];
			var j=0;
			for(i=3;i<n1;i+=6){		
				adbaoguang[j]=parseFloat(m[i]);
				j++;
			}
			var adclick=[];
			var j=0;
			for(i=4;i<n1;i+=6){
				adclick[j]=parseFloat(m[i]);
				j++;
			}
			var shouru=[];
			var j=0;
			for(i=5;i<n1;i+=6){
				shouru[j]=parseFloat(m[i]);
				j++;
			}	
			setinterval=1;				
		}else{
			var selectedZoneId=$("#selectedZoneId").val();
			if(selectedZoneId==null || selectedZoneId==""){selectedZoneId=-1}
			var dateRangePicker=$("#dateRangePicker").val();
			url = ctx+"/operator/main/ad/report/advertiser-report/listChartData?dateRangePicker="+dateRangePicker+"&selectedZoneId="+selectedZoneId;
			var pv = [];
			var uv = [];
			var adbaoguang = [];
			var adclick = [];
			var shouru = [];
			var xlabel = []
			var k = 0;
					
 			$.ajaxSettings.async = false; //getJSON同步执行
			$.getJSON(url, function(data) {
				$.each(data, function(key, val) {
					xlabel[k] = val.reportDate;
					xlabel[k] = getDate(xlabel[k]);
					pv[k] = val.pv;
					uv[k] = val.uv;
					adbaoguang[k] = val.impression;
					adclick[k] = val.click;
					shouru[k] = val.income;
					k++;
				});	
			});	
			//alert(xlabel);
			setinterval=parseInt(xlabel.length/10);							
		}
		var chartDivs = $("#chartDivs").find("div").length;
		var names = ['PV','UV','广告曝光数','广告点击数','收入'];
		var values = [pv,uv,adbaoguang,adclick,shouru];
		for(var i = 0;i<chartDivs;i++){
			getHighchart_homePage($("#linechart"+i),names[i],names[i],values[i],xlabel);
		}
	}
	function getHighchart_homePage(obj,title,name,value,xlabel){
		obj.highcharts({
	        title: {
	            text: title,
	        },
	        xAxis: {
	            categories: xlabel,
	            tickInterval: setinterval 
	        },
	        yAxis: {
	            plotLines: [{
	                value: 0,
	                width: 1,
	               color: '#36aeea'
	           }]
	        },
	        legend: {
	           layout: 'vertical',
	            align: 'right',
	          verticalAlign: 'middle',
	           borderWidth: 0
	        },
	        series: [{
	            name: name,
	            data: value,
	            color:'#36aeea'
	        }]
	    });
	}
	
	function getele(){
		try{
			var b=document.getElementsByTagName("td");	  				
				var c=[];	  			
  			for(i=0;i<b.length;i++){  
  				c[i]=b[i].childNodes[0].nodeValue;
  			}	
		}
		catch(e){}
		return c;
	}

	function initChart_other(){
		m=getele();
		n=$("#listSize").val();
		var color=[];
		for(i=0;i<n;i++){
			color[i]='#36aeea';
		}
		n2=10;
		n1=n*n2;
		var xlabel=[];
		var j=0;
		for(i=0;i<n1;i+=n2){
			xlabel[j]=m[i];
			   j++;
		}
		var lll=[];
		var j=0;
		for(i=1;i<n1;i+=n2){
			lll[j]=parseFloat(m[i]);
		    j++;
		}
		//排序
		var lllxlabel = xlabel;
		var uselllxlabel = [];
		var uselll = [];
		var num;
		for(a=0;a<lll.length;a++){
			uselll[a]=0;
			num=a;
			for(b=0;b<lll.length;b++){
				if(uselll[a]<=lll[b]){
				 	uselll[a]=lll[b];
				 	uselllxlabel[a]=lllxlabel[b];
				 	num=b;
				}
			}
			lll[num]=0;
		}

		var pv=[];
		var j=0;
		for(i=2;i<n1;i+=n2){
			if(m[i]!=null){
				pv[j]=parseFloat(m[i]);
			}
			j++;
		}
		//排序
		var pvxlabel = xlabel;
		var usepvxlabel = [];
		var usepv = [];
		var num;
		for(a=0;a<pv.length;a++){
			usepv[a]=0;
			num=a;
			for(b=0;b<pv.length;b++){
				if(usepv[a]<=pv[b]){
					usepv[a]=pv[b];
					usepvxlabel[a]=pvxlabel[b];
					num=b;
			 	}
			 }
			pv[num]=0;
		}

		var uv=[];
		var j=0;
		for(i=3;i<n1;i+=n2){
			if(m[i]!=null){
				uv[j]=parseFloat(m[i]);
			}
			j++;
		}
		//排序
		var uvxlabel = xlabel;
		var useuvxlabel = [];
		var useuv = [];
		var num;
		for(a=0;a<uv.length;a++){
			useuv[a]=0;
			num=a;
			for(b=0;b<uv.length;b++){
				if(useuv[a]<=uv[b]){
					useuv[a]=uv[b];
					useuvxlabel[a]=uvxlabel[b];
					num=b;
			 	}
			}
			uv[num]=0;
		}

		var adbaoguang=[];
		var j=0;
		for(i=4;i<n1;i+=n2){
			if(m[i]!=null){
				adbaoguang[j]=parseFloat(m[i]);
			}
			j++;
		}
		//排序
		var adbaoguangxlabel = xlabel;
		var useadbaoguangxlabel = [];
		var useadbaoguang = [];
		var num;
		for(a=0;a<adbaoguang.length;a++){
			useadbaoguang[a]=0;
			num=a;
			for(b=0;b<adbaoguang.length;b++){
				if(useadbaoguang[a]<=adbaoguang[b]){
					useadbaoguang[a]=adbaoguang[b];
					useadbaoguangxlabel[a]=adbaoguangxlabel[b];
					num=b;
			 	}
			}
			adbaoguang[num]=0;
		}
		var adtianchong=[];
		var j=0;
		for(i=5;i<n1;i+=n2){
			if(m[i]!=null){
				adtianchong[j]=parseFloat(m[i]);
			}
			j++;
		}
		//排序
		var adtianchongxlabel = xlabel;
		var useadtianchongxlabel = [];
		var useadtianchong = [];
		var num;
		for(a=0;a<adtianchong.length;a++){
			useadtianchong[a]=0;
			num=a;
			for(b=0;b<adtianchong.length;b++){
			 	if(useadtianchong[a]<=adtianchong[b]){
			 		useadtianchong[a]=adtianchong[b];
			 		useadtianchongxlabel[a]=adtianchongxlabel[b];
			 		num=b;
			 	}
			}
			adtianchong[num]=0;
		}
		var adclick=[];
		var j=0;
		for(i=6;i<n1;i+=n2){
			if(m[i]!=null){
				adclick[j]=parseFloat(m[i]);
			}
			j++;
		}

		//排序
		var adclickxlabel = xlabel;
		var useadclickxlabel = [];
		var useadclick = [];
		var num;
		for(a=0;a<adclick.length;a++){
			useadclick[a]=0;
			num=a;
			for(b=0;b<adclick.length;b++){
			 	if(useadclick[a]<=adclick[b]){
			 		useadclick[a]=adclick[b];
			 		useadclickxlabel[a]=adclickxlabel[b];
			 		num=b;
			 	}
			}
			adclick[num]=0;
		}
		var adclickl=[];
		var j=0;
		for(i=7;i<n1;i+=n2){
			if(m[i]!=null){
				adclickl[j]=parseFloat(m[i]);
			}
			j++;
		}

		//排序
		var adclicklxlabel = xlabel;
		var useadclicklxlabel = [];
		var useadclickl = [];
		var num;
		for(a=0;a<adclickl.length;a++){
			useadclickl[a]=0;
			num=a;
			for(b=0;b<adclickl.length;b++){
			 	if(useadclickl[a]<=adclickl[b]){
			 		useadclickl[a]=adclickl[b];
			 		useadclicklxlabel[a]=adclicklxlabel[b];
			 		num=b;
			 	}
			}
			adclickl[num]=0;
		}

		var ecpm=[];
		var j=0;
		for(i=8;i<n1;i+=n2){
			if(m[i]!=null){
				ecpm[j]=parseFloat(m[i]);
			}
			j++;
		}

		//排序
		var ecpmxlabel = xlabel;
		var useecpmxlabel = [];
		var useecpm = [];
		var num;
		for(a=0;a<ecpm.length;a++){
			useecpm[a]=0;
			num=a;
			for(b=0;b<ecpm.length;b++){
			 	if(useecpm[a]<=ecpm[b]){
			 		useecpm[a]=ecpm[b];
			 		useecpmxlabel[a]=ecpmxlabel[b];
			 		num=b;
			 	}
			}
			ecpm[num]=0;
		}

		var shouru=[];
		var j=0;
		for(i=9;i<n1;i+=n2){
			if(m[i]!=null){
				shouru[j]=parseFloat(m[i]);
			}
			j++;
		}
		
		//排序
		var shouruxlabel = xlabel;
		var useshouruxlabel = [];
		var useshouru = [];
		var num;
		for(a=0;a<shouru.length;a++){
			useshouru[a]=0;
			num=a;
			for(b=0;b<shouru.length;b++){
			 	if(useshouru[a]<=shouru[b]){
			 		useshouru[a]=shouru[b];
			 		useshouruxlabel[a]=shouruxlabel[b];
			 		num=b;
			 	}
			}
			shouru[num]=0;
		}
		var chartDivs = $("#chartDivs").find("div").length;
		var names = ['浏览量占比','PV','UV','广告曝光数','广告填充率','广告点击数','广告点击率','eCPM','收入'];
		var values = [uselll,usepv,useuv,useadbaoguang,useadtianchong,useadclick,useadclickl,useecpm,useshouru];
		var xlabels = [uselllxlabel,usepvxlabel,useuvxlabel,useadbaoguangxlabel,useadtianchongxlabel,useadclickxlabel,useadclicklxlabel,useecpmxlabel,useshouruxlabel];
		for(var i = 0;i<chartDivs;i++){
			if(i==0){
				getHighchart_other($("#linechart"+i),names[i],names[i],values[i],xlabels[i],"of total",color);
			}else{
				getHighchart_other($("#linechart"+i),names[i],names[i],values[i],xlabels[i],"",color);
			}
		}
	}
	function getHighchart_other(obj,title,name,value,uselllxlabel,defaultValue,color){
		obj.highcharts({
			chart: {
				type: 'column'
			},
			title: {
				text: title
			},
			xAxis: {
				categories: uselllxlabel
			},       
			legend: {
				enabled: false
			},
			plotOptions: {
				series: {
					borderWidth: 0,
					pointWidth: 30,
					dataLabels: {
						enabled: true
			       
					}
				}
			},
			plotOptions: {
				series: {
				    borderWidth: 0,
				     pointWidth: 30,
				     dataLabels: {
				    	 enabled: true,
				        format: '{point.y:.2f}%'
				    }
				}
			},
			tooltip: {
				headerFormat: '<span style="font-size:11px">{series.name}</span><br>',
				pointFormat: '<span style="color:{point.color}">{point.name}</span>: <b>{point.y:.2f}%</b> '+defaultValue+'<br/>'
			},
			series: [{
				name: name,
				colorByPoint: true,
				colors:color,
				data: value
			}]       
		});   
	}

	/** 实现日期选择的样式 **/
	$('#dateChoose_span a').click(function(e){
       	inputForDate(e,$(this),$('#dateRangePicker'));
        $("#dateTab").val($(this).attr("value"));
        $(this).addClass('dateChange').siblings('.dateChange').removeClass('dateChange');
    });
   
    $('#dateRangePicker').dateRangePicker();
    if($('#dateRangePicker').val() == ""){
		$('#dateRangePicker').val(getlastDate(1)+" to "+getlastDate(1));
		$("#dateChoose_span #yesterday").addClass('dateChange');
		$("#dateTab").val("yesterday");
	}
    if($("#chartDivs").attr("value") == "total"){
    	initChart_homePage();
    }else{
    	initChart_other();
    }
	$('#dateChoose_span a').each(function(){
		var value = $(this).attr("value");
		if($("#dateTab").val() == value){
			$(this).addClass('dateChange');
		}
	});
	//点击日期控件的按钮时，去掉前面时间标签的样式
	$(".apply-btn").click(function(){
		$("#dateTab").val("");
		$('#dateChoose_span a').removeClass('dateChange');
	});

 	//控制页码
    $('#pageDiv .pagination ul li a[href]').click(function(){
		var _page = $(this).attr('href').substr(1);//获取当前点击的页码
		console.log(_page);
		var _form = $('#myform');
		_form.find('input:hidden[name="page"]').val(_page);//将当前点击的页码赋值给搜索form中页码输入框
		_form.submit();
	});
    
    $("#img-city").live("click",function(){
		var img = $(this);
		var cnName = img.attr("value");
		if(img.attr("type") == "close"){
			$.ajax({
		    	type:"POST",
		    	url:ctx+"/back/main/ad/operator/operator-report/city",
		    	data:{cn:cnName,selectedOperatorId:$("#selectedOperatorId").val(),dateRangePicker:$("#dateRangePicker").val()},
		    	success:function(data){
		    		var html = "";
		    		if(data.length >0){
		    			for(var i = 0;i<data.length;i++){
		    				html += '<tr id='+cnName+' class="changeColor" style="background-color:#CCCCCC;">';
		    				html += '<td class="tbodyTd" style="margin-left:20px;"><img src="'+ctx+'/assets/images/defaultLeaf.gif" style="position: relative;top:-2px;left:-5px;"/>'+data[i].cn_city+'</td>';
		    				html += '<td class="tbodyTd">'+data[i].viewRate+'%</td>';
		    				html += '<td class="tbodyTd">'+data[i].pv+'</td>';
		    				html += '<td class="tbodyTd">'+data[i].uv+'</td>';
		    				html += '<td class="tbodyTd">'+data[i].impression+'</td>';
		    				html += '<td class="tbodyTd">'+data[i].fillRate+'%</td>';
		    				html += '<td class="tbodyTd">'+data[i].click+'</td>';
		    				html += '<td class="tbodyTd">'+data[i].clickRate+'%</td>';
		    				html += '<td class="tbodyTd">'+data[i].eCpm+'</td>';
		    				html += '<td class="tbodyTd">'+data[i].income+'</td>';
		    				html += '</tr>';
		    			}
		    		img.parents('tr').after(html);
		    		}
		    		img.attr("src",ctx+"/assets/images/folderOpen.gif");
		    	}     
			});	
			img.attr("type","open");
		}else if(img.attr("type") == "open"){
		    $('tr').remove("#"+cnName);//删除id为cname的所有 tr
		    img.attr("src",ctx+"/assets/images/folderClose.gif");
		    img.attr("type","close");
			//location.href = "${ctx}/back/main/ad/operator/operator-report/areal";
		}
	});
	
});

function exportExcel(tab,url,type){
	var params = "?"+tab+"=1&dateRangePicker="+$("#dateRangePicker").val();
	if(type == "operator_total"){
		params += "&selectedZoneId="+$("#selectedZoneId").val();
	}else if(type == "back_total"){
		params += "&selectedOperatorId"+$("#selectedOperatorId").val()+"&selectedZoneId="+$("#selectedZoneId").val();
	}else{
		params += "&selectedOperatorId"+$("#selectedOperatorId").val();
	}
	$("#exportExcel").attr("href",url+params);
}

