jQuery.fn.fileUpload = function(options) {
    return this.each(function(index) {
        var $this = $(this);
		var $tForm 	= $this.parents("form");
		var $tInput = $("input[type='file']",$this);
		var contId 	= $this.attr("id") || 'fileUpload-'+index;
		var formUrl = $tForm.attr("action");
		var pre 	= (formUrl.lastIndexOf("?") != -1) ? "&": "?";

		var opts 	= jQuery.extend(
			{
				width:				385,
				height:				186,
				version: 			9, 		
				background: 		'F4F4F4',
				src:    			'fileUpload/FlashFileUpload.swf',
				fileDone:			function(file){}, 
				callback:			function(files){}, 
				uploadScript:    	'/fileupload/flash_upload.php?uploadOnly=1',
				varName:	        $tInput.attr("name"),
				allowedExt:	      	"",
				params:           	{menu:false},
				flashvars:        	{},
				maxTotalSize:		0,
				maxFileSize:      	105801318 // 100.9MB
			},
			options || {}
		);
		var par = '';
		for (var i in opts.params){par += i+'='+opts.params[i]+',';}
		par = par.substring(0, par.length-1);
		// Functions called from flash uploader
		FileUploaded = function(fileJSON){
			opts.fileDone(fileJSON);
		};
		UploadsComplete = function(filesJSON){
			opts.callback(filesJSON);
		};
		jQuery(this).flash(
			{
				src:		opts.src,
				width:		opts.width,
				height:		opts.height,
				id:			'movie_player-'+index,
				bgcolor:	'#'+opts.background,
				flashvars: 	{
								completeFunction	: "UploadsComplete",
								fileCompleteFunction: "FileUploaded",
								fileTypes			: opts.allowedExt,
								fileTypeDescription	: opts.allowedExtDescr,
								totalUploadSize		: opts.maxTotalSize,
								fileSizeLimit		: opts.maxFileSize,
								uploadPage			: opts.uploadScript
							},
				params: 	par
			},
			{
				version:	opts.version,
				update:		false
			},
			function(htmlOptions){
				var $el = $('<div id="'+contId+'" class="flash-replaced"><div class="alt">'+this.innerHTML+'</div></div>');
					 $el.prepend($.fn.flash.transform(htmlOptions));
					 $('div.alt',$el).remove();
					 $(this).after($el).remove();
			}
		);
	});
};
