/*
 * 校区地理组织信息编辑保存
 * add by yubing 2017-1-6
 * */

var geocoder, map ,marker= null;

function codeAddress(lat,lng){
	var latLng = new qq.maps.LatLng(lat,lng);
    geocoder.getAddress(latLng);
    geocoder.setComplete(function(result){
        map.setCenter(result.detail.location);
        marker.setPosition(result.detail.location);
        $("#address").val(result.detail.address);
    });
}

function initAddress(address,type){
	if($.trim(address)){
		if(type=='1'){
			var latlng= $("#latlng").val()
    		if(latlng){
    			var center = new qq.maps.LatLng(latlng.split(",")[0],latlng.split(",")[1]);
    			map.setCenter(center);
                marker.setPosition(center);
                return false;
    		}else{
    			geocoder.getLocation(address);
                geocoder.setComplete(function(result){
               		$("#latlng").val(result.detail.location);
                    map.setCenter(result.detail.location);
                    marker.setPosition(result.detail.location);
                });
    		}
		}else{
			geocoder.getLocation(address);
            geocoder.setComplete(function(result){
           		$("#latlng").val(result.detail.location);
                map.setCenter(result.detail.location);
                marker.setPosition(result.detail.location);
            });
		}
	}
	else{
	    //获取城市列表接口设置中心点
	    citylocation = new qq.maps.CityService({
	        complete : function(result){
	            map.setCenter(result.detail.latLng);
	        }
	    });
	    //调用searchLocalCity();方法    根据用户IP查询城市信息。
	    citylocation.searchLocalCity();
	}
}

function chooseCity(){
	var provinceCode = $("#provinceSelect option:selected").val();
	if(provinceCode){
		initCity(provinceCode,"");
	}
}

function getCityOnMap(){
	var provinceName=$("#provinceSelect option:selected").html();
	var cityName = $("#citySelect option:selected").html();
	$("#address").val(provinceName+cityName);
	if(cityName!=""){
		initAddress(cityName,"2");
	}
}

function organizationChange(){
	var organizationId=$("#organizationId").val();
	var lnglat=$("#lnglat_"+organizationId).val();
	if(lnglat!=""&&";"!=lnglat){
		var lng=lnglat.split(",")[0];
		var lat=lnglat.split(",")[1];
		/*center = new qq.maps.LatLng(lat,lng);
		map = new qq.maps.Map(document.getElementById("container"),{
			center: center,
	        zoom: 13                       //设置缩放级别
	    });*/
		var latLng = new qq.maps.LatLng(lat,lng);
	    geocoder.getAddress(latLng);
	    geocoder.setComplete(function(result){
	        map.setCenter(result.detail.location);
	        marker.setPosition(result.detail.location);
	    });
	}
}

$(function(){
	//初始化城市
	var lng=$("#lng").val();
	var lat=$("#lat").val();
	
	//初始化地图
	geocoder = new qq.maps.Geocoder();
	var center = null;
	if(lng==""){
		center = new qq.maps.LatLng(39.916527,116.397128); //默认北京
		map = new qq.maps.Map(document.getElementById("container"),{
			center: center,
	        zoom: 13                       //设置缩放级别
	    });
		initAddress("","")
	}
	else{
		center = new qq.maps.LatLng(lat,lng);
		map = new qq.maps.Map(document.getElementById("container"),{
			center: center,
	        zoom: 13                       //设置缩放级别
	    });
		
		var latLng = new qq.maps.LatLng(lat,lng);
	    geocoder.getAddress(latLng);
	    geocoder.setComplete(function(result){
	        map.setCenter(result.detail.location);
	        marker.setPosition(result.detail.location);
	    });
	}
	marker = new qq.maps.Marker({map: map});
	qq.maps.event.addListener(map, 'click', function(event) {
        //获取选取点的经纬度；
        $("#latlng").val(event.latLng.getLng()+","+ event.latLng.getLat());
        codeAddress(event.latLng.getLat(),event.latLng.getLng());
    }); 
	
	$("#btn-save").bind("click",function(){
		var name=$("#name").val();
		if($.trim(name)==""){
			alert("楼栋名称不能为空");
			$("#name").focus();
			return;
		}
		var organizationId=$("#organizationId").val();
		if($.trim(organizationId)==""){
			alert("请选择所属组织");
			return;
		}
		
		var latlng = $("#latlng").val();
		if(latlng){
			$("#lng").val($.trim(latlng.split(",")[0])); //经度
			$("#lat").val($.trim(latlng.split(",")[1])); //纬度
		}
		showBlock();
		$("#conditionform").submit();
	});
});