(function($){IMAGE_PATH='/sites/default/modules/custom/patient_flows/images/';ICON_IMAGES={'person':{'src':'person.png','srcDead':'person-death.png','tailWidth':9,'width':22,'height':24},'person-blue':{'src':'person-blue.png','srcDead':'person-death.png','tailWidth':9,'width':22,'height':24},'person-green':{'src':'person-green.png','srcDead':'person-death.png','tailWidth':9,'width':22,'height':24},'person-red':{'src':'person-red.png','srcDead':'person-death.png','tailWidth':9,'width':22,'height':24},'person-yellow':{'src':'person-yellow.png','srcDead':'person-death.png','tailWidth':9,'width':22,'height':24},'person-beige':{'src':'person-beige.png','srcDead':'person-death.png','tailWidth':9,'width':22,'height':24},'person-grey':{'src':'person-grey.png','srcDead':'person-death.png','tailWidth':9,'width':22,'height':24},'person-lime':{'src':'person-white.png','srcDead':'person-death.png','tailWidth':9,'width':22,'height':24},'person-teal':{'src':'person-teal.png','srcDead':'person-death.png','tailWidth':9,'width':22,'height':24},'group':{'src':'people.png','srcDead':'people-death.png','width':32,'height':28},'group-blue':{'src':'people-blue.png','srcDead':'people-death.png','width':32,'height':28},'group-green':{'src':'people-green.png','srcDead':'people-death.png','width':32,'height':28},'group-red':{'src':'people-red.png','srcDead':'people-death.png','width':32,'height':28},'group-yellow':{'src':'people-yellow.png','srcDead':'people-death.png','width':32,'height':28},'group-beige':{'src':'people-beige.png','srcDead':'people-death.png','width':32,'height':28},'group-grey':{'src':'people-grey.png','srcDead':'people-death.png','width':32,'height':28},'group-lime':{'src':'people-lime.png','srcDead':'people-death.png','width':32,'height':28},'group-teal':{'src':'people-teal.png','srcDead':'people-death.png','width':32,'height':28},'person-large':{'src':'person-big-white.png','srcDead':'person-big-death.png','width':34,'height':37},'person-large-blue':{'src':'person-big-blue.png','srcDead':'person-big-death.png','width':34,'height':37},'person-large-green':{'src':'person-big-green.png','srcDead':'person-big-death.png','width':34,'height':37},'person-large-red':{'src':'person-big-red.png','srcDead':'person-big-death.png','width':34,'height':37},'person-large-yellow':{'src':'person-big-yellow.png','srcDead':'person-big-death.png','width':34,'height':37},'person-large-beige':{'src':'person-big-beige.png','srcDead':'person-big-death.png','width':34,'height':37},'person-large-grey':{'src':'person-big-grey.png','srcDead':'person-big-death.png','width':34,'height':37},'person-large-lime':{'src':'person-big-lime.png','srcDead':'person-big-death.png','width':34,'height':37},'person-large-teal':{'src':'person-big-teal.png','srcDead':'person-big-death.png','width':34,'height':37},'group-large':{'src':'people-big-white.png','srcDead':'people-big-death.png','tailWidth':20,'width':54,'height':46},'group-large-blue':{'src':'people-big-blue.png','srcDead':'people-big-death.png','tailWidth':20,'width':54,'height':46},'group-large-green':{'src':'people-big-green.png','srcDead':'people-big-death.png','tailWidth':20,'width':54,'height':46},'group-large-red':{'src':'people-big-red.png','srcDead':'people-big-death.png','tailWidth':20,'width':54,'height':46},'group-large-yellow':{'src':'people-big-yellow.png','srcDead':'people-big-death.png','tailWidth':20,'width':54,'height':46},'group-large-beige':{'src':'people-big-beige.png','srcDead':'people-big-death.png','tailWidth':20,'width':54,'height':46},'group-large-grey':{'src':'people-big-grey.png','srcDead':'people-big-death.png','tailWidth':20,'width':54,'height':46},'group-large-lime':{'src':'people-big-lime.png','srcDead':'people-big-death.png','tailWidth':20,'width':54,'height':46},'group-large-teal':{'src':'people-big-teal.png','srcDead':'people-big-death.png','tailWidth':20,'width':54,'height':46}}
$.fn.patientFlows=function(opts){var container=this;var globalOpts=opts;var WIDTH_INCLUDING_BORDERS=784;var WIDTH=WIDTH_INCLUDING_BORDERS-10;var HEIGHT=436;container.addClass('patient-flows-active').css({'width':WIDTH+'px'});var headingElem=$('<div class="heading"></div>');container.append(headingElem);var captionElem=$('<div class="caption"></div>');headingElem.append(captionElem);var titleElem=$('<h2></h2>').text(opts.title);captionElem.append(titleElem);var descriptionElem=$('<p></p>').text(opts.description);captionElem.append(descriptionElem);if(opts.key){var keyElem=$('<div class="key"><h2>Key:</h2></div>');headingElem.prepend(keyElem);var keyListElem=$('<ul></ul>');keyElem.append(keyListElem);for(var i=0;i<opts.key.length;i++){if(!opts.key[i])continue;var label=opts.key[i]['label'];var keyLi=$('<li></li>').text(label);if(i==0)keyLi.addClass('first');keyLi.css({'background-image':"url('"+IMAGE_PATH+ICON_IMAGES[opts.key[i].icon].src+"')"})
keyListElem.append(keyLi);}}
headingElem.append('<div style="clear: both;"></div>');var raphaelElem=$('<div></div>');container.append(raphaelElem);var raph=Raphael(raphaelElem.get(0),WIDTH,HEIGHT);function circleSizeToRadius(size){return Math.sqrt(size)*7;}
function Controller(){var self={};var currentTime=0;var playing=false;var finished=false;var playStartTime,playStartRealTime;var callbacks={};self.bind=function(eventType,callback){if(!callbacks[eventType])callbacks[eventType]=[];callbacks[eventType].push(callback);}
function fire(eventType,eventData){if(!callbacks[eventType])return;for(var i=0;i<callbacks[eventType].length;i++){callbacks[eventType][i](eventData);}}
var actors=[];self.addActor=function(actor){actor.update(currentTime);actors.push(actor);}
function setTime(t){if(t==currentTime)return;currentTime=t;for(var i=0;i<actors.length;i++){actors[i].update(t);}}
self.skipTo=function(t){if(playing){playing=false;fire('pause');}else{playing=false;}
finished=false;setTime(t);}
function tick(){if(!playing)return;var currentRealTime=(new Date()).getTime();var newTime=playStartTime+(currentRealTime-playStartRealTime)*opts.speed/1000;if(newTime>=opts.days){setTime(opts.days-1);playing=false;finished=true;fire('finish');}else{setTime(newTime);setTimeout(tick,20);}}
self.play=function(){if(finished){currentTime=0;finished=false;}
playStartTime=currentTime;playStartRealTime=(new Date()).getTime();playing=true;fire('play');tick();}
self.pause=function(){playing=false;fire('pause');}
return self;}
var controller=Controller();var TIMELINE_CONTROLS_X=Math.floor(WIDTH/8);var TIMELINE_AND_CONTROLS_WIDTH=WIDTH*3/4;var rewindButton=raph.set([raph.rect(0.5,0.5,27,27).attr({'fill':'#ebe9e5'}),raph.rect(5.5,4.5,2,18).attr({'fill':'#1c1b1a'}),raph.path('M 20.5 6.5 l v 14 l -10 -7 z').attr({'fill':'#1c1b1a'})]).translate(TIMELINE_CONTROLS_X,HEIGHT-51).attr({'cursor':'pointer'}).click(function(){controller.skipTo(0);});var playButton=raph.set([raph.rect(0.5,0.5,27,27).attr({'fill':'#ebe9e5'}),raph.path('M 9.5 6.5 l 10 7 l -10 7 z').attr({'fill':'#1c1b1a'})]).translate(TIMELINE_CONTROLS_X+32,HEIGHT-51).attr({'cursor':'pointer'}).click(function(){controller.play();});var pauseButton=raph.set([raph.rect(0.5,0.5,27,27).attr({'fill':'#ebe9e5'}),raph.rect(8.5,7.5,3,12).attr({'fill':'#1c1b1a'}),raph.rect(16.5,7.5,3,12).attr({'fill':'#1c1b1a'})]).translate(TIMELINE_CONTROLS_X+32,HEIGHT-51).attr({'cursor':'pointer'}).click(function(){controller.pause();});pauseButton.hide();controller.bind('play',function(){playButton.hide();pauseButton.show();});controller.bind('pause',function(){pauseButton.hide();playButton.show();});controller.bind('finish',function(){pauseButton.hide();playButton.show();});function Timeline(){var self={};var TIMELINE_WIDTH=Math.floor(WIDTH*3/4)-70;var TIMELINE_X=TIMELINE_CONTROLS_X+70;var sliderTarget=raph.rect(0,0,TIMELINE_WIDTH,28).attr({'stroke-width':0,'fill':'rgba(28, 27, 26, 0)'});var sliderFill=raph.rect(0.5,17.5,TIMELINE_WIDTH,9).attr({'fill':'#5d594b'});var sliderOutline=raph.rect(0.5,17.5,TIMELINE_WIDTH,9).attr({'stroke-width':1,'stroke':'#ffffff'});var sliderPointer=raph.image(IMAGE_PATH+'slider_pointer.png',7,11,17,25);var sliderCaption=raph.text(0,0,'').attr({'text-anchor':'start','fill':'#ffffff','font-size':16});var sliderBarElements=[sliderTarget,sliderFill,sliderOutline,sliderCaption];if(opts.days<=30){var scaleSpacing=1;}else if(opts.days<=720){var scaleSpacing=30;}else{var scaleSpacing=360;}
for(var i=scaleSpacing;i<opts.days;i+=scaleSpacing){sliderBarElements.push(raph.path('M'+(Math.floor(i/opts.days*TIMELINE_WIDTH)+0.5)+' 19 v 6').attr({'stroke-width':1,'stroke':'#ffffff'}));}
sliderBarElements.push(sliderPointer);sliderPointer.toFront();var sliderBar=raph.set(sliderBarElements).translate(TIMELINE_X,HEIGHT-51);var globalMouseMove=function(e){setTimelinePosition(e.clientX-container.position().left-TIMELINE_X);}
var globalMouseUp=function(e){$(window).unbind('mousemove',globalMouseMove).unbind('mouseup',globalMouseUp);}
sliderBar.mousedown(function(e){if(e.which==1||(e.which==null&&e.button==1)){setTimelinePosition(e.clientX-container.position().left-TIMELINE_X);$(window).mousemove(globalMouseMove).mouseup(globalMouseUp);}})
function setTimelinePosition(x){if(x<0)x=0;if(x>=TIMELINE_WIDTH)x=TIMELINE_WIDTH-1;controller.skipTo(x/TIMELINE_WIDTH*opts.days);}
self.update=function(t){var timelineX=TIMELINE_WIDTH*(t/opts.days);sliderFill.attr({'width':timelineX});sliderPointer.attr({'x':TIMELINE_X+timelineX-7});var day=Math.floor(t);sliderCaption.attr({'text':'Month '+(Math.floor(day/30)+1)+', day '+(day%30+1)});}
self.toFront=function(){for(var i=0;i<sliderBarElements.length;i++){sliderBarElements[i].toFront();}}
return self;}
var timeline=Timeline();controller.addActor(timeline);function EventList(){var self={};var events=[];var currentT=0;var nextEventIndex=0;self.lastEvent=null;self.nextEvent=null;self.addEvent=function(newEvent){if(events.length==0||events[events.length-1].t<=newEvent.t){events.push(newEvent);}else{var i=0;while(events[i].t<newEvent.t)i++;events.splice(i,0,newEvent);if(nextEventIndex>i)nextEventIndex++;}}
self.insertEvent=function(t,newEventCallback,laterEventsCallback){if(events.length==0||events[events.length-1].t<=t){var newEvent=newEventCallback(events[events.length-1]);events.push(newEvent);}else{var i=0;while(events[i].t<t)i++;var newEvent=newEventCallback(events[i-1]);events.splice(i,0,newEvent);if(laterEventsCallback){for(var j=i+1;j<events.length;j++){laterEventsCallback(events[j]);}}
if(nextEventIndex>i)nextEventIndex++;}}
self.setTime=function(t){if(t>=currentT){while(events[nextEventIndex]&&events[nextEventIndex].t<=t)nextEventIndex++;self.lastEvent=events[nextEventIndex-1];self.nextEvent=events[nextEventIndex];currentT=t;}else{while(events[nextEventIndex-1]&&events[nextEventIndex-1].t>t)nextEventIndex--;self.lastEvent=events[nextEventIndex-1];self.nextEvent=events[nextEventIndex];currentT=t;}}
return self;}
function BaseNumberCounter(opts){var self={};var cx=opts.x;var cy=opts.y;var eventList=EventList();eventList.addEvent({'t':0,'value':0});var shadowElem=raph.text(cx+1,cy+1,'x').attr({'fill':'#000000','font-size':opts.fontSize,'opacity':0.8});var elem=raph.text(cx,cy,'x').attr({'fill':'#ffffff','font-size':opts.fontSize});self.addMovement=function(t,delta){eventList.insertEvent(t,function(prevEvent){return{'t':t,'value':prevEvent.value+delta}},function(e){e.value+=delta;});}
self.update=function(t){eventList.setTime(t);var text=self.textFormat(eventList.lastEvent.value);elem.attr({'text':text});shadowElem.attr({'text':text});}
self.textHeight=elem.getBBox().height+2;self.setYPosition=function(y){elem.attr({'y':y});shadowElem.attr({'y':y+1});}
return self;}
function NumberCounter(opts){opts.fontSize=13;var self=BaseNumberCounter(opts);self.textFormat=function(value){return opts.label+': '+value;}
return self;}
function MoneyCounter(opts){opts.fontSize=18;var self=BaseNumberCounter(opts);function commaFormat(nStr,separator){nStr+='';var rgx=/(\d+)(\d{3})/;while(rgx.test(nStr)){nStr=nStr.replace(rgx,'$1'+(separator||',')+'$2');}
return nStr;}
self.textFormat=function(value){return"\u00a3"+commaFormat(value);}
return self;}
function SizeCounter(opts){var self={};var eventList=EventList();eventList.addEvent({'t':0,'value':opts.size});self.addMovement=function(t,delta){var startT=t-(globalOpts.speed*0.1);var endT=t+(globalOpts.speed*0.1);eventList.insertEvent(startT,function(prevEvent){return{'t':startT,'value':prevEvent.value}});eventList.insertEvent(endT,function(prevEvent){return{'t':endT,'value':prevEvent.value+delta}},function(e){e.value+=delta;});}
self.update=function(t){eventList.setTime(t);var lastPosition=eventList.lastEvent;var nextPosition=eventList.nextEvent;if(nextPosition&&(nextPosition.value!=lastPosition.value)){var distance=(t-lastPosition.t)/(nextPosition.t-lastPosition.t)
opts.circleElem.attr({'r':circleSizeToRadius((1-distance)*lastPosition.value+distance*nextPosition.value)});}else{opts.circleElem.attr({'r':circleSizeToRadius(lastPosition.value)});}}
self.textHeight=0;self.setYPosition=function(y){};return self;}
var COUNTER_STYLES={'number':NumberCounter,'money':MoneyCounter,'size':SizeCounter};LOCATION_TOP_OFFSET=HEIGHT/7;LOCATION_HEIGHT=2*HEIGHT/7;LOCATION_LEFT_OFFSET=3*WIDTH/22;LOCATION_WIDTH=4*WIDTH/22;function Location(locationOpts){var self={};var occupiedSlots=[];var cx=(locationOpts.position-1)%5*LOCATION_WIDTH+LOCATION_LEFT_OFFSET;var cy=Math.floor((locationOpts.position-1)/5)*LOCATION_HEIGHT+LOCATION_TOP_OFFSET;var initialSize=locationOpts.size||50;self.getSlot=function(){var i=0;while(occupiedSlots[i])i++;occupiedSlots[i]=true;var angle=i*7/17*2*Math.PI;var skew=Math.floor(i/17)*3;return{'id':i,'x':cx+50*Math.sin(angle)+skew,'y':cy+50*Math.cos(angle)+skew};}
self.freeSlot=function(slot){occupiedSlots[slot.id]=null;}
self.getBubblePosition=function(){return{'x':cx-80+Math.random()*30,'y':cy-70+Math.random()*20};}
var circle=raph.circle(cx,cy,circleSizeToRadius(initialSize)).attr({'fill':locationOpts.color,'stroke':'#272626','stroke-width':3});var labelShadowElem=raph.text(cx+1,cy-16+1,locationOpts.name).attr({'fill':'#000000','font-size':13,'font-weight':'bold','opacity':0.8});var labelElem=raph.text(cx,cy-16,locationOpts.name).attr({'fill':'#ffffff','font-size':13,'font-weight':'bold'});var LABEL_HEIGHT=16;var elem=raph.set([circle,labelElem,labelShadowElem])
var counters={};var countersInOrder=[];var totalTextHeight=LABEL_HEIGHT;for(var i=1;i<=5;i++){var counterStyle=locationOpts['counter-style-'+i];if(counterStyle){var counter=COUNTER_STYLES[counterStyle]({'label':locationOpts['counter-label-'+i],'x':cx,'y':cy,'circleElem':circle,'size':initialSize});totalTextHeight+=counter.textHeight;counters[i]=counter;countersInOrder.push(counter);}}
var lineY=cy-(totalTextHeight/2);labelElem.attr({'y':lineY+LABEL_HEIGHT/2});labelShadowElem.attr({'y':lineY+LABEL_HEIGHT/2+1});lineY+=LABEL_HEIGHT;for(var i=0;i<countersInOrder.length;i++){var counter=countersInOrder[i];counter.setYPosition(lineY+counter.textHeight/2);lineY+=counter.textHeight;}
self.addArrival=function(t,newEvent){for(var i in counters){var counterIncrement=newEvent['counter-'+i];if(counterIncrement!=null){counters[i].addMovement(t,counterIncrement);}}}
self.addDeparture=function(t,newEvent){for(var i in counters){var counterIncrement=newEvent['from-counter-'+i];if(counterIncrement!=null){counters[i].addMovement(t,counterIncrement);}}}
self.update=function(t){for(var i in counters){counters[i].update(t);}}
return self;}
function HomeLocation(opts){var self={};if(!opts)opts={};var occupiedSlots=[];var position=opts.position||8;var cx=(position-1)%5*LOCATION_WIDTH+LOCATION_LEFT_OFFSET;var cy=Math.floor((position-1)/5)*LOCATION_HEIGHT+LOCATION_TOP_OFFSET;var slotPositions=[[cx,cy-32],[cx,cy+32],[cx-64,cy+32],[cx+64,cy+32],[cx-64,cy-32],[cx+64,cy-32],[cx-32,cy+32],[cx+32,cy+32],[cx-32,cy-32],[cx+32,cy-32],[cx-64,cy],[cx+64,cy],[cx-32,cy],[cx+32,cy]]
var elem=raph.set([raph.rect(cx-(182/2),cy-(122/2),182,122,10).attr({'fill':'#333231','stroke':'#514f4c','stroke-dasharray':'-'}),raph.text(cx,cy,opts.name||'Home').attr({'fill':'#ffffff','font-size':15})])
self.getSlot=function(){var i=0;while(occupiedSlots[i])i++;occupiedSlots[i]=true;var xy=slotPositions[i%slotPositions.length];var skew=Math.floor(i/slotPositions.length)*3;return{'id':i,'x':xy[0]+3-skew,'y':xy[1]+3-skew};}
self.freeSlot=function(slot){occupiedSlots[slot.id]=null;}
self.getBubblePosition=function(){return{'x':cx-(182/2)+Math.random()*60,'y':cy-(122/2)+Math.random()*20};}
self.update=function(t){}
self.addArrival=function(newEvent){}
self.addDeparture=function(newEvent){}
return self;}
function Sprite(location,opts){var self={};if(!opts)opts={};var tailElem=raph.path('M 0 0 z').attr({'fill':'#888888','opacity':0.3,'stroke-width':0});var icon=ICON_IMAGES[opts.icon||'person'];var tailWidth=icon.tailWidth||15;var elem=raph.set([tailElem,raph.image(IMAGE_PATH+icon.src,-icon.width/2,-icon.height/2,icon.width,icon.height)])
var currentX=0;var currentY=0;function moveTo(x,y){elem.translate(x-currentX,y-currentY);currentX=x;currentY=y;}
function drawTail(dx,dy){if(dx==0&&dy==0){tailElem.hide();}else{if(Math.abs(dx)>Math.abs(dy)){tailElem.attr({'path':'M 16 '+(16-tailWidth)+' v '+(2*tailWidth)+' L '+(15+dx)+' '+(15+dy)+' z'});}else{tailElem.attr({'path':'M '+(16-tailWidth)+' 21 h '+(2*tailWidth)+' L '+(15+dx)+' '+(15+dy)+' z'});}
tailElem.translate(currentX-15,currentY-15);tailElem.show();}}
drawTail(0,0);var slot=location.getSlot();var isDead=false;moveTo(slot.x,slot.y);var eventList=EventList();eventList.addEvent({'t':0,'x':slot.x,'y':slot.y,'dead':isDead});self.addMovement=function(startTime,endTime,destination,event,isDeath){eventList.addEvent({'t':startTime,'x':slot.x,'y':slot.y,'dead':isDead});location.addDeparture(startTime,event);location.freeSlot(slot);slot=destination.getSlot();location=destination;if(isDeath)isDead=true;eventList.addEvent({'t':endTime,'x':slot.x,'y':slot.y,'dead':isDead});destination.addArrival(endTime,event);}
self.addDeath=function(time){isDead=true;eventList.addEvent({'t':time,'x':slot.x,'y':slot.y,'dead':true});}
self.getLastRecordedLocation=function(){return location;}
function quadraticEasing(k){if(k<0.5){return 2*k*k;}else{return-2*k*k+4*k-1;}}
function quadraticEasingDeriv(k){if(k<0.5){return 4*k;}else{return-4*k+4;}}
self.update=function(t){eventList.setTime(t);var lastPosition=eventList.lastEvent;var nextPosition=eventList.nextEvent;if(nextPosition&&(nextPosition.x!=lastPosition.x||nextPosition.y!=lastPosition.y)){var distance=(t-lastPosition.t)/(nextPosition.t-lastPosition.t)
distance=quadraticEasing(distance);var distanceDeriv=quadraticEasingDeriv(distance);var vx=(nextPosition.x-lastPosition.x)*distanceDeriv;var vy=(nextPosition.y-lastPosition.y)*distanceDeriv;moveTo(nextPosition.x*distance+lastPosition.x*(1-distance),nextPosition.y*distance+lastPosition.y*(1-distance));drawTail(-vx*0.15,-vy*0.15);}else{moveTo(lastPosition.x,lastPosition.y);drawTail(0,0);}
if(lastPosition&&lastPosition['dead']){elem.attr({'src':IMAGE_PATH+icon.srcDead});}else{elem.attr({'src':IMAGE_PATH+icon.src});}}
return self;}
function InfoBubble(startTime,x,y,title,description){var self={};var DURATION=globalOpts.speed*2;var PEAK_OPACITY=0.9;var endTime=startTime+DURATION;var rectElem,line1Elem,line2Elem;if(title&&description){var rectElem=raph.rect(0,0,106,36,4).attr({'fill':'#000000'});var line1Elem=raph.text(8,10,title).attr({'fill':'#ffffff','text-anchor':'start','font-size':12,'font-weight':'bold'});var line2Elem=raph.text(8,24,description).attr({'fill':'#ffffff','text-anchor':'start','font-size':12});var width=Math.max(line1Elem.getBBox().width,line2Elem.getBBox().width);rectElem.attr({'width':width+16});var elem=raph.set([rectElem,line1Elem,line2Elem]).translate(x,y);}else if(title){var rectElem=raph.rect(0,0,106,20,4).attr({'fill':'#000000'});var line1Elem=raph.text(8,10,title).attr({'fill':'#ffffff','text-anchor':'start','font-size':12,'font-weight':'bold'});rectElem.attr({'width':line1Elem.getBBox().width+16});var elem=raph.set([rectElem,line1Elem]).translate(x,y);}else{var rectElem=raph.rect(0,0,106,20,4).attr({'fill':'#000000'});var line1Elem=raph.text(8,10,description).attr({'fill':'#ffffff','text-anchor':'start','font-size':12});rectElem.attr({'width':line1Elem.getBBox().width+16});var elem=raph.set([rectElem,line1Elem]).translate(x,y);}
self.update=function(t){if(t<startTime||t>endTime){elem.hide();}else{elem.show();var distance=(t-startTime)/DURATION;if(distance<0.05){var level=distance/0.05;var opacity=level*0.99;}else if(distance<0.3){var opacity=0.99;}else{var level=(distance-0.3)/0.7;var opacity=(1-level*level*level*level)*0.99;}
rectElem.attr({'opacity':opacity*PEAK_OPACITY});line1Elem.attr({'opacity':opacity});if(line2Elem)line2Elem.attr({'opacity':opacity});}}
return self;}
var locationsById={};for(locationId in opts.locations){if(locationId=='home'){var location=HomeLocation(opts.locations[locationId]);}else{var location=Location(opts.locations[locationId]);}
locationsById[locationId]=location;controller.addActor(location);}
if(!locationsById['home']){var home=HomeLocation();locationsById['home']=home;controller.addActor(home);}
var sprites={};if(typeof(opts.sprites)=='number'){for(var i=0;i<opts.sprites;i++){var sprite=Sprite(locationsById['home']);sprites[i+1]=sprite
controller.addActor(sprite);}}else{for(var spriteId in opts.sprites){var sprite=Sprite(locationsById['home'],opts.sprites[spriteId]);sprites[spriteId]=sprite;controller.addActor(sprite);}}
for(var i=0;i<opts.events.length;i++){opts.events[i].originalIndex=i;}
opts.events.sort(function(a,b){return(a.day==b.day)?(a.originalIndex-b.originalIndex):(a.day-b.day);});var lastEventTimeBySprite={};for(var i=0;i<opts.events.length;i++){var event=opts.events[i];var destination=locationsById[event['to']];var startTime=event.day;if(opts.queueEvents&&lastEventTimeBySprite[event.sprite]>=startTime){startTime=lastEventTimeBySprite[event.sprite]+0.01;}
var bubbleLocation,effectTime;if(destination){var endTime=startTime+globalOpts.speed*1;sprites[event.sprite].addMovement(startTime,endTime,destination,event,event['death']);bubbleLocation=destination;effectTime=endTime;}else{bubbleLocation=sprites[event.sprite].getLastRecordedLocation();effectTime=startTime;if(event['death']){sprites[event.sprite].addDeath(effectTime);}}
lastEventTimeBySprite[event.sprite]=effectTime;if(event.title||event.description){var xy=bubbleLocation.getBubblePosition();controller.addActor(InfoBubble(effectTime-(globalOpts.speed*0.2),xy.x,xy.y,event.title,event.description));}}
rewindButton.toFront();playButton.toFront();pauseButton.toFront();timeline.toFront();var copyright=raph.text(WIDTH-8,HEIGHT-8,'\u00a9 Nuffield Trust').attr({'text-anchor':'end','fill':'#888888','font-size':12});copyright.toFront();}})(jQuery);
