
function FileProgress(file, targetID, serverData) {
    $('.swfupload-visible').hide();
    $('.swfupload-hidden').removeClass('swfupload-hidden');

    if (targetID == "file_progress" && !$('#file_edit').get(0)) {
        var $tmpl = $('#file_tmpl').clone();

        $tmpl
            .removeAttr('id', 'file_tmpl')
            .show();
        $('#file_progress_tmpl', $tmpl).attr('id', 'file_progress');

        modal.add(false, 'file_edit', false, { html:$tmpl, view:2 });
    }

	this.fileProgressID = file.id;
	this.serverData = serverData;

	this.fileProgressWrapper = document.getElementById(this.fileProgressID);
	if (!this.fileProgressWrapper) {
		$('#' + targetID).append('<div id="' + this.fileProgressID + '" class="swfupload-item"><div class="swfupload-progress"><span class="swfupload-file">' + file.name + '</span><span class="swfupload-cancel"><a class="png" href="#:close"></a></span><div class="swfupload-bar"><div style="width:0;"></div></div><span class="swfupload-proc"></span><span class="swfupload-status"></span></div></div>');

		this.fileProgressWrapper = $('#' + this.fileProgressID).get(0);
	}

	this.flashID = file.id.replace(/_[0-9]+$/, '');
	this.buttonID = targetID.replace('_progress', '');

}

FileProgress.prototype.setProgress = function (percentage) {
	$("span.swfupload-proc", this.fileProgressWrapper).html( percentage + '%' );
	$("div.swfupload-bar div", this.fileProgressWrapper).css({ width:percentage + '%' });
};

FileProgress.prototype.setComplete = function () {
	var self = this;

	if (this.serverData) {
		page.script(this.serverData, this.fileProgressID);
	} else {
		this.setError();
	}

	$('span.swfupload-cancel a', this.fileProgressWrapper).click(function(){
		self.disappear();
		return false;
	});
};

FileProgress.prototype.setError = function () {
	var self = this;

	this.setStatus( SWFUpload.msg['Server (IO) Error'] );

	$('div.swfupload-bar div', this.fileProgressWrapper).css({ width:0 });

	$('span.swfupload-cancel a', this.fileProgressWrapper).click(function(){
		self.disappear();
		return false;
	});
};

FileProgress.prototype.setCancelled = function () {
	var self = this;
	setTimeout(function () {
		self.disappear();
	}, 2000);
};

FileProgress.prototype.setStatus = function (status) {
	$("span.swfupload-status", this.fileProgressWrapper).html(status);
};

FileProgress.prototype.disappear = function () {
	var $flash = $('#' + this.flashID);

	if ($flash.hasClass('swfupload-hide')) {
		$(this.fileProgressWrapper).remove();
	} else {
		$(this.fileProgressWrapper).fadeOut('fast', function() { $(this).remove(); });
	}
};

