(function(){u8.Base.Jobs=function(e){$.extend(this,e);this.id="jobs";this.polledJobs={};u8.services.events.bind("u8.session.onLogout",this._onSessionLogout.bind(this),this.id)};u8.Base.Jobs.makeClass("Jobs");$.extend(u8.Base.Jobs.prototype,{dispose:function(){if(this._isDisposed){return}this._isDisposed=true;u8.services.events.unbind("u8.session.onLogout",this.id);this.stopPolling()},getJobs:function(e){var o=this;u8.request("JobsChannel.GetJobList",{},function(s,t){t=t.error?t:{jobs:t.response&&t.response.jobs};return e&&e(o,t)})},getJob:function(e,o){$assert(e>0,"Jobs.getJob: Invalid job execution id (must be > 0).");var s=this;u8.request("JobsChannel.GetJob",{executionId:e},function(e,t){t=t.error?t:{job:t.response&&t.response.job};return o&&o(s,t)})},dequeueJob:function(e,o){var s=this;u8.request("JobsChannel.DequeueJob",{executionId:e},function(e,t){t=t.error?t:{job:t.response&&t.response.job};return o&&o(s,t)})},setAdapter:function(e){this._assertJobs();for(var o in this.polledJobs){this.polledJobs[o].setAdapter(e)}},addPolledJob:function(e,o){$assert(typeof e!=="undefined"&&e!==null,"Can not add job for polling without an id.");$assert(typeof o!=="undefined"&&e!==null,"Can not add null or undefined as job for polling.");this.polledJobs[e]=o;o.create({parent:this})},removePolledJob:function(e){$assert(typeof e!=="undefined"&&e!==null,"Can not remove job from polling without an id.");delete this.polledJobs[e]},startPolling:function(){this.stopPolling();this._assertJobs();var e=u8.session.webConfigValues.getInt("Jobs.SessionCheckInterval",5);this._pollTimerId=setInterval(this._onTick.bind(this),e*1e3)},stopPolling:function(){if(typeof this._pollTimerId!="undefined"){clearInterval(this._pollTimerId);delete this._pollTimerId}},poll:function(){this._assertJobs();this._poll({force:true})},_assertJobs:function(){if(this._isCreated){return}this._isCreated=true;for(var e in this.polledJobs){this.polledJobs[e].create({parent:this})}},_poll:function(o){o=$.extend(o,{jobNames:[]});for(var s in this.polledJobs){var t=this.polledJobs[s];if(!o.force&&!e(t.lastPoll,t.pollInterval)){continue}t.poll(o)}if(!o.jobNames.length){return}var n=o.jobNames;o.jobNames=n.join(",");u8.request("JobsChannel.Poll",{options:o},this._onPollComplete.bind(this,n),true)},_onTick:function(){if(this._isDisposed){return}this._poll()},_onPollComplete:function(e,o,s){var t=s.response&&s.response.jobs;if(!t){return}s={jobs:t};u8.services.events.raise("u8.services.jobs.onPoll",this,s);var n=new Date;for(var i=0;i<e.length;i++){var r=e[i];var l=this.polledJobs[r];if(l){l.lastPoll=n;l.update(t[r])}}},_onSessionLogout:function(){this.stopPolling()}});u8.Base.Jobs.JobStatus={queued:"Queued",executing:"Executing",complete:"Complete",failed:"Failed"};u8.services.add({name:"jobs",scope:"Login",ctor:u8.Base.Jobs});function e(e,o){if(o<0){return false}if(!e){return true}if(!o){return false}var s=(new Date).getTime();var t=e.getTime();var n=(s-t)/6e4;return n>=o}})();