$(document).ready(function(){
	var ctx = $("#ctx").val();//获取服务器地址
	
	/***************************    广告位列表页    ***************************/

	//获取尺寸
	if($("#size").val() == 0 || $("#size").val() == ""){
		$("#selectBar_size a:first").addClass("selectBar_change");
	}
	$("#selectBar_size a").each(function(){
 		var value = $(this).attr("value");
 		if($("#size").val() == value){
 			$(this).addClass("selectBar_change").siblings('.selectBar_change').removeClass('selectBar_change');
 		}
 	});
	$("#selectBar_size a").click(function(){
		$("#size").val($(this).attr("value"));
		if($(this).attr("value") != ""){
			var _size = $(this).attr("value").split('X',2);
			$("#width").val(_size[0]);
			$("#height").val(_size[1]);
		}else{
			$("#width").val("");
			$("#height").val("");
		}
 		refreshData();
 	});
	
	//类型
 	if($("#device").val() == 0 || $("#device").val() == ""){
 		$("#selectBar_type a:first").addClass("selectBar_change");
	}
 	$("#selectBar_type a").each(function(){
 		var value = $(this).attr("value");
 		if($("#device").val() == value){
 			$(this).addClass("selectBar_change").siblings('.selectBar_change').removeClass('selectBar_change');
 		}
 	});
 	$("#selectBar_type a").click(function(){
 		$("#device").val($(this).attr("value"));
 		refreshData();
 	});
 	
 	//获取创意尺寸
	if($("#position").val() == 0 || $("#position").val() == ""){
		$("#selectBar_position a:first").addClass("selectBar_change");
	}
	$("#selectBar_position a").each(function(){
 		var value = $(this).attr("value");
 		if($("#position").val() == value){
 			$(this).addClass("selectBar_change").siblings('.selectBar_change').removeClass('selectBar_change');
 		}
 	});
	$("#selectBar_position a").click(function(){
		$("#position").val($(this).attr("value"));
 		refreshData();
 	});
	
 	//状态全部、开启、停用
 	if($("#status").val() == -1 || $("#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+"/operator/main/ad/zone/enable";
		}else if($(this).attr("id") == "btn-disable"){
			url = ctx+"/operator/main/ad/zone/disable";
		}else if($(this).attr("id") == "btn-delete"){
			url = ctx+"/operator/main/ad/zone/delete";
		}
		singleOper($(this),url);
	});
			
	//批量开启,批量暂停
	$("#all-btn-enable,#all-btn-disable,#all-btn-delete").click(function(){
		if($(this).attr("id") == "all-btn-enable"){
			url = ctx+"/operator/main/ad/zone/enable";
		}else if($(this).attr("id") == "all-btn-disable"){
			url = ctx+"/operator/main/ad/zone/disable";
		}else if($(this).attr("id") == "all-btn-delete"){
			url = ctx+"/operator/main/ad/zone/delete";
		}
		batchOpear($(this),url);
	});
	
	/***************************    创建，编辑广告主    ***************************/
	
	//初始化加载上传控件
	if($("#filepicture_picture").html()){
		initFile($("#uploadfile"),$("#default_path").val());
		$('#uploadfilestr').unbind('click').click(function() {
			if($(this).html() == "上传"){
	 			getUploadFile1($(this),"uploadfile","filepicture","uploadfilehidden","default_path_small","imgspan","smallImg");
	 		}
		});
	}
	
	 //选中右移
	$("#all_industry li").live("click",function() {
		var mychoice = this.innerHTML;
		$(this).remove();
	    var ul = document.getElementById("blacklist");
		$(ul).append("<li value='"+$(this).attr("value")+"'>" + mychoice + "<input type='button' class='movedel'/></li>");
		//将黑名单数据传入后端，格式“***,***,***”	
   	    if($("#industry_blacklist").val() == ""){
			$("#industry_blacklist").val($(this).attr("value"));
		}else{
			$("#industry_blacklist").val($("#industry_blacklist").val()+","+$(this).attr("value"));
		}
	});
	
	  //选中左移
	$('#blacklist li input.movedel').live("click",function() {
		var mychoice = $(this).parents("li").text();
		var mychoicevalue = $(this).parents("li").attr("value");
		$(this).parents("li").remove();
	    var ul = document.getElementById("all_industry");
		$(ul).append("<li value="+mychoicevalue+">" + mychoice + "</li>");
		//左移黑名单删除传值
		var industryArr = $("#industry_blacklist").val().split(",");
		for(var i=0;i<industryArr.length;i++){
			if(industryArr[i] == mychoicevalue){
				industryArr.splice(i,1);
			}
		} 
		if(industryArr != 0){
			for(var j=0;j<industryArr.length;j++){
				if(j == 0){
					$("#industry_blacklist").val(industryArr[0]);
				}else{
					$("#industry_blacklist").val($("#industry_blacklist").val()+","+industryArr[j]);
				}
			} 
		}else{
			$("#industry_blacklist").val("");
		}
	});


	//添加全部
	$('#add_all').live("click",function() {
		var lis = $('#all_industry li');
		var industry_blackname = ""; 
		var len = lis.length;		
		var ul = document.getElementById("blacklist");		
		for(var i=0; i<len;i++){			
			var mychoice = lis[i].innerHTML;
			var mychoicevalue = lis[i].value;
			lis[i].remove();
			$(ul).append("<li value="+mychoicevalue+">" + mychoice + "<input type='button' class='movedel'/></li>");  		 
			if($("#industry_blacklist").val() == ""){
				$("#industry_blacklist").val(mychoicevalue);
			}else{
				$("#industry_blacklist").val($("#industry_blacklist").val()+","+mychoicevalue);
			}
    	}	
    	
    	//传值同步添加全部
   	   // document.getElementById("industry_blacklist").value = industry_blackname;		
	});	
	
	//清空全部
	$('#clear_all').live("click",function() {
		var lis = $('#blacklist li');		
		var len = lis.length;		
		var ul = document.getElementById("all_industry");		
		for(var i=0; i<len;i++){			
			var mychoice = $(lis[i]).text();
			lis[i].remove();
			$(ul).append("<li>" + mychoice +"</li>");			
		}
   	    $("#industry_blacklist").val("");
		//传值同步清空
		 // document.getElementById("industry_blacklist").value ="";		
	});	
	
	//网站添加黑名单
	var web_blackname = "";
	var glo_web_blacklist = $('#web_blacklist li');
	for(var counts=0;counts<glo_web_blacklist.length;counts++){
      if(web_blackname){
	   web_blackname = web_blackname +"," + $(glo_web_blacklist[counts]).text();
	  }else{
	  	web_blackname =$(glo_web_blacklist[counts]).text();
	   }
	}
	$("#add_web").live("click",function() {
		var blackname=$('#blackname').val();
		if(blackname == ""){
			bootbox.alert("请输入要添加的网站黑名单！");
			return false;
		}	
		var blacknameArr = $("#web_blacklistvalue").val().split(",");
		if($.inArray(blackname, blacknameArr) != -1){  
			bootbox.alert("您输入的网站黑名单已存在！");
			return false;
		}  
		var ul = document.getElementById("web_blacklist");
		$(ul).append("<li>" + blackname + "<input type='button' class='movedel'/></li>");	
		$('#blackname').val("");
		//黑名单传值
		if($("#web_blacklistvalue").val() == ""){
			$("#web_blacklistvalue").val(blackname);
		}else{
			$("#web_blacklistvalue").val($("#web_blacklistvalue").val()+","+blackname);
		}
	});
	//删除黑名单
	$('#web_blacklist li input.movedel').live("click",function() {		
		$(this).parents("li").remove(); 
		var mychoice = $(this).parents("li").text();
		var blacknameArr = $("#web_blacklistvalue").val().split(",");
		for(var i=0;i<blacknameArr.length;i++){
			if(blacknameArr[i] == mychoice){
				blacknameArr.splice(i,1);
			}
		} 
		//左移黑名单删除传值
		if(blacknameArr.length !=0){
			for(var j=0;j<blacknameArr.length;j++){
				if(j == 0){
					$("#web_blacklistvalue").val(blacknameArr[0]);
				}else{
					$("#web_blacklistvalue").val($("#web_blacklistvalue").val()+","+blacknameArr[j]);
				}
			} 
		}else{
			$("#web_blacklistvalue").val("");
		}
		
	});
	
	//清空黑名单
	$("#clear_allweb").live("click",function() {
		var lis = $('#web_blacklist li');		
		var len = lis.length;			
		for(var i=0; i<len;i++){			
			lis[i].remove();
		}
		//传值处同步删除
		document.getElementById("web_blacklistvalue").value ="";
	
	});
 	
 	//初始校验表单
 	if($("#validateForm").html()){
 		$("#validateForm").autovalidate();
 	}
	$(".btn-save").click(function(){
		if (!$("#validateForm").validate()) {
			return false;
		}
		$("#validateForm").submit();
		return false;
	});
	
	$(".btn-cancel").click(function(){
		location.href = ctx+"/operator/main/ad/zone";
	});
	
	//鼠标划过小图片显示大图片
	 $("#smallImg").mousemove(function(event){
		var event = event || window.event;
		$("#enlarge_images").css("display","block");
		var imgsrc = $(this).attr("src");
		imgsrc = imgsrc.substring(0,imgsrc.lastIndexOf(".")-6)+imgsrc.substring(imgsrc.lastIndexOf("."),imgsrc.length);
		$("#enlarge_images").html('<img src="'+imgsrc+'" />');
		$("#enlarge_images").css("top",document.body.scrollTop + event.clientY + 10 + "px");
		$("#enlarge_images").css("left",document.body.scrollLeft + event.clientX + 10 + "px");
	});
	$("#smallImg").mouseout(function(event){
		$("#enlarge_images").html("");
		$("#enlarge_images").css("display","none");
	});
	
});
		
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 initFile(obj,pictureName){
	var name = "";
	if(pictureName == "" || pictureName == null || pictureName == "noFiles"){
		name = "No File ...";
	}else{
		name = pictureName.substring(pictureName.lastIndexOf("\\")+1,pictureName.length);
	}
	obj.ace_file_input({
		no_file:name,
		btn_choose:'选择',
		btn_change:'改变',
		droppable:false,
		onchange:null,
		thumbnail:false
	});
}

//文件上传ajax文件上传
function getUploadFile1(obj,_elemId,_removeId,picPathId,smallpicPathId,imgSpanId,smallpicObj){
	var _href = obj.attr('href');
	$.ajaxFileUpload({
		url:_href, 
		secureuri:false,
		fileElementId:_elemId,
		dataType: 'json',
		success: function (result, status) {
			var path = result.data['url'];
			removeFile1(_elemId,_removeId,picPathId,result);
			if (result.status == 'ok') {
				var orgImg = path.substring(0,path.indexOf(","));
				var smallImg = path.substring(path.indexOf(",")+1);
				$("#"+picPathId).val(orgImg);
				$("#"+smallpicPathId).val(smallImg);
				$("#"+smallpicObj).attr("src",smallImg);
				$("#"+smallpicObj).css("display","block");
				$("#"+imgSpanId).css("display","block");
				var result = 0;
				removeFile1(_elemId,_removeId,picPathId,result);
				bootbox.alert("上传成功！");
				if(obj.hasClass("uploadOrView")){
					obj.html("预览");
				}
			}else if (result.status == 'error') {
				$("#"+picPathId).val("");
				$("#"+smallpicPathId).val("");
				$("#"+smallpicObj).css("display","none");
				$("#"+imgSpanId).css("display","none");
			    bootbox.alert(result.msg);
			    if(obj.hasClass("uploadOrView")){
					obj.html("上传");
				}
			}
		},
		error: function (data, status, e)
		{
			$("#"+picPathId).val("");
			$("#"+smallpicPathId).val("");
			$("#"+smallpicObj).css("display","none");
			$("#"+imgSpanId).css("display","none");
			removeFile1(_elemId,_removeId,picPathId);
			console.log(e);
			if(obj.hasClass("uploadOrView")){
				obj.html("上传");
			}
		}
	});
	return false;
}


//上传文件时conle file 删除 file
function removeFile1(_elemId,_removeId,picPathId,result){
	var filename = $('#'+_elemId).parent().find("span").attr("data-title");
	var isChrome = navigator.userAgent.toLowerCase().match(/chrome/) != null;//判断是否是谷歌浏览器
	if(filename==null || filename==''){
		filename = "No File ...";
	}
	var option = {}; 
	var defaultOptions = {
			no_file:'No File ...',
			btn_choose:'选择',
			btn_change:'改变',
			droppable:false,
			onchange:null,
			thumbnail:false
	};
	if(isChrome && result == 0){
		option = {no_file:filename };
	}else if(isChrome && result == 1){
		option = {no_file:'No File ...' };
	}else{
		option = {no_file:filename };
	}
	var opts = $.extend(defaultOptions,option);
	var newfile = $("#"+_elemId).clone();
	$("#"+_removeId).find(".ace-file-input").remove();
	$("#"+_removeId+" #"+picPathId).after(newfile);
	$("#"+_elemId).ace_file_input(opts);
}
