var bench_type = 1 , curTab = 1;

function delBenId(obj){
	$(obj).closest("li").remove();
	var somearray = decodeURI($.getParam("ids")).split('|');
	removeByValue(somearray, obj.dataset.brand);
	window.location.href='benchmarking.do?ids='+somearray.join('|');
}
function addBenId(){
	var url = 'oepn_benchmark_brand_choose.do?ids='+decodeURI($.getParam("ids"));
	$.openWindow('请添加对标品牌',url, 470, 260, true, null, 'auto');
}

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

function changeBenchTab(val){
	val = val || curTab;
	if(val == 1){
		getTab1();
	}else if(val == 2){
		getTab2();
	}else if(val == 3){
		getTab3();
	}
	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,brand,div1,div2){
	var data = {
			"type" : type,
			"brand": brand,
			"name": $("#keyword").val(),
			"beginDate": $("#start_time").val(),
			"endDate": $("#end_time").val()
		};
	ajax("list_program_placement4Brand.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.program_name +"》" + ((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,1,'chart_1');
	getRatingChart(bench_type,arr[1],1,1,'chart_2');
	if(bench_type == 4){
		getCboChart4Tab2(bench_type,arr[0],'chart_3');
		getCboChart4Tab2(bench_type,arr[1],'chart_4');
	}else{
		getDoubanChart(bench_type,arr[0],'chart_3');
		getDoubanChart(bench_type,arr[1],'chart_4');
	}
}


/**
 * 电视频道品牌软广植入受众人群一览
 * @param type 1综艺 2栏目 3电视剧 4电影
 * @param rateType 1：35城  2:29城
 * @param ratioValue 1：4岁以上   2：A18-45  3:F18-45     
 */
function getRatingChart(type,brand,rateType,ratioValue,div){
	var data = {
			"type" : type,
			"rateType" : rateType,
			"brand": brand,
			"name": $("#keyword").val(),
			"beginDate": $("#start_time").val(),
			"endDate": $("#end_time").val()
		};
	ajax("list_program_rating4Brand.do",data,function(res){
		if(res.code == 1 && res.list.length > 0){
			var legend = ['收视率(%)','到达率(000)'],xdata = [],ydata = [],ys = [];
			$.each(res.list, function(index, item){
				var program = "《" + item.program_name + "》" + "-" + res.adMode[item.form-1].val;
				xdata.push(program);
				if(ratioValue == 2){
					ydata.push(item.audience_rating_18_45);
					ys.push(item.avg_num_18_45);
				}else if(ratioValue == 3){
					ydata.push(item.audience_rating_18_45_woman);
					ys.push(item.avg_num_18_45_woman);
				}else{
					ydata.push(item.audience_rating_4);
					ys.push(item.avg_num_4);
				}
				
			});
			trendBarWithLine(legend,xdata,ydata,ys,div);
		}else if(res.list && res.list.length == 0){
			$("#"+div).info("找不到数据");
		}
		else{
			$("#"+div).info(res.msg);
		}
	});
}

/**
 * 植入综艺豆瓣网评价
 */
function getDoubanChart(type,brand,div){
	var data = {
			"type" : type,
			"brand": brand,
			"beginDate": $("#start_time").val(),
			"endDate": $("#end_time").val()
		};
	ajax("list_click_score4Brand.do",data,function(res){
		if(res.code == 1 && res.list.length > 0){
			var legend = ['点击率(千万)','豆瓣评分(分)'],xdata = [],ydata = [],ys = [];
			$.each(res.list, function(index, item){
				xdata.push("《" + item.program_name + "》" + ((type == 1 || type == 2) ? ("-" + res.adMode[item.form-1].val) : ''));
				ydata.push((item.click_times * 10000) / 1000 );
				ys.push(item.score);
			});
			trendBarWithLine(legend,xdata,ydata,ys,div);
		}else if(res.list && res.list.length == 0){
			$("#"+div).info("找不到数据");
		}
		else{
			$("#"+div).info(res.msg);
		}
	});
}


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

function getDiffChart4Frequency(type){
	var data = {
			"type" : type,
			"ids": decodeURI($.getParam("ids")),
			"beginDate": $("#start_time").val(),
			"endDate": $("#end_time").val()
		};
	ajax("list_frequency_length_total4Brand.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.brand);
				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,
			"ids": decodeURI($.getParam("ids")),
			"beginDate": $("#start_time").val(),
			"endDate": $("#end_time").val()
		};
	ajax("list_audience_rate_click4Brand.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.brand);
				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 getCboChart4Tab2(type,brand,divId){
	var data = {
			"type" : type,
			"brand": brand,
			"beginDate": $("#start_time").val(),
			"endDate": $("#end_time").val()
		};
	ajax("list_cbo_total4Brand.do",data,function(res){
		if(res.code == 1 && res.list.length > 0){
			var legend = ['票房','票房趋势'],xdata = [],ydata = [];
			$.each(res.list, function(index, item){
				xdata.push(item.program_name);
				ydata.push(item.cbo_total);
			});
			trendBarWithLine(legend,xdata,ydata,calTrendline(ydata),divId);
		}else if(res.list && res.list.length == 0){
			$("#"+div).info("找不到数据");
		}
		else{
			$("#"+div).info(res.msg);
		}
	});
}

function getMovieChart4Tab3(type){
	var data = {
			"type" : type,
			"ids": decodeURI($.getParam("ids")),
			"beginDate": $("#start_time").val(),
			"endDate": $("#end_time").val()
		};
	ajax("list_cbo_click4Brand.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.brand);
				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>");
	}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,"网络电影植入效果一览",""));
	}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>");
	}
	
}