/**
 * 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('demo-list', '#demo-attach, #demo-attach-2', {
		path: '../fancyupload/source/Swiff.Uploader.swf',
		url: '../php/fancyupload.php',
		fileSizeMax: 2 * 1024 * 1024,
		typeFilter: {'PDB files (*.pdb, *.ent, *.pdb.gz, *.ent.gz)': '*.pdb; *.ent; *.pdb.gz; *.ent.gz; *.PDB; *.ENT; *.PDB.gz; *.ENT.gz'},
		verbose: true,
		
		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) {
		    var fname = JSON.decode(file.response.text, true).src;
		    var el = document.getElementById('fileupload');
		    el.inject(file.ui.element, 'top');
		    el.value = fname;
		    el.setStyle('display','inline');
		    el.checked = true;
		    file.ui.element.highlight('#e6efc2');
		},
		
		onFileError: function(file) {
			file.ui.cancel.set('html', 'Retry').removeEvents().addEvent('click', function() {
				file.requeue();
				return false;
			});
			
			new Element('span', {
				html: file.errorMessage,
				'class': 'file-error'
			}).inject(file.ui.cancel, 'after');
		},
		
		onFileRequeue: function(file) {
			file.ui.element.getElement('.file-error').destroy();
			
			file.ui.cancel.set('html', 'Cancel').removeEvents().addEvent('click', function() {
				file.remove();
				return false;
			});
			
			this.start();
		}
		
	});

});

