/* Demo Note:  This demo uses a FileProgress class that handles the UI for displaying the file name and percent complete.
The FileProgress class is not part of SWFUpload.
*/


/* **********************
   Event Handlers
   These are my custom event handlers to make my
   web application behave the way I went when SWFUpload
   completes different tasks.  These aren't part of the SWFUpload
   package.  They are part of my application.  Without these none
   of the actions SWFUpload makes will show up in my application.
   ********************** */
function preLoad() {
	if (!this.support.loading) {
		Upload.flash_support = false;
		Upload.showAlert('You need the Flash Player 9.028 or above to use flash uploader(advanced uploader).', 650, 85, Upload.block_ui_enabled);
		Upload.changeFileUploadPage('file');
		return false;
	}
	else
	{
		Upload.flash_support = true;
		Upload.changeFileUploadPage('flash');
	}
}
function swfUploadLoaded() {
	JQ('#loadingFlashuploader').attr('style','font-size:12px;color:black;');
	JQ('#loadingFlashuploader').html('&nbsp;(You can also choose Multiple Files for upload.Up to '+Upload.max_upload_slots+' File\'s ONLY.)');
}
function loadFailed() {
	alert("Something went wrong while loading advanced uploader.");
}

function fileQueued(file) {
	try {
			var listitem='<div id="'+file.id+'" class="selectedFile" ><a class="cancelFile" href="javascript:" style="visibility: visible; "></a><div>'+file.name+' ('+Math.round(file.size/1024)+' KB)</div></div>';
			JQ('#showSelectedFile').append(listitem);
			
			JQ('#'+file.id+' .cancelFile').bind('click', function(){
			swfu.cancelUpload(file.id);
			JQ('#'+file.id).slideUp('fast');
			});
			
		this.customSettings.tdFilesQueued.innerHTML = this.getStats().files_queued;
	} catch (ex) {
		this.debug(ex);
	}

}
function fileQueueError(file, errorCode, message) {
	try {
		if (errorCode === SWFUpload.QUEUE_ERROR.QUEUE_LIMIT_EXCEEDED) {
			Upload.showAlert("Up to "+Upload.max_upload_slots+" File's ONLY", 400, 80, Upload.block_ui_enabled);
			return;
		}

		switch (errorCode) {
		case SWFUpload.QUEUE_ERROR.FILE_EXCEEDS_SIZE_LIMIT:
			Upload.showAlert("File is too big.", 650, 85, Upload.block_ui_enabled);
			this.debug("Error Code: File too big, File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
			break;
		case SWFUpload.QUEUE_ERROR.ZERO_BYTE_FILE:
			Upload.showAlert("Cannot upload Zero Byte files.", 650, 85, Upload.block_ui_enabled);
			this.debug("Error Code: Zero byte file, File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
			break;
		case SWFUpload.QUEUE_ERROR.INVALID_FILETYPE:
			Upload.showAlert("Invalid File Type.", 650, 85, Upload.block_ui_enabled);
			this.debug("Error Code: Invalid File Type, File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
			break;
		default:
			if (file !== null) {
				Upload.showAlert("Unhandled Error", 650, 85, Upload.block_ui_enabled);
			}
			this.debug("Error Code: " + errorCode + ", File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
			break;
		}	
	} catch (ex) {
        this.debug(ex);
    }
}
function fileDialogComplete() {
	//this.startUpload();
}

function uploadStart(file) {
	try {
		JQ("#pb4").attr('style','visibility:show;');
		JQ("#pb4").progressBar(0, { showText: true, barImage: 'images/'+Upload.progressbar_images[Upload.current_pr_num]} );
		JQ("#progress_text").html('Uploading <font style="font-size:20px;font-style:italic;color:red;">'+file.name+'</font><br><br>');
		
		JQ('#pb4_detail').fadeIn('fast');
		
		this.customSettings.progressCount = 0;
		updateDisplay.call(this, file);
	}
	catch (ex) {
		this.debug(ex);
	}
	
}

function uploadProgress(file, bytesLoaded, bytesTotal) {
	try {
		var percentage=Math.round((bytesLoaded/file.size)*100);
		JQ("#pb4").progressBar(percentage, { showText: true, barImage: 'images/'+Upload.progressbar_images[Upload.current_pr_num]} );
		if(percentage == 100)
		{
			JQ("#progress_text").html('<img align="center" src="images/uploading4.gif"></img><font style="font-size:20px;font-style:italic;color:black;">Verifying file please wait It may take several seconds</font> (up to 1 minute)<br><br>');
		}
		
		this.customSettings.progressCount++;
		updateDisplay.call(this, file);
	} catch (ex) {
		this.debug(ex);
	}
	
}

function uploadSuccess(file, serverData) {
	try {
		updateDisplay.call(this, file);
		
		this.customSettings.tdServerData.innerHTML = serverData;
	
		JQ('#'+file.id).slideUp('fast');
		JQ('#pb4_detail').fadeOut('fast');
		
		var link='http://www.embedupload.com/?d='+serverData;
		var uniqid = new Date().getTime();
		JQ('#mylink').append("<table width=\"100%\" height=\"70\"><tr><td align=\"left\" valign=\"center\">Here is<font style=\"font-size:20px;font-style:italic;color:red;\">   "+file.name+"   </font> download link: [<a href=\"javascript:copyText(document.mylinkform_"+uniqid+".downloadLink_"+uniqid+")\">copy</a>]</td></tr><tr align=\"center\"><td><form name=\"mylinkform_"+uniqid+"\" id=\"mylinkform\"><input type=\"hidden\" name=\"downloadLink_"+uniqid+"\" value=\""+link+"\"><b><a href=\""+link+"\" style=\"font-size:25px;font-style:italic\" target=\"_blank\" >"+link+"</a></b></form></td></tr></table><br>");
						
		JQ("#pb4").progressBar(0, { showText: true, barImage: 'images/'+Upload.progressbar_images[Upload.current_pr_num]} );
		JQ("#pb4").attr('style','visibility:hidden;');
		JQ("#progress_text").html('');
		
		//this.setUploadURL('/hi.php');
		
	} catch (ex) {
		this.debug(ex);
	}
}

function uploadComplete(file) {
	this.customSettings.tdFilesQueued.innerHTML = this.getStats().files_queued;
	this.customSettings.tdFilesUploaded.innerHTML = this.getStats().successful_uploads;
	this.customSettings.tdErrors.innerHTML = this.getStats().upload_errors;

}

function updateDisplay(file) {
	this.customSettings.tdCurrentSpeed.innerHTML = SWFUpload.speed.formatBPS(file.currentSpeed);
	this.customSettings.tdAverageSpeed.innerHTML = SWFUpload.speed.formatBPS(file.averageSpeed);
	this.customSettings.tdMovingAverageSpeed.innerHTML = SWFUpload.speed.formatBPS(file.movingAverageSpeed);
	this.customSettings.tdTimeRemaining.innerHTML = SWFUpload.speed.formatTime(file.timeRemaining);
	this.customSettings.tdTimeElapsed.innerHTML = SWFUpload.speed.formatTime(file.timeElapsed);
	this.customSettings.tdPercentUploaded.innerHTML = SWFUpload.speed.formatPercent(file.percentUploaded);
	this.customSettings.tdSizeUploaded.innerHTML = SWFUpload.speed.formatBytes(file.sizeUploaded);
	this.customSettings.tdProgressEventCount.innerHTML = this.customSettings.progressCount;

}