// these variables are just for this example
var actionElement;
var progressElement;

// html element initializzation
function elementsInit() {
	if(document.getElementById) {
		actionElement = document.getElementById('action');
		progressElement = document.getElementById('progress');
	}
	else {
		actionElement = document.all.action;
		progressElement = document.all.progress;
	}
}

// function to write what's up
function trace(what) {
	if(actionElement.innerHTML != what)
		actionElement.innerHTML = what;
}

// function to emulate a progress bar
function bar(p) {
	var newWidth = Math.floor(p * 300 / 100) + 'px';
	if(progressElement.style.width != newWidth)
		progressElement.style.width = newWidth;
}

// listener as FileReferenceList Object listener
var listener = new Object();
		
// invoked on cancel when upload dialog is open
listener.onCancel = function(file) {
	actionElement.innerHTML = 'onCancel';
}

// invoked when a user chose a file
listener.onSelect = function(file) {
	var b = file.fileList.length;
	var str = 'onSelect: selected ' + b + ' files'
	+ '<br /><span style="font-weight: normal; color: #565656; font-size: 7pt;"><br />';
	for(var a = 0; a < b; a++) {
		var fl = file.fileList[a];
		str += fl.name + ' &nbsp; [' + fl.size + ' bytes]<br />';
	}
	str += '</span>';
	actionElement.innerHTML = str;
	
	// just an upload test of first selected file
	// remember that as Flash 8 does, upload should be always one file
	// each time, then use a better method to implement multiple uploads
	// ( something like an asyncronous upload method )
	if(file.fileList.length > 0) {
		// listener as FileReference Object listener ... same methods
		// same returned parameters, isn't cool ? :-)
		var FileListener = new Object();
		
		// where is you uploaad server side script ?
		FileListener.serverfile = 'FileReference.php';
		
		// usefull one4allErrors function, created by myself
		FileListener.onError = function(file, errorString) {
			trace("Error with: " + file.name + "<br />Type: " + errorString);
		}
		
		// common error functions
		FileListener.onHTTPError =
		FileListener.onIOError =
		FileListener.onSecurityError = function(file, errorString) {
			if(errorString == undefined)
				errorString = 'HTTP or Input Output Error';
			this.onError(file, errorString);
		}
		
		// invoked on cancel when upload dialog is open
		FileListener.onCancel = function(file) {
			trace('onCancel');
		}
		
		// invoked when file is starting to be uploaded / downloaded
		FileListener.onOpen = function(file) {
			progressElement.style.display = 'block';
			trace('onOpen: ' + file.name);
		}
		
		// ... while file is uploaded / downloaded
		FileListener.onProgress = function(file, bytesLoaded, bytesTotal) {
			var p = new Number(Math.floor(bytesLoaded / bytesTotal * 100));
			trace('onProgress: ' + p + '%');
			bar(p);
		}
		
		// invoked when file is uploaded / downloaded
		FileListener.onComplete = function(file) {
			document.getElementById('progress').style.display = 'none';
			trace('onComplete: ' + file.name);
		}
		
		// add listener to file
		file.fileList[0].addListener(FileListener);
		
		// now onSelect is not defined because file was just selected from List
		// ... then just check size and try to upload
		var maxSize = 204800;
		if(file.fileList[0].size > maxSize)
			trace('File is too big for a test ... max size is ' + maxSize + ' bytes<br />your file is: ' + file.fileList[0].size + ' bytes.');
		else if(!file.fileList[0].upload(FileListener.serverfile))
			trace('Upload dialog failed to open.');
	}
}

// FileReferenceList class initializzation
var fileRef = new FileReferenceList();

// adding a listener
fileRef.addListener(listener);

// filetypes variable example
// this file must be an array with valid objects inside.
// each object must have a description and an extension parameter
// ....look here!
var filetype = [
	// first chose, some images types
	{description : "Images (*.jpg, *.jpeg, *.gif, *.png)",
	// ... and their extensions
	extension : "*.jpg; *.jpeg; *.gif; *.png"},
	
	// some text files
	{description : "Text Files (*.txt, *.rtf)",
	extension : "*.txt; *.rtf"},
	
	// a specific file
	{description : "Specific File (test.swf)",
	extension : "test.swf"},
	
	// and All Files as last choice
	{description : "All Files (*.*)",
	extension : "*.*"}
];
