$(document).ready(function(){
	var ctx = $("#ctx").val();//获取服务器地址
	
	/***************************    广告主列表页    ***************************/
	//获取行业
	if($("#industryId").val() == 0 || $("#industryId").val() == ""){
		$("#selectBar_industry a:first").addClass("selectBar_change");
	}
	$.ajax({
		url: ctx+"/widget/selector/provide/industry",
		type: "post",
		contentType: "application/json",
		dataType:"json",
		success: function (data) {
			$.each( data.allIndustry, function( key, val ){
				if($("#industryId").val() == val.id){
				   $("#selectBar_industry").append('<a value='+ val.id +' class="selectBar_change">'+val.name+'</a>');
				}else{
				   $("#selectBar_industry").append('<a value='+ val.id +'>'+val.name+'</a>');
				}
			});
		}
	});
			
	$("#selectBar_industry a").live("click",function(){
		$("#industryId").val($(this).attr("value"));
		refreshData();
	});
	
 	//状态全部、开启、停用
 	if($("#status").val() == 0 || $("#status").val() == ""){
 		$("#selectBar_status a:first").addClass("selectBar_change");
	}
 	$("#selectBar_status a").each(function(){
 		var value = $(this).attr("value");
 		if($("#status").val() == value){
 			$(this).addClass("selectBar_change").siblings('.selectBar_change').removeClass('selectBar_change');
 		}
 	});
 	$("#selectBar_status a").click(function(){
 		$("#status").val($(this).attr("value"));
 		refreshData();
 	});
 
	//开启,暂停
	$("#btn-enable,#btn-disable,#btn-delete").click(function(){
		if($(this).attr("id") == "btn-enable"){
			url = ctx+"/advertiser/main/ad/advertiser/enable";
		}else if($(this).attr("id") == "btn-disable"){
			url = ctx+"/advertiser/main/ad/advertiser/disable";
		}else if($(this).attr("id") == "btn-delete"){
			url = ctx+"/advertiser/main/ad/advertiser/delete";
		}
		singleOper($(this),url);
	});
			
	//批量开启,批量暂停
	$("#all-btn-enable,#all-btn-disable,#all-btn-delete").click(function(){
		if($(this).attr("id") == "all-btn-enable"){
			url = ctx+"/advertiser/main/ad/advertiser/enable";
		}else if($(this).attr("id") == "all-btn-disable"){
			url = ctx+"/advertiser/main/ad/advertiser/disable";
		}else if($(this).attr("id") == "all-btn-delete"){
			url = ctx+"/advertiser/main/ad/advertiser/delete";
		}
		batchOpear($(this),url);
	});
	
	/***************************    创建，编辑广告主    ***************************/
	
	//校验所属行业
	$("#industryul_select_box").mouseleave(function(){
 		$('#industry_son_ul').hide(); //ul隐藏
 		if($("#industryId").val() == "" || $("#industryId").val() == 0){
			$("#main_industryul").addClass("mainChange").attr("title","请选择所属行业");
			$("#industryul_selectdiv").find('span.spanVaildate_ul').html("请选择所属行业");
		}else{
			$("#main_industryul").removeClass("mainChange").attr("title","");
			$("#industryul_selectdiv").find('span.spanVaildate_ul').html("");
		}
 	});
	
	//初始化加载上传控件
	if($("#cpiPath_div").html()){
		uploadFileHandler("icp_input_id","cpiPath","cpiPathUploadfilestr","cpiPath_div");
	}
 	if($("#licencePath_div").html()){
 		uploadFileHandler("licencePath_input_id","licencePath","licencePathUploadfilestr","licencePath_div");
	}
 	if($("#idPath_div").html()){
 		uploadFileHandler("idPath_input_id","idPath","idPathUploadfilestr","idPath_div");
	}
 	if($("#orgPath_div").html()){
 		uploadFileHandler("orgPath_input_id","orgPath","orgPathUploadfilestr","orgPath_div");
	}
 	
 	//初始校验表单
 	if($("#validateForm").html()){
 		$("#validateForm").autovalidate();
 	}
	$(".btn-save").click(function(){
		if (!$("#validateForm").validate()) {
			return false;
		}
		if($("#industryId").val() == "" || $("#industryId").val() == 0 ){
			$("#main_industryul").addClass("mainChange").attr("title","请选择所属行业");
			$("#industryul_selectdiv").find('span.spanVaildate_ul').html("请选择所属行业");
			return false;
		}
		$("#validateForm").submit();
		return false;
	});
	
	$(".btn-cancel").click(function(){
		location.href = ctx+"/advertiser/main/ad/advertiser/list";
	});
	
});
		
function exportExcel_list(){
	var searchValue = $("#searchValue").val();
	if(searchValue == "请输入关键字"){
		searchValue = "";
	}
	var param = "?industryId="+$("#industryId").val()+"&status="+$("#status").val()+"&searchValue="+searchValue;
	$("#exportExcel").attr("href",$("#ctx").val()+"/advertiser/main/ad/advertiser/exportDataReportExcel"+param);
	return false;
}

function uploadFileHandler(elemIdObjId,objId,fileStrObj,objDivId){
	initFile($("#"+elemIdObjId),$("#"+objId).val());
	if($("#"+objId).val() != "" && $("#"+objId).val() != "noFiles"){
 		$('#'+fileStrObj).html("预览");
 	}
	$('#'+fileStrObj).unbind('click').click(function() {
		if($(this).html() == "上传"){
			getUploadFile($(this),elemIdObjId,objDivId,objId,null,null,null);
		}else if($(this).html() == "预览"){
			if($("#"+objId).val() != ""){
				bootbox.dialog({
				message:'<img src="'+$("#"+objId).val()+'"/>',
				title: "浏览素材",
				locale: 'zh_CN'
			});
			}
		}
	});
}