
function CheTravel() {
	
}
// 历史轨迹
CheTravel.carTraceMarker = [];
CheTravel.gpsCarHisLine = null;
CheTravel.PointArray = [];

// 轨迹回放
CheTravel.carTraceReplayId = null;
CheTravel.carTraceReplayEndPoint = null;
CheTravel.carTraceReplayTempPoint = null;
CheTravel.carTraceReplayLineList = [];
CheTravel.carTraceReplayAscArray = null;
CheTravel.carTraceReplayDescArray = null;
CheTravel.carTraceReplayTempArray = null;

//过车轨迹
CheTravel.cheTravelTrace = function(rs){
	CheTravel.carTraceReplayAscArray = rs;
	CheTravel.carTraceReplayDescArray = [];
	for (var i=0; i<rs.length; i++) {
		CheTravel.carTraceReplayDescArray[i] = rs[rs.length - i - 1];
	}
	var tmpStartPoint = rs[0];
	var tmpEndPoint = rs[rs.length - 1];
	var startIcon = MapConst.MARKER_ICON_START;
	var endIcon = MapConst.MARKER_ICON_END;
	var END_MARKER_ICON_WIDTH = 33;
	var END_MARKER_ICON_HEIGHT = 33;
	var START_MARKER_ICON_WIDTH = 36;
	var START_MARKER_ICON_HEIGHT = 36;
	MapAPI.centerAndZoom(new Point(tmpStartPoint.jd,tmpStartPoint.wd), top.ditu.mapLevel);
	var startMarker = CheTravel.drawMarker(tmpStartPoint, tmpStartPoint.tdmc, START_MARKER_ICON_WIDTH, 
			START_MARKER_ICON_HEIGHT, startIcon, PopUpBase.createCheTravelHisStartPopUp);
	CheTravel.carTraceMarker = [];
	CheTravel.carTraceMarker.push(startMarker);
	if (rs.length == 1) {
		$.showMsg('当前只查询到一条记录！');
	} else if (rs.length > 1) {
		var endMarker = CheTravel.drawMarker(tmpEndPoint, tmpEndPoint.tdmc, END_MARKER_ICON_WIDTH, 
				END_MARKER_ICON_HEIGHT, endIcon, PopUpBase.createCheTravelHisEndPopUp);
		CheTravel.carTraceMarker.push(endMarker);
		CheTravel.drawGpsCarHisLineTrace(rs);
	}
}

//打点
CheTravel.addOverlay = function(rs){
	CheTravel.PointArray = [];
	for (var i = 0; i < rs.length; i++) {
		var marker = CheTravel.drawMarker(rs[i], rs[i].tdmc, MapConst.LAYER_ICON_WIDTH, 
				MapConst.LAYER_ICON_HEIGHT, MapConst.MARKER_ICON_CHEGPS, PopUpBase.createCheTravelPopUp);
		CheTravel.PointArray.push(marker);
	}
}

//添加轨迹起点、终点
CheTravel.drawMarker = function (result, title, width, height, iconSource, popUpBaseFunc) {
	var pIcon=new Icon(); 
	pIcon.height = width;
	pIcon.width = height;
	pIcon.topOffset = -10;
	pIcon.leftOffset = -10;
	pIcon.image = iconSource;
	var point=new Point(result.jd,result.wd);	
	var resultMsg = popUpBaseFunc && popUpBaseFunc(result);
	var marker;
	if (title) {
		var title = new Title(title);
		marker = new Marker(point,pIcon, title) ;
	} else {
		marker = new Marker(point,pIcon) ;
	}
	if (resultMsg) { 
		marker.addListener("click", function () {
			marker.openInfoWindowHtml(resultMsg);
			MapMain.setCurrPupUpObject([]);
		});	
	}
	marker.setZIndex(MapConst.MARKER_ZINDEX_TRACE);
	MapAPI.addOverlay(marker);
	return marker;
}

//画轨迹
CheTravel.drawGpsCarHisLineTrace = function (data) {
	CheTravel.gpsCarHisLine = null;
	var pointList=[];
	for(var i=0;i<data.length;i++){
		pointList.push(new Point(data[i].jd, data[i].wd));
	}
	CheTravel.gpsCarHisLine = new Polyline(pointList, "red", 4, 1, 1);
	CheTravel.gpsCarHisLine.setZIndex(MapConst.MARKER_ZINDEX_TRACELINE);
	MapAPI.addOverlay(CheTravel.gpsCarHisLine);
}

//移除上一个轨迹
CheTravel.clearPreCarHisTrace = function () {
	if (CheTravel.gpsCarHisLine) {
		MapAPI.removeOverlay(CheTravel.gpsCarHisLine);
		CheTravel.gpsCarHisLine = null;
	}
	if (CheTravel.carTraceMarker) {
		for (var i=0; i<CheTravel.carTraceMarker.length; i++) {
			CarTrace.closeInfoWindowHtml(CheTravel.carTraceMarker[i]);
			MapAPI.removeOverlay(CheTravel.carTraceMarker[i]);
		}
		CheTravel.carTraceMarker = [];
	}
	if (CheTravel.PointArray) {
		for (var i=0; i<CheTravel.PointArray.length; i++) {
			CarTrace.closeInfoWindowHtml(CheTravel.PointArray[i]);
			MapAPI.removeOverlay(CheTravel.PointArray[i]);
		}
		CheTravel.PointArray = [];
	}
}

//车辆历史轨迹回放
CheTravel.carTraceReplay = function(type){
	if (CheTravel.carTraceReplayDescArray.length <= 1 || CheTravel.carTraceReplayAscArray.length <= 1) {
		$.showMsg('当前只查询到一条记录！');
		return;
	}
	if (CheTravel.carTraceReplayId) {
		$.showMsg('正在轨迹回放！');
		return;
	}
	
	if (CheTravel.gpsCarHisLine) {
		MapAPI.removeOverlay(CheTravel.gpsCarHisLine);
		CheTravel.gpsCarHisLine = null;
	}
	if (CheTravel.PointArray) {
		for (var i=0; i<CheTravel.PointArray.length; i++) {
			CheTravel.closeInfoWindowHtml(CheTravel.PointArray[i]);
			MapAPI.removeOverlay(CheTravel.PointArray[i]);
		}
		CheTravel.PointArray = [];
	}
	if (type == 'desc') {
		CheTravel.carTraceReplayTempArray = CheTravel.carTraceReplayDescArray;
	} else if (type == 'asc') {
		CheTravel.carTraceReplayTempArray = CheTravel.carTraceReplayAscArray;
	}
	var tempData = CheTravel.carTraceReplayTempArray[0];
	CheTravel.carTraceReplayEndPoint = CarTrace.getGpsCarRealStartPoint(tempData);
	MapAPI.centerAtLatLng(new Point(tempData.longitude,tempData.latitude));
	var cheGpsLayer = MapMain.getLayer(MapConst.LAYER_CHETRAVEL);
	MapMain.setCurrPupUpObject([cheGpsLayer.seq, 'cheTravelReplay', tempData.longitude,tempData.latitude]);
	var num = 0;
	CheTravel.carTraceReplayId = setInterval(function() {
        num++;
        CheTravel.drawCarTraceReplay(num);
        if (num >= CheTravel.carTraceReplayTempArray.length -1) {
            clearInterval(CheTravel.carTraceReplayId);
            $.showMsg('轨迹回放结束！');
        }
    }, 3000);

}

//车辆历史轨迹回放划线
CheTravel.drawCarTraceReplay = function(index){
	if(CheTravel.carTraceReplayEndPoint!=null){
		CheTravel.carTraceReplayTempPoint = CheTravel.carTraceReplayEndPoint;
		MapAPI.removeOverlay(CheTravel.carTraceReplayEndPoint);
	}
	var tempData = CheTravel.carTraceReplayTempArray[index];
	var cheGpsLayer = MapMain.getLayer(MapConst.LAYER_CHETRAVEL);
//	tempData = CarTrace.getCarInfo(tempData);
	CheTravel.carTraceReplayEndPoint = CheTravel.drawMarker(tempData, tempData.channelName, MapConst.LAYER_ICON_WIDTH, MapConst.LAYER_ICON_HEIGHT, MapConst.MARKER_ICON_CHEGPS, PopUpBase.createCheTravelHisStartPopUp);
	CheTravel.carTraceReplayEndPoint.addListener("click", function () {
		var resultMsg = PopUpBase.getGpsCarHisStartPointHtml(tempData);
		CheTravel.carTraceReplayEndPoint.openInfoWindowHtml(resultMsg);
		MapMain.setCurrPupUpObject([cheGpsLayer.seq, 'cheTravelReplay', tempData.longitude,tempData.latitude]);
	});	
	var pointList=[];
	pointList.push(new Point(CheTravel.carTraceReplayTempPoint.point.x, CheTravel.carTraceReplayTempPoint.point.y));
	pointList.push(new Point(CheTravel.carTraceReplayEndPoint.point.x, CheTravel.carTraceReplayEndPoint.point.y));
	var carTraceReplayLine = new Polyline(pointList, "blue", 1);
	CheTravel.carTraceReplayLineList = CarTrace.carTraceReplayLineList || [];
	CheTravel.carTraceReplayLineList.push(carTraceReplayLine);
	carTraceReplayLine.setZIndex(MapConst.MARKER_ZINDEX_TRACELINE);
	MapAPI.addOverlay(carTraceReplayLine);
	MapAPI.centerAtLatLng(new Point(tempData.longitude,tempData.latitude));
	CheTravel.tryOpenWindowHtml(CheTravel.carTraceReplayEndPoint, tempData, MapConst.LAYER_CHETRAVEL);
}

//停止车辆历史轨迹回放划线
CheTravel.stopCarTraceReplay = function(){
	if (!CheTravel.carTraceReplayId) {
//		$.showMsg('未开始轨迹回放！');
		return;
	} 
	else {
		clearInterval(CheTravel.carTraceReplayId);
		CheTravel.carTraceReplayId = null;
		if (CheTravel.carTraceReplayEndPoint) {
			CheTravel.carTraceReplayEndPoint.map && CheTravel.carTraceReplayEndPoint.closeInfoWindowHtml();
			MapAPI.removeOverlay(CheTravel.carTraceReplayEndPoint);
			CheTravel.carTraceReplayEndPoint = null;
		}
		CheTravel.clearPreCarHisTrace();
		if (CheTravel.carTraceReplayLineList) {
			for (var index = 0; index < CheTravel.carTraceReplayLineList.length; index++) {
				MapAPI.removeOverlay(CheTravel.carTraceReplayLineList[index]);
			}
			CheTravel.carTraceReplayLineList = [];
		}
		$.showMsg('已停止轨迹回放！');
	}
}

CheTravel.tryOpenWindowHtml = function(marker, data, seq){
	var pop = MapMain.getCurrPupUpObject();
	if (pop && pop.length > 1 && 'cheTravelReplay' == pop[1] && seq == pop[0]) {
		marker.openInfoWindowHtml(PopUpBase.createCheTravelHisStartPopUp(data));
	}
}

//移除上一个轨迹
CheTravel.clearPreCarHisTrace = function () {
	if (CheTravel.gpsCarHisLine) {
		MapAPI.removeOverlay(CheTravel.gpsCarHisLine);
		CheTravel.gpsCarHisLine = null;
	}
	
	if (CheTravel.carTraceMarker) {
		
		for (var i=0; i<CheTravel.carTraceMarker.length; i++) {
			CheTravel.closeInfoWindowHtml(CheTravel.carTraceMarker[i]);
			MapAPI.removeOverlay(CheTravel.carTraceMarker[i]);
		}
		CheTravel.carTraceMarker = [];
	}
	if (CheTravel.PointArray) {
		
		for (var i=0; i<CheTravel.PointArray.length; i++) {
			CheTravel.closeInfoWindowHtml(CheTravel.PointArray[i]);
			MapAPI.removeOverlay(CheTravel.PointArray[i]);
		}
		CheTravel.PointArray = [];
	}
}

CheTravel.closeInfoWindowHtml = function (marker) {
	var pop = MapMain.getCurrPupUpObject();
	if (pop && pop.length == 0) {
		marker.closeInfoWindowHtml();
	}
	if (pop && pop.length > 1 && 'cheTravelReplay' == pop[1] ) {
		marker.closeInfoWindowHtml();
	}
}