$(document).ready(function() {
	var ctx = $("#ctx").val();//获取服务器地址
	
	/***************************    热点管理列表页    ***************************/

	//获取省份信息
	if($("#main_cnul").html()){
		$("#cn_son_ul").hide();
		if($("#cn").val() == "省"){
			$("#cn").val("");
		}
		$("#cn_son_ul").append("<li value='-1'>省</li>");
		var url = ctx+"/operator/main/ap/apmac/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隐藏
		});
		
		$("#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","市",ctx+"/operator/main/ap/apmac/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隐藏
		});
	}
	
	//获取场景信息
	if($("#main_placeul").html()){
		$("#placeul_son_ul").hide(); //ul隐藏
		if($("#place").val() == "场景"){
			$("#place").val("");
		}
		$("#placeul_span").on("click",function(){
			initSelectUlData("placeul_son_ul","场景",ctx+"/operator/main/ap/apmac/place","0","placeul_box","place");
		});
		$("ul#placeul_son_ul li").live("click",function(){
	 		$("#placeul_span").text($(this).text());
	 		if($(this).text() == "场景"){
	 			$("#place").val("");
	 		}else{
	 			$("#place").val($(this).text());
	 		}
	 		$("#placeul_son_ul").hide(); //ul隐藏
		});
	}
	
	//获取数据场景信息
	if($("#main_placeIdul").html()){
		$("#placeIdul_son_ul").hide(); //ul隐藏
		if($("#placeId").val() == "数聚场景"){
			$("#placeId").val("");
		}
		$("#placeIdul_span").on("click",function(){
			initSelectUlData("placeIdul_son_ul","数聚场景",ctx+"/operator/main/ap/apmac/placeName","0","placeIdul_box","place_id");
		});
		$("ul#placeIdul_son_ul li").live("click",function(){
	 		$("#placeIdul_span").text($(this).text());
	 		if($(this).attr("value") == -1){
	 			$("#placeId").val("");
	 			$("#placeName").val("数聚场景");
	 		}else{
	 			$("#placeId").val($(this).attr("value"));
	 			$("#placeName").val($(this).text());
	 		}
	 		$("#placeIdul_son_ul").hide(); //ul隐藏
		});
	}
	
	function initSelectUlData(target_sonulId,defaultName,url,paramValue,boxName,tabId){
		$("#"+target_sonulId).empty();
	 	$("#"+target_sonulId).append("<li value='-1'>"+defaultName+"</li>");
	 	if(paramValue != -1 && paramValue != ""){
	 	 	$.getJSON( url, function(data) {
	 	 		if(tabId != null && tabId == "place_id"){
	 	 			$.each( data, function( key, val ) {
						$("#"+target_sonulId).append('<li value='+ val.place_id +'>' + val.name + '</li>');
					});
	 	 		}else{
	 	 			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");
	 	});
	}
	
});