var uploads_in_progress = 0;

function beginAsyncUpload(ul, sid) {
    ul.form.submit();
    uploads_in_progress = uploads_in_progress + 1;
    var pb = document.getElementById(ul.name + '_progress');
    pb.parentNode.style.display = '';
    var uploaddiv = document.getElementById(ul.name + '_div');
    uploaddiv.style.display = 'none';
    var sc = document.getElementById(ul.name + '_success');
    new ProgressTracker(sid,{
	url: '/include/upload/fileprogress.php',
	progressBar: pb,
    	onComplete: function() {
    	    var inp_id = pb.id.replace('_progress', '');
    	    uploads_in_progress = uploads_in_progress - 1;
    	    var inp = document.getElementById(inp_id);
	    if (inp) {
		inp.value = sid;
    	    }
	    pb.parentNode.style.display = 'none';
	    sc.style.display = '';
    	},
    	onFailure: function(msg) {
    	    pb.parentNode.style.display='none';
    	    alert(msg);
    	    uploads_in_progress = uploads_in_progress - 1;
    	}
    });
}
    
function PeriodicalAjax(url, parameters, frequency, decay, onSuccess, onFailure) {
    function createRequestObject() {
	var xhr;
	try {
	    xhr = new XMLHttpRequest();
	}
	catch (e) {
	    xhr = new ActiveXObject("Microsoft.XMLHTTP");
	}
	return xhr;
    }
	
    function send() {
	if(!stopped) {
	    xhr.open('post', url, true);
	    xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	    xhr.onreadystatechange = function() { self.onComplete(); };
	    xhr.send(parameters);
	}
    }
	
    this.stop = function() {
	stopped = true;
	clearTimeout(this.timer);
    }

    this.start = function() {
	stopped = false;
	this.onTimerEvent();
    }
	
    this.onComplete = function() {
	if(this.stopped) return false;
	if (xhr.readyState == 4) {
	    if(xhr.status == 200) {
		if(xhr.responseText == lastResponse) {
		    decay = decay * originalDecay;
		} else {
		    decay = 1;
		}
		lastResponse = xhr.responseText;
		if(onSuccess instanceof Function) {
		    onSuccess(xhr);
		}
		this.timer = setTimeout(function() { self.onTimerEvent(); }, decay * frequency * 1000);
	    } else {
		if(onFailure instanceof Function) {
		    onFailure(xhr);
		}
	    }
	}
    }
	
    this.getResponse = function() {
	if(xhr.responseText) {
	    return xhr.responseText;
	}
    }
	
    this.onTimerEvent = function() {
	send();
    }
	
    var self = this;
    var stopped = false;
    var originalDecay = decay || 1.2;
    decay = originalDecay;
    var xhr = createRequestObject();
    var lastResponse = "";
    this.start();
}

function ProgressTracker(sid, options) {

    this.onSuccess = function(xhr) {
	if(parseInt(xhr.responseText) >= 100) {
	    periodicalAjax.stop();
	    if(options.onComplete instanceof Function) {
		options.onComplete();
	    }
	} else if(xhr.responseText && xhr.responseText != lastResponse) {
	    if(options.onProgressChange instanceof Function) {
		options.onProgressChange(xhr.responseText);
	    }
	    if(options.progressBar && options.progressBar.style) {
		options.progressBar.style.width = parseInt(xhr.responseText) + "%";
	    }
	}
    }
	
    this.onFailure = function(xhr) {
	if(options.onFailure instanceof Function) {
	    options.onFailure(xhr.responseText);
	} else {
	    alert(xhr.responseText);
	}
	periodicalAjax.stop();
    }

    var self = this;
    var lastResponse = -1;
    options = options || {};
    var url = options.url || 'fileprogress.php';
    var frequency = options.frequency || 0.5;
    var decay = options.decay || 2;
    var periodicalAjax = new PeriodicalAjax(url, 'sid=' + sid, frequency, decay, function(request){self.onSuccess(request);},function(request){self.onFailure(request);});
}

