/* 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 fileQueued(file) {
	try {
		this.customSettings.tdFilesQueued.innerHTML = this.getStats().files_queued;
	} catch (ex) {
		this.debug(ex);
	}

}

function fileDialogComplete() {
	if(this.getStats().files_queued*1 > 0){
		jQuery("#goodupload, #exterror, #sizeerror, #btnclose").hide(0);
		document.getElementById("loading").style.visibility = '';
		jQuery.blockUI({ message: jQuery('#uploadingcontainer'), 
				css: { width: jQuery("#uploadingcontainer").width(),
						height: 'auto',
						padding: '10px',
						top:  (jQuery(window).height() - jQuery("#uploadingcontainer").height()) /2 + 'px',
						left: '50%',
						marginLeft: -((jQuery("#uploadingcontainer").width()*1)/2)} });
		this.startUpload();
	}
}

function uploadStart(file) {
	try {
		this.customSettings.progressCount = 0;
		updateDisplay.call(this, file);
	}
	catch (ex) {
		this.debug(ex);
	}
	
}

function uploadProgress(file, bytesLoaded, bytesTotal) {
	try {
		this.customSettings.progressCount++;
		updateDisplay.call(this, file);
	} catch (ex) {
		this.debug(ex);
	}
	
}

function uploadSuccess(file, serverData) {
	try {
		document.getElementById("loading").style.visibility = 'hidden';
		var splitdatas = serverData.split(";");
		if(splitdatas[0] == 'imgsize'){
			sizeerror(splitdatas[1],splitdatas[2]);
		}
		else if(splitdatas[0] == 'errext'){
			exterror();
		}
		else{
			goodupload();
		}
		jQuery("#parcourircontainer").css("left","auto");
		jQuery("#parcourircontainer").css("position","static");
	} catch (ex) {
		this.debug(ex);
	}
}

function goodupload(){
	jQuery("#goodupload").slideDown(100, function(){setTimeout(setnext, 4000); });
}

function exterror(){
	jQuery("#btnclose").show();
	jQuery("#exterror").slideDown(100, function(){jQuery('.blockOverlay, #btnclose').attr('title','Cliquez ici pour fermer cette boite').click(jQuery.unblockUI); });
}

function sizeerror(w,h){
	var inner = document.getElementById("sizeerror").innerHTML;
	inner = inner.replace('#WIDTH#',w);
	inner = inner.replace('#HEIGHT#',h);
	document.getElementById("sizeerror").innerHTML = inner;
	jQuery("#sizeerror, #btnclose").slideDown(100, function(){jQuery('.blockOverlay, #btnclose').attr('title','Cliquez ici pour fermer cette boite').click(jQuery.unblockUI); });
}

function uploadComplete(file) {
	this.customSettings.tdPercentUploaded.innerHTML = '100 %';
	this.customSettings.pourcentbar.style.width = this.customSettings.widthbar+'px';
}

function updateDisplay(file) {
	this.customSettings.tdPercentUploaded.innerHTML = SWFUpload.speed.formatPercent(file.percentUploaded);
	this.customSettings.pourcentbar.style.width = file.percentUploaded*1*(this.customSettings.widthbar*1/100)+"px";
}