$(function(){
	$("#menu_report").click(function(){
		if($("#report").attr("src") != ""){
			return;
		}
		closeAllWin();
	    $("#menu_report").attr("class","current");
		$("#menu_track").removeClass();
		$("#menu_manager").removeClass();
		$("#menu_lostfound").removeClass();
		$("#index_main").hide();
		$("#manager").hide();
		$("#lostfound").hide();
		$("#report").show();
		
		$.getScript("http://"+$("#serverip").val()+"/exstatic/jsp/login/version.jsp",null);
		//$.getScript("http://121.42.162.93:89/exstatic/jsp/login/version.jsp",null);
		setTimeout(function(){
//						console.info("---->:"+"http://121.42.162.93:89/exstatic/jsp/login/loginx.jsp?uName="+encodeURIComponent($("#uN").val())+"&uPass=123456&language=zh-CN&uType=exlivebs" + ( curVehicleOfReport != null ? "&vhcid=" + curVehicleOfReport : "" ));
				   		if(version && $("#usertypeId").val() != "-100"){
							$("#report").height("90%");
							$("#report").attr("src","http://"+$("#serverip").val()+"/exstatic/jsp/login/loginx.jsp?uName="+encodeURIComponent($("#uN").val())+"&uPass="+$.trim($("#uP").val())+"&uType=exlivebs&language=zh-CN" + ( curVehicleOfReport != null ? "&vhcid=" + curVehicleOfReport : "" ));
//							$("#report").attr("src","http://121.42.162.93:89/exstatic/jsp/login/loginx.jsp?uName="+encodeURIComponent("gjgw")+"&uPass=000000&language=zh-CN&uType=exlivebs" + ( curVehicleOfReport != null ? "&vhcid=" + curVehicleOfReport : "" ));
							curVehicleOfReport = null;//重置选中设备
							$(window.frames["report"].document).find("body").css("overflow","hidden");
						}else{
//							$("#report").attr("src","gpsonline/jsp/report/report.jsp");
							
							$("#report").height("90%");
							  $("#report").attr("src","http://"+$("#serverip").val()+"/exstatic/jsp/login/loginx.jsp?uName="+encodeURIComponent($("#uN").val())+"&uPass="+$.trim($("#uP").val())+"&language=zh-CN&logintype=1");
//							console.info("http://172.18.0.17:8080/exstatic/jsp/login/loginx.jsp?uName="+encodeURIComponent($("#uN").val())+"&uPass="+$.trim($("#uP").val())+"&language=zh-CN&logintype=1");
							curVehicleOfReport = null;//重置选中设备
							$(window.frames["report"].document).find("body").css("overflow","hidden");
						}
					},2000);
		
		$("#manager").attr("src","");
		$("#lostfound").attr("src","");
		if(mapType==2){
			google.maps.event.trigger(map, 'resize');
		}
	});
	
	$("#menu_track").click(function(){
		if($("#manager").attr("src") == "" && $("#report").attr("src") == "" && $("#lostfound").attr("src") == ""){
			return;
		}
		//$("#index_report").hide();
		$("#menu_track").attr("class","current");
		$("#menu_report").removeClass();
		$("#menu_manager").removeClass();
		$("#menu_lostfound").removeClass();
		
		$("#index_main").show();
		$("#lostfound").hide();
		
		$("#lostfound").attr("src","");
		$("#manager").attr("src","");
		$("#report").attr("src","");
		if(mapType==2){
			google.maps.event.trigger(map, 'resize');
		}
	});
	//后台管理按钮 点击事件
	$("#dianji").click(function(){
		var pass = $.cookie("externalPass");
		var str = "account="+encodeURI(encodeURI($("#uN").val()))+"&password="+pass;
		console.info(str);
		//var str = "account="+encodeURI(encodeURI("北京admin"))+"&password=hycx1221";
		window.open("http://123.56.96.23:8099/cl-system/admin/loginSlient?"+str);
	});
	//后台管理按钮 点击事件
	$("#menu_manager").click(function(){
		if($("#manager").attr("src") != ""){
			return;
		}
		closeAllWin();
	    $("#menu_manager").attr("class","current");
		$("#menu_report").removeClass();
		$("#menu_track").removeClass();
		$("#menu_lostfound").removeClass();
		$("#index_main").hide();
		$("#report").hide();
		$("#manager").show();
		$("#lostfound").hide();
		$("#report").attr("src","");
		$("#lostfound").attr("src","");
		$("#manager").attr("src","gpsonline/jsp/manager/manager.jsp");
		if(mapType==2){
			google.maps.event.trigger(map, 'resize');
		}
	});
	
	//失物招领 点击事件
	$("#menu_lostfound").click(function(){
		if($("#lostfound").attr("src") != ""){
			return;
		}
		//closeAllWin();
	    $("#menu_lostfound").attr("class","current");
		$("#menu_report").removeClass();
		$("#menu_track").removeClass();
		$("#menu_manager").removeClass();
		
		$("#index_main").hide();
		$("#report").hide();
		$("#manager").hide();
		
		$("#lostfound").show();
		$("#index_main").attr("src","");
		$("#report").attr("src","");
		$("#manager").attr("src","");
		$("#lostfound").attr("src","gpsonline/jsp/lostfound.jsp");
		if(mapType==2){
			google.maps.event.trigger(map, 'resize');
		}
	});
	
	var curmenu="行驶统计";
	$("ul[name='statics_menus'] li").click(function(){
	
		  if( $(this).attr("class") == "first on"){
		  	 return;
		  }
			
	      $("ul[name='statics_menus'] li").each(function(){
	      		$(this).attr("class","");
	      });
	   
	      $(this).attr("class","first on");
	      
	      var menuName=$(this).find("a").text();
	      
	      $("table[name='report_table']").each(function(){
	      		$(this).hide();
	      });
	      var id=$("#userid").val();
		  var startTime=$("#start").val();
		  var endTime=$("#end").val();
	      switch(menuName){
	         case "行驶统计": 
	                         $("#report_title").html("<h2>行驶统计</h2>");
					         $("#running_report_table").show();
					         curmenu="行驶统计";
					         if(id!=""&&startTime!=""&&endTime!=""){
	         				 	showrunningreport(startTime,endTime,id);
	         				 }else{
	         				 	$("#running_report_table tr").eq(0).nextAll().remove();
	         				 	$("#running_report_table").append("<tr class='total'><td>合计</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>0</td></tr>");
	         				 }
	          break;
	         case "里程统计":	 $("#report_title").html("<h2>里程统计</h2>");
					         $("#mileage_report_table").show();
					         curmenu="里程统计";
					         if(id!=""&&startTime!=""&&endTime!=""){
	         				 	showmileagereport(startTime,endTime,id);
	         				 }else{
	         				 	$("#mileage_report_table tr").eq(0).nextAll().remove();
	         				 	$("#mileage_report_table").append("<tr class='total'><td>合计</td><td>&nbsp;</td><td>&nbsp;</td><td>0</td></tr>");
	         				 }
	          break;
	         case "超速统计": $("#report_title").html("<h2>超速统计</h2>");
					         $("#overspeed_report_table").show();
					         curmenu="超速统计";
					         if(id!=""&&startTime!=""&&endTime!=""){
	         				 	showoverspeedreport(startTime,endTime,id);
	         				 }else{
	         				 	$("#overspeed_report_table tr").eq(0).nextAll().remove();
	         				 	$("#overspeed_report_table").append("<tr class='listrow'><td colspan='6'><div class='nulldata'>没有符合条件的信息</div></td></tr>");
	         				 }
	          break;
	         case "报警统计": $("#report_title").html("<h2>报警统计</h2>");
					         $("#warm_report_table").show();
					         curmenu="报警统计";
					         if(id!=""&&startTime!=""&&endTime!=""){
	         				 	showwarmmingreport(startTime,endTime,id);
	         				 }else{
	         				 	$("#warm_report_table tr").eq(0).nextAll().remove();
	         				 	$("#warm_report_table").append("<tr class='listrow'><td colspan='6'><div class='nulldata'>没有符合条件的信息</div></td></tr>");
	         				 }
	          break;
	      }
	});
	
	$("#statistics_button").click(function(){
		switch(curmenu){
	         case "行驶统计": $("#running_report_table tr").eq(0).nextAll().remove();
	         				 $("#running_report_table").append("<tr class='total'><td>合计</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>0</td></tr>");
	          break;
	         case "里程统计":	 $("#mileage_report_table tr").eq(0).nextAll().remove();
	         				 $("#mileage_report_table").append("<tr class='total'><td>合计</td><td>&nbsp;</td><td>&nbsp;</td><td>0</td></tr>");
	          break;
	         case "超速统计": $("#overspeed_report_table tr").eq(0).nextAll().remove();
	         				 $("#overspeed_report_table").append("<tr class='listrow'><td colspan='6'><div class='nulldata'>没有符合条件的信息</div></td></tr>");
	          break;
	         case "报警统计": $("#warm_report_table tr").eq(0).nextAll().remove();
	         				 $("#warm_report_table").append("<tr class='listrow'><td colspan='6'><div class='nulldata'>没有符合条件的信息</div></td></tr>");
	          break;
	    }	
		var id=$("#userid").val();
		var startTime=$("#start").val();
		var endTime=$("#end").val();
		if(id=="" || id==null){
			alert("请选择设备");
			return;
		}else if(startTime=="" || startTime==null){
			alert("开始时间不能为空");
			return;
		}else if(endTime=="" || endTime==null){
			alert("结束时间不能为空");
			return;
		}else{			
			switch(curmenu){
	         case "行驶统计": showrunningreport(startTime,endTime,id);
	          break;
	         case "里程统计":	 showmileagereport(startTime,endTime,id);
	          break;
	         case "超速统计": showoverspeedreport(startTime,endTime,id);
	          break;
	         case "报警统计": showwarmmingreport(startTime,endTime,id);
	          break;
	       }		
	   }
	
	});
});	
function showmileagereport(startTime,endTime,id){
	$("#mileage_report_table tr").eq(0).nextAll().remove();
	$("#mileage_report_table tr").eq(0).after("<tr><td><div style='padding:10px; font-size:14px; color:#0099FF;'> loading... </div></td>"+
                                      "<td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>");
    $.ajax({
	        type: "POST",
	        dataType: "json",
	        url: "json/reportsAction_licheng.action",
	        data: {'rf.st':startTime,'rf.ed':endTime,'rf.ids':id},
	        success: function(json) {
	        	 $("#mileage_report_table tr").eq(0).nextAll().remove();
		         if (json.flag==true) {		             
			         //alert(JSON.stringify(json));
			         var mileagecount=0;
					 $("#mileage_report_table tr").eq(0).nextAll().remove();
					 if(json.data.length>0){
					     for(var i=0;i<json.data.length;i++){
					    	   mileage=toDecimal2(json.data[i][3]);
					    	   mileagecount+=mileage;
					       	   $("#mileage_report_table").append("<tr onmouseover='trmouseOver(this);' onmouseout='trmouseOut(this);' class='listrow"+(i%2)+"'> <td>"+(i+1)+"</td><td>"+json.data[i][1]+"</td><td><a href='javascript:;'>"+json.data[i][2].replace("T"," ")+"</a></td><td>"+mileage+"</td></tr>");
					     }
					     $("#mileage_report_table").append("<tr class='total'> <td>&nbsp;</td><td>&nbsp;</td><td><a href='javascript:;'>合计</a></td><td>"+toDecimal2(mileagecount)+"</td></tr>");
		             }else{    
		             	 $("#mileage_report_table").append("<tr class='total'><td>合计</td><td>&nbsp;</td><td>&nbsp;</td><td>0</td></tr>");
		             }
		         }
	        },
	        error: function() {
	            //alert('获取里程统计报表失败，请稍后重试!');
	        }
	});
	
}

function showrunningreport(startTime,endTime,id){
	$("#running_report_table tr").eq(0).nextAll().remove();
	$("#running_report_table tr").eq(0).after("<tr><td><div style='padding:10px; font-size:14px; color:#0099FF;'> loading... </div></td>"+
                                      "<td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>");
    $.ajax({ 
	        type: "POST",
	        dataType: "json",
	        url: "json/reportsAction_xingshi.action",
	        data: {'rf.st':startTime,'rf.ed':endTime,'rf.ids':id},
	        success: function(json) {
	       //alert(JSON.stringify(json));
		         if (json.flag==true) {			         
			         var mileagecount=0;
					 $("#running_report_table tr").eq(0).nextAll().remove();
					 if(json.data.length>0){
					     for(var i=0;i<json.data.length;i++){				    	   
					    	   mileage=toDecimal2(json.data[i][8]);
					    	   mileagecount+=mileage;
					    	   var tr=$("<tr onmouseover='trmouseOver(this);' onmouseout='trmouseOut(this);' class='listrow"+(i%2)+"'> <td>"+(i+1)+"</td><td>"+json.data[i][1]+"</td><td>"+json.data[i][5].replace("T"," ")+"(<span title='"+json.data[i][10]+"' name='startPosition' style='white-space:nowrap;display:inline-block;line-height:15px; overflow:hidden;text-overflow: ellipsis;'>"+$.trim(json.data[i][10])+"</span>)</td><td>"+json.data[i][6].replace("T"," ")+"(<span name='endPosition' title='"+json.data[i][11]+"' style='white-space:nowrap;display:inline-block;line-height:15px; overflow:hidden;text-overflow: ellipsis;'>"+$.trim(json.data[i][11])+"</span>)</td><td>"+mileage+"</td></tr>");
					       	   $("#running_report_table").append(tr);
					       	   var startPosition=tr.find("span[name='startPosition']");
					       	   if(startPosition.width()>200){
					       	     startPosition.css("width",200);
					       	   }
					       	   var endPosition=tr.find("span[name='endPosition']");
					       	   if(endPosition.width()>200){
					       	     endPosition.css("width",200);
					       	   }
					     }
					     $("#running_report_table").append("<tr class='total'><td>合计</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>"+toDecimal2(mileagecount)+"</td></tr>");
		             }else{	                                                  
		             	$("#running_report_table").append("<tr class='total'><td>合计</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>0</td></tr>");
		             }
		         }
	        },
	        error: function() {
	            //alert('获取行驶统计报表失败，请稍后重试!');
	        }
	});
	
}
function showoverspeedreport(startTime,endTime,id){
	$("#overspeed_report_table tr").eq(0).nextAll().remove();
	$("#overspeed_report_table tr").eq(0).after("<tr><td><div style='padding:10px; font-size:14px; color:#0099FF;'> loading... </div></td>"+
                                      "<td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>");
    $.ajax({
	        type: "POST",
	        dataType: "json",
	        url: "json/reportsAction_chaosu.action",
	        data: {'rf.st':startTime,'rf.ed':endTime,'rf.ids':id},
	        success: function(json) {
	        //alert(JSON.stringify(json));
		         if (json.flag==true) {			         
			         var mileagecount=0;
					 $("#overspeed_report_table tr").eq(0).nextAll().remove();
					 if(json.data.length>0){
					     for(var i=0;i<json.data.length;i++){
					    	   mileagecount+=json.data[i][3];
					       	   $("#overspeed_report_table").append("<tr onmouseover='trmouseOver(this);' onmouseout='trmouseOut(this);' class='listrow"+(i%2)+"'> <td style='width:100px;'>"+(i+1)+"</td><td style='width:150px;'>"+json.data[i][2].replace("T"," ")+"</td><td style='width:100px;'>"+json.data[i][7]+"</td><td>"+json.data[i][10]+"</td></tr>");
					     }					    
		         	 }else{                                                   
		             	 $("#overspeed_report_table").append("<tr class='listrow'><td colspan='6'><div class='nulldata'>没有符合条件的信息</div></td></tr>");
		             }
		         }
	        },
	        error: function() {
	            //alert('获取超速统计报表失败，请稍后重试!');
	        }
	});
	
}
function showwarmmingreport(startTime,endTime,id){
	$("#warm_report_table tr").eq(0).nextAll().remove();
	$("#warm_report_table tr").eq(0).after("<tr><td><div style='padding:10px; font-size:14px; color:#0099FF;'> loading... </div></td>"+
                                      "<td>&nbsp;</td><td>&nbsp;</td></tr>");	
    $.ajax({
	        type: "POST",
	        dataType: "json",
	        url: "json/reportsAction_baojing.action",//"rf.st=2013-12-30%2000:00:00&rf.ed=2013-12-30%2023:59:59&rf.ids=4732042",
	        data: {'rf.st':startTime,'rf.ed':endTime,'rf.ids':id},
	        success: function(json) {
		         if (json.flag==true) {			         
			         var mileagecount=0;
					 $("#warm_report_table tr").eq(0).nextAll().remove();
					 if(json.data.length>0){
					     for(var i=0;i<json.data.length;i++){
					    	   mileagecount+=json.data[i][3];
					       	    $("#warm_report_table").append("<tr onmouseover='trmouseOver(this);' onmouseout='trmouseOut(this);' class='listrow"+(i%2)+"'> <td>"+(i+1)+"</td><td>"+json.data[i][2].replace("T"," ")+"</td><td><a href='javascript:;'>"+json.data[i][11]+"</a></td></tr>");
					     }
		          	 }else{                                                   
		             	 $("#warm_report_table").append("<tr class='listrow'><td colspan='6'><div class='nulldata'>没有符合条件的信息</div></td></tr>");
		             }
		         }
	        },
	        error: function() {
	            //alert('获取报警统计报表失败，请稍后重试!');
	        }
	});
	
}


var trclass="";
function trmouseOver(el){
  trclass=$(el).attr("class");
  $(el).attr("class",trclass+" highlight");
}

function trmouseOut(el){
  $(el).attr("class",trclass);
}


//制保留2位小数，如：2，会在2后面补上00.即2.00    
function toDecimal2(x) {    
    var f = parseFloat(x);    
    if (isNaN(f)) {    
        return false;    
    }    
    var f = Math.round(x*100)/100;    
    var s = f.toString();    
    var rs = s.indexOf('.');    
    if (rs < 0) {    
        rs = s.length;    
        s += '.';    
    }    
    while (s.length <= rs + 2) {    
        s += '0';    
    }    
    return parseFloat(s);    
}    

function toRunExcel(){
    var id=$("#userid").val(),
	   startTime=$("#start").val(),
	   endTime=$("#end").val();
		   
	if(id=="" || id==null){
		alert("请选择设备");
		return;
	}else if(startTime=="" || startTime==null){
		alert("开始时间不能为空");
		return;
	}else if(endTime=="" || endTime==null){
		alert("结束时间不能为空");
		return;
	}
	
	if($("#mileage_report_table").is(":visible")){
		downExcel(id,startTime,endTime,"export/reportsAction_licheng.action")
	}else if($("#running_report_table").is(":visible")){
		downExcel(id,startTime,endTime,"export/reportsAction_xingshi.action")
	}else if($("#overspeed_report_table").is(":visible")){
		downExcel(id,startTime,endTime,"export/reportsAction_chaosu.action")
	}else{
		downExcel(id,startTime,endTime,"export/reportsAction_baojing.action")
	}
}

function downExcel(id,startTime,endTime,url) {
       window.location = $("#serverUrl",parent.document).val()+url+"?"+"rf.st="+startTime+"&rf.ed="+endTime+"&rf.ids="+id;
}

var version = null

function callback(data){

	version = data;
	
}
