jQuery(document).ready(function() 
{
	if ($("#sScriptSrc").length)
	{
		var sScriptSrc = $("#sScriptSrc").attr('path');
		var sFileFiltersSrc = $("#sScriptSrc").attr('filter');
		
		jQuery('.inputWrapper').upload(
		{
			bDebug: false,
			oBrowse: jQuery('#btnBrowse'),
			oUpload: jQuery('#btnUpload'),
			oReset: jQuery('#btnReset'),
			sScript: sScriptSrc,
			sMovie: 'flash/ui.upload.swf',
			dMaxSize: 5120000,
			sFileFilters: [
							sFileFiltersSrc
						],
			aCallback: 
			{
				swfInitialized: function(oUI) 
				{
						// Flash movie succesfully created, now remove old file objects
						jQuery('.inputWrapper').remove();
	
						// And show the UI upload
						jQuery('.uiUpload').show();
				},
				dialogPreShow: function(oUI) 
				{
						jQuery('.cbStatus').html('please choose some files (or show greybox)!');
				},
				dialogPostShow: function(oUI) 
				{
					if (oUI.success)
					{
						$("#status").html('');
					}
					else
					{
						$("#status").html($("#status_none").html());
					}
//						jQuery('.cbStatus').html('you have hidden the dialog and you ' + ((oUI.success == true) ? 'DID' : 'DID NOT') + ' select files!!');
				},
				queueStarted: function(oUI) 
				{
						jQuery('.cbStatus').html('upload of current queue started!');
				},
				queueEmpty: function(oUI) 
				{
						jQuery('.cbStatus').html('current upload queue is empty!');
				},
				fileAdded: function(oUI) 
				{
						sHTML = '<div class="uiUploadItem" id="' + oUI.oFile.id + '">';
						sHTML+= '<div class="uiUploadItemWrapper">';
						sHTML+= '<a href="javascript://" title="Delete upload">' + oUI.oFile.name + '</a> <span class="cbInformation">(' + $.ui.upload.formatSize(oUI.oFile.size) + ')</span>';
						sHTML+= '<div class="uiUploadProgress"><div class="uiUploadProgressBar"> 0% </div></div>';
						sHTML+= '&#0187; <strong>Uploaded:</strong> <span class="cBytesProcessed">0 bytes</span> of <span class="cBytesTotal">' + $.ui.upload.formatSize(oUI.oFile.size) + '</span> <span class="cSpeedTime">&nbsp;</span>';
						sHTML+= '</div>';
						oHTML = jQuery(sHTML);
						jQuery('.uiUploadQueue').append(oHTML);
						
						// Add CSS class
						jQuery('A', oHTML).attr('class', 'uploadDelete');
	
						// Add handler
						var _oUI = oUI;
						jQuery('A', oHTML).click(function() 
						{
								// Callback to Flash to remove the file from the queue
								jQuery('#' + _oUI.options.sName)[0].cancelFile(_oUI.oFile.id);
	
								// If the file was succesfully removed, Flash calls back to Javascript
								return false;
						});
				},
				fileCancelled: function(oUI) 
				{
						jQuery('#' + oUI.oFile.id).remove();												
						jQuery('.cbStatus').html('file (' + oUI.oFile.name + ') cancelled!');
				},
				fileRemoved: function(oUI) 
				{
						jQuery('#' + oUI.oFile.id).remove();
						jQuery('.cbStatus').html('File (' + oUI.oFile.name + ') removed from queue');
				},
				queueCancelled: function(oUI) 
				{
						jQuery('.cbStatus').html('complete current queue cancelled!');
				},
				queueCompleted: function(oUI) 
				{
						$("#nextframe1").css('display', 'none');
						$("#nextframe2").css('display', 'block');
						
						sStatus = 'Upload queue: Uploaded ' + oUI.oProgress.qIndex + ' of ' + oUI.oProgress.qCount + ' files. ';
						sStatus+= $.ui.upload.formatSize(oUI.oProgress.qbDone) + ' at ';
						sStatus+= $.ui.upload.formatSize(oUI.oProgress.qbSpeed) + '/sec (' + oUI.oProgress.qProgress + '%)';
						jQuery('h2.uiUploadStatus').html(sStatus);
	
						jQuery('.cbStatus').html('complete current queue (' + oUI.oProgress.qCount + ' items) uploaded!');
				},
				fileStarted: function(oUI) 
				{
						jQuery('#' + oUI.oFile.id).addClass('active').animate({ height: 80 }, 200);
						jQuery('span.cbInformation', '#' + oUI.oFile.id).html('&nbsp;');										
				},
				fileProgress: function(oUI) 
				{
						sStatus = 'Upload queue: uploading ' + oUI.oProgress.qIndex + ' of ' + oUI.oProgress.qCount + ' files. ';
						sStatus+= $.ui.upload.formatSize(oUI.oProgress.qbDone) + ' at ';
						sStatus+= $.ui.upload.formatSize(oUI.oProgress.qbSpeed) + '/sec (' + oUI.oProgress.qProgress + '%) ';
						sStatus+= $.ui.upload.formatTime(oUI.oProgress.qtRemain) + ' remaining...';
						jQuery('h2.uiUploadStatus').html(sStatus);
	
						// Other shite
						jQuery('div.uiUploadProgressBar', '#' + oUI.oFile.id).css('width', oUI.oProgress.cProgress + '%').html('&nbsp;' + oUI.oProgress.cProgress + '%&nbsp;');
						jQuery('span.cBytesProcessed', '#' + oUI.oFile.id).html($.ui.upload.formatSize(oUI.oProgress.cbDone));
						jQuery('span.cSpeedTime', '#' + oUI.oFile.id).html('at ' + $.ui.upload.formatSize(oUI.oProgress.cbSpeed) + '/sec; ' + $.ui.upload.formatTime(oUI.oProgress.ctRemain));
				},
				fileCompleted: function(oUI) 
				{
						
						jQuery('#' + oUI.oFile.id).removeClass('active').animate({ height: 40 }, 200);
						jQuery('span.cbInformation:eq(0)', '#' + oUI.oFile.id).html('(' + $.ui.upload.formatSize(oUI.oFile.size) + ' uploaded at ' + $.ui.upload.formatSize(oUI.oProgress.cbSpeed) + '/sec; ' + $.ui.upload.formatTime(oUI.oProgress.ctBusy) + ')');
						jQuery('A', '#' + oUI.oFile.id).attr('class', 'uploadSuccess').unclick();
				},
				fileErrorSize: function(oUI) 
				{
						jQuery('.cbStatus').html('file error: size');
						alert('Error: The file you\'ve selected (' + oUI.oFile.name + ') exceeds the maximum filesize of ' + (oUI.options.dMaxSize * 1024) + ' bytes.');
				},
				fileErrorIO: function(oUI) 
				{
						jQuery('.cbStatus').html('file error: I/O');
						alert('Error: The file you\'ve selected (' + oUI.oFile.name + ') raised an I/O error.');
				},
				fileErrorSecurity: function(oUI) 
				{
						jQuery('.cbStatus').html('file error: security');
						alert('Error: The file you\'ve selected (' + oUI.oFile.name + ') raised an security error.');
				},
				fileErrorHTTP: function(oUI) 
				{
						jQuery('.cbStatus').html('file error: HTTP error');
						alert('Error: The file you\'ve selected (' + oUI.oFile.name + ') gave an HTTP error while uploading: HTTP 1.1/' + oUI.errCode);
				}
			}
		});
	}
});