jQuery.fn.jqUploader = function(options) {
    return this.each(function(index) {
        var $this = jQuery(this);
				// fetch label value if any, otherwise set a default one
				var $thisForm =  $this.parents("form");
				var $thisInput = $("input[@type='file']",$this);
				var $thisLabel = $("label",$this);
				var containerId = $this.attr("id") || 'jqUploader-'+index;
				var startMessage = ($thisLabel.text() =='') ? 'Please select a file' : $thisLabel.text();
				// get form action attribute value as upload script, appending to it a variable telling the script that this is an upload only functionality
				var actionURL = $thisForm.attr("action");
				// adds a var setting jqUploader to 1, so you can use it for serverside processing
				var prepender = (actionURL.lastIndexOf("?") != -1) ? "&": "?";
				actionURL = actionURL+prepender+'jqUploader=1';
				// check if max file size is set in html form
				var maxFileSize = $("input[@name='MAX_FILE_SIZE']", $(this.form)).val();
				var opts = jQuery.extend({
            width:            320,
            height:           85,
            version:          8, // version 8+ of flash player required to run jqUploader
            background:       'FFFFFF', // background color of flash file
            src:              'jqUploader.swf',
						uploadScript:     actionURL,
						afterScript:      '', // if this is empty, jqUploader will replace the upload swf by a hidden input element
						varName:	        $thisInput.attr("name"),  //this holds the variable name of the file input field in your html form
						allowedExt:	      '*.jpg; *.jpeg; *.png', // allowed extensions
						allowedExtDescr:  'Images (*.jpg; *.jpeg; *.png)',
						params:           {},
            flashvars:        {},
            hideSubmit:       true,
						barColor:		      '0000CC',
						maxFileSize:      maxFileSize,
						startMessage:     startMessage,
						errorSizeMessage: 'File is too big!',
						validFileMessage: 'Click Upload to proceed',
						progressMessage: 'Please wait, uploading ',
						endMessage:    'Upload completed'
						}, options || {}
				);
				// disable form submit button
				if (opts.hideSubmit==true) {
					$("#audioSubmit",this.form).hide();
					$("#docSubmit",this.form).hide();
					$("vidSubmit",this.form).hide();
				}
				
				// THIS WILL BE EXECUTED IN THE USECASE THAT THERE IS NO REDIRECTION TO BE DONE AFTER UPLOAD
				TerminateJQUploader = function(containerId,filename,varname, hideSubmit, endMessage){
					$this= $('#'+containerId).empty();
					$this.text('').append('<p class="small">Upload of <strong>'+filename+'</strong> is complete! You can now enter an accompanying announcenment below.</p><p class="small important"><strong>Important:</strong> to post this file, you must submit this form using the button located beneath the announcement field.</p><input name="'+varname+'" type="hidden" id="'+varname+'" value="'+filename+'"/>');
					var myForm = $this.parents("form");
					myForm.submit(function(){return true});
					$("*[@type='submit']",myForm).show();
				}
		
				//TODO : ADD PARAMS THE JQUERY_FLASH PLUGIN WAY
				
				var myParams = '';
				for (var p in opts.params){
						myParams += p+'='+opts.params[p]+',';
				}
				myParams = myParams.substring(0, myParams.length-1);
				// this functions interfaces with the jquery flash plugin 
				function jqUploader_replace(htmlOptions) {
							 var $el = $('<div id="'+containerId+'" class="flash-replaced"><div class="alt">'+this.innerHTML+'</div></div>');
							 $el.prepend($.fn.flash.transform(htmlOptions));
							 $('div.alt',$el).hide();
							 jQuery(this).after($el).remove();
				};
				//$this.after($el).remove();
				jQuery(this).flash({
											src: opts.src,
											width: opts.width,
											height: opts.height,
											id:'movie_player-'+index,
											bgcolor:'#'+opts.background,
											flashvars: {
												containerId: containerId, 
												uploadScript: opts.uploadScript,
												afterScript: opts.afterScript,
												allowedExt: opts.allowedExt,
												allowedExtDescr: opts.allowedExtDescr,
												varName :  opts.varName,
												barColor : opts.barColor,
												maxFileSize :opts.maxFileSize,
												startMessage : opts.startMessage,
												errorSizeMessage : opts.errorSizeMessage,
												validFileMessage : opts.validFileMessage,
												progressMessage : opts.progressMessage,
												endMessage: opts.endMessage
											},
											params: myParams
										},
										{
											version: opts.version,
											update: false
										},
										jqUploader_replace
					);
		});
};
		
