/**
 * FancyUpload Showcase
 *
 * @license		MIT License
 * @author		Harald Kirschner <mail [at] digitarald [dot] de>
 * @copyright	Authors
 */

window.addEvent('domready', function() {

	/**
	 * Uploader instance
	 */
	var up = new FancyUpload3.Attach('file-list', '#file-attach, #file-attach-2', {
		path: '/templatesMedias/contact/js/Swiff.Uploader.swf',
		url: '/templates/contact/Fancy.Script.php',
		allowDuplicates: true,
		data: 'sessid=' + $('sessid').value,
		fileSizeMax: 2 * 1024 * 1024,
		typeFilter : {
			'Documents (*.doc, *.xls, *.pdf, ...)': '*.doc; *.xls; *.ppt; *.rtf; *.odt; *.ods; *.odg; *.csv; *.txt; *.pdf; *.html',
			'Images (*.jpg, *.bmp, *.png)': '*.jpg; *.jpeg; *.bmp; *.png'
		},
		
		onSelectFail: function(files) {
			files.each(function(file) {
				new Element('li', {
					'class': 'file-invalid',
					events: {
						click: function() {
							this.destroy();
						}
					}
				}).adopt(
					new Element('span', {html: file.validationErrorMessage || file.validationError})
				).inject(this.list, 'bottom');
			}, this);	
		},
		
		onFileSuccess: function(file, response) {
			var json = new Hash(JSON.decode(response, true) || {});
						
			if (json.get('status') == '1') {
				new Element('input', {type: 'checkbox', 'checked': true, 'name': 'Fichier[' + file.id + ']', 'value': json.get('hash')}).inject(file.ui.element, 'top');
				file.ui.element.highlight('#e6efc2');
			} else {
				new Element('span', {
					html: json.get('error'),
					'class': 'file-error'
				}).inject(
					new Element('br').inject(file.ui.size, 'after')
				, 'after');
			}
		},
		
		onFileError: function(file) {
			file.ui.cancel.set('html', 'R&eacute;peter').removeEvents().addEvent('click', function() {
				file.requeue();
				return false;
			});
			
			new Element('span', {
				html: file.errorMessage,
				'class': 'file-error'
			}).inject(
				new Element('br').inject(file.ui.cancel, 'after')
			, 'after');
		},
		
		onFileRequeue: function(file) {
			file.ui.element.getElement('.file-error').getPrevious().destroy();
			file.ui.element.getElement('.file-error').destroy();
			
			file.ui.cancel.set('html', 'Annuler').removeEvents().addEvent('click', function() {
				file.remove();
				return false;
			});
			
			this.start();
		}
		
	});

});

