$(document).ready(function() {
	var ctx = $("#ctx").val();//获取服务器地址
	
	/***************************        新增数聚场景列表页    ***************************/

	$("#btn-submit").click(function(){
		var checkboxs = $(".place_left input[type='checkbox']:checked");
		var placeId = "";
		if(checkboxs.length > 0){
			placeId = checkboxs.map(function(i,span){
				return $(span).val();
			}).toArray().join('-');
		}else{
			return false;
		}
		var placeClassId = $(".place_right input[type='radio']:checked").val();
		if(placeClassId == undefined || placeClassId == ""){
			return false;
		}
		$.post(ctx+"/back/main/ap/place/add",{placeId:placeId,placeClassId:placeClassId},function(data){
			$("#placeTopform").submit();
		});
		return false;
	});
	
	//获取所有的子场景
	if($("#main_placeClassIdul").html()){
		$("#placeClassIdul_son_ul").hide();
		$("#placeClassIdul_span").on("click",function(){
			if($('#placeClassIdul_son_ul').height()>210){
				$('#placeClassIdul_son_ul').addClass("son_ul_change");
	 		}
	 		$('#placeClassIdul_son_ul').show(); //ul显示
		});
		$("ul#placeClassIdul_son_ul li").live("click",function(){
	 		$("#placeClassIdul_span").text($(this).text());
	 		$("#placeClassId").val($(this).attr("value"));
	 		$("#placeClassIdul_son_ul").hide(); //ul隐藏
	 		var url = "";var param = null;
	 		if($("#placeClassId").val() == -1 || $("#placeClassId").val() == ""){
	 			$("#place_bottom .input-checkboxsDiv_d").empty();
	 			return false;
	 		}else{
	 			refreshPlaceData();
	 		}
		});
		$("#placeClassIdul_box").mouseleave(function(){
			$('#placeClassIdul_son_ul').hide(); //ul隐藏
			$('#placeClassIdul_son_ul').removeClass("son_ul_showchange");
		});
	}
	
	$("#btn-delete").click(function(){
		var checkboxs = $("#place_bottom input[type='checkbox']:checked");
		var id = [];
		if(checkboxs.length > 0){
			checkboxs.each(function(){
				id.push(parseInt($(this).val()));
			});
		}else{
			return false;
		}
		$.ajax({
			async: false, 
			traditional: true, 
            type: "POST",
             url: ctx+"/back/main/ap/place/delete-place",
             data:{"id":id},
             success: function(data){
            	 refreshPlaceData();
             }
		});
		return false;
	});
	
	$("#btn-sort-submit").click(function(){
		var checkboxs = $(".place_left input[type='checkbox']:checked");
		var ids = [];
		if(checkboxs.length > 0){
			checkboxs.each(function(){
				ids.push(parseInt($(this).attr("value")));
			});
		}else{
			return false;
		}
		var placeId = $(".place_right input[type='radio']:checked").val();
		if(placeId == undefined || placeId == ""){
			return false;
		}
		$.ajax({
			async: false, 
			traditional: true, 
            type: "POST",
             url: ctx+"/back/main/ap/place/save",
             data:{"id":ids,"placeId":placeId},
             success: function(data){
            	 refreshData();
             }
		});
		return false;
	});
	
	
	
	$("#save").click(function(){
		var alllength  = $(".place_left input[type='checkbox']").length;
		var length  = $(".place_left input[type='checkbox']:checked").length;
		if(length < alllength){
			$(".place_left input[type='checkbox']").attr("checked",true);
		}else{
			$(".place_left input[type='checkbox']").attr("checked",false);
		}
	});
	
	$("#save_a").click(function(){
		$(".input-checkboxsDiv_d input[type='checkbox']").attr("checked",true);
	});
	
	$("#del").click(function(){
			$(".input-checkboxsDiv_d input[type='checkbox']").attr("checked",false);
s	});
	
});



function refreshPlaceData(){
	var param = {
		'placeClassId':$("#placeClassId").val()
	};
	$.post($("#ctx").val()+"/back/main/ap/place/placeName",param,function(data){
		$("#place_bottom .input-checkboxsDiv_d").empty();
		var html = "";
		$.each(data,function(key, val){
			html += '<div class="single_div_d">';
			html += '<input name="place" type="checkbox" value='+val.id+' id="placeson_'+val.id+'"><label class="single_div_span" for="placeson_'+val.id+'">'+val.name+'</label>';
			html += '</div>';
		});
		$("#place_bottom .input-checkboxsDiv_d").append(html);
	});
}