/* Demo Note:  This demo uses a FileProgress class that handles the UI for displaying the file name and percent complete.
The FileProgress class is not part of SWFUpload.
*/

/* **********************
   Event Handlers
   These are my custom event handlers to make my
   web application behave the way I went when SWFUpload
   completes different tasks.  These aren't part of the SWFUpload
   package.  They are part of my application.  Without these none
   of the actions SWFUpload makes will show up in my application.
   ********************** */

var totalFilesSize = 0;
var currentBytesLoaded = new Array();
  
function fileQueued(file) {
	try {
		totalFilesSize += file.size;
	} catch (ex) {
		this.debug(ex);
	}

}

function fileQueueError(file, errorCode, message) {
	try {
		if (errorCode === SWFUpload.QUEUE_ERROR.QUEUE_LIMIT_EXCEEDED) {
			alert("Próbujesz wysłać zbyt wiele plików.\n" + (message === 0 ? "You have reached the upload limit." : "Możesz wybrać " + (message > 1 ? "up to " + message + " plików." : "jeden plik.")));
			return;
		}

		//var progress = new FileProgress(file, this.customSettings.progressTarget);
		//progress.setError();
		//progress.toggleCancel(false);

		switch (errorCode) {
		case SWFUpload.QUEUE_ERROR.FILE_EXCEEDS_SIZE_LIMIT:
			alert("File is too big.");
			this.debug("Error Code: File too big, File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
			break;
		case SWFUpload.QUEUE_ERROR.ZERO_BYTE_FILE:
			alert("Cannot upload Zero Byte files.");
			this.debug("Error Code: Zero byte file, File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
			break;
		case SWFUpload.QUEUE_ERROR.INVALID_FILETYPE:
			alert("Invalid File Type.");
			this.debug("Error Code: Invalid File Type, File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
			break;
		default:
			if (file !== null) {
				alert("Unhandled Error");
			}
			this.debug("Error Code: " + errorCode + ", File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
			break;
		}
		
	} catch (ex) {
        this.debug(ex);
    }
}

function fileDialogComplete(numFilesSelected, numFilesQueued) {
	try {
		if (numFilesSelected > 0) {
			//document.getElementById(this.customSettings.cancelButtonId).disabled = false;

			/* I want auto start the upload and I can do that here */
			
			/*
			currentBytesLoaded = new Array();
			
			var url =  Browser.getFrame("FilesView").location.href;
			var id = Browser.getFrame('FoldersView').TreeMenu.ActiveNodeId();
		
			if(!Browser.Get('id'))
			{
				url = url + '&id=' + id;
			}

			this.setUploadURL(url + '&action=uploadFile')

			Browser.ProgressBar.Show();
			*/
			//currentBytesLoaded = new Array();
			//showProgressBar('', "", "swfu.cancelQueue()", true);
			
			this.startUpload();
		}
	} catch (ex)  {
        this.debug(ex);
	}
}

function uploadStart(file) {
	try {
		/* I don't want to do any file validation or anything,  I'll just update the UI and
		return true to indicate that the upload should start.
		It's important to update the UI here because in Linux no uploadProgress events are called. The best
		we can do is say we are uploading.
		 */
		 
		//var progress = new FileProgress(file, this.customSettings.progressTarget);
		//alert("Uploading...");
		//progress.toggleCancel(true, this);
		//totalFilesSize = 0;
		$("#progres_bar_holder").show();
	}
	catch (ex) {}
	
	return true;
}

function uploadProgress(file, bytesLoaded, bytesTotal) {
	try {
		
		currentBytesLoaded[file.index] = bytesLoaded;
		
		var BytesLoadedSum = 0;
		for ( var i = 0; i < currentBytesLoaded.length; i++)
		{
			BytesLoadedSum += currentBytesLoaded[i];
		}

		var progress = Math.round(100 * BytesLoadedSum / totalFilesSize);
		//alert(BytesLoadedSum+" "+totalFilesSize);
		
		setProgressValue(progress);
	} catch (ex) {
		this.debug(ex);
	}
}

function uploadSuccess(file, serverData) {
	try {
		//alert(file);
		
		//$('#foto_success_upload_holder').append('<img style="display:none" src="'+serverData+'" />').hide().fadeIn("slow");
		//$('#foto_success_upload_holder').append('<img style="display:none" src="'+serverData+'" />').children(':last').hide().fadeIn("slow");
		$('#foto_success_upload_holder').append(  serverData );
		
	
	} catch (ex) {
		this.debug(ex);
	}
}

function uploadError(file, errorCode, message) {
	try {
		//var progress = new FileProgress(file, this.customSettings.progressTarget);
		//progress.setError();
		
		//hideMessageBox();

		switch (errorCode) {
		case SWFUpload.UPLOAD_ERROR.HTTP_ERROR:
			alert("Upload Error: " + message);
			this.debug("Error Code: HTTP Error, File name: " + file.name + ", Message: " + message);
			break;
		case SWFUpload.UPLOAD_ERROR.UPLOAD_FAILED:
			alert("Upload Failed.");
			this.debug("Error Code: Upload Failed, File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
			break;
		case SWFUpload.UPLOAD_ERROR.IO_ERROR:
			alert("Server (IO) Error");
			this.debug("Error Code: IO Error, File name: " + file.name + ", Message: " + message);
			break;
		case SWFUpload.UPLOAD_ERROR.SECURITY_ERROR:
			alert("Security Error");
			this.debug("Error Code: Security Error, File name: " + file.name + ", Message: " + message);
			break;
		case SWFUpload.UPLOAD_ERROR.UPLOAD_LIMIT_EXCEEDED:
			alert("Limit uploadu został wykorzystany.");
			this.debug("Error Code: Upload Limit Exceeded, File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
			break;
		case SWFUpload.UPLOAD_ERROR.FILE_VALIDATION_FAILED:
			alert("Failed Validation.  Upload skipped.");
			this.debug("Error Code: File Validation Failed, File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
			break;
		case SWFUpload.UPLOAD_ERROR.FILE_CANCELLED:
			// If there aren't any files left (they were all cancelled) disable the cancel button
			if (this.getStats().files_queued === 0) {
				//document.getElementById(this.customSettings.cancelButtonId).disabled = true;
			}
			//alert("Cancelled");
			//progress.setCancelled();
			break;
		case SWFUpload.UPLOAD_ERROR.UPLOAD_STOPPED:
			//alert("Stopped");
			break;
		default:
			alert("Unhandled Error: " + errorCode);
			this.debug("Error Code: " + errorCode + ", File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
			break;
		}
	} catch (ex) {
        this.debug(ex);
    }
}

function uploadComplete(file) {
	if (this.getStats().files_queued === 0) {
		//document.getElementById(this.customSettings.cancelButtonId).disabled = true;
	}
}

// This event comes from the Queue Plugin
function queueComplete(numFilesUploaded) {
	
	//Browser.ProgressBar.Hide();
	//Browser.Toolbar.ClearSearch();
	//Browser.Toolbar.Refresh();
//	hideMessageBox();
	reloadAfterUpload();
	//alert(totalFilesSize);
	for ( var i = 0; i < currentBytesLoaded.length; i++)
	{
		currentBytesLoaded[i] = 0;
	}

	totalFilesSize = 0;
	
	//$("#progres_bar").width("0px");
	$("#progres_bar").css("width", "1px");
	
//	$('#inner_content').jScrollPane();
	$("#progres_bar_holder").hide();
}
function setProgressValue(progress) {
	//$("#progres_bar_holder").show();
	$("#progres_bar").width(progress+"%");
	//$("#progres_bar").css("width", progress+"%");
}

