// ProBase callback functions for SWFUpload

var swfuploadQueued = PbLib.g('Queued');
var swfuploadUploading = PbLib.g('Uploading');
var swfuploadCancelled = PbLib.g('Cancelled');
var swfuploadUploadingFinished = PbLib.g('Done');

// Javascript component has loaded: hide the "no javascript" file upload html element
function swfuploadLoaded()
{
	$('degradedUI' + this.id).hide();
	$('flashUI' + this.id).show();

	$(this.id + '_use_js').value = '1';

	this.showEmptyRow();

	// flag using Flash
	window.top.uploadUsingFlash = true;

	this.setButtonAction(this.multiple ? SWFUpload.BUTTON_ACTION.SELECT_FILES : SWFUpload.BUTTON_ACTION.SELECT_FILE);
}

// File selection dialog is closed; start uploading immediately
function fileDialogComplete(filesSelected, filesQueued)
{
	this.startUpload();
	return true;
}

// If no file is present in the upload table, show an empty row
// It also affects the "disabled" status of the upload button
SWFUpload.prototype.showEmptyRow = function()
{
	$("uploadlist" + this.id).show();
	var uploadListTBody = $("uploadlist" + this.id).tBodies[0];
	var show = (uploadListTBody.childNodes.length == 1);
	uploadListTBody.childNodes[0].style.display = show ? '' : 'none';

	if (!this.multiple) {
		this.setButtonDisabled(uploadListTBody.childNodes.length > 1);
	}
}

// Returns all submit buttons (FormElementButton) in the current form
// Presumes that the buttons have class 'submit'
function getSubmitButtons(swfupload)
{
	var form = $(swfupload.id + '_use_js').form;
	return $(form).select('button.submit');
}

// Deactivates all submit buttons. This is done while files are uploading
// to assure that the form is not submitted
function deactivateSubmitButtons(swfupload)
{
	var submits = getSubmitButtons(swfupload);
	swfupload.oldSubmitButtonTexts = [];
	for (var i = 0; i < submits.length; i++) {
		var submit = submits[i];
		var title = submit.innerHTML;
		swfupload.oldSubmitButtonTexts[i] = title;
		submit.innerHTML = "<img src='" + PbLib.getNewURI('/ui/uibase/img/loader.gif') + "' width='16' height='16' /> " +
			title + ' (' + PbLib.g('Please wait for uploads ...') + ')';
		submit.disabled = true;
	}
}

// Activates all submit buttons (again)
function activateSubmitButtons(swfupload)
{
	var submits = getSubmitButtons(swfupload);
	for (var i = 0; i < submits.length; i++) {
		var submit = submits[i];
		submit.innerHTML = swfupload.oldSubmitButtonTexts[i];
		submit.disabled = false;
	}
}

// Removes a file that has been uploaded this session, before the form was last submitted unsuccessfully,
// but has not yet been stored in Library
SWFUpload.prototype.removeUploadMeta = function(formelementId, tempMetaFile)
{
	new Ajax.Request(PbLib.getNewURI('l/' + this.module + '/removeupload/meta/' + formelementId + '/' + tempMetaFile));
	$('row_' + tempMetaFile).remove();

	this.showEmptyRow();
}

// Removes a file that has been uploaded this session, since the form was last submitted
SWFUpload.prototype.removeUploadFilename = function(formelementId, fileId, filename)
{
	new Ajax.Request(PbLib.getNewURI('l/' + this.module + '/removeupload/filename/' + formelementId + '/' + filename));
	$('row_' + fileId).remove();

	this.showEmptyRow();
}

// Removes a document, a value of this formelement, that is stored in Library
SWFUpload.prototype.removeDocument = function(formelementId, docId)
{
	$(this.id + '_keep_' + docId).checked = false;
	$(this.id + '_doc_' + docId).remove();

	this.showEmptyRow(true);
}

fileQueued =  function(file, queuelength)
{
	var uploadlistTable = document.getElementById("uploadlist" + this.id);
	uploadlistTable.style.display = '';

	if (file.size > 1024 * 1024 * 1024) {
		var readable = file.size / (1024 * 1024 * 1024);
		var scale = 'GB';
	} else if (file.size > 1024 * 1024) {
		var readable = file.size / (1024 * 1024);
		var scale = 'MB';
	} else if (file.size > 1024) {
		var readable = file.size / 1024;
		var scale = 'kB';
	} else {
		var readable = file.size;
		var scale = 'B';
	}

	readable = Math.round(readable) + ' ' + scale;

	if (uploadlistTable && document.createElement) {
		var oRow = document.createElement('TR');
		oRow.id = 'row_' + file.id;

		var oCell = document.createElement('TD');
		oCell.innerHTML = file.name;
		oCell.style.height = '2em';
		oRow.appendChild(oCell);

		var oCell = document.createElement('TD');
		oCell.innerHTML = readable;
		oRow.appendChild(oCell);

		var oCell = document.createElement('TD');
		oCell.innerHTML = swfuploadQueued + "<span style='padding-left: 5px; display: none;' id='progress_" + file.id + "'></span>";
		oCell.id = 'status_' + file.id;
		oRow.appendChild(oCell);

		var oCell = document.createElement('TD');
		var removeImg = PbLib.getNewURI('ui/uibase/icons/16/remove.png');
		oCell.innerHTML = "<a id='deletebtn_" + file.id + "' class='cancelbtn' href='javascript:swfu" + this.id + ".cancelUpload(\"" + file.id + "\");'><img src='" + removeImg + "'></a>";
		oRow.appendChild(oCell);

		uploadlistTable.tBodies[0].appendChild(oRow);
	}

	this.showEmptyRow();
}

SWFUpload.prototype.uploadFileCancelled = function(file, queuelength)
{
	if (file) {
		$('row_' + file.id).remove();
	}
	this.showEmptyRow();
}

function uploadFileStart(file, position, queuelength)
{
	var cell = document.getElementById('status_' + file.id);
	cell.childNodes[0].nodeValue = swfuploadUploading;

	var progress = document.getElementById('progress_' + file.id);
	progress.style.display = '';

	deactivateSubmitButtons(this);
}

function uploadFileComplete(file) {
	if (file.filestatus == -4) {

		var cell = document.getElementById('status_' + file.id);
		cell.childNodes[0].nodeValue = swfuploadUploadingFinished;
		var elem = document.getElementById('status_' + file.id);
		elem.style.background = "";

		var elem = document.getElementById('progress_' + file.id);
		elem.style.display = 'none';

		var elem = document.getElementById('deletebtn_' + file.id);
		elem.href = 'javascript:swfu' + this.id + '.removeUploadFilename("' + this.id + '", "' + file.id + '", "' + file.name + '")';
	}

	activateSubmitButtons(this);

	this.startUpload();
}

function uploadProgress(file, bytesLoaded) {
	var progress = document.getElementById('status_' + file.id);
	var percent = Math.ceil((bytesLoaded / file.size) * 100);
	var imgProgress = PbLib.getNewURI('/ui/uibase/img/progressbar.png');
	var parentWidth = progress.offsetWidth;
	var offset = -400 + (percent / 100) * parentWidth;
	progress.style.background = "url(" + imgProgress + ") no-repeat " + offset + "px 0";

	var progress = document.getElementById('progress_' + file.id);
	progress.innerHTML = '(' + percent + '%)';
}

function uploadError(file, errorCode, message) {
	this.uploadFileCancelled(file);

	activateSubmitButtons(this);
}
