var requestInterval = function (fn, delay) {
    if (!self.requestAnimationFrame &&
        !self.webkitRequestAnimationFrame &&
        !(self.mozRequestAnimationFrame && self.mozCancelRequestAnimationFrame) && // Firefox 5 ships without cancel support
        !self.oRequestAnimationFrame &&
        !self.msRequestAnimationFrame)
        return self.setInterval(fn, delay);

    var start = new Date().getTime(),
        handle = new Object();

    function loop() {
        var current = new Date().getTime(),
            delta = current - start;

        if (delta >= delay) {
            fn.call();
            start = new Date().getTime();
        }

        handle.value = self.requestAnimationFrame(loop);
    };

    handle.value = self.requestAnimationFrame(loop);
    return handle;
}
var clearRequestInterval = function (handle) {
    self.cancelAnimationFrame ? self.cancelAnimationFrame(handle.value) :
        self.webkitCancelAnimationFrame ? self.webkitCancelAnimationFrame(handle.value) :
            self.webkitCancelRequestAnimationFrame ? self.webkitCancelRequestAnimationFrame(handle.value) : /* Support for legacy API */
                self.mozCancelRequestAnimationFrame ? self.mozCancelRequestAnimationFrame(handle.value) :
                    self.oCancelRequestAnimationFrame ? self.oCancelRequestAnimationFrame(handle.value) :
                        self.msCancelRequestAnimationFrame ? self.msCancelRequestAnimationFrame(handle.value) :
                            clearInterval(handle);
};


var lpPop;
var request;
var count = 0;
var f = function (e) {
    if (e.data != null) {
        if (e.data.status == "StartUpload") {
            request = e.data.totalF;
        }
        else if (e.data.status == "Uploaded")
        {
            var b = e.data.bucket;
            var flag = true;
            for (var i = 0; i < b.length; i++) {
                if (b[i] == 0) {
                    flag = false;
                }
            }
            if (flag) {
                count += b.length;
                if (count == request) {
                    self.postMessage("Uploaded");
                    lpPop = requestInterval(function() {
                        self.postMessage("Tick");
                    }, 500);
                } else {
                    self.postMessage("Upload");
                }
            }
        }
        else if (e.data.status == "NoItem")
        {
            clearRequestInterval(lpPop);
            self.postMessage("Finish");
        }
    }
}
self.addEventListener("message", f, false);

