// (c) RaceMyRace.com

RaceReplayer.TICK_SIM_MS=66;RaceReplayer.speedFactor=1.0;function RaceReplayer(ge,paths,race,racers,opt_opts){this.ge=ge;this.paths=paths;this.view_settings={};this.options=opt_opts||{};this.race=race;this.racers=racers;this.stage=0;this.view_settings[0]={altitude:3,heading:0,tilt:85,roll:0,cam_offset:0,locked:false};this.view_settings[1]={altitude:1,heading:0,tilt:90,roll:0,heading_rel:0};this.view_settings[2]={range_y:300,heading_rel:0,tilt:60,roll:0,range:500,range_max:2000,range_min:50};this.focusRacer=-1;this.TICK_REAL_MS=50;this.speedFactor=5;this.speedFactorRatio=5;this.vMode=1;this.prestartPercentComplete=0;this.smode=0;this.currentLoc=this.paths[0][0].loc;this.currentLoc_fr=this.paths[0][0].loc;this.currentTilt=90;this.curr_distance=0;this.curr_alt=this.paths[0][0].loc.alt();this.geHelpers_=new GEHelpers(ge);this.doTick_=false;this.pathIndex_=0;this.headingCurrent_=null;this.totalTimeSelected_=0;this.totalTimeSelectedPrevious_=0;this.totalTimeReal_=0;this.totalTime_=0;this.tp_elapsed_=0;this.currRacer_=0;this.focusDistance_=0;this.focusTime_=0;this.tickAdvance_=0;this.tickStart_=0;this.tickEnd_=0;this.timerAdjustment_=1.0;}
RaceReplayer.prototype.initUI=function(opt_cb){var me=this;this.tickListener=function(){if(me.doTick_){me.tick_();}};window.google.earth.addEventListener(this.ge,'frameend',this.tickListener);google.earth.addEventListener(ge.getWindow(),'click',function(event){if(event.getTarget().getType()=='KmlPlacemark'&&event.getTarget().getGeometry().getType()=='KmlPoint'){event.preventDefault();placemark_click(event.getTarget());}});if(r_r.race.has_sview==null){test_sview(r_r.currentLoc);}
if(so_eprog){so_eprog.getOverlayXY().setX(0);}
r_r.speedFactor=$("#slider_speed").slider('option','value');r_initSettings();init_raceview();if(opt_cb){opt_cb();}};RaceReplayer.prototype.rewind=function(){b_v_racer_click();this.stop();this.stage=1;this.prestartPercentComplete=0;this.headingCurrent_=this.race.courses[c_cIx].startHeading;this.currentLoc=this.race.courses[c_cIx].startLoc;this.pathIndex_=0;this.totalTimeSelected_=0;this.totalTimeSelectedPrevious_=0;this.totalTimeReal_=0;this.totalTime_=0;this.tp_elapsed_=0;this.currRacer_=0;this.focusDistance_=0;this.focusTime_=0;this.tickAdvance_=0;this.tickStart_=0;this.tickEnd_=0;this.timerAdjustment_=1.0;r_preStart_Overlays(0);updateDisplay();if(so_eprog){so_eprog.getOverlayXY().setX(0);}
for(var i=0;i<r_r.racers.length;i++){this.racers[i].currentLoc=this.race.courses[this.racers[i].courseIndex].startLoc;this.racers[i].pm.getGeometry().setLatitude(this.racers[i].currentLoc.lat());this.racers[i].pm.getGeometry().setLongitude(this.racers[i].currentLoc.lng());this.racers[i].pm.getGeometry().setAltitude(this.racers[i].currentLoc.alt());this.racers[i].pm.setName(r_r.racers[i].rno+'');}
this.ge.getView().setAbstractView(this.race.courses[c_cIx].prestartCam);settings.trig_events.set4recalc=true;chk_trigEvents(-1);};RaceReplayer.prototype.destroy=function(){this.stop();window.google.earth.removeEventListener(this.ge,'frameend',this.tickListener);};RaceReplayer.prototype.start=function(){if(this.doTick_){return;}
this.oldFlyToSpeed=this.ge.getOptions().getFlyToSpeed();this.ge.getOptions().setFlyToSpeed(this.ge.SPEED_TELEPORT);if(r_r.vMode===3){r_setViewMode(r_r.vMode_prev);}
this.doTick_=true;this.tick_();};RaceReplayer.prototype.stop=function(){if(!this.doTick_){return;}
this.ge.getOptions().setFlyToSpeed(this.ge.SPEED_TELEPORT);this.oldFlyToSpeed=this.ge.getOptions().getFlyToSpeed();this.tickStart_=null;this.doTick_=false;};RaceReplayer.prototype.moveView_=function(loc){switch(this.vMode){case 0:break;case 1:this.moveCamera_(loc);break;case 2:this.moveCamera_(loc);break;case 3:break;default:break;}}
RaceReplayer.prototype.moveCamera_=function(loc){var cam=this.ge.createCamera('');switch(this.vMode){case 0:break;case 1:cam.set(loc.lat(),loc.lng(),loc.alt()+this.view_settings[1].altitude,this.ge.ALTITUDE_RELATIVE_TO_GROUND,this.headingCurrent_+this.view_settings[1].heading_rel,90,0);break;case 2:this.view_settings[2].heading=this.geHelpers_.fixAngle(this.view_settings[2].heading+1*this.getHeadingChange_(this.view_settings[2].heading,this.headingCurrent_));this.view_settings[2].range_x=this.view_settings[2].range*Math.sin(this.geHelpers_.deg2rad(this.view_settings[2].tilt));this.view_settings[2].range_y=this.view_settings[2].range*Math.cos(this.geHelpers_.deg2rad(this.view_settings[2].tilt));this.view_settings[2].offset_loc=get_offsetLoc(loc,this.view_settings[2].range_x,this.geHelpers_.fixAngle(this.view_settings[2].heading+180));var gAltLA=ge.getGlobe().getGroundAltitude(loc.lat(),loc.lng());var gAltCam=ge.getGlobe().getGroundAltitude(this.view_settings[2].offset_loc.lat(),this.view_settings[2].offset_loc.lng());this.view_settings[2].altitude=this.view_settings[2].range_y+(gAltLA-gAltCam);cam.set(this.view_settings[2].offset_loc.lat(),this.view_settings[2].offset_loc.lng(),this.view_settings[2].altitude,this.ge.ALTITUDE_RELATIVE_TO_GROUND,this.view_settings[2].heading,this.view_settings[2].tilt,this.view_settings[2].roll);break;default:break;}
this.ge.getView().setAbstractView(cam);};RaceReplayer.prototype.moveLookAt_=function(loc){this.view_settings[2].heading=this.geHelpers_.fixAngle(this.view_settings[2].heading+1*this.getHeadingChange_(this.view_settings[2].heading,this.headingCurrent_));var la=this.ge.createLookAt('');la.set(loc.lat(),loc.lng(),loc.alt(),this.ge.ALTITUDE_RELATIVE_TO_GROUND,this.view_settings[2].heading,this.view_settings[2].tilt,this.view_settings[2].range);this.ge.getView().setAbstractView(la);}
RaceReplayer.prototype.getHeadingChange_=function(heading1,heading2){if(Math.abs((heading1)-(heading2))<1)
return heading2-heading1;return(this.geHelpers_.fixAngle(heading2-heading1)<0)?-1:1;}
RaceReplayer.prototype.getPathIndexes_=function(courseIndex,distance,i_offset){var ret={};var curr_path;var curr_pathLength;for(var i=0;i<this.race.courses[courseIndex].path_sequence.length;i++){curr_path=this.race.courses[courseIndex].path_sequence[i].path_index;curr_pathLength=(this.paths[curr_path].length-1)*this.race.courses[courseIndex].path_sequence[i].segment_length;if(distance<=curr_pathLength){ret.path=curr_path;ret.segment_length=this.race.courses[courseIndex].path_sequence[i].segment_length;if(distance===0){ret.pathIndex=0;}else{ret.pathIndex=parseInt((distance/this.race.courses[courseIndex].path_sequence[i].segment_length),10);}
if(i_offset>0){ret.pathIndex+=i_offset;if(ret.pathIndex>this.paths[curr_path].length-1){if(i==this.race.courses[courseIndex].path_sequence.length-1){ret.path=-1;ret.pathIndex=-1;ret.segment_length=-1;}else{ret.path=this.race.courses[courseIndex].path_sequence[i+1].path_index;ret.segment_length=this.race.courses[courseIndex].path_sequence[i+1].segment_length;ret.pathIndex=ret.pathIndex-(this.paths[curr_path].length-1);}}}
return ret;}else{if(i==this.race.courses[courseIndex].path_sequence.length-1){ret.path=-1;ret.pathIndex=-1;ret.segment_length=-1;return ret;}else{distance-=curr_pathLength;}}}};RaceReplayer.prototype.getLoc_=function(i,time_elapsed){for(var j=0;j<this.racers[i].splits.length;j++){var split_endTime=((this.racers[i].splits[j].time_secs+get_sdelay(this.racers[i].sdelay))*1000);if(time_elapsed<=split_endTime){this.racers[i].curr_split=j;break;}}
if(this.racers[i].curr_split===0){this.racers[i].distance_prev=0;this.racers[i].time_prev=0;}else{this.racers[i].distance_prev=this.racers[i].splits[this.racers[i].curr_split-1].distance_meters;this.racers[i].time_prev=this.racers[i].splits[this.racers[i].curr_split-1].time_secs;}
this.racers[i].time_todo=this.totalTimeReal_-(get_sdelay(this.racers[i].sdelay)*1000)-(this.racers[i].time_prev*1000);this.racers[i].split_percent=this.racers[i].time_todo/((this.racers[i].splits[this.racers[i].curr_split].time_secs-this.racers[i].time_prev)*1000);this.racers[i].distance_todo=this.racers[i].split_percent*(this.racers[i].splits[this.racers[i].curr_split].distance_meters-this.racers[i].distance_prev);this.racers[i].distance_completed=(this.racers[i].distance_prev+this.racers[i].distance_todo)/this.race.d_adjustment;var p0=this.getPathIndexes_(this.racers[i].courseIndex,this.racers[i].distance_completed,0);this.racers[i].pathIndexes0=p0;this.racers[i].path_Curr=p0.path;this.racers[i].pathIndex_Curr=p0.pathIndex;this.racers[i].segment_length_Curr=p0.segment_length;var p1=this.getPathIndexes_(this.racers[i].courseIndex,this.racers[i].distance_completed,1);this.racers[i].pathIndexes1=p1;this.racers[i].path_Next=p1.path;this.racers[i].pathIndex_Next=p1.pathIndex;var p2=this.getPathIndexes_(this.racers[i].courseIndex,this.racers[i].distance_completed,2);this.racers[i].pathIndexes2=p2;this.racers[i].path_Next2=p2.path;this.racers[i].pathIndex_Next2=p2.pathIndex;var t_v=null;if(this.racers[i].distance_completed===0){t_v=0;}else if(this.racers[i].pathIndex_Curr==-1){t_v=1;}else{t_v=this.racers[i].distance_completed/this.racers[i].segment_length_Curr;}
this.racers[i].segmentPercent=t_v-parseInt(t_v,10);if(this.racers[i].pathIndex_Next==-1){newLoc=this.race.courses[this.racers[i].courseIndex].finishLoc;}else{newLoc=get_midLoc(this.paths[this.racers[i].path_Curr][this.racers[i].pathIndex_Curr].loc,this.paths[this.racers[i].path_Next][this.racers[i].pathIndex_Next].loc,this.racers[i].segmentPercent);}
return newLoc;};RaceReplayer.prototype.tick_=function(){switch(this.stage){case 1:if(!this.headingCurrent_){this.headingCurrent_=this.race.courses[this.racers[this.focusRacer].courseIndex].startHeading;}
var ppt=(RaceReplayer.TICK_SIM_MS*this.timerAdjustment_)/psData.duration;this.prestartPercentComplete+=ppt;var newLoc=get_midLoc(this.race.courses[c_cIx].prestartLoc,this.race.courses[c_cIx].startLoc,this.prestartPercentComplete);this.moveView_(newLoc);r_preStart_Overlays(this.prestartPercentComplete);if(this.prestartPercentComplete>=1){this.stage=2;}
var endDT=new Date();this.tickEnd_=endDT.getTime();if(this.tickStart_){var tickDuration=this.tickEnd_-this.tickStart_;this.timerAdjustment_=tickDuration/RaceReplayer.TICK_SIM_MS;}
this.tickStart_=this.tickEnd_;break;case 2:this.totalTimeSelected_=0;for(var i=0;i<this.racers.length;i++){if(this.racers[i].included==1){if((this.racers[i].totaltime+get_sdelay(this.racers[i].sdelay))*1000>this.totalTimeSelected_){this.totalTimeSelected_=(this.racers[i].totaltime+get_sdelay(this.racers[i].sdelay))*1000;}}}
if(this.totalTimeSelectedPrevious_==this.totalTimeSelected_){this.totalTimeReal_=this.tp_elapsed_*this.totalTimeSelected_;}else{this.totalTimeReal_=this.tp_elapsed_*this.totalTimeSelectedPrevious_;this.totalTimeSelectedPrevious_=this.totalTimeSelected_;}
if(this.totalTimeReal_>=this.totalTimeSelected_||this.tp_elapsed_==1){this.doTick_=false;this.totalTimeReal_=this.totalTimeSelected_;this.tp_elapsed_=1;if(this.options.on_tick){this.options.on_tick();}
return;}
this.totalTime_+=RaceReplayer.TICK_SIM_MS;if(this.vMode===0){this.totalTimeReal_+=(this.TICK_REAL_MS*this.speedFactor);}else{this.totalTimeReal_+=(this.TICK_REAL_MS*this.speedFactor/this.speedFactorRatio);}
for(i=0;i<this.racers.length;i++){this.currRacer_=i;var finished=false;if(this.racers[i].included==1){if(this.totalTimeReal_<(this.racers[i].totaltime+get_sdelay(this.racers[i].sdelay))*1000){this.currentLoc=this.getLoc_(i,this.totalTimeReal_);}else{this.currentLoc=this.race.courses[this.racers[i].courseIndex].finishLoc;finished=true;}
var race_length=this.race.splits[this.race.splits.length-1].distance_meters;this.racers[i].distanceToFocus=0;this.racers[i].timeToFocus=0;this.racers[i].distanceToFocus=-(this.focusDistance_-this.racers[i].distance_completed);this.racers[i].currentLoc=this.currentLoc;this.racers[i].pm.getGeometry().setLatitude(this.currentLoc.lat());this.racers[i].pm.getGeometry().setLongitude(this.currentLoc.lng());this.racers[i].pm.getGeometry().setAltitude(this.currentLoc.alt());if(i==this.focusRacer){this.racers[i].pm.setName(r_r.racers[i].rno+'');this.currentLoc_fr=this.currentLoc;this.focusDistance_=this.racers[i].distance_completed;this.focusTime_=this.totalTimeReal_;var groundAltitude=ge.getGlobe().getGroundAltitude(this.currentLoc.lat(),this.currentLoc.lng());this.curr_distance=this.racers[i].distance_completed*this.race.d_adjustment;this.curr_alt=groundAltitude+this.currentLoc.alt()
chk_trigEvents(this.racers[i].distance_completed*this.race.d_adjustment);if(this.racers[i].pathIndex_Next2==-1||this.racers[i].pathIndex_Next==-1){this.headingCurrent_=this.race.finishHeading;}else{var headingSegmentStart=this.geHelpers_.getHeading(this.paths[this.racers[i].path_Curr][this.racers[i].pathIndex_Curr].loc,this.paths[this.racers[i].path_Next][this.racers[i].pathIndex_Next].loc);var headingSegmentEnd=this.geHelpers_.getHeading(this.paths[this.racers[i].path_Next][this.racers[i].pathIndex_Next].loc,this.paths[this.racers[i].path_Next2][this.racers[i].pathIndex_Next2].loc);this.headingCurrent_=headingSegmentStart+(this.geHelpers_.fixAngle(headingSegmentEnd-headingSegmentStart)*this.racers[i].segmentPercent);}
this.moveView_(this.currentLoc);if(so_eprog){so_eprog.getOverlayXY().setX(this.racers[i].distance_completed/race_length);}}else{if(finished===true){var tmp_time=(r_r.totalTimeReal_/1000)-r_r.racers[i].totaltime;if(r_r.racers[r_r.focusRacer].totaltime<(r_r.totalTimeReal_/1000)){this.racers[i].pm.setName(r_r.racers[i].rno+' ('+fmtTimeToFocus(tmp_time)+')');}else{this.racers[i].pm.setName(r_r.racers[i].rno+' ('+fmtTimeToFocus(tmp_time)+')');}}else{this.racers[i].pm.setName(r_r.racers[i].rno+' ('+fmtDistanceToFocus(r_r.racers[i].distanceToFocus)+')');}}}}
this.tp_elapsed_=this.totalTimeReal_/this.totalTimeSelected_;if(this.totalTimeReal_>=this.totalTimeSelected_||this.tp_elapsed_==1){this.doTick_=false;this.totalTimeReal_=this.totalTimeSelected_;this.tp_elapsed_=1;if(this.options.on_tick){this.options.on_tick();}
return;}
break;default:break;}
if(this.options.on_tick){this.options.on_tick();}};