//speed in seconds?
function start_progress_bar(speed) {

	speed = speed ? speed : 60;
	
	speed *= 2/200;
	
    var bar = new ProgressBar("bar", "flush");
    bar.speed = speed;
    bar.width = 200;
    bar.height = 15;
    bar.start();
    
    return bar;
}

function ProgressBar(id, flush) {
        
        this.id = id;
        var self = this;
        
        this.speed = null;
        this.width = null;
        this.height = null;
        
        this.x = 0;
        this.timerId = null;
        this.end = false;

        this.start = function() {
            if (self.end) {
                self.stop();
                self.end = false;
            }
            if (self.timerId) {
                self.stop();
            }
            self.timerId = setTimeout(self.timer, parseInt(self.speed * 1000));
        }

        this.pause = function() {
            if (self.timerId) {
                clearTimeout(self.timerId);
                self.timerId = null;
            }
        }

        this.stop = function() {
            self.pause();
            self.x = 0;
            document.getElementById(self.id).style.clip = "rect(0px "+self.x+"px "+self.height+"px 0px)";
        }

        this.timer = function() {
            if (self.x >= self.width) {
                self.pause();
                self.end = true;
                //self.start();
                return;
            }
			if(flush != 0) {
				appendLayer(flush, '<!-- .........flushing......... -->');
			}
            self.x += parseInt(self.width / 100);
            document.getElementById(self.id).style.clip = "rect(0px "+self.x+"px "+self.height+"px 0px)";
            self.timerId = setTimeout(self.timer, parseInt(self.speed * 1000));
        }

}

