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

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

	var link = $('attach');
	var linkIdle = link.get('html');
	var fileError = $('file_error');
	var remove = $('remove_attach');
	var doc_id = $('document_id');

	function linkUpdate() {
		if (!swf.uploading) return;
		var size = Swiff.Uploader.formatUnit(swf.size, 'b');
		link.set('html', '<span class="small">' + swf.percentLoaded + '% de ' + size + '</span>');
	}

	// Uploader instance
	var swf = new Swiff.Uploader({
		path: '/cms/themes/default/js/Swiff.Uploader.swf',
		url: '/cms/upload/documents/upload.php',
		verbose: true,
		queued: false,
		multiple: false,
		target: link,
		instantStart: true,
		typeFilter: {
			'Documents (*.doc, *.docx, *.pdf, *.ppt)': '*.doc; *.docx; *.pdf; *.ppt'
		},
		fileSizeMax: 3 * 1024 * 1024,
		onSelectSuccess: function(files) {
			if (Browser.Platform.linux) window.alert('Warning: Due to a misbehaviour of Adobe Flash Player on Linux,\nthe browser will probably freeze during the upload process.\nSince you are prepared now, the upload will start right away ...');
			//alert('Starting Upload', 'Uploading <em>' + files[0].name + '</em> (' + Swiff.Uploader.formatUnit(files[0].size, 'b') + ')');
			this.setEnabled(false);
			fileError.set('html', '')
		},
		onSelectFail: function(files) {
			fileError.set('html', files[0].name +' - Por favor seleccione un archivo menor a 3 MB.');
		},
		appendCookieData: true,
		onQueue: linkUpdate,
		onFileComplete: function(file) {

			if (file.response.error) {
				//window.alert('Failed Upload', 'Uploading <em>' + this.fileList[0].name + '</em> failed, please try again. (Error: #' + this.fileList[0].response.code + ' ' + this.fileList[0].response.error + ')');
				window.alert(file.response.error);
				this.setEnabled(true);
				link.set('html', linkIdle);
			} else {
				var text = JSON.decode(file.response.text, true);
				link.addClass('hide');
				this.setEnabled(false);

				remove.toggleClass('hide');
				doc_id.set('value', text.document_id);
				$('div_document').removeClass('hide');
				$('document_name').set('value', text.document_name);
			}
		},
		onComplete: function() {
			link.set('html', linkIdle);
		}
	});

	// Button state
	link.addEvents({
		click: function() {
			return false;
		},
		mouseenter: function() {
			this.addClass('hover');
			swf.reposition();
		},
		mouseleave: function() {
			this.removeClass('hover');
			this.blur();
		},
		mousedown: function() {
			this.focus();
		}
	});

	remove.addEvent('click', function(e) {
		e.stop();

		var req = new Request({
			url: this.get('href') + '/' + doc_id.get('value'),
			onComplete: function(response) {
				if(response == '_ok') {
					doc_id.set('value', '')
					$('document_name').set('value', '')
					$('div_document').addClass('hide');
					$('attach').toggleClass('hide');
					remove.toggleClass('hide');
					swf.setEnabled(true);
				}
			}
		}).send();
	});

	if(doc_id.get('value') == '') {
		$('div_document').addClass('hide');
		remove.toggleClass('hide');
	} else {
		$('attach').addClass('hide');
	}

});