(function(){return function(factory){if(typeof define==="function"&&define.amd){return define("background",factory)}else{return factory.call(this)}}(function(){var Background,legacyToLatestTask,root,__hasProp={}.hasOwnProperty,__extends=function(child,parent){for(var key in parent){if(__hasProp.call(parent,key))child[key]=parent[key]}function ctor(){this.constructor=child}ctor.prototype=parent.prototype;child.prototype=new ctor;child.__super__=parent.prototype;return child};root=this;Background=this.Background=typeof exports!=="undefined"?exports:{};Background.VERSION="0.3.3";legacyToLatestTask=function(){var functions;functions={};if(arguments.length>0){functions.start=arguments[0]}if(arguments.length>1){functions.tick=arguments[1]}if(arguments.length>2){functions.finish=arguments[2]}return functions};Background._JobContainer=function(){function _JobContainer(frequency){this.frequency=frequency;this.jobs=[];this.interval=0;this.in_destroy=false}_JobContainer.prototype.destroy=function(){return this.in_destroy=true};_JobContainer.prototype.isDestroyed=function(){return this.in_destroy||this.destroyed};_JobContainer.prototype.isEmpty=function(){return this.jobs.length===0};_JobContainer.prototype.tick=function(){if(this.in_destroy){if(!this.is_destroyed){this._doDestroy()}return}this._doTick();if(this.in_destroy){if(!this.is_destroyed){this._doDestroy()}}};_JobContainer.prototype.clear=function(){var job;while(job=this.jobs.shift()){job.destroy(true)}if(this.interval){root.clearInterval(this.interval);return this.interval=null}};_JobContainer.prototype._appendJob=function(functions){var job,_this=this;if(arguments.length>1){functions=legacyToLatestTask.apply(null,arguments)}if(this.isDestroyed()){throw"Trying to append a job to a destroyed container"}if(functions instanceof Background.Job){job=functions}else{job=new Background.Job(functions)}this.jobs.push(job);if(!this.interval){return this.interval=root.setInterval(function(){return _this.tick()},this.frequency)}};_JobContainer.prototype._waitForJobs=function(){if(this.interval){root.clearInterval(this.interval);return this.interval=null}};_JobContainer.prototype._doDestroy=function(){if(!this.in_destroy||this.is_destroyed){throw"Destroy state is corrupted"}this.is_destroyed=true;return this.clear()};return _JobContainer}();Background._ArrayIterator=function(){function _ArrayIterator(batch_length,total_count,current_range){this.batch_length=batch_length;this.total_count=total_count;this.current_range=current_range;if(!this.batch_length||this.total_count===void 0||!this.current_range){throw"Iterator parameters invalid"}this.reset()}_ArrayIterator.prototype.reset=function(){this.batch_index=-1;return this.batch_count=Math.ceil(this.total_count/this.batch_length)};_ArrayIterator.prototype.isDone=function(){return this.batch_index>=this.batch_count-1};_ArrayIterator.prototype.updateCurrentRange=function(){var excluded_boundary,index;index=this.batch_index*this.batch_length;excluded_boundary=index+this.batch_length;if(excluded_boundary>this.total_count){excluded_boundary=this.total_count}if(index>=excluded_boundary){return this.current_range._setIsDone()}this.current_range._addBatchLength(excluded_boundary-index);return this.current_range};_ArrayIterator.prototype.step=function(){if(this.isDone()){return this.current_range._setIsDone()}this.batch_index++;if(this.batch_index===0){return this.current_range}else{return this.updateCurrentRange()}};return _ArrayIterator}();Background.Job=function(){function Job(functions){var key,value;if(arguments.length>1){functions=legacyToLatestTask.apply(null,arguments)}if(!functions){throw"Missing task functions"}if(typeof functions==="function"){this.functions={tick:functions}}else{this.functions={};for(key in functions){value=functions[key];this.functions[key]=value}}this.was_completed=false}Job.prototype.destroy=function(){this._cleanup();return this.functions=null};Job.prototype.run=function(){if(this.functions.start){this.functions.start(this);this.functions.start=null}this.was_completed=this.functions.tick?this.functions.tick(this):true;if(this.was_completed){this._cleanup()}return this.was_completed};Job.prototype._cleanup=function(){if(this.functions.finish){this.functions.finish(this.was_completed,this);return this.functions.finish=null}};return Job}();Background.JobQueue=function(_super){__extends(JobQueue,_super);function JobQueue(frequency){JobQueue.__super__.constructor.call(this,frequency);this.current_job=null}JobQueue.prototype._doTick=function(){if(!this.current_job){if(!this.jobs.length){this._waitForJobs();return}this.current_job=this.jobs.shift()}if(this.current_job.run()){this.current_job.destroy(false);return this.current_job=null}};JobQueue.prototype.push=function(functions){return this._appendJob.apply(this,arguments)};JobQueue.prototype._doDestroy=function(){if(this.current_job){this.current_job.destroy(true);this.current_job=null}return JobQueue.__super__._doDestroy.call(this)};return JobQueue}(Background._JobContainer);Background.JobList=function(_super){__extends(JobList,_super);function JobList(frequency){JobList.__super__.constructor.call(this,frequency)}JobList.prototype._doTick=function(){var job,jobs,_i,_len;if(!this.jobs.length){this._waitForJobs();return}jobs=this.jobs.slice();for(_i=0,_len=jobs.length;_i<_len;_i++){job=jobs[_i];if(!job.run()){continue}this.jobs.splice(this.jobs.indexOf(job),1);job.destroy(false)}};JobList.prototype.append=function(functions){return this._appendJob.apply(this,arguments)};return JobList}(Background._JobContainer);Background.Range=function(){function Range(index,excluded_boundary){this.index=index;this.excluded_boundary=excluded_boundary;if(this.index===void 0||!this.excluded_boundary){throw"Background.Range: parameters invalid"}return this}Range.prototype.isDone=function(){return this.index>=this.excluded_boundary};Range.prototype.step=function(){this.index++;if(this.index>=this.excluded_boundary){return-1}else{return this.index}};Range.prototype.getItem=function(array){return array[this.index]};Range.prototype.getSlice=function(array){return array.slice(this.index,this.excluded_boundary)};Range.prototype._setIsDone=function(){this.index=-1;this.excluded_boundary=-1;return this};Range.prototype._addBatchLength=function(batch_length){if(!batch_length){throw"Background.Range._addBatchLength: batch_length invalid"}this.excluded_boundary+=batch_length;return this};Range.prototype.reset=function(){this.index=0;return this};Range.prototype._stepToEnd=function(){return this.index=this.excluded_boundary};return Range}();Background.Range_xN=function(){function Range_xN(ranges,batch_length){this.ranges=ranges;this.batch_length=batch_length;if(!this.ranges||!this.batch_length){throw"Background.Range_xN: parameters invalid"}this.batch_index=0;return this}Range_xN.prototype.isDone=function(){return this.batch_index>=this.batch_length};Range_xN.prototype.step=function(){var current_range,index;this.batch_index++;index=this.ranges.length-1;while(index>=0){current_range=this.ranges[index];current_range.step();if(!current_range.isDone()){return this}current_range.reset();index--}this._setIsDone();return null};Range_xN.prototype.getItems=function(arrays){var array,index,items;items=[];for(index in arrays){array=arrays[index];items.push(array[this.ranges[index].index])}return items};Range_xN.prototype.getCombinations=function(arrays){var combination,combinations,index,range,_ref;combinations=[];while(!this.isDone()){combination=[];_ref=this.ranges;for(index in _ref){range=_ref[index];combination.push(range.getItem(arrays[index]))}combinations.push(combination);this.step()}return combinations};Range_xN.prototype._setIsDone=function(){this.batch_index=-1;this.batch_length=-1;return this};Range_xN.prototype._addBatchLength=function(batch_length){if(!batch_length){throw"Background.Range_xN._addBatchLength: batch_length invalid"}this.batch_index=0;this.batch_length=batch_length;return this};return Range_xN}();Background.ArrayIterator=function(_super){__extends(ArrayIterator,_super);function ArrayIterator(array,batch_length){var excluded_boundary;this.array=array;if(!this.array){throw"Background.ArrayIterator: missing array"}this.reset();excluded_boundary=batch_length