$(document).ready(function(){
	var ctx = $("#ctx").val();//获取服务器地址
	
	/***************************    选择媒体    ***************************/
	
	$(".selectOperator_save").click(function(){
		var checkboxs = $("#selectOperatorDiv input[type='checkbox']:checked");
		var operatorIds = [];
		checkboxs.each(function(){
			var id = parseInt($(this).val());
			operatorIds.push(id);
		});
		parent.$("#input_selectOperator").val(operatorIds);//父层的元素
		parent.$("#selectOperastor_span").text("已选择"+ checkboxs.length +"个媒体");
		var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
		parent.layer.close(index);//再执行关闭
	});
	
	$("#selectOperatorDiv input[type='checkbox']").each(function(){
		var obj = $(this);
		var ids = parent.$("#input_selectOperator").val().split(",");
		for(var i = 0,l=ids.length;i<l;i++){
			if(obj.attr("id") == ids[i]){
				obj.attr('checked',true);
			}
		}
	});
	
	// 分页中超链接跳转
	$('#selectOperatorForm .pagination ul li a[href]').click(function(){
		var _page = $(this).attr('href').substr(1);
		$('#selectOperatorForm').find('input:hidden[name="page"]').val(_page);
		$('#selectOperatorForm').submit();
	});
	
/***************************    选择媒体场景    ***************************/
	
	$(".selectOperatorPlace_save").click(function(){
		var checkboxs = $("#selectOperatorPlaceDiv input[type='checkbox']:checked");
		var placeId = "a";
		var placeName= "";
		if(checkboxs.length > 0){
			checkboxs.each(function(){
				var id = parseInt($(this).val());
				placeId += ","+id;
				placeName += $(this).attr("title")+",";
			});
			parent.$("#btn-selectPlace").addClass("aChange");
			parent.$("#btn-selectPlaceClass").addClass("aChange");
		}else{
			placeId = "";
			parent.$("#btn-selectPlace").removeClass("aChange");
			parent.$("#btn-selectPlaceClass").removeClass("aChange");
		}
		parent.$("#selectPlaceId").val(placeId);//父层的元素
		parent.$("#selectPlaceName").val(placeName);//父层的元素
		var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
		parent.layer.close(index);//再执行关闭
	});
	
	$("#selectOperatorPlaceDiv input[type='checkbox'],#selectPlaceDiv input[type='checkbox'],#selectPlaceClassDiv input[type='checkbox']").each(function(){
		var obj = $(this);
		var ids = parent.$("#selectPlaceId").val().split(",");
		for(var i = 1,l=ids.length;i<l;i++){
			if(obj.val() == ids[i]){
				obj.attr('checked',true);
			}
		}
	});
	
	// 分页中超链接跳转
	$('#selectOperatorPlaceForm .pagination ul li a[href]').click(function(){
		var _page = $(this).attr('href').substr(1);
		$('#selectOperatorPlaceForm').find('input:hidden[name="page"]').val(_page);
		$('#selectOperatorPlaceForm').submit();
	});
	
/***************************    选择小场景    ***************************/
	
	$(".selectPlace_save").click(function(){
		var checkboxs = $("#selectPlaceDiv input[type='checkbox']:checked");
		var placeId = "b";
		var placeName= "";
		if(checkboxs.length > 0){
			checkboxs.each(function(){
				var id = parseInt($(this).val());
				placeId += ","+id;
				placeName += $(this).attr("title")+",";
			});
			parent.$("#btn-selectOperatorPlace").addClass("aChange");
			parent.$("#btn-selectPlaceClass").addClass("aChange");
		}else{
			placeId = "";
			parent.$("#btn-selectOperatorPlace").removeClass("aChange");
			parent.$("#btn-selectPlaceClass").removeClass("aChange");
		}
		parent.$("#selectPlaceId").val(placeId);//父层的元素
		parent.$("#selectPlaceName").val(placeName);//父层的元素
		var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
		parent.layer.close(index);//再执行关闭
	});
	
	// 分页中超链接跳转
	$('#selectPlaceForm .pagination ul li a[href]').click(function(){
		var _page = $(this).attr('href').substr(1);
		$('#selectPlaceForm').find('input:hidden[name="page"]').val(_page);
		$('#selectPlaceForm').submit();
	});
	
/***************************    选择数聚场景    ***************************/
	
	$(".selectPlaceClass_save").click(function(){
		var checkboxs = $("#selectPlaceClassDiv input[type='checkbox']:checked");
		var placeId = "c";
		var placeName= "";
		if(checkboxs.length > 0){
			checkboxs.each(function(){
				var id = parseInt($(this).val());
				placeId += ","+id;
				placeName += $(this).attr("title")+",";
			});
			parent.$("#btn-selectPlace").addClass("aChange");
			parent.$("#btn-selectOperatorPlace").addClass("aChange");
		}else{
			placeId = "";
			parent.$("#btn-selectPlace").removeClass("aChange");
			parent.$("#btn-selectOperatorPlace").removeClass("aChange");
		}
		parent.$("#selectPlaceId").val(placeId);//父层的元素
		parent.$("#selectPlaceName").val(placeName);//父层的元素
		var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
		parent.layer.close(index);//再执行关闭
	});
	
	// 分页中超链接跳转
	$('#selectPlaceClassForm .pagination ul li a[href]').click(function(){
		var _page = $(this).attr('href').substr(1);
		$('#selectPlaceClassForm').find('input:hidden[name="page"]').val(_page);
		$('#selectPlaceClassForm').submit();
	});
	
/***************************    选择终端    ***************************/
	
	$(".selectWifi_save").click(function(){
		var checkboxs = $("table tbody .checkboxStyle_change");
		var ids = [];
		checkboxs.each(function(){
			var id = parseInt($(this).attr("id"));
			ids.push(id);
		});
		parent.$("#selectWifi").val(ids);//父层的元素
		var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
		parent.layer.close(index);//再执行关闭
	});
	
	// 分页中超链接跳转
	$('#selectWifiForm .pagination ul li a[href]').click(function(){
		var _page = $(this).attr('href').substr(1);
		$('#selectWifiForm').find('input:hidden[name="page"]').val(_page);
		$('#selectWifiForm').submit();
	});
	
	$("table tbody .checkboxStyleTd").each(function(){
		var obj = $(this);
		var ids = parent.$("#selectWifi").val().split(",");
		for(var i = 0,l=ids.length;i<l;i++){
			if(obj.attr("id") == ids[i]){
				obj.addClass("checkboxStyle_change");
			}
		}
	});
	
	//获取省份信息
	if($("#main_cnul").html()){
		$("#cn_son_ul").hide();
		if($("#cn").val() == "省"){
			$("#cn").val("");
		}
		$("#cn_son_ul").append("<li value=''>省</li>");
		var url = "cn";
		$.getJSON( url, function(data) {
			var data_1 = data.province;
			$.each( data_1, function( key, val ) {
				$("#cn_son_ul").append('<li>' + val + '</li>');
			});
		});
		$("#cnul_span").on("click",function(){
			if($('#cn_son_ul').height()>210){
				$('#cn_son_ul').addClass("son_ul_change");
	 		}
	 		$('#cn_son_ul').show(); //ul显示
		});
		$("ul#cn_son_ul li").live("click",function(){
	 		$("#cnul_span").text($(this).text());
	 		if($(this).text() == "省"){
	 			$("#cn").val("");
	 		}else{
	 			$("#cn").val($(this).text());
	 		}
	 		$("#cnCity").val("");
	 		$("#cnCityul_span").text("市");
	 		$("#cn_son_ul").hide(); //ul隐藏
	 		$('#selectWifiForm').submit();
		});
		
		$("#cnul_box").mouseleave(function(){
			$('#cn_son_ul').hide(); //ul隐藏
			$('#cn_son_ul').removeClass("son_ul_showchange");
		});
	}
	
	//获取省份对应的城市信息
	if($("#main_cnCityul").html()){
		$("#cnCityul_son_ul").hide();
		if($("#cnCity").val() == "市"){
			$("#cnCity").val("");
		}
		$("#cnCityul_span").on("click",function(){
			initSelectUlData("cnCityul_son_ul","市","city?province=" + $("#cn").val(),$("#cn").val(),"cnCityul_box","city");
		});
	 	$('ul#cnCityul_son_ul li').live("click",function(){
			$("#cnCityul_span").text($(this).text());
			if($(this).text() == "市"){
				$("#cnCity").val("");
	 		}else{
	 			$("#cnCity").val($(this).text());
	 		}
			$("#cnCityul_son_ul").hide(); //ul隐藏
			$('#selectWifiForm').submit();
		});
	}
});

function initSelectUlData(target_sonulId,defaultName,url,paramValue,boxName,tabId){
	$("#"+target_sonulId).empty();
 	$("#"+target_sonulId).append("<li value=''>"+defaultName+"</li>");
 	if(paramValue != -1 && paramValue != ""){
 	 	$.getJSON( url, function(data) {
 	 		var data_1 = data[tabId];
 	 		$.each( data_1, function( key, val ) {
 	 			$("#"+target_sonulId).append('<li>' + val + '</li>');
	 		});
 			$("#"+target_sonulId).show(); //ul显示
			if($("#"+target_sonulId).height()>210){
				$("#"+target_sonulId).addClass("son_ul_change");
	 		};
 		});
 	}else{
 		$("#"+target_sonulId).show();
 	}
	$("#"+boxName).mouseleave(function(){
		$("#"+target_sonulId).hide(); //ul隐藏
		$("#"+target_sonulId).removeClass("son_ul_change");
 	});
}