(function(){dojo.require("dojox.collections.Dictionary");dojo.require("dijit.form.CheckBox");function a(d){return typeof(d)!="undefined"}function b(f,d){var g=f.split(".");var h=dojo.global;var e;if(!(g[0] in h)&&h.execScript){h.execScript("var "+g[0])}while((e=g.shift())){if(!g.length&&a(d)){h[e]=d}else{if(h[e]){h=h[e]}else{h=h[e]={}}}}}var c=c||{};c.rotate=function(e,d){return e.slice(d).concat(e.slice(0,d))};c.getKeys=function(f){var d=[];for(var e in f){d.push(e)}return d};c.cloneProperties=function(d){for(var e in d){this[e]=d[e]}};c.getStopName=function(e){var d=e.name2;if(d=="Pierpont"){var f=e.name[e.name.length-1];if(f=="N"||f=="E"){d+=" Northbound"}else{d+=" Southbound"}}return d};c.getRouteName=function(f){var d=f.name;var e=d.lastIndexOf(" (");if(e!=-1){d=d.slice(0,e)}return d};c.Stop=function(d){this.fullname=d.name;this.latitude=d.latitude;this.longitude=d.longitude;this.name=c.getStopName(d);this.waiters_=[];this.routes_=new dojox.collections.Dictionary()};c.Stop.ARRIVAL_MARGIN_OF_ERROR=30;c.Stop.prototype.waitFor=function(f,e,g){var d=[];this.routes_.forEach(dojo.hitch(this,function(h){if(h.key.connects(this,e)){d.push(h.key)}}));console.log(this.name+" is on routes "+this.routes_);console.log("Eligible routes: "+d);if(d.length==0){throw new Error("No route connects "+this.name+" and "+e.name+"!")}this.waitForRoutes(f,d,g)};c.Stop.prototype.waitForRoutes=function(e,d,f){this.waiters_.push({routes:d,leadTime:e,callback:f})};c.Stop.prototype.update=function(l,d){this.routes_.add(l,[]);console.log("adding route: "+l);var i=this.routes_.item(l);for(var m=0;m<d.toas.length;++m){i[m]=Math.round(d.toas[m])}var j=[];for(m=0;m<this.waiters_.length;++m){var k=this.waiters_[m];for(var h=0;h<k.routes.length;++h){var l=k.routes[h];for(var e=0;e<this.routes_.item(l).length;++e){var g=this.routes_.item(l)[e];console.log("Considering "+l+" with TOA "+g+" at stop "+this.name);if(Math.abs(g-k.leadTime)<=c.Stop.ARRIVAL_MARGIN_OF_ERROR){j.push(m);k.callback(this,l,g);break}}}}var f=[];for(m=0;m<this.waiters_.length;++m){if(j.indexOf(m)==-1){f.push(this.waiters_[m])}}this.waiters_=f};c.Route=function(g,f){this.name=c.getRouteName(g);this.parent=f;this.stops=[];this.stopsMap={};for(var e=0;e<g.stops.length;++e){var d=f.getStop(c.getStopName(g.stops[e]));this.stops[e]=d;d.routes_.add(this,null);this.stopsMap[d.name]=d}this.topofloop=g.topofloop;this.isCircular=(this.topofloop!=0)};c.Route.prototype.update=function(e){console.log("Route "+this+" updated");for(var d=0;d<e.stops.length;++d){this.stopsMap[c.getStopName(e.stops[d])].update(this,e.stops[d])}};c.Route.prototype.toString=function(){return this.name};c.Route.prototype.connects=function(i,e){if(i===e){return false}if(this.isCircular){if(this.stops.indexOf(e)==-1){return false}var g=i.fullname.slice(0,-1);var d=this.stops.indexOf(i);var h=c.rotate(this.stops,d);for(var f=1;f<h.length;++f){if(h[f]===e){return true}else{if(h[f].fullname.slice(0,-1)==g){return false}}}console.log("Route.connects: should have returned by now!");return false}else{return this.stops.slice(this.stops.indexOf(i)+1).indexOf(e)!=-1}};c.Route.prototype.hasStop_=function(d){for(var e=0;e<stops.length;++e){if(stops[e].name==d){return true}}return false};c.BusSystemWatcher=function(d,e){this.url=d;this.createCallback_=e;dojo.xhrGet({url:this.url,handleAs:"json",preventCache:true,error:function(g,f){alert("error: "+g)},load:dojo.hitch(this,this.finishCreate_)})};c.BusSystemWatcher.prototype.finishCreate_=function(d,f){this.makeStops_(d);this.routes_={};for(var e=0;e<d.length;++e){this.routes_[c.getRouteName(d[e])]=new c.Route(d[e],this)}this.finishUpdate_(d);this.createCallback_()};c.BusSystemWatcher.prototype.makeStops_=function(d){this.stops_={};for(var g=0;g<d.length;++g){var f=d[g];for(var h=0;h<f.stops.length;++h){var e=new c.Stop(f.stops[h]);this.stops_[e.name]=e}}};c.BusSystemWatcher.prototype.getStop=function(d){return this.stops_[d]};c.BusSystemWatcher.prototype.getStops=function(){var d=c.getKeys(this.stops_);d.sort();return d};c.BusSystemWatcher.prototype.getRoutes=function(){var d=c.getKeys(this.routes_);d.sort();return d};c.BusSystemWatcher.prototype.update=function(){dojo.xhrGet({url:this.url,handleAs:"json",preventCache:true,load:dojo.hitch(this,this.finishUpdate_)})};c.BusSystemWatcher.prototype.finishUpdate_=function(d,f){console.log("update finished, response object has "+d.length+" routes");for(var e=0;e<d.length;++e){this.routes_[c.getRouteName(d[e])].update(d[e])}};c.App=function(e,d,g,f,h){this.waitButton=f;this.waiting=false;this.startingStops=d;this.destinationStops=g;this.routeBoxes=new dojox.collections.Dictionary();this.routeContainer=h;this.bsw=new c.BusSystemWatcher(e,dojo.hitch(this,this.finishCreate_))};c.App.REFRESH_INTERVAL=10000;c.App.prototype.finishCreate_=function(){var i=this.bsw.getStops();for(var g=0;g<i.length;++g){this.startingStops.options[this.startingStops.options.length]=new Option(i[g],i[g]);this.destinationStops.options[this.destinationStops.options.length]=new Option(i[g],i[g])}var e=this.bsw.getRoutes();for(g=0;g<e.length;++g){console.log("Creating checkbox for route "+e[g]);var f=document.createElement("p");var h=document.createElement("input");h.setAttribute("type","checkbox");h.setAttribute("checked",false);h.checked=false;h.setAttribute("name",e[g]);f.appendChild(h);var d=document.createTextNode(e[g]);f.appendChild(d);this.routeContainer.appendChild(f);this.routeBoxes.add(e[g],h)}};c.App.prototype.showAlert=function(e,d,f){this.stopWaiting();alert(e.name+" will be visited by "+d+" in "+f+" seconds!")};c.App.prototype.waitForBus=function(g,f,e){var h=this.bsw.getStop(g);var d=this.bsw.getStop(f);h.waitFor(e,d,dojo.hitch(this,this.showAlert));this.startWaiting()};c.App.prototype.waitForRoutes=function(f,e){var g=this.bsw.getStop(f);var d=[];this.routeBoxes.forEach(function(h){if(h.value.checked){d.push(h.key)}});if(d.length==0){throw new Error("No routes selected!")}g.waitForRoutes(e,d,dojo.hitch(this,this.showAlert));this.startWaiting()};c.App.prototype.startWaiting=function(){this.waitButton.disabled=true;this.waiting=true;this.updateBus();if(this.waiting){this.timer=setInterval(dojo.hitch(this,this.updateBus),c.App.REFRESH_INTERVAL)}};c.App.prototype.stopWaiting=function(){clearInterval(this.timer);this.waitButton.disabled=false;this.waiting=false};c.App.prototype.updateBus=function(){console.log("update bus called");this.bsw.update()};b("ghettobus.App",c.App);b("ghettobus.exportPath",b);b("ghettobus.BusSystemWatcher",c.BusSystemWatcher)})();