﻿ var AllCarLines={CarLines:[]};
    
    AllCarLines.Del = function(){
       for (var i=0; i<CarLines.length; i++) 
         this.CarLines[i]=null;
    };    
    
    AllCarLines.Update= function(Json){
       for (var i=0; i<Json.length; i++) 
         if (this.CarLines[Json[i].CarId]!=null) this.CarLines[Json[i].CarId].UpdateLines(Json[i]);
    };
    
    AllCarLines.Test=function(param){

    }
    
    AllCarLines.SetColor = function(CarId,Color){
         if ((this.CarLines[CarId]!=null)&&(Color.length<7)) this.CarLines[CarId].ColorLine='#'+Color;
    }
    
    AllCarLines.Add=function(maps,CarId,Name){
        if (this.CarLines[CarId]==null) this.CarLines[CarId]= new CarLinesClass(maps,CarId,Name);
        else{
            var d=new Date(); d.setHours(0,0,0,0);
            this.CurrentDatef(CarId,d.format('m/d/Y'));
            Onbind.call(this.CarLines[CarId].PlotTime,maps,CarId);
            Draw.call(this.CarLines[CarId].PlotSpeed);
            OnbindSpeed.call(this.CarLines[CarId].PlotSpeed,maps,CarId);
            
        }
    }
    
    AllCarLines.Draw= function(CarId){
        if (this.CarLines[CarId].PlotTime!=null) {
            this.CarLines[CarId].UpdateDataLoad();
            Draw.call(this.CarLines[CarId].PlotTime);
       }
    }
    
    AllCarLines.LoadDrawSpeed = function(CarId) {
        if (this.CarLines[CarId].PlotSpeed!=null) {
            this.CarLines[CarId].UpdateDataSpeed();
            Draw.call(this.CarLines[CarId].PlotSpeed);
        }
    }    
    
    AllCarLines.CurrentDatef= function(CarId,CurDate){
        this.CarLines[CarId].currentdate=CurDate;
        if (this.CarLines[CarId].PlotTime!=null) {
            this.CarLines[CarId].UpdateDataLoad();
            this.CarLines[CarId].UpdateDataSpeed();
            Draw.call(this.CarLines[CarId].PlotTime);
            Draw.call(this.CarLines[CarId].PlotSpeed);
        }
        
    }    
    
    CarLinesClass=function(maps,CarId,Name){
        this.CarId=CarId;
        this.Maps=maps;
        this.Name=Name;
        if (this.DataPoints==null) {
            this.DataPoints = [];
        }
        var d=new Date();
        d.setHours(0,0,0,0);
        this.currentdate=d.format('m/d/Y')
        this.Dt=[];       
        this.Dt2=[]; 
        this.DtPoint=[]; 
        this.DtPointSel=[];
        this.area={};
        this.num=-1;      
        this.ColorLine='#FF0000';
        this.UpdateDataLoad();
        var dt=this.currentdate;
        this.PlotTime = new PlotTimeClass(this.Maps,this.CarId,this.Dt);
        this.PlotSpeed = new PlotSpeedClass(this.Maps,this.CarId,this.Dt2);
    }
    
    CarLinesClass.prototype.OnLoadtreack= function(){
        var  nt=this.Dt2.length;
        for (var i=0; i<nt; i++)
            if (this.Dt2[i][0]>this.area.x1) break;
        if (i<nt) this.i1=i;
        for (var j=i; j<nt; j++)
            if (this.Dt2[j][0]>this.area.x2) break;
        if (j<nt) this.i2=j;
        
        if (this.area.x1<this.area.x2) CarMaps.AddLinesG(this.Maps,this.CarId,this.DtPoint.slice(this.i1,this.i2),this.ColorLine,this.Name);
    }

    CarLinesClass.prototype.Removetreack= function(){
        CarMaps.RemoveLinesG(this.Maps,this.CarId);
    }
    CarLinesClass.prototype.UpdateLines=function(Jsonm){
        var j=-1;
        var n = this.DataPoints.length;
        for (var i=0; i<n; i++)
            if (this.DataPoints[i].Datep==Jsonm.Date) j=i;
        if (j>-1) {
            this.DataPoints[j].points[Jsonm.Interval]={};
            this.DataPoints[j].points[Jsonm.Interval].taf = [].concat(Jsonm.points);
            this.DataPoints[j].points[Jsonm.Interval].len = Jsonm.points.length;
            this.DataPoints[j].points[Jsonm.Interval].flag = 1;
            }
        else {
            this.DataPoints[n]={points:[]};
            this.DataPoints[n].points[Jsonm.Interval]={};
            this.DataPoints[n].points[Jsonm.Interval].taf = [].concat(Jsonm.points);
            this.DataPoints[n].points[Jsonm.Interval].len = Jsonm.points.length;
            this.DataPoints[n].points[Jsonm.Interval].flag = 1;
            this.DataPoints[n].Datep=Jsonm.Date;  
        }      
    }
    
    CarLinesClass.prototype.UpdateDataLoad=function(){
       var dat= new Date(this.currentdate);
       var mill=dat.getTime();
       var j=-1;
       n = this.DataPoints.length;
       for (var i=0; i<n; i++)
          if (this.DataPoints[i].Datep==this.currentdate) {j=i;break;}
       if (j>-1&&this.DataPoints[j].points!=null) {
           for (var t=0; t<48; t++) {
              if (this.DataPoints[j].points[t]!=null) this.Dt[t]=[mill+t*1800000,100];          
              else this.Dt[t]=[mill+t*1800000,0]; 
           }   
       }else{
           for (var t=0; t<48; t++) 
              this.Dt[t]=[mill+t*1800000,0];
           this.DtPoint=[];   
           var ndel=this.Dt2.length-48;
           this.Dt2.splice(49,ndel,0);
           for (var t=0; t<=48; t++) 
              this.Dt2[t]=[mill+t*1800000,0];      
       }
   }

    CarLinesClass.prototype.UpdateDataSpeed=function(){
       var dat= new Date(this.currentdate)
       var mill=dat.getTime();
       var j=-1;
       n = this.DataPoints.length;
       for (var i=0; i<n; i++)
          if (this.DataPoints[i].Datep==this.currentdate) {j=i;break;}
       this.num=j;
       if (j>-1&&this.DataPoints[j].points!=null) {
           this.Dt2.length=0;
           for (var t=0; t<=48; t++) {
              if (this.DataPoints[j].points[t]!=null){ 
                      this.Dt2[this.Dt2.length]=[mill+t*1800000,0]; 
                      var nmax=this.DataPoints[j].points[t].taf.length;
                      //this.Dt2[t]=[mill+t*1800000,100];          
                      for (var m=0; m<nmax; m++) {
                        nn=this.Dt2.length;
                        this.Dt2[nn]=[mill+t*1800000+this.DataPoints[j].points[t].taf[m][2]*1000,this.DataPoints[j].points[t].taf[m][3]]
                        this.DtPoint[nn] = new GLatLng(this.DataPoints[j].points[t].taf[m][0],this.DataPoints[j].points[t].taf[m][1]);          
                        this.DtPoint[nn].Time=mill+t*1800000+this.DataPoints[j].points[t].taf[m][2]*1000;
                        this.DtPoint[nn].Speed=this.DataPoints[j].points[t].taf[m][3];
                      }  
              }
              else {
                    this.Dt2[this.Dt2.length]=[mill+t*1800000,0]; 
              }
           }   
       }else{
           for (var t=0; t<=48; t++) 
              this.Dt2[t]=[mill+t*1800000,0];      
       }
   }    
   function dub(a)
{
    if (a<10) return '0'+a;
    return a; 
    
}
function TimeValue(t)
{
    var h=dub(Math.floor(t/3600));
    var m=dub(Math.floor(t%3600/60));
    var s=dub(Math.floor(t%3600%60));
    return h+':'+m+':'+s;
    
}
function setWord(l, r) {
	//document.getElementById("firstChar").value =TimeValue(l);
	//document.getElementById("secondChar").value = TimeValue(r);
} 
   PlotTimeClass = function(Maps,CarId,dat){

        this.div='placeholder'+CarId;
        this.data={
            data: dat,
            bars: { show: true, barWidth: 1800000,lineWidth: 0 }
        };
        this.opt={   xaxis: {
                    mode: "time",
                    ticks: 24
                },
                yaxis:{
                    min:0,
                    max:100
                },
                selection: { mode: "x" }
        };
        this.Plot= $.plot($("#"+this.div),[this.data],this.opt);
        this.bind=$("#"+this.div).bind("selected", function (event, area) {
            var d = new Date(area.x1)
                d.setHours(0,0,0,0);
            var st=Math.round((area.x1-d.getTime())/1800000);
            var fn=Math.round((area.x2-d.getTime())/1800000);
            CarTrack.SetSF(Maps,CarId,st,fn,d);
        });
        
        this.divBar='placeholderBar'+CarId;
        treckbardraw(CarId);
    }   
    PlotSpeedClass = function(MapId,CarId,dat){
        this.div='placeSpeed'+CarId;
        this.data={
            data: dat
        };
        this.opt={   
                xaxis: {mode: "time", ticks: 24 },
                grid: { clickable: false, mouseCatchingArea: 6, triggerOnMouseOver: true },
                yaxis:{ min:0, max:100 },
                selection: { mode: "x" },
              //  points: { show: true,radius: 1 }, 
                lines: { show: true, lineWidth: 1 },
                grid: { clickable: true, mouseCatchingArea: 30, triggerOnMouseOver: true }
        };

            
        this.Plot= $.plot($("#"+this.div),[this.data],this.opt);
        $("#"+this.div).bind("selected", function (event, area) {
            bingPlot(area,MapId,CarId);
        });  
        this.bind1=$("#"+this.div).bind("plotclick", function (e, pos) {
            AllCarLines.CarLines[CarId].PlotSpeed.Plot.setSelection({ x1: pos.x, x2: pos.x });
            CarMaps.Maps[MapId].SetMarker(CarId,pos.x);
        });
        this.bind=$("#"+this.div).bind("plotmousemove", function (e, pos) {
             //   Plot.setData( [dat, [pos.selectedItem]] );
             //   Plot.draw();
        });
    }   
    
    bingPlot= function(area,MapId,CarId){
        AllCarLines.CarLines[CarId].area={x1:area.x1,x2:area.x2};
    }

    Draw = function(){
       if (this.Plot!=null) {
           this.Plot = $.plot($("#"+this.div),[this.data],this.opt);
       }
        var i=1;   
    }
    Onbind = function(Maps,CarId){
        this.bind=$("#"+this.div).bind("selected", function (event, area) {
            var d = new Date(area.x1)
                d.setHours(0,0,0,0);
            var st=Math.round((area.x1-d.getTime())/1800000);
            var fn=Math.round((area.x2-d.getTime())/1800000);
            CarTrack.SetSF(Maps,CarId,st,fn,d);
        });  
    }
    OnbindSpeed = function(maps,CarId){
        this.bind=$("#"+this.div).bind("selected", function (event, area) {
            bingPlot(area,maps,CarId);
        });  
        this.bind1=$("#"+this.div).bind("plotclick", function (e, pos) {
            AllCarLines.CarLines[CarId].PlotSpeed.Plot.setSelection({ x1: pos.x, x2: pos.x });
            CarMaps.Maps[maps].SetMarker(CarId,pos.x);
        });     
    }
    treckbardraw= function(CarId){
       var divbarWidth=$('#placeholderBar'+CarId).width(); 
      //  this.trackbar= $('#placeholderBar'+CarId).trackbar({
	  //              onMove : function() {
	//	                setWord(this.leftValue, this.rightValue);
	 //               },
	 //               width : divbarWidth, // px
	 //               leftLimit : 1, // unit of value
	 //               leftValue : 1, // unit of value
	 //               rightLimit : 86400, // unit of value
	 //               rightValue : 86400, // unit of value
	           //     clearLimits : true,
               // 	clearValues : true,
               // 	roundUp : 30,
	  //              hehe : ":-)"
      //  });           
}