function table(){
	$('#gtable').datagrid({
		title:'轨迹信息窗口',
		iconCls:'icon-save',
		width:1197,
		height:240,
		nowrap: false,
		striped: true,
		collapsible:true,
		remoteSort: false,
		resizeHandle: 'right',
		align:'center',
		fixed:true,
		singleSelect:true,
		columns:[[
		          {field:'序号',title:'序号',width:50},
		          {field:'服务器时间',title:'服务器时间',width:130},
		          {field:'GPS时间',title:'GPS时间',width:130},
		          {field:'状态',title:'状态',width:120},
		          {field:'方向',title:'方向',width:120},
		          {field:'速度',title:'速度',width:50},
		          {field:'油量',title:'油量',width:50},
		          {field:'温度1',title:'温度1',width:50},
		          {field:'温度2',title:'温度2',width:50},
		          {field:'温度3',title:'温度3',width:50},
		          {field:'总里程',title:'总里程',width:50},
		          {field:'纬度',title:'纬度',width:120},
		          {field:'经度',title:'经度',width:120},
		          {field:'位置',title:'位置',width:600}
		          ]],
		          onDblClickRow:function(rowIndex, rowData){
		var zoom = map3.getZoom();
		map3.toCenter(rowData.纬度,rowData.经度,zoom);
		
	}
	});	
}
var map3;
function guijimap(){
    if(map3) $("guijimap").empty(); 
    else{
		 map3=new OpenLayers.MapUtils("guijimap");
		 map3.toCenter(113.88537605246529,22.579730486497283,9);
	}
}

function fhButShow(){

  $("#OpenPlay").hide();
  $("#close").show();
}
function fhButHide(){

  $("#OpenPlay").show();
  $("#close").hide();
}


$(document).ready(function(){
	//轨迹回放添加单击事件
	$("#OpenPlay").click(function() {     
		if($("#vhcid").val()!=null&&$("#vhcid").val()!='') {
			$("#vehName").html($("#vehicle").val());    
			//$("#guiji").show();
			//$("#guiji").window("open");
			//$("#guijimap").window("open");
			fhButHide();
			$("#guiji").show();
			$("#guijimappanel").parent().show();
			$("#mappanel").parent().hide();
			
			var time = new Date();
			table();
			guijimap();
		}else{
			$.messager.alert('提示','没有选择车辆');
			return;
		} 
		
		var time = $("#playdata").val();
		 fhButShow();    
		$('#gjhf_form_id').form({
			    url: "json/vehicleAction_GetJsonVdata.action",
			    onSubmit: function(){
			          
			          
			           
			          $('#b_time_gjhf_v').val($('#date_gjhf').datebox("getValue")+" "+$('#b_time_gjhf').val());
			          $('#e_time_gjhf_v').val($('#date_gjhf').datebox("getValue")+" "+$('#e_time_gjhf').val());
			         
			          return true;
			          
			        //进行表单验证
			        //如果返回false阻止提交
			    },
			    success:function(result_){
			           var json=jQuery.parseJSON(result_); 
			          
						if (json.result==true){	
								
							var data = geshiData(json);
							 $('#gtable').datagrid('loadData', data);
							
							 $("#close").show();
						}else {
						      var data = geshiData(json);
							 $.messager.alert('错误', json.message, 'error');
							 $('#gtable').datagrid('loadData', data);							
							 $("#close").show();
						}
			    }
			});
			//提交表单
			$('#gjhf_form_id').submit();		
		
	});

	//添加文本框日期控件
	$('#playdata').datebox({    
		required:true    
	}); 

	//选择日期后触发
	$("#playdata").datebox({
		onSelect: function(date){
		var da = playdate.substring(0, 4)+"-"+playdate.substring(5, 7)+"-"+playdate.substring(8, 10);    	
		var playdate = new Date(date).toLocaleDateString();
		$("#playdata").val(playdate);
	}
	});
	
	$("#close").click(function(){
	     fhButHide();
		$("#PlayTime").hide();
		$("#guiji").hide();
		$("#close").hide();
		$("#guijimappanel").parent().hide();
		$("#mappanel").parent().show();
	});
});
function  geshiData(data){


		var lnglat = new Array(data.topics.length);
		var centerlng = "";
		var centerlat = "";
	var formatStr="{\"total\":"+ data.topics.length+",\"rows\":[";	
	var carts=data.topics;
	for(var i=0; i< data.topics.length; i++){
		
		
		var lng = carts[i].lng + carts[i].lng_xz;
		var lat= carts[i].lat + carts[i].lat_xz;	
		if(i==0){
			centerlng = lng ;
			centerlat = lat ;		
		}
		
		var ll =lng+","+lat;
		map3.addPoint(carts[i].id,lng,lat,"image/index/vhc_icons/green_0.gif",17,17);
		
		lnglat[i]=ll;
		
		formatStr=formatStr+"{\"序号\":\""+(i+1)+"\""+
		",\"服务器时间\":\""+((carts[i].recvt!=null)?carts[i].recvt:'')+
		"\",\"GPS时间\":\""+((carts[i].gpst!=null)?carts[i].gpst:'')+
		"\",\"状态\":\""+((carts[i].state!=null)?carts[i].state:'')+
		"\",\"方向\":\""+((carts[i].dir!=null)?carts[i].dir:'')+
		"\",\"速度\":\""+((carts[i].veo!=null)?carts[i].veo:'')+
		"\",\"油量\":\""+((carts[i].oil!=null)?carts[i].oil:'')+
		"\",\"温度1\":\""+((carts[i].temp!=null)?carts[i].temp:'')+
		"\",\"温度2\":\""+((carts[i].temp!=null)?carts[i].temp:'')+
		"\",\"温度3\":\""+((carts[i].temp!=null)?carts[i].temp:'')+
		"\",\"总里程\":\""+((carts[i].totaldistance!=null)?carts[i].totaldistance:'')+
		"\",\"纬度\":\""+((carts[i].lng!=null)?carts[i].lng+carts[i].lng_xz:'')+
		"\",\"经度\":\""+((carts[i].lat!=null)?carts[i].lat+carts[i].lat_xz:'')+
		"\",\"位置\":\""+((carts[i].posinfo!=null)?carts[i].posinfo:'')+"\"},";
	}
	var zoo = map3.getZoom();
	map3.toCenter(centerlng,centerlat,zoo);
	map3.addLine(lnglat);

 
  
	if(data.topics.length>0)
	 formatStr=formatStr.substring(0,formatStr.length-1);
	formatStr=formatStr+"]}";
	return $.parseJSON(formatStr);
}

