
var degrees=0
var vplay_stat=1

window.addEventListener('load', function() {
                        var video = document.querySelector('#video');
                        var preloader = document.querySelector('.preloader');
                        
                        function checkLoad() {
                        
                        if (video.readyState === 4) {
                        
                        
                        // Video
                        var videos = document.getElementById("video");
                        
                        // Sliders
                        var seekBar = document.getElementById("seek-bar");
                        var volumeBar = document.getElementById("volume-bar");
                        
                        
                        // Event listener for the seek bar
                        seekBar.addEventListener("change", function() {
                                                 // Calculate the new time
                                                 var time = videos.duration * (seekBar.value / 100);
                                                 // Update the video time
                                                 videos.currentTime = time;
                                                 if(time==0){$("#curtimetext").html("00:00:00")}
                                                 });
                        
                        
                        
                        // Update the seek bar as the video plays
                        var now_g=-100;
                        videos.addEventListener("timeupdate", function() {
                                                // Calculate the slider value
                                                var value = (100 / videos.duration) * videos.currentTime;
                                                // Update the slider value
                                                
                                                if(play_stat==1){
                                                seekBar.value = value;
                                                }
                                                clearTimeout(ckt) //有繼續跑不用檢查下一則
                                                ckt=setTimeout(go_next,2000)
                                                
                                                var tts=video.duration+""
                                                $("#durtimetext").html(tts.toHHMMSS());
                                                
                                                var tt=Math.floor(videos.currentTime)+"";
                                                
                                                var val = ($("#seek-bar").val() - $("#seek-bar").attr('min')) / ($("#seek-bar").attr('max') - $("#seek-bar").attr('min'));
                                                $("#seek-bar").css('background-image',
                                                                   '-webkit-gradient(linear, left top, right top, '
                                                                   + 'color-stop(' + val + ', #94A14E), '
                                                                   + 'color-stop(' + val + ', #000000)'
                                                                   + ')'
                                                                   );


                                                
                                                
                                                //計算目前時間位置
                                                //var add_t=((Math.floor(videos.currentTime*10)/10)+".0").split(".")
                                                //var run_time=DateAdd("ms",add_t[0]*1000+add_t[1]*100-1900,data_start_date)
                                                //var run_time1=DateAdd("ms",add_t[0]*1000+add_t[1]*100+100-1900,data_start_date)
                                                
                                                //if(now_g==-100){now_g=data[now_play_id][run_time1]['x']*1}
                                                
                                                
                                                
                                                //方向指示
                                                //if(Math.round(data[now_play_id][run_time]['speed'])>10){
                                                
                                                
                                                
                                                
                                                //$(".top_funs").html(max_gs+"//"+now_g+"//"+(max_gs-now_g))
                                                
                                                
                                                
                                                //前進
                                                //if(data[now_play_id][run_time1]['speed']*1-data[now_play_id][run_time]['speed']*1>1){
                                                // $("#g_loc").attr("src","images/g1.png")
                                                // }
                                                
                                                
                                                //var max_gs = max_g(30)
                                                //右轉==>
                                                //if(data[now_play_id][run_time]['degrees']*1>data[now_play_id][run_time1]['degrees']*1){$("#g_loc").attr("src","images/gr.png");}
                                                
                                                //左轉<==
                                                //if(max_gs-data[now_play_id][run_time]['x']*1>-0.04&&data[now_play_id][run_time]['x']*1>0){$("#g_loc").attr("src","images/gl.png");}
                                                
                                                //}  //方向指示--end
                                                
                                                
                                                
                                                
                                                });
                        
                        // Pause the video when the seek handle is being dragged
                        seekBar.addEventListener("mousedown", function() {
                                                 play_stat=0
                                                 videos.pause();
                                                 });
                        
                        // Play the video when the seek handle is dropped
                        seekBar.addEventListener("mouseup", function() {
                                                 play_stat=1
                                                 if(play_stat==1){
                                                 videos.play(); }else{
                                                 videos.pause();
                                                 }
                                                 });
                        
                        
                        // Event listener for the volume bar
                        volumeBar.addEventListener("change", function() {
                                                   // Update the video volume
                                                   videos.volume = volumeBar.value;
                                                   var val = ($("#volume-bar").val() - $("#volume-bar").attr('min')) / ($("#volume-bar").attr('max') - $("#volume-bar").attr('min'));
                                                   $("#volume-bar").css('background-image',
                                                                        '-webkit-gradient(linear, left top, right top, '
                                                                        + 'color-stop(' + val + ', #94A14E), '
                                                                        + 'color-stop(' + val + ', #000000)'
                                                                        + ')'
                                                                        );
                                                   
                                                   });
                        
                        
                        
                        } else {
                        setTimeout(checkLoad, 100);
                        }
                        }
                        
                        
                        //3秒內最大值
                        function max_g(nn){
                        var videos = document.getElementById("video");
                        var run_times;var max_v=-100;
                        var add_t=((Math.floor(videos.currentTime*10)/10)+".0").split(".")
                        
                        for(i=0;i<nn;i++){
                        
                        run_times=DateAdd("ms",add_t[0]*1000+add_t[1]*100+(i*100)-1900,data_start_date)
                        
                        if(max_v<data[now_play_id][run_times]['degrees']){
                        max_v=data[now_play_id][run_times]['degrees']
                        }
                        }
                        
                        
                        return max_v
                        }
                        
                        checkLoad();
                        
                        
                        function go_next(){
                        //$(".top_funs").html("next")
                        if(play_stat==1){
                        for(i=0;i<$("#file_list a").length;i++){
                        
                        var ss1=$("#"+$("input.files:not(.title)")[i].id).attr("value").split("/")
                        var ss2=$("#video").attr("src").split("/")
                        
                        //$(".top_funs").html(ss1[ss1.length-1]+","+ss2[ss2.length -1])
                        
                        if(ss1[ss1.length-1]==ss2[ss2.length -1]){
                        if(i+1<$("#file_list a").length){
                        eval($($("#file_list a")[i+1]).attr("ondblclick"));
                        }else{
                        eval($($("#file_list a")[0]).attr("ondblclick"));
                        }myVideo.play();
                        return
                        }
                        }
                        play_stat=0;
                        myVideo.pause(); $("#plays").attr("src","images/play.png");
                        }
                        }
                        
                        //每毫秒執行
                        var avs1=0;var avs2=0;var av_min=0
                        function check_data(){
                        var videos = document.getElementById("video");
                        var seekBar = document.getElementById("seek-bar");
                        
                        
                        setTimeout(check_data, 100)
                        
                        if (video.readyState === 4&& play_stat==1) {// video ready
                        
                        // Calculate the slider value
                        var value = (100 / videos.duration) * videos.currentTime;
                        // Update the slider value
                        var tt=Math.floor(videos.currentTime)+"";
                        
                        var sec_num = parseInt(tt, 10); // don't forget the second param
                        var hours   = Math.floor(sec_num / 3600);
                        var minutes = Math.floor((sec_num - (hours * 3600)) / 60);
                        var seconds = sec_num - (hours * 3600) - (minutes * 60);
                        
                        if (hours   < 10) {hours   = "0"+hours;}
                        if (minutes < 10) {minutes = "0"+minutes;}
                        if (seconds < 10) {seconds = "0"+seconds;}
                        var time    = hours+':'+minutes+':'+seconds;
                        
                        document.getElementById("curtimetext").innerHTML=time
                        
                        
                        
                        //計算目前時間位置
                        var add_t=((Math.floor(videos.currentTime*10)/10)+".0").split(".")
                        var run_time=DateAdd("ms",add_t[0]*1000+add_t[1]*100-1900,data_start_date)
                        var run_time1=DateAdd("ms",add_t[0]*1000+add_t[1]*100+100-1900,data_start_date)
                        var run_time2=DateAdd("ms",add_t[0]*1000+add_t[1]*100+400-1900,data_start_date)
                        var run_time3=DateAdd("ms",add_t[0]*1000+add_t[1]*100+700-1900,data_start_date)
                        var run_time4=DateAdd("ms",add_t[0]*1000+add_t[1]*100+1000-1900,data_start_date)
                        var run_time5=DateAdd("ms",add_t[0]*1000+add_t[1]*100+1300-1900,data_start_date)
                        var run_time6=DateAdd("ms",add_t[0]*1000+add_t[1]*100+1400-1900,data_start_date)
                        var run_time7=DateAdd("ms",add_t[0]*1000+add_t[1]*100+1700-1900,data_start_date)
                        var run_time8=DateAdd("ms",add_t[0]*1000+add_t[1]*100+2000-1900,data_start_date)
                        var run_time9=DateAdd("ms",add_t[0]*1000+add_t[1]*100+2300-1900,data_start_date)
                        
                        //過濾無效資料
                        if (data[now_play_id][run_time]){
                        //過濾無效定位
                        if(data[now_play_id][run_time]['lat']*1!=0&&data[now_play_id][run_time]['lon']*1!=0){
                        
                        
                        //速度
                        speed_fun(Math.floor(data[now_play_id][run_time]['speed']*unit_diss));
                        
                        
                        //lbs
                        var lbs=data[now_play_id][run_time]['lbs'].replace("[","°").replace("[","°").replace(":","\"").replace(":","\"");
                        var lbss=lbs.split(",");
                        if(lbss.length*1>1){
                        $("#lons").html((lbss[0]).replace("-",""));$("#lats").html((lbss[1]).replace("-",""))
                        }
                        
                        
                        
                        if(data[now_play_id][run_time]['lon']*1<0){$("#latsn").attr("src","images/w.png")}else{$("#latsn").attr("src","images/e.png")}
                        if(data[now_play_id][run_time]['lat']*1<0){$("#lonsn").attr("src","images/s.png");}else{$("#lonsn").attr("src","images/n.png");}
                        
                        
                        
                        //Google Map
                        $('#maps')[0].contentWindow.setMarker(data[now_play_id][run_time]['lat'], data[now_play_id][run_time]['lon'], "")
                        //XYZ
                        $("#gx").html("X:"+data[now_play_id][run_time]['x']);
                        $("#gy").html("Y:"+data[now_play_id][run_time]['y']);
                        $("#gz").html("Z:"+data[now_play_id][run_time]['z']);
                        $("#gl").css("width",410/videos.duration*videos.currentTime)
                        
                        
                        //>10 km
                        if(Math.round(data[now_play_id][run_time]['speed'])>10){
                        
                        
                        //算出平均角度===========
                        var ann_s=0;var degrees_count=0;var d_max=0;var d_min=0;
                        var degrees_old=degrees
                        for(ann=0;ann<50;ann++){
                        var run_times=DateAdd("ms",add_t[0]*1000+add_t[1]*100+ann*100-1900,data_start_date)
                        var run_times1=DateAdd("ms",add_t[0]*1000+add_t[1]*100+(ann+1)*100-1900,data_start_date)
                        if(data[now_play_id][run_times1]['lat']&&data[now_play_id][run_times1]['lon']){
                        var de_v=bearing(data[now_play_id][run_times]['lat']*1,data[now_play_id][run_times]['lon']*1,data[now_play_id][run_times1]['lat']*1,data[now_play_id][run_times1]['lon']*1)*1
                        
                        degrees_count=degrees_count+de_v
                        ann_s=ann_s+1
                        }
                        }
                        degrees=degrees_count/ann_s
                        //$(".top_funs").html("time:"+run_time+",time1:"+run_time1+",degrees:"+degrees)
                        //算出平均角度===========
                        
                        
                        Compass_fun(degrees)
                        
                        //方向=============
                        
                        
                        //方向指示===============================
                        var g_status=0
                        
                        
                        
                        //還是無方向時進行短時間檢查
                        if(g_status==0){
                        var defs=data[now_play_id][run_time]['degrees']-data[now_play_id][run_time1]['degrees'];
                        var defs1=data[now_play_id][run_time1]['degrees']-data[now_play_id][run_time2]['degrees'];
                        var defs2=data[now_play_id][run_time2]['degrees']-data[now_play_id][run_time3]['degrees'];
                        var defs3=data[now_play_id][run_time3]['degrees']-data[now_play_id][run_time4]['degrees'];
                        var defs4=data[now_play_id][run_time4]['degrees']-data[now_play_id][run_time5]['degrees'];
                        var defs5=data[now_play_id][run_time5]['degrees']-data[now_play_id][run_time6]['degrees'];
                        var defs6=data[now_play_id][run_time6]['degrees']-data[now_play_id][run_time7]['degrees'];
                        var defs7=data[now_play_id][run_time7]['degrees']-data[now_play_id][run_time8]['degrees'];
                        var defs8=data[now_play_id][run_time8]['degrees']-data[now_play_id][run_time9]['degrees'];
                        //右轉==>
                        if(defs<-0.15&&defs1<-0.15&&defs2<-0.15&&defs3<-0.15&&defs4<-0.15&&defs5<-0.15&&defs6<-0.15&&defs7<-0.15&&defs8<-0.15){$("#g_loc").attr("src","images/gr.png"); g_status=1;}
                        //左轉<==
                        if(defs>0.15&&defs1>0.15&&defs2>0.15&&defs3>0.15&&defs4>0.15&&defs5>0.15&&defs6>0.15&&defs7>0.15&&defs8>0.15){$("#g_loc").attr("src","images/gl.png"); g_status=1;}
                        
                        //$(".top_funs").html("time:"+run_time+",defs:"+defs+",defs1:"+defs1)
                        }
                        
                        
                        //無方向
                        if(g_status==0){$("#g_loc").attr("src","images/g.png");}
                        
                        
                        //方向指示 end===============================
                        
                        
                        
                        
                        }else{
                        //若小於10km時
                        $("#g_loc").attr("src","images/g.png");//無方向
                        }
                        //<10 km end
                        
                        }//過濾無效定位
                        }//過濾無效資料
                        
                        }// video ready
                        }
                        
                        check_data()
                        
                        
                   
                        

                        
                        
                        }, false);



function DateAdd(timeU,byMany,datestr) {
    var millisecond=1;
    var second=millisecond*1000;
    var minute=second*60;
    var hour=minute*60;
    var day=hour*24;
    var year=day*365;
    
    var datestr1=ReplaceAll(ReplaceAll(ReplaceAll(ReplaceAll(datestr,"/",","),":",",")," ",","),".",",").split(",")
    var dateObj=new Date(datestr1[0]*1,datestr1[1]*1-1,datestr1[2]*1,datestr1[3]*1,datestr1[4]*1,datestr1[5]*1,datestr1[6]*100)
    
    
    var newDate;
    var dVal=dateObj.valueOf();
    switch(timeU) {
        case "ms": newDate=new Date(dVal+millisecond*byMany); break;
        case "s": newDate=new Date(dVal+second*byMany); break;
        case "mi": newDate=new Date(dVal+minute*byMany); break;
        case "h": newDate=new Date(dVal+hour*byMany); break;
        case "d": newDate=new Date(dVal+day*byMany); break;
        case "y": newDate=new Date(dVal+year*byMany); break;
    }
    
    return newDate.getFullYear()  + '/' + fnum(newDate.getMonth() + 1,2) + '/' + fnum(newDate.getDate(),2) + ' ' + fnum(newDate.getHours(),2) + ':' + fnum(newDate.getMinutes(),2) + ':' + newDate.getSeconds()+ '.' + newDate.getMilliseconds()/100;
    
}

function ReplaceAll(strSource, strFind, strRepl) {
    var str5 = new String(strSource);
    while (str5.indexOf(strFind) != -1) {
        str5=str5.replace(strFind, strRepl);
    }
    return str5;
}

//補零
function fnum(n,len){
    if(n*1>9){return n}
    var str1="00000";var ns=n+"";
    return (str1.substr(0,len-ns.length)+n).replace("NaN","0");
}

