﻿/* Portail Avatar */
function initAvatarFileUpload(){
	jQuery('#file_upload').fileUploadUI({
		namespace: 'file_upload',
		cssClass: 'file_upload',
		method: 'POST',
		fieldName: 'file-data',
		dragDropSupport : false,
		url: 'portail/ajax/portail.avatar.ajax.php',
		imageTypes: '/^image\/(gif|jpeg|jpg|png)$/',
		beforeSend: function (event, files, index, xhr, handler, callBack) {
						var regexp = /\.(png)|(jpg)|(jpeg)|(gif)$/i;
						var errorText='';
						jQuery('#file_upload_status').val(index);
						if (index === 0) {
							files.uploadSequence = [];
							files.uploadSequence.start = function (index) {
								var next = this[index];
								if (next) {
									next.apply(null, Array.prototype.slice.call(arguments, 1));
									this[index] = null;
								}
							};
						}
						if(index > 1){errorText+='Vous pouvez importer seulement 1 image. ';}
						if (!regexp.test(files[index].name)) {errorText+=((errorText!='')?'<br/>':'')+'Seul les extensions "png,jpg,jpeg ou gif" sont acceptées. ';}
						if (files[index].size > 4096000) {errorText+=((errorText!='')?'<br/>':'')+'La limite de taille est de 4mo ! ';}//max size bytes - 800kb:819200,2mb:2048000,3mb:3072000,4mb:4096000,5mb:5120000
						if(errorText!=''){
							handler.uploadRow.find('.file_upload_progress').html(errorText);
							setTimeout(function () {handler.removeNode(handler.uploadRow);}, 10000);
							jQuery('.file_upload_infos').html(errorText).show();
							return;
						}
						files.uploadSequence.push(callBack);
						if ((index + 1 === files.length) || (files.length > 1 && index + 1 === 1) ) { files.uploadSequence.start(0); }
					},
		onProgress : function (event, files, index, xhr, handler) {
						if (handler.progressbar) {
							handler.progressbar.progressbar('value',parseInt(event.loaded / event.total * 100, 10));
						}
					},
		onComplete : function (event, files, index, xhr, handler) {
						var json = handler.response;
						if(json.result==1){
							if( (jQuery('#file_upload_status').val()==index) || (jQuery('#file_upload_status').val()>=1 && index==1) ){
								jQuery('#avatar_tempfile').val(json.avatar_tempfile);
								jQuery('#avatar_preview').attr('src',json.avatar_tempfile);
								jQuery('#has_avatar').val('ok');
							}
						}else{ alert("Votre avatar n'a pas \351t\351 enregistr\351e."); }
						files.uploadSequence.start(index + 1);
					},
		onAbort: function (event, files, index, xhr, handler) {
					handler.removeNode(handler.uploadRow);
					files.uploadSequence[index] = null;
					files.uploadSequence.start(index + 1);
				},
		onError : function (event, files, index, xhr, handler) {
					// For JSON parsing errors, the load event is saved as handler.originalEvent:
					if (handler.originalEvent) {
						/* handle JSON parsing errors ... */
						alert('Erreur lors du traitement de l\'image. Seul les images ayant les extensions suivantes sont accept\351es : jpg,jpeg,png et gif.');
					} else {
						/* handle XHR upload errors ... */
						alert('Erreur lors du traitement de l\'image. Seul les images ayant les extensions suivantes sont accept\351es : jpg,jpeg,png et gif.');
					}
				},
		uploadTable: jQuery('#filesQueue'),
		downloadTable: jQuery('#filesQueue'),
		buildUploadRow: function (files, index) {
							if(index > 1){return null;}
							return jQuery('<tr><td>' + files[index].name + '<\/td>' +
									'<td class="file_upload_progress"><div><\/div><\/td>' +
									'<td class="file_upload_cancel">' +
									'<button class="ui-state-default ui-corner-all" title="Annuler">' +
									'<span class="ui-icon ui-icon-cancel">Annuler<\/span>' +
									'<\/button><\/td><\/tr>');
					},
		buildDownloadRow: function (file) {
							return jQuery('<tr><td>termin\351.<\/td><\/tr>');
					}
	});
	refreshPositionFileUpload();
}

window.onresize = function() { refreshPositionFileUpload(); } 

function refreshPositionFileUpload(){
	jQuery('#file_upload').css({'display':'block','left':'685px','top':'305px'});
}
