bbc.fmtj.utils.createObject("bbc.fmtj.apps.f1CountdownManager");bbc.fmtj.utils.createObject("bbc.fmtj.apps.Countdown");gloader.load(["glow","1","glow.anim","glow.dom","glow.net","glow.events"],{onLoad:function(glow){bbc.fmtj.apps.Countdown=(function(o){var _self=this,domId=o.id,targetTime=new Date(o.to)||new Date(),currentTime=new Date(o.serverTime)||new Date(),lap=new Date(o.lap)||new Date(),largestUnit='seconds',msgClass=o.msgClass||'message',timeClass=o.timeClass||'time',outerDiv=o.outerDiv||'f1CountdownTimer',configuration={interval:1*1000,message:'Default Message',countSilent:false,expired:false,formatTemplate:'<span class="time"><span class="days">{days} {days_lbl}</span> <span class="hours">{hrs} {hrs_lbl}</span> <span class="minutes">{mins} {mins_lbl}</span> <span class="seconds">{secs} {secs_lbl}</span></span>',formatLabels:{days_lbl:"days",hrs_lbl:"hours",mins_lbl:"minutes",secs_lbl:"seconds"},formatValues:{days:"00",hrs:"00",mins:"00",secs:"00"}},onAttach=function(){},onTick=function(){},onTickSilent=function(){},onExpiry=function(){},tickInterval=null
var windowListener=glow.events.addListener(window,'focus',function(){if(Math.ceil((new Date().getTime()-new Date(currentTime.setSeconds(currentTime.getSeconds()+1)).getTime())/(1000*60))>63){resyncTime()}})
var lastTimeObj={}
function attach(){var targetElm=glow.dom.get('#'+domId+' .countdownInner'),cfg=configuration,template=applyTemplate({template:cfg.formatTemplate,labels:cfg.formatLabels,values:cfg.formatValues})
largestUnit=determineLargestUnit(configuration.formatTemplate);glow.dom.get(targetElm).append(template);glow.dom.get('#'+domId+' .'+msgClass).css('background','none')
glow.dom.get('#'+domId+' .'+msgClass).html('')
var wrapper=glow.dom.create('<div class="hqCountdownWrapper"></div>');var dashboard=glow.dom.create('<div class="dashBoard"></div>')
var digits=glow.dom.create('<div class="digitWrapper"><span class="secs1"></span><span class="secs2"></span><span class="mins1"></span><span class="mins2"></span><span class="hrs1"></span><span class="hrs2"></span><span class="days1"></span><span class="days2"></span><span class="days3"></span></div>')
glow.dom.get(wrapper).append(dashboard);glow.dom.get(wrapper).append(digits);if(glow.dom.get(".hqCountdownWrapper").length>0){glow.dom.get(".hqCountdownWrapper").remove()}
glow.dom.get('#'+domId+' .countdownInner').css('display','none')
glow.dom.get('#'+domId).append(wrapper)
_self.broadcastEvent(domId,'attach',{countdownId:domId,message:cfg.message})
glow.dom.get('#'+domId).css('display','block');tick();glow.dom.get('#'+domId+' .'+msgClass).html(configuration.message);tickInterval=setInterval(function(){tick()},configuration.interval)}
function tick(){var now=new Date(currentTime.setSeconds(currentTime.getSeconds()+1)),target=targetTime,cfg=configuration,duration=getDuration(now,target),template=applyTemplate({template:cfg.formatTemplate,labels:cfg.formatLabels})
if(now.getTime()>target.getTime()){if(lap.getTime()>now.getTime()){targetTime=lap
configuration.countSilent=true
if(configuration.lapMessage){configuration.message=configuration.lapMessage
glow.dom.get('#'+domId+' .'+msgClass).html(configuration.message)
_self.broadcastEvent(domId,'applyLapMessage',{countdownId:domId,message:configuration.message});}
changeDisplay(template,configuration.formatValues)
_self.broadcastEvent(domId,'tickSilent',{countdownId:domId,dateObj:display});}
else{configuration.expired=true;changeDisplay(template,configuration.formatValues)
clearInterval(tickInterval)
glow.events.removeListener(windowListener)
_self.broadcastEvent(domId,'countdownComplete',{countdownId:domId,time:now})}}
if(configuration.expired==false){var display=getDisplayDigits(duration)
if(configuration.countSilent!=true){changeDisplay(template,display)
tickAnimation(display)
_self.broadcastEvent(domId,'tick',{countdownId:domId,dateObj:display});}}}
function tickAnimation(dateObj){var changedDigits=getChangedDigits(dateObj)
var digitSelection;for(prop in changedDigits){digitSelection=changedDigits[prop].toString();changedDigits.prop=digitSelection.split('');moveDigit('.'+prop+'1',-23*changedDigits.prop[1]++)
moveDigit('.'+prop+'2',-23*changedDigits.prop[0]++)}}
function moveDigit(digitToMove,toPosition){glow.anim.css(digitToMove,0.2,{"top":{to:toPosition}},{tween:glow.tweens.elasticBoth()}).start()}
function getChangedDigits(dateObj){var changedDigitsObj={};if(lastTimeObj){for(prop in dateObj){if(dateObj[prop]!=lastTimeObj[prop]){changedDigitsObj[prop]=dateObj[prop]}}
lastTimeObj=dateObj}
return changedDigitsObj}
function changeDisplay(template,valuesToDisplay){glow.dom.get('#'+domId+' .'+timeClass).remove();glow.dom.get('#'+domId+' .countdownInner').append(glow.lang.interpolate(template,valuesToDisplay));return true}
function getDuration(now,targetTime){now=now.setMilliseconds(0)
return new Date(targetTime-now)}
function applyTemplate(o){var rtnTemplate;if(o.labels&&o.values){var a=o.values;var b=o.labels
rtnTemplate=glow.lang.apply(a,b);rtnTemplate=glow.lang.interpolate(o.template,rtnTemplate);return rtnTemplate;}
if(o.labels){rtnTemplate=glow.lang.interpolate(o.template,o.labels);return rtnTemplate}
if(o.values){rtnTemplate=glow.lang.interpolate(o.template,o.values);return rtnTemplate}}
function determineLargestUnit(formatTemplate){var unit=null;if(formatTemplate.match("{secs}"))
unit="seconds";if(formatTemplate.match("{mins}"))
unit="mins";if(formatTemplate.match("{hrs}"))
unit="hours";if(formatTemplate.match("{days}"))
unit="days";if(formatTemplate.match("{wks}"))
unit="weeks";return unit;}
function getDisplayDigits(timeOffset){switch(largestUnit){case"days":var days=Math.floor(timeOffset.getTime()/(24*3600000)),hours=timeOffset.getUTCHours(),minutes=timeOffset.getUTCMinutes(),seconds=timeOffset.getUTCSeconds()
var timeObj={days:days,hrs:hours,mins:minutes,secs:seconds}
break;case"hours":var hours=timeOffset.getUTCHours(),minutes=timeOffset.getUTCMinutes(),seconds=timeOffset.getUTCSeconds()
var timeObj={hrs:hours,mins:minutes,secs:seconds}
break;case"minutes":var minutes=timeOffset.getUTCMinutes(),seconds=timeOffset.getUTCSeconds()
var timeObj={mins:minutes,secs:seconds}
break;case"seconds":var seconds=timeOffset.getUTCSeconds()
var timeObj={secs:seconds}
break;}
for(prop in timeObj){timeObj[prop]=timeObj[prop].toString()
if(timeObj[prop].length<2){timeObj[prop]="0"+timeObj[prop]}}
return timeObj}
function applyCountdownSettings(obj){glow.lang.apply(configuration,obj)}
function resyncTime(){glow.net.get('/sol/shared/inc/v4/formula_one/countdown/server_time.stm',{onLoad:function(response){currentTime=new Date(response.text())}})}
this.broadcastEvent=function(id,evtName,o){var event=glow.events.fire(this,evtName,o)}
this.init=function(o){if(o.configuration){applyCountdownSettings(o.configuration)}
attach()}})}})