
var drawingManager=null;
var selectedShape;
var map;
var infowindow;
var loadingmarker=null;

var isOpenQuery =true;

var isQuery = false ;

function clearSelection() {

	drawingManager.setMap(null);

	if (selectedShape) {
		selectedShape.setMap(null);
	}
	if (infowindow) {
		infowindow.close();
	}
	if(menu!=null){
		menu.hide();
	}

	isOpenQuery=true;
}

function setSelection(shape) {
	//clearSelection();
	selectedShape = shape;
	//shape.setEditable(true);
}

function addRectangShape() {

	
//	console.info("---->" + mapType);
//
//	if(mapType==1){
//		initializeBaiduDraw();
//		return;
//	}
	closeRectangShape();
	closeDistance();
	initializeDraw();
	if(menu!=null){
		menu.hide();
	}
	if(isOpenQuery){
		isQuery = true ;
		isOpenQuery=false;
		//console.info("==========>" + (drawingManager==null));
		drawingManager.setMap(map);
	}else{
		closeRectangShape();
	}
	hidetoolPanel();
}

function closeRectangShape(){
	isOpenQuery=true;
	isQuery = false ;

	if (selectedShape) {
		selectedShape.setMap(null);
	}
	if (infowindow) {
		infowindow.setMap(null);
		infowindow=null;
	}
	if(drawingManager!=null){
		drawingManager.setMap(null);
	}

	clearAll();//关闭百度区域查车
}


/**
	   加载地图初始化绘图控件
 */
function initializeDraw() {

	if (typeof map == "undefined" || map == null) {
		setTimeout("initializeDraw()", 500);//每五秒刷新一次报警信息
		return;
	}
	drawingManager = new google.maps.drawing.DrawingManager({
		drawingControl: false, 
		drawingMode: google.maps.drawing.OverlayType.RECTANGLE,
		polylineOptions: {
		//editable:true, 
		//draggable:true
	}, 
	rectangleOptions : {
		strokeWeight:0, 
		fillOpacity:0.3, 
		fillColor:"blue",
		//editable:true, 
		draggable:true,
		zIndex : 999
	}
	});

	google.maps.event.addListener(drawingManager, "overlaycomplete", function (e) {
		if(menu!=null){
			menu.hide();
		}
		var newShape = e.overlay;
		newShape.type = e.type;

		/*google.maps.event.addListener(newShape, "click", function () {
			setSelection(newShape);
			if (infowindow) {
				infowindow.open(map);
			}
		});*/

		/*
		google.maps.event.addListener(newShape, "bounds_changed", function () {	

			 if(infowindow!=null){
				 	infowindow.close();
				 	infowindow=null;
			 }

			if(loadingmarker!=null){	
			 	loadingmarker.setMap(null);
			}	
		});*/

		google.maps.event.addListener(newShape, 'dragstart', function() {
			if(menu!=null){
				menu.hide();
			}
			if(infowindow!=null){
				infowindow.close();
				infowindow=null;
			}

			if(loadingmarker!=null){	
				loadingmarker.setMap(null);
			}


		});

		google.maps.event.addListener(newShape, 'dragend', function() {
			if(menu!=null){
				menu.hide();
			}

			if(loadingmarker!=null){	
				loadingmarker.setMap(null);
			}
			openInfowin(newShape);
		});

		setSelection(newShape);

		openInfowin(newShape);




		drawingManager.setMap(null);

	});

	google.maps.event.addListener(map, "mousedown", clearSelection);//去除选择

}


var timestamp=0;
function closeAreaQuery() {
	closeRectangShape();
}
function openInfowin(newShape) {
	if(infowindow){
		infowindow.close();
	}
	loadingmarker = new queryVechileLoading(map,"<div ><img src='"+$("#serverUrl").val()+Url.queryLoading+"'></img></div>",newShape.getBounds().getCenter());

	$.ajax({type:"POST", dataType:"json", url:"json/vehicleAction_onlinevids.action", data:{"ids":cartIds, "p.leftLat":newShape.getBounds().getSouthWest().lat(), "p.leftLng":newShape.getBounds().getSouthWest().lng(), "p.rightLat":newShape.getBounds().getNorthEast().lat(), "p.rightLng":newShape.getBounds().getNorthEast().lng(),"timestamp":0}, success:function (json) {

		if (json.flag == true) {	

			//timestamp=json.data.timestamp;


			var content=   "<div class='tanchukuang_wrap'>"+
			"<h3>"+
			"<a href='javascript:closeRectangShape()' class='tanchukuang_close'></a>"+
			areaquery.dkcx+
			"</h3>"+							    
			"<div class='tanchukuang_main' style='height:225px;width:163px; overflow:auto'>";

			//"<div class='xboxcontent' style='height:120px;background-color:#FFF;'><p style='height:110px;overflow: auto;margin-right:20px;'>" ;

			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 style='display:inline-block;height:25px;'><img src='"+imagesrc+"'></img>&nbsp;&nbsp;<span style='white-space:nowrap;display:inline-block;width:80px; overflow:hidden;text-overflow: ellipsis;'>"+equipmentObject[json.data[i].vehicleId].vehicleName+"</span></b><br/>";
					}

				}

			}else{
				content+="<b>the area without vehicle</b><br/>";
			}			

		}else{

			content+="<b>"+json.msg+"</b>";
		}

		content+="</div></div>";

		var boxText = document.createElement("div");
		boxText.className = "map_tanchu_wrap";
		boxText.innerHTML = content;
		boxText.id="areaQuery";
		boxText.style.width="180px";
		boxText.style.height="280px";
		boxText.style.margin=0;

		var myOptions = {
				content: boxText
				,disableAutoPan: false
				,maxWidth: 0
				,pixelOffset: new google.maps.Size(0, 0)
		,zIndex: null
		,boxStyle: { 
			// background: "url('tipbox.gif') no-repeat"
			// background: " no-repeat"
			//,opacity: 0.75
			//
			background: "no-repeat"								  
				,width: "180px"
					,height: "280px"
		}
		,infoBoxClearance: new google.maps.Size(1, 1)
		,isHidden: false
		,pane: "floatPane"
			,enableEventPropagation: false
		};

		infowindow = new InfoBox(myOptions);
		infowindow.draw = function () {

			this.createInfoBoxDiv_();

			var pixPosition = this.getProjection().fromLatLngToDivPixel(this.position_);

			this.div_.style.left = (pixPosition.x + this.pixelOffset_.width) + "px";

			if (this.alignBottom_) {
				this.div_.style.bottom = -(pixPosition.y + this.pixelOffset_.height) + "px";
			} else {
				this.div_.style.top = (pixPosition.y + this.pixelOffset_.height) + "px";
			}

			this.div_.style.width = 0; 

			var contentDiv = document.getElementById("areaQuery");

			contentDiv.style.width = 0; 

			if (this.isHidden_) {

				this.div_.style.visibility = 'hidden';

			} else {

				this.div_.style.visibility = "visible";
			}

			var div = this.div_;	

			if(tanchukuangWidthEvent){
				clearInterval(tanchukuangWidthEvent);
			}

			tanchukuangWidthEvent=setInterval(function(){

				if(div.style.width.split("p")[0] < 180){

					div.style.width = (parseInt(div.style.width.split("p")[0])+8)+"px";

					contentDiv.style.width = (parseInt(contentDiv.style.width.split("p")[0])+8)+"px";
				}else{
					clearInterval(tanchukuangWidthEvent);
					return;
				}
			},10);
		};	
		infowindow.open(map, new google.maps.Marker({
			position:newShape.getBounds().getNorthEast()
		}));
		//infowindow.show();
		//loadingmarker.setMap(null);
	}});
}
var tanchukuangWidthEvent = null;
function getPic(state2,speed,recvtime,icon,direct){
	var s=10;
	var condition=state2;
	var sp = parseInt(speed);
	if (condition != null) {
		if (condition.indexOf("不在线")>=0 || condition.indexOf("掉线")>=0)
			s = 2;
		else {
			if (condition.indexOf("速报警")>=0)
				s = 4;
			else if (condition.indexOf("报警")>=0)
				s = 3;
			else if (sp > 0.5)
				s = 0;
			else
				s = 1;
		}
		if (s != 4 && s != 3 && condition.indexOf("停车")>=0
				&& !condition.indexOf("不在线")>= 0)
		{
			if(condition.indexOf("停车超时")>=0){
				s = 5;	
			}else{
				s = 6;
			}
		}
	}else{
		if (sp > 0.5)
			s = 0;
		else
			s = 1;
	}

	var newtime = new Date().getTime();
	var ttime =recvtime.replace("-","/");

	ttime=ttime.replace("-","/");
	ttime=ttime.replace("T"," ");

	var oldtime = new Date(ttime).getTime();

	if(newtime-oldtime > 600000){
		s=2;
	}
	var state =s;

	var basesrc=$("#serverUrl").val()+"gpsonline/image/pic/";

	var pngicon = basesrc+icon+"/red_0.gif";
	if(state == 0) pngicon = basesrc+icon+"/green_";
	else if(state == 1) pngicon = basesrc+icon+"/red_";
	else if(state == 2) pngicon = basesrc+icon+"/gray_";
	else if(state == 3) pngicon = basesrc+icon+"/yellow_";
	else if(state == 4) pngicon = basesrc+icon+"/pink_";
	else if(state == 5) pngicon = basesrc+icon+"/stopalarm";
	else pngicon = basesrc+icon+"/red_";
	var direction="";
	var t = parseInt(direct);
	if(t>27 && t<=72){
		if(state == 5){
			var image=".gif";
		}else{
			var image="45.gif";
		}

		direction="东北";
	}else if(t>72 && t<=117){
		if(state == 5){
			var image=".gif";
		}else{
			var image="90.gif";
		}

		direction="东";
	}
	else if(t>117 && t<=162){
		if(state == 5){
			var image=".gif";
		}else{
			var image="135.gif";
		}
		direction="东南";
	}
	else if(t>162 && t<=207){
		if(state == 5){
			var image=".gif";
		}else{
			var image="180.gif";
		}
		direction="南";
	}
	else if(t>207 && t<=252){
		if(state == 5){
			var image=".gif";
		}else{
			var image="225.gif";
		}
		direction="西南";
	}
	else if(t>252 && t<=297){
		if(state == 5){
			var image=".gif";
		}else{
			var image="270.gif";
		}
		direction="西";
	}
	else if(t>297 && t<=342){
		if(state == 5){
			var image=".gif";
		}else{
			var image="315.gif";
		}
		direction="西北";
	}
	else {
		if(state == 5){
			var image=".gif";
		}else{
			var image="0.gif";
		}
		direction="北";
	}

	return {imageSrc:pngicon+image,direction:direction};
}

function queryVechileLoading(map, text, latLng){
	this.map_ = map;
	this.text_ = text;
	this.latLng_ = latLng;
	this.div_ = null;
	//this.setMap(map);
}
//继承自  google.maps.OverlayView
if(mapType==2){
	queryVechileLoading.prototype = new google.maps.OverlayView();
}
//当准备将 悬浮层 添加到地图上时 调用
queryVechileLoading.prototype.onAdd = function(){
	var div = document.createElement('DIV');
	div.style.border = 'none';
	div.style.position='absolute';   
	div.innerHTML = this.text_;
	this.div_ = div;	    
	var panes = this.getPanes();
	panes.overlayLayer.appendChild(div);
};

//当第一次在地图上显示时 调用
queryVechileLoading.prototype.draw = function(){
	var overlayProjection = this.getProjection();
	var latLng = overlayProjection.fromLatLngToDivPixel(this.latLng_);
	// 设置层的大小 和 位置
	var div = this.div_;
	var size = new google.maps.Size(17, -10); // 修正坐标的值;Size(width:number, height:number, widthUnit?:string, heightUnit?:string)
	div.style.left = (latLng.x) + 'px';
	div.style.top = (latLng.y) + 'px';
	div.style.zIndex=9999;
};
//当设置 悬浮层的 setMap(null) 会自动调用 
queryVechileLoading.prototype.onRemove = function(){
	this.div_.parentNode.removeChild(this.div_);
	this.div_ = null;
};
var historyQuery = {
		isShowHistoryWin : false,
		showHistoryWin : function(event){
	stopBubble(event);
	$("#showHistoryWin").show();
	$("#showHistoryWin").animate( { width: 90}, 100 );
},
closeHistoryWin : function(event){
	if(event != null){
		stopBubble(event);
	}
	setTimeout(function(){

		if(!historyQuery.isShowQueryTimeWin){

			$("#showHistoryWin").animate( { width: 0}, 300 );
			$("#showHistoryWin").hide();

			hidetoolPanel();

		}
	},100);	
},
toHistoryQuery : function(event){
	this.isShowHistoryWin = false;
	isShowToolPane=false;
	//this.closeHistoryWin(null);
	hidetoolPanel();
	if(mapType != 1){
		$("#toHistoryQuery_ids").val(cartIds);
		$("#toHistoryQuery_center_lng").val(map.getCenter().lng());
		$("#toHistoryQuery_center_lat").val(map.getCenter().lat());
		$("#toHistoryQuery_mapType").val(mapType);
		$("#toHistoryQuery_zoom").val(map.getZoom());
		//window.open(url+"gpsonline_EN_guowai017/jsp/monitor/historyQuery.jsp?ids="+cartIds+"&center_lng="+map.getCenter().lng()+"&center_lat="+map.getCenter().lat()+"&mapType="+mapType+"&zoom="+map.getZoom());
	}else{
		$("#toHistoryQuery_ids").val(cartIds);
		$("#toHistoryQuery_center_lng").val(baidMap.getCenter().lng);
		$("#toHistoryQuery_center_lat").val(baidMap.getCenter().lat);
		$("#toHistoryQuery_mapType").val(mapType);
		$("#toHistoryQuery_zoom").val(baidMap.getZoom());
		//window.open(url+"gpsonline_EN_guowai017/jsp/monitor/historyQuery.jsp?ids="+cartIds+"&center_lng="+baidMap.getCenter().lng+"&center_lat="+baidMap.getCenter().lat+"&mapType="+mapType+"&zoom="+baidMap.getZoom());
	}

	$("#toHistoryQuery").submit();
	$("#toHistoryQuery_ids").val("");
	$("#toHistoryQuery_center_lng").val("");
	$("#toHistoryQuery_center_lat").val("");
	$("#toHistoryQuery_mapType").val("");
	$("#toHistoryQuery_zoom").val("");
}
}
