$(function(){
	$("#nearbyu").children('li').each(function(e){
		$("#address").hide();
		$(this).bind('click',function(){
			if(e==0){$("#near").show();$("#address").hide();}
			if(e==1){$("#near").hide();$("#address").show();}
			$(this).addClass('on');
			$(this).siblings('li').removeClass('on');
		});
	});
	var xpoint=$("input[name='xpoint']").val();
	var ypoint=$("input[name='ypoint']").val();
	if(!xpoint||!ypoint){
		var xpoint=114.48;
		var ypoint=38.03;	
	}
	userxypoint(ypoint,xpoint);
});

function relocation(){
	var ua = window.navigator.userAgent.toLowerCase();
    if(ua.match(/MicroMessenger/i) == 'micromessenger'){
         wxlocation();
    }else{
        bdPosition();
    }   
}
function bdPosition(){
    var geolocation = new BMap.Geolocation();
	geolocation.getCurrentPosition(function(r) {
	    if(this.getStatus() === 0) {
			userxypoint(r.latitude,r.longitude);
	    }
	    else {
			console.log('定位失败，原因：' + this.getStatus());
	    }        
	},{enableHighAccuracy: true});
}
//将坐标返回到服务端;
function userxypoint(latitude,longitude){
		var query = new Object();
		var geo_url = APP+'Home/ajax/setgeo.html';
		query.ypoint = latitude;
		query.xpoint = longitude;
		var type = $("input[name='type']").val();
		var shop_id = $("input[name='shop_id']").val();
		$.ajax({
			url:geo_url,
			data:query,
			type:"post",
			dataType:"json",
			success:function(data){
				if(data.status == 0){
					create_map(longitude,latitude);
					if(data.result.pois){
						var html='';
						var pois=data.result.pois;
						for(var i in pois){
							//var setinfo = new Array();
							var x=pois[i]['point']['x'];
							var y=pois[i]['point']['y'];
							var name=pois[i]['name'];
							html+='<li><a onclick="change_position('+"'"+x+"'"+','+"'"+y+"'"+','+"'"+name+"'"+','+"'"+type+"'"+','+"'"+shop_id+"'"+')"><h3>'+pois[i]['name']+'</h3><p class="address">'+pois[i]['addr']+'</p></a></li><div class="clear"></div>';
						}
						$("#pois").html(html);
					}else{
						$.showErr('获取小区信息失败',0,'hc');
					}
				}
			}
			,error:function(){					
			}
	});		 		
}
function create_map(x,y){
	var map;
	map = new BMap.Map("map");	
    map.centerAndZoom(new BMap.Point(x,y),14);
	//地图的缩放
	var navControl = new BMap.NavigationControl({anchor:BMAP_ANCHOR_BOTTOM_RIGHT,type:1});
    map.addControl(navControl);
	//点击获取坐标
	map.addEventListener("click",function(e){ 
		userxypoint(e.point.lat,e.point.lng);
		//alert(e.point.lng+","+e.point.lat);// 单击地图获取坐标点；  
		//map.panTo(new BMap.Point(e.point.lng,e.point.lat));// map.panTo方法，把点击的点设置为地图中心点 
	});
	//移除标注重新添加
	map.removeOverlay(marker);
    map.clearOverlays(marker);
	var point={"lng":x,"lat":y};
    var marker = new BMap.Marker(point);
    map.addOverlay(marker);
    map.panTo(point);
    marker.setAnimation(BMAP_ANIMATION_BOUNCE); //跳动的动画
}
function searchLocation(){
	var site = $("input[name='address']").val();
	if(!site){$.showErr('请输入搜索地址',0,'hc');return false;}
	var option = {
		onSearchComplete: function(results){
			if (local.getStatus() == BMAP_STATUS_SUCCESS){
				if(results.getCurrentNumPois()){
					//alert(JSON.stringify(results.getPoi(0).point));
					userxypoint(results.getPoi(0).point.lat,results.getPoi(0).point.lng)
				}
			}else{
				$.showErr('没有相匹配的结果',0,'hc');
			}
		}
	};
	var city=$("input[name='city']").val();
	var local = new BMap.LocalSearch(city,option);
    
	local.search(site);
}

function change_position(x,y,name,type,shop_id){
	var query = new Object();
	var url = APP+'Mobile/ShopsTakeout/set_siteinfo.html';
	query.ypoint = y;
	query.xpoint = x;
	query.name = name;
	query.type = type;
	query.shop_id=shop_id;
	$.ajax({
		url:url,
		data:query,
		type:"post",
		dataType:"json",
		success:function(data){
			if(data.status==1){
				window.location.href=data.url;
			}
		}
		,error:function(){					
		}
	});		 		
}
