var overlays = [];
var baiduinfoWindows = [];
var newRectangle;
var baiduinfoWindow=null;
var newLeftPointX;
var newLeftPointY;
var newRightPointX;
var newRightPointY;
var leftPointLng;
var leftPointLat;
var rightPointLng;
var rightPointLat;
var pointCenter;
var pointCenterNew;
var baidudrawingManager;
var baiduQueryVechileloading=null;
/*
*初始化百度地图区域查车
*/
function initializeBaiduDraw(){
	//矩形的样式
    var styleOptions = {
        strokeColor:"blue",    //边线颜色。
        fillColor:"blue",      //填充颜色。当参数为空时，圆形将没有填充效果。
        strokeWeight: 1,       //边线的宽度，以像素为单位。
        strokeOpacity: 0.1,	   //边线透明度，取值范围0 - 1。
        fillOpacity: 0.3,      //填充的透明度，取值范围0 - 1。
        strokeStyle: 'solid'   //边线的样式，solid或dashed。
    }

    //实例化鼠标绘制工具
    baidudrawingManager = new BMapLib.DrawingManager(baidMap, {
        isOpen: false, 							//是否开启绘制模式
        drawingMode: BMAP_DRAWING_RECTANGLE,
        enableDrawingTool: false, 				//是否显示工具栏
        drawingToolOptions: {
            anchor: BMAP_ANCHOR_TOP_RIGHT, 		//位置
            offset: new BMap.Size(5, 5), 		//偏离值
            scale: 0.8, 						//工具栏缩放比例
            drawingModes: [BMAP_DRAWING_RECTANGLE] 
        },
        rectangleOptions: styleOptions 			//矩形的样式
    });
    
    //添加鼠标绘制工具监听事件
	baidudrawingManager.addEventListener('overlaycomplete',function(e){
		overlays.push(e.overlay);
		baidudrawingManager.close();
		newRectangle = e.overlay;
		newRectangle.type = e.type;
		leftPointLng = newRectangle.getPath()[3].lng;
		leftPointLat = newRectangle.getPath()[3].lat;
		rightPointLng = newRectangle.getPath()[1].lng;
		rightPointLat = newRectangle.getPath()[1].lat;
		
		var browser=navigator.appName
		var b_version=navigator.appVersion
		var version=parseFloat(b_version)
		var a = "Microsoft Internet Explorer";
		
		if(version <= 4 && a == browser){
		}else{
			Drag();
		}
	})
    
	//添加清空监听事件，清除当前记录
    baidMap.addEventListener('click', function(e){
    	if(e.point.lng < leftPointLng || e.point.lng > rightPointLng || e.point.lat > rightPointLat || e.point.lat < leftPointLat){
    		clearAll();
    	}
    });

    //添加矩形绘画完成监听事件，用于信息窗的弹出
    baidudrawingManager.addEventListener("rectanglecomplete", function(e){
    	pointCenter = e.getBounds().getCenter();	
    	openInfo(pointCenter)
    }); 
}

function Drag(){
	var mouseStartX = 0;
	var mouseStartY = 0;
	var mouseEndX = 0;
	var mouseEndY = 0;
	var xChange = 0;
	var yChange = 0;
	var temp1 = false;
	var temp2 = false;
	newRectangle.addEventListener('mousedown',function(e){
		if(e.point.lng > leftPointLng && e.point.lng < rightPointLng && e.point.lat < rightPointLat && e.point.lat > leftPointLat){
			temp1 = true;
			baidMap.disableDragging();
			mouseStartX = e.point.lng;
			mouseStartY = e.point.lat;
			baidMap.removeOverlay(baiduQueryVechileloading);
			newRectangle.addEventListener('mousemove',function(e){
				if(temp1){
					temp2 = true;
					mouseEndX = e.point.lng;
					mouseEndY = e.point.lat;
					xChange = mouseEndX-mouseStartX;
					yChange = mouseEndY-mouseStartY;
					newRightPointX = rightPointLng+xChange;
					newRightPointY = rightPointLat+yChange;
					newLeftPointX = leftPointLng+xChange;
					newLeftPointY = leftPointLat+yChange;					
					drawNewR(newLeftPointX,newLeftPointY,newRightPointX,newRightPointY);
					if(baiduinfoWindow){
							baiduinfoWindow.close();
					}
		
				}
				newRectangle.addEventListener('mouseup',function(e){
					if(e.point.lng > newLeftPointX && e.point.lng < newRightPointX && e.point.lat < newRightPointY && e.point.lat > newLeftPointY){
						leftPointLng = newLeftPointX;
						rightPointLng = newRightPointX;
						leftPointLat = newLeftPointY;
						rightPointLat = newRightPointY;
						//最终矩形区域的两点经纬度
						/*document.getElementById("showPane1").innerHTML = newRectangle.getPath()[3].lng;
						document.getElementById("showPane2").innerHTML = newRectangle.getPath()[3].lat;
						document.getElementById("showPane3").innerHTML = newRectangle.getPath()[1].lng;
						document.getElementById("showPane4").innerHTML = newRectangle.getPath()[1].lat;*/
						baidMap.enableDragging();
						if(temp2){
							pointCenterNew = newRectangle.getBounds().getCenter();
							openInfo(pointCenterNew);
							mapMove();		
							temp1 = false;
							temp2 = false;
					}
					}
				})
			})
		}
	})
}	

//设置矩形变动后的点坐标
function drawNewR(leftX,leftY,rightX,rightY){
	newRectangle.setPath([
		new BMap.Point(leftX,rightY),
		new BMap.Point(rightX,rightY),
		new BMap.Point(rightX,leftY),
		new BMap.Point(leftX,leftY)
	])
}

//地图平移
function mapMove(){
	var bs = baidMap.getBounds();
	var mapCenterX = baidMap.getBounds().getCenter().lng;
	var mapCenterY = baidMap.getBounds().getCenter().lat;
	var mapCenterNewX = newRectangle.getBounds().getCenter().lng;
	var mapCenterNewY = newRectangle.getBounds().getCenter().lat;
	var leftDownXover = newLeftPointX - bs.getSouthWest().lng;
	var leftDownYover = newLeftPointY - bs.getSouthWest().lat;
	var rightUpXover = newRightPointX - bs.getNorthEast().lng;
	var rightUpYover = newRightPointY - bs.getNorthEast().lat;
	//左
	if(leftDownXover < 0 && leftDownYover > 0 && rightUpYover < 0){
		setTimeout(function(){
			baidMap.panTo(new BMap.Point(mapCenterNewX,mapCenterNewY));
		}, 300);
	}
	//下
	if(leftDownYover < 0 && leftDownXover > 0 && rightUpXover < 0){
		setTimeout(function(){
			baidMap.panTo(new BMap.Point(mapCenterNewX,mapCenterNewY));
		}, 300);
	}
	//右
	if(rightUpXover > 0 && leftDownYover > 0 && rightUpYover < 0){
		setTimeout(function(){
			baidMap.panTo(new BMap.Point(mapCenterNewX,mapCenterNewY));
		}, 300);
	}
	//上
	if(rightUpYover > 0 && leftDownXover > 0 && rightUpXover < 0){
		setTimeout(function(){
			baidMap.panTo(new BMap.Point(mapCenterNewX,mapCenterNewY));
		}, 300);
	}
	//左下
	if(leftDownXover < 0 && leftDownYover < 0){
		setTimeout(function(){
			baidMap.panTo(new BMap.Point(mapCenterNewX,mapCenterNewY));
		}, 300);
	}
	//左上
	if(leftDownXover < 0 && rightUpYover > 0){
		setTimeout(function(){
			baidMap.panTo(new BMap.Point(mapCenterNewX,mapCenterNewY));
		}, 300);
	}
	//右下
	if(rightUpXover > 0 && leftDownYover < 0){
		setTimeout(function(){
			baidMap.panTo(new BMap.Point(mapCenterNewX,mapCenterNewY));
		}, 300);
	}
	//右上
	if(rightUpXover > 0 && rightUpYover > 0){
		setTimeout(function(){
			baidMap.panTo(new BMap.Point(mapCenterNewX,mapCenterNewY));
		}, 300);
	}
}
//清空记录
function clearAll() {
	if(typeof baidMap !="undefined" && baidMap!=null){
	    for(var i = 0; i < overlays.length; i++){
	    	baidMap.removeOverlay(overlays[i]);
	    }
	    overlays.length = 0;
			//drawingManager.close();
		
	    baidMap.enableDragging();
	    
		/*for(var j = 0; j < baiduinfoWindows.length; j++){
	        baiduinfoWindow.close();
	    }
	    baiduinfoWindows.length = 0;*/
	    if(baiduinfoWindow!=null){
	   		 baiduinfoWindow.close();
	    }
	    
	    if(baiduQueryVechileloading!=null){
	    	baidMap.removeOverlay(baiduQueryVechileloading);
	    }
    }
}

//信息浮窗
function openInfo(point){
	var opts = {
				  position : point,    // 指定文本标注所在的地理位置
				  offset   : new BMap.Size(0, -10)    //设置文本偏移量
				 
				}
	baiduQueryVechileloading = new BMap.Label("<img src='"+$("#serverUrl").val()+Url.queryLoading+"'></img>", opts);  // 创建文本标注对象
	
	baiduQueryVechileloading.setStyle({
		  backgroundColor : "",
		  border : "0px",
		 fontSize : "12px",
		 height : "16px",
		 lineHeight : "16px",
		 fontWeight:"bold",
		 fontFamily:""
	});
	
	baidMap.addOverlay(baiduQueryVechileloading); 
				
	var content="<div class='baiduboxcontent_areaQuery'><p>";
		$.ajax({type:"POST",
		 dataType:"json",
		 async:false,
		 url:"json/vehicleAction_onlinevids.action", 
		 data:{"ids":cartIds,
		 	   "p.leftLat":leftPointLat,
		 	   "p.leftLng":leftPointLng, 
		 	   "p.rightLat":rightPointLat,
		 	   "p.rightLng":rightPointLng,
		 	   "timestamp":0},
	    success:function (json) {
		console.info("json:",JSON.stringify(json));
			if (json.flag == true) {	
			
			    //timestamp=json.data.timestamp;
	
				if(json.data.length>0){
				
					var imagesrc="";
					
					for (var i = 0; i < json.data.length; i++) {
					
						if(json.data[i].lat!=0 && json.data[i].lng!=0){
							imagesrc=getPic(json.data[i].state2,json.data[i].speed,json.data[i].recvtime,equipmentObject[json.data[i].vehicleId].icon,json.data[i].direct).imageSrc;
							content+="<b><span class='baiduareaQuery_img'><img src='"+imagesrc+"'></img></span>&nbsp;&nbsp;&nbsp;<span>"+equipmentObject[json.data[i].vehicleId].name+"</span></b>";
					    }
					    
					}
					
				}else{
					content+="此区域没有设备";
				}			
				
			}else{
			
				content+=""+json.msg+"";
			}
		}});
		
	var opts = {
  		enableCloseOnClick:false,
  		enableAutoPan:false,
  		enableMessage:false,
  		height:120
	}
	baiduinfoWindow = new BMap.InfoWindow(
		content+"</p></div>",
		opts
	);
	baiduinfoWindow.addEventListener('clickclose',function(e){
		clearAll();
	});
	
	//baiduinfoWindows.push(baiduinfoWindow);
	
	baidMap.removeOverlay(baiduQueryVechileloading);
	
	baidMap.openInfoWindow(baiduinfoWindow,point);
	
	
}