jQuery(document).ready(function() 
{	
	if ($(".wuQ").length)
	{
		/**
		 * Disables the link
		 */
		jQuery.fn.disableLink = function(fn) {
			return jQuery(this).blur().unbind('click').fadeTo("fast", 0.2, (fn ? fn : null));
		};

		//
		// Constants
		jQuery.uploader.swfURL = '../flash/jQuery.uploader.swf';
//		jQuery.uploader.backendScript = '../../backend.php';
		jQuery.uploader.backendScript = $("#sScriptSrc").attr('path');

		/**
		 * Removes the upload (manually) by clicking the eject button
		 */
		function uploadDelete(_link, _self) {
			var _id = _link.parent().parent().attr('id');

			//
			// Disable links to prevent user from clicking again which could screw up the interface
			jQuery('#' + _id + ' A.delete').disableLink();
			jQuery('#' + _id + ' A.start').disableLink();
			jQuery('#' + _id + ' A.cancel').disableLink();
			jQuery('#' + _id + ' A.retry').disableLink();

			//
			// Send the "remove" event to flash
			_self.remove(_id);
		}

		/**
		 * Starts the upload (manually) by clicking the play button
		 */
		function uploadStart(_link, _self) {
			_self.upload(_link.parent().parent().attr('id'), jQuery.uploader.backendScript);
		}

		/**
		 * Restores the element back to it's "not started" state.
		 */
		function uploadRestore(_elem, _self, _allowRemoveOrRetry) {
			//
			// Fix links
			jQuery('A.cancel', _elem).hide();
			if (_allowRemoveOrRetry) {
				jQuery('A.delete', _elem).fadeTo("fast", 1.0).click(function() { uploadDelete(jQuery(this), _self) });
				jQuery('A.retry', _elem).show();
			} else {
				jQuery('A.retry', _elem).show().disableLink();
			}

			//
			// Fix progressbar
			jQuery('DIV.wuICPB', _elem).hide();
			jQuery('div.wuICPBF', _elem).width(0).html(' 0% ');
			jQuery('DIV.wuICPT', _elem).hide();

			//
			// Animate the progressbar
			_elem.removeClass('active').animate({ height: 40 }, 200);
		}

		/**
		 * Document ready function
		 */
		jQuery(document).ready(function() {
				//
				// Step 1: Create fancy hover buttons
				jQuery('button:not(#btnBrowse)').hover(
														function() {
															$(this).css('background-position', '0px -30px');
														},
														function() {
									  						$(this).css('background-position', '0px 0px');
														}
													);

				//
				// Step 2: Add the uploader
				var oUpload = $('#uploaderOverlay').uploader({
															swfURL: jQuery.uploader.swfURL,
															buttonSkin: '/jQuery.uploader/img/demo1/buttonSkin.png',
															logging: "1"
														});

				//
				// Step 3: Overwrite the events (base events have allready been setup)
				jQuery.extend(oUpload.events, {

						/**
						 * SWF ready handler, used to bind the events the way YOU want.
						 */
						uploaderReady: function() {
								var _self = this;

								// Upload ALL files (therefore the empty '' at the beginning)
								jQuery('#btnUpload').click(function() { _self.upload('', jQuery.uploader.backendScript); });

								//
								// Upload ALL files (therefore the empty '' at the beginning)
								jQuery('#btnUpload').click(function() { _self.upload('', jQuery.uploader.backendScript); });

								//
								// Clear the entire queue (e.g. remove all files)
								jQuery('#btnCancel').click(function() { _self.remove(); });
							},

						/**
						 * File added handler
						 */
						fileAdded: function(args) {
								var _id = '';
								_id = args.fileData.id;

								var sHTML = '';
								sHTML+= '<div class="wuI" id="' + _id + '">';
								sHTML+= '<div class="wuIC">';
								sHTML+= '<a href="javascript://" class="delete" title="Delete upload">&nbsp;</a> ';
								sHTML+= '<a href="javascript://" class="start" title="Start upload">&nbsp;</a> ';
								sHTML+= '<a href="javascript://" class="cancel" title="Cancel upload">&nbsp;</a> ';
								sHTML+= '<a href="javascript://" class="retry" title="Retry upload">&nbsp;</a> ';
								sHTML+= '&nbsp;' + args.fileData.name + '<br />';
								sHTML+= '<div class="wuICPB"><div class="wuICPBF"> 0% </div></div>';
								sHTML+= '<div class="wuICPT"><span class="wuICBytesDone">0 bytes</span> of <span class="wuICBytesTotal">' + this.formatSize(args.fileData.size) + '</span> <span class="wuICSpeedTime">&nbsp;</span></div>';
								sHTML+= '</div></div>';
								var _elem = jQuery(sHTML);
								jQuery('.wuQ').append(_elem);

								// Add handler
								var _self = this;

								//
								// Triggers file removal
								jQuery('A.delete', _elem).click(function() { uploadDelete(jQuery(this), _self) });
								jQuery('A.start', _elem).click(function() { uploadStart(jQuery(this), _self) });
								jQuery('A.retry', _elem).click(function() { uploadStart(jQuery(this), _self) });
								jQuery('A.cancel', _elem).click(function() { _self.cancel(_id); });

								//
								// Prevent me from calling this in a later function ...
								_elem = null;
							},

						/**
						 * File removed handler
						 */
						fileRemoved: function(args) {
								jQuery('#' + args.fileData.id).slideUp("fast", function() { jQuery(this).remove(); });
							},

						/**
						 * File upload handler,
						 * in this demo used to change the icons
						 */
						fileUploadStarted: function(args) {
								var _id = args.fileData.id;
								var _self = this;
								var _elem = jQuery('#' + _id);

								//
								// You could delete the file while uploading,
								// (since this would trigger a "cancel" event in the Flash movie which is send to JavaScript)
								// but for the sake of the demo, we don't allow it.

								//
								// Fix links
								jQuery('A.delete', _elem).disableLink();
								jQuery('A.start', _elem).hide();
								jQuery('A.retry', _elem).hide();
								jQuery('A.cancel', _elem).show();

								//
								// Animate the progressbar
								_elem.addClass('active').animate({ height: 80 }, 200);

								//
								// Show elements
								jQuery('DIV.wuICPB', _elem).show();
								jQuery('DIV.wuICPT', _elem).show();
							},

						/**
						 * File progress handler
						 * in this demo used to update the progressbar
						 * @param args.fileData
						 * @param args.fileProgress
						 * @param args.queueProgress
						 */
						fileUploadProgress: function(args) {
								var _id = args.fileData.id;
								var _self = this;
								var _elem = jQuery('#' + _id);

								jQuery('h2.wuQS').html('Uploader queue: ' + args.queueProgress.progress + '% of ' + (args.queueProgress.filesAdded - args.queueProgress.filesCompleted) + ' files');
								jQuery('div.wuICPBF', _elem).css('width', args.fileProgress.progress + '%').html('&nbsp;' + args.fileProgress.progress + '%&nbsp;');
								jQuery('span.wuICBytesDone', _elem).html(this.formatSize(args.fileProgress.bytesCompleted));
								jQuery('span.wuICSpeedTime', _elem).html('at ' + this.formatSize(args.fileProgress.bytesPerSecond) + '/sec; ' + this.formatTime(args.fileProgress.timeRemaining) + ' remain');
							},

						/**
						 * Triggered when a file is cancelled
						 */
						fileUploadCancelled: function(args) {
								var _id = args.fileData.id;
								var _self = this;
								var _elem = jQuery('#' + _id);
								uploadRestore(_elem, _self, true);
							},

						/**
						 * Triggered when a file is uploaded (or has an error)
						 */
						fileUploadCompleted: function(args) {
								$("#nextframe1").css('display', 'none');
								$("#nextframe2").css('display', 'block');

								var _id = args.fileData.id;
								var _self = this;
								var _elem = jQuery('#' + _id);
								uploadRestore(_elem, _self, false);
							},

						/**
						 * Triggered when a file is uploaded (or has an error)
						 */
						fileUploadError: function(args) {
								var _id = args.fileData.id;
								var _self = this;
								var _elem = jQuery('#' + _id);
								uploadRestore(_elem, _self, false);
							},

						/**
						 * Triggered when there are no files in the queue
						 */
						queueErrorEmpty: function(args) {
								alert('No files to upload. Queue is empty!');
							},

						/**
						 * Called when the entire queue is cancelled
						 */
						queueCancelled: function() {
								jQuery('h2.wuQS').html('Uploader queue');
							},

						/**
						 * Called when the entire queue is cancelled
						 */
						queueCleared: function() {
								jQuery('h2.wuQS').html('Uploader queue');
							},

						/**
						 * Called when the entire queue is uploaded/completed
						 */
						queueCompleted: function() {
								jQuery('h2.wuQS').html('Uploader queue');
							}
					});
			});
	}
});