//loops through an array in segments
var threadedLoop = function(__array, __chunk) {
    var self = this;

    //holds the threaded work
    var thread = {
        work: null,
        wait: null,
        index: 0,
        total: __array.length,
        finished: false
    };

    //set the properties for the class
    this.collection = __array;
    this.finish = function() { };
    this.action = function() { throw "You must provide the action to do for each element"; };
    this.interval = 50;

    //set this to public so it can be changed
    var chunk = __chunk ? __chunk : parseInt(thread.total * .005);
    this.chunk = (chunk == NaN || chunk == 0) ? thread.total : chunk;

    //end the thread interval
    thread.clear = function() {
        window.clearInterval(thread.work);
        window.clearTimeout(thread.wait);
        thread.work = null;
        thread.wait = null;
    };

    //checks to run the finish method
    thread.end = function() {
        if (thread.finished) { return; }
        self.finish();
        thread.finished = true;
    };

    //set the function that handles the work
    thread.process = function() {      
        if (thread.index >= thread.total) { return false; }

        //thread, do a chunk of the work
        if (thread.work) {
            var part = Math.min((thread.index + self.chunk), thread.total);
            while (thread.index < part) {
                thread.index++;
                self.action(self.collection[thread.index - 1], thread.index - 1, thread.total);
            }
        }
        else {
                  
            //no thread, just finish the work
            while(thread.index++ < thread.total) {
                self.action(self.collection[thread.index], thread.index, thread.total);
            }
        }

        //check for the end of the thread
        if (thread.index >= thread.total) {
            thread.clear();
            thread.end();
        }

        //return the process took place
        return true;

    };

    //set the working process
    self.start = function() {
        thread.finished = false;
        thread.index = 0;
        thread.work = window.setInterval(thread.process, self.interval);
    };

    //stop threading and finish the work
    self.wait = function(timeout) {

        //create the waiting function
        var complete = function() {
            thread.clear();
            thread.process();
            thread.end();
        };

        //if there is no time, just run it now
        if (!timeout) {
            complete();
        }
        else {
            thread.wait = window.setTimeout(complete, timeout);
        }
    };

};

