var curTab = 1;


function delBenId(obj){
	$(obj).closest("li").remove();
	var somearray = decodeURI($.getParam("ids")).split('|'),type = $.getParam("type");
	removeByValue(somearray, obj.dataset.brand);
	window.location.href='program_marking.do?type='+type+'&ids='+somearray.join('|');
}

function addBenId(){
	var url = 'oepn_benchmark_program_choose.do?type='+$.getParam("type")+'&ids='+decodeURI($.getParam("ids"));
	$.openWindow('请添加对标节目',url, 470, 280, true, null, 'auto');
}

function changeBenchType(){
	bench_type = $('#benchType').val();
	changeBenchTab();
}

function changeBenchTab(val){
	val = val || curTab;
	if(val == 1){
		getTab1();
		$('#ratio_1,#ratio_2,#ratio_3,#ratio_4').hide();
	}else if(val == 2){
		getTab2();
		$('#ratio_1,#ratio_2,#ratio_3,#ratio_4').show();
	}else if(val == 3){
		getTab3();
		$('#ratio_1,#ratio_2,#ratio_3,#ratio_4').hide();
	}
	curTab = val;
	changeTagName();
}

/**
 * 切换时间
 */
function changeDateTab(day){
	var end_date = '', start_date = '';
	if(day > 0){
		end_date = new Date().addDate(-1).Format("yyyy-MM-dd"),
		start_date = new Date(end_date).addDate(-day).Format("yyyy-MM-dd");
		$('#start_time').val(start_date);
		$('#end_time').val(end_date);
	}else{
		$('#start_time').val('');
		$('#end_time').val('');
	}
	$(".text").input();
	changeBenchTab();
}

function getTab1(){
	var arr = decodeURI($.getParam("ids")).split('|');
	getPieData(bench_type,arr[0],'chart_1','chart_3');
	getPieData(bench_type,arr[1],'chart_2','chart_4');
}

function getPieData(type,program,div1,div2){
	var data = {
			"type" : type,
			"program": program,
			"beginDate": $("#start_time").val(),
			"endDate": $("#end_time").val()
		};
	ajax("list_program_frqc_lth.do",data,function(res){
		if(res.code == 1 && res.list.length > 0){
			var legend = [];
			var series_frequency = [];
			var series_length = [];
			$.each(res.list, function(index, item){
				var program = item.brand + ((type == 1 || type == 2) ? ("-" + res.adMode[item.form-1].val) : '');
				legend.push(program);
				var lv1 = {
						value:item.frequency, 
						name:program
				};
				series_frequency.push(lv1);
				var lv2 = {
						value:item.length, 
						name:program
				};
				series_length.push(lv2);
			});
			trend_pie("频次分布",legend,series_frequency,div1);
			trend_pie("时长分布",legend,series_length,div2);
		}else if(res.list && res.list.length == 0){
			$("#"+div1).info("找不到数据");
			$("#"+div2).info("找不到数据");
		}
		else{
			$("#"+div1).info(res.msg);
			$("#"+div2).info(res.msg);
		}
	});
}


function getTab2(){
	var arr = decodeURI($.getParam("ids")).split('|');
	getRatingChart(bench_type,arr[0],'','1');
	getRatingChart(bench_type,arr[1],'','2');
	getNetChart(bench_type,arr[0],'','3');
	getNetChart(bench_type,arr[1],'','4');
}



function getRatingChart(type,program,platform,id){
	var data = {
			"type" : type,
			"program": program,
			"beginDate": $("#start_time").val(),
			"endDate": $("#end_time").val()
		};
	ajax("list_program_audience_rating.do",data,function(res){
		if(res.code == 1 && res.list.length > 0){
			var legend = ['收视率(%)','线性(收视率)'],xdata = [],ydata = [],ys = [],platforms = [],html = [];
			$.each(res.list, function(index, item){
				if(platform == '' && index == 0){
					platform = item.platform;
				}
				if(item.platform == platform){
					xdata.push("第"+item.phase+(type == 1 || type == 2 ? '期' : '集'));
					ydata.push(item.audience_rating_4);
				}
				platforms.push(item.platform);
			});
			ys = calTrendline(ydata);
			trendBarWithLine(legend,xdata,ydata,ys,'chart_'+id);
			
			platforms = unique(platforms);
			for(var i in platforms){
				html +='<input type="radio" name="rating_ratio_35" class="radio {click:getRatingRatio}" data-program="'+program+'" data-id="'+id+'" value="'+platforms[i]+'" '+(platforms[i] == platform ? 'checked' : '')+'/><label>'+platforms[i]+'</label>&nbsp;&nbsp;&nbsp;&nbsp;';
			}
			$("#ratio_"+id).html(html);
			$.radio();
		}else{
			$("#chart_"+id).info("暂无数据。",true);
			$("#ratio_"+id).html('');
		}
	},false);
}

function getRatingRatio(obj){
	getRatingChart(bench_type,$(obj).data('program'),$(obj).val(),$(obj).data('id'));
}

/**
 * 网络综艺节目植入效果一览
 */
function getNetChart(type,program,platform,id){
	var data = {
			"type" : type,
			"program": program,
			"beginDate": $("#start_time").val(),
			"endDate": $("#end_time").val()
		};
	ajax("list_program_net_clicktimes.do",data,function(res){
		if(res.code == 1 && res.list.length > 0){
			var legend = ['点击率(千万)','趋势(点击率)'],xdata = [],ydata = [],ys = [],platforms = [],html = "";
			$.each(res.list, function(index, item){
				if(platform == '' && index == 0){
					platform = item.platform_name;
				}
				if(item.platform_name == platform){
					xdata.push(item.phase);
					ydata.push(item.click_times);
				}
				platforms.push(item.platform_name);
			});
			ys = calTrendline(ydata);
			trendBarWithLine(legend,xdata,ydata,ys,'chart_'+id);
			
			platforms = unique(platforms);
			for(var i in platforms){
				html +='<input type="radio" name="net_ratio" class="radio {click:getRatingRatio2}" data-program="'+program+'" data-id="'+id+'" value="'+platforms[i]+'" '+(platforms[i] == platform ? 'checked' : '')+'/><label>'+platforms[i]+'</label>&nbsp;&nbsp;&nbsp;&nbsp;';
			}
			$("#ratio_"+id).html(html);
			$.radio();
		}else{
			$("#chart_"+id).info("暂无数据。",true);
			$("#ratio_"+id).html('');
		}
	},false);
}

function getRatingRatio2(obj){
	getRatingChart(bench_type,$(obj).data('program'),$(obj).val(),$(obj).data('id'));
}


function getTab3(){
	getDiffChart4Frequency(bench_type);
	if(bench_type == 4){
		getMovieChart4Tab3(bench_type);
	}else{
		getDiffChart4View(bench_type);
	}
}

function getDiffChart4Frequency(type){
	var data = {
			"type" : type,
			"programs": decodeURI($.getParam("ids")),
			"beginDate": $("#start_time").val(),
			"endDate": $("#end_time").val()
		};
	ajax("list_frequency_length_4program.do",data,function(res){
		if(res.code == 1 && res.list.length > 0){
			var xdata = [],ydata = [],ydata2 = [];
			$.each(res.list, function(index, item){
				xdata.push(item.program_name);
				ydata.push(item.frequency);
				ydata2.push(item.length);
			});
			trendBar('频次',xdata,ydata,'chart_1');
			trendBar('时长',xdata,ydata2,'chart_2');
		}else if(res.list && res.list.length == 0){
			$("#chart_1").info("找不到数据");
			$("#chart_2").info("找不到数据");
		}
		else{
			$("#chart_1").info(res.msg);
			$("#chart_2").info(res.msg);
		}
	});
}

function getDiffChart4View(type){
	var data = {
			"type" : type,
			"programs": decodeURI($.getParam("ids")),
			"beginDate": $("#start_time").val(),
			"endDate": $("#end_time").val()
		};
	ajax("list_audience_rate_click4Program.do",data,function(res){
		if(res.code == 1 && res.list.length > 0){
			var xdata = [],ydata = [],ydata2 = [];
			$.each(res.list, function(index, item){
				xdata.push(item.program_name);
				ydata.push(item.audience_rating);
				ydata2.push(item.click_times);
			});
			trendBar('收视率',xdata,ydata,'chart_3');
			trendBar('点击量',xdata,ydata2,'chart_4');
		}else if(res.list && res.list.length == 0){
			$("#chart_3").info("找不到数据");
			$("#chart_4").info("找不到数据");
		}
		else{
			$("#chart_3").info(res.msg);
			$("#chart_4").info(res.msg);
		}
	});
}


function getMovieChart4Tab3(type){
	var data = {
			"type" : type,
			"programs": decodeURI($.getParam("ids")),
			"beginDate": $("#start_time").val(),
			"endDate": $("#end_time").val()
		};
	ajax("list_cbo_total_click_4Program.do",data,function(res){
		if(res.code == 1 && res.list.length > 0){
			var xdata = [],ydata = [],ydata2 = [];
			$.each(res.list, function(index, item){
				xdata.push(item.program_name);
				ydata.push(item.cbo_total);
				ydata2.push(item.click_times);
			});
			trendBar('票房',xdata,ydata,'chart_3');
			trendBar('点击量',xdata,ydata2,'chart_4');
		}else if(res.list && res.list.length == 0){
			$("#chart_3").info("找不到数据");
			$("#chart_4").info("找不到数据");
		}
		else{
			$("#chart_3").info(res.msg);
			$("#chart_4").info(res.msg);
		}
	});
}

function changeTagName(){
	var somearray = decodeURI($.getParam("ids")).split('|');
	$("span[name='brand_1']").html("《"+somearray[0]+"》");
	$("span[name='brand_2']").html("《"+somearray[1]+"》");
	if(curTab == 1){
		$("span[name='title_1']").html(bench_type.toString().decode(1,"综艺节目软性广告植入频次分布",2,"栏目软性广告植入频次分布",3,"电视剧软广植入频次分布",4,"电影软广植入频次分布","") + " &nbsp;&nbsp;<span class='orange font12'>单位（次）</span>");
		$("span[name='title_2']").html(bench_type.toString().decode(1,"综艺节目软性广告植入频次分布",2,"栏目软性广告植入频次分布",3,"电视剧软广植入频次分布",4,"电影软广植入频次分布","") + " &nbsp;&nbsp;<span class='orange font12'>单位（次）</span>");
		$("span[name='title_3']").html(bench_type.toString().decode(1,"综艺节目软性广告植入时长分布",2,"栏目软性广告植入时长分布",3,"电视剧软广植入时长分布",4,"电影剧软广植入时长分布","") + " &nbsp;&nbsp;<span class='orange font12'>单位（秒）</span>");
		$("span[name='title_4']").html(bench_type.toString().decode(1,"综艺节目软性广告植入时长分布",2,"栏目软性广告植入时长分布",3,"电视剧软广植入时长分布",4,"电影剧软广植入时长分布","") + " &nbsp;&nbsp;<span class='orange font12'>单位（秒）</span>");
		$("#title_1").html('软广频次');
		$("#title_2").html('软广时长');
	}else if(curTab == 2){
		$("span[name='title_1']").html(bench_type.toString().decode(1,"电视综艺节目植入效果一览",2,"电视栏目植入效果一览",3,"电视剧植入效果一览",4,"电影票房植入效果一览",""));
		$("span[name='title_2']").html(bench_type.toString().decode(1,"电视综艺节目植入效果一览",2,"电视栏目植入效果一览",3,"电视剧植入效果一览",4,"电影票房植入效果一览",""));
		$("span[name='title_3']").html(bench_type.toString().decode(1,"网络综艺节目植入效果一览",2,"网络栏目植入效果一览",3,"电视剧植入效果一览",4,"网络电影植入效果一览",""));
		$("span[name='title_4']").html(bench_type.toString().decode(1,"网络综艺节目植入效果一览",2,"网络栏目植入效果一览",3,"电视剧植入效果一览",4,"网络电影植入效果一览",""));
		$("#title_1").html('收视效果');
		$("#title_2").html('点击效果');
	}else if(curTab == 3){
		$("span[name='title_1']").html(bench_type.toString().decode(1,"品牌综艺软广频次比较",2,"品牌栏目软广频次比较",3,"电视剧软广植入频次比较",4,"电影软广植入频次比较","") + " &nbsp;&nbsp;<span class='orange font12'>单位（次）</span>");
		$("span[name='title_2']").html(bench_type.toString().decode(1,"品牌综艺软广时长比较",2,"品牌栏目软广时长比较",3,"电视剧软广植入时长比较",4,"电影软广植入时长比较","") + " &nbsp;&nbsp;<span class='orange font12'>单位（秒）</span>");
		$("span[name='title_3']").html(bench_type.toString().decode(1,"综艺软广收视比较",2,"栏目软广收视比较",3,"电视剧软广收视比较",4,"电影软广收视比较","") + " &nbsp;&nbsp;<span class='orange font12'>单位（%）</span>");
		$("span[name='title_4']").html(bench_type.toString().decode(1,"综艺软广点击比较",2,"栏目软广点击比较",3,"电视剧软广点击比较",4,"电影软广点击比较","") + " &nbsp;&nbsp;<span class='orange font12'>单位（万）</span>");
		$("#title_1").html('植入资源比较分析');
		$("#title_2").html('植入效果比较分析');
	}
	
}