JSGestorFicheiros = new Class({

	ficheiroCount: 0,

	// ----------------------------------------------------------------------------------------------------
	initialize: function() {
	},

	// ----------------------------------------------------------------------------------------------------
	anexaFicheiro: function() {

		// div do novo ficheiro
		var ficheiroDiv = new Element(
			'div',
			{
				id: 'div_novo_ficheiro_' + this.ficheiroCount
			}
		);

		// text do novo ficheiro
		var ficheiroInput = new Element(
			'input',
			{
				type: 'file',
				name: 'ficheiros[]',
				id: 'ficheiro_' + this.ficheiroCount,
				style: 'width: 260px; margin-bottom: 2px;'
			}
		);

		// a do novo ficheiro
		var ficheiroA = new Element(
			'a',
			{
				id: 'a_anexar_ficheiro_' + this.ficheiroCount,
				href: 'javascript: gestorFicheiros.removeFicheiro(' + this.ficheiroCount + ')'
			}
		);
		ficheiroA.innerHTML = 'Remover';

		// A div agarra o texto e o a; o fieldset agarra a div
		ficheiroDiv.grab(ficheiroInput);
		ficheiroDiv.appendText(' ');
		ficheiroDiv.grab(ficheiroA);
		$('div_ficheiros').grab(ficheiroDiv);

		var a_copy = $('a_anexar_ficheiro').clone(true, true);
		a_copy.innerHTML = 'Anexar outro ficheiro';
		$('a_anexar_ficheiro').destroy();
		$('div_ficheiros').grab(a_copy);
		ficheiroInput.select();

		this.ficheiroCount++;

	},

	// ----------------------------------------------------------------------------------------------------
	removeFicheiro: function(a) {

		$('div_novo_ficheiro_' + a).destroy();

		// Contar as caixas de texto
		var c = $('div_ficheiros').getElements('input');
		var isTextbox = function(t) {
			return t.type == 'file';
		}
		c = c.filter(isTextbox);
		if(c.length > 0) $('a_anexar_ficheiro').innerHTML = 'Anexar outro ficheiro';
		else $('a_anexar_ficheiro').innerHTML = 'Anexar um ficheiro';

	},

	// ----------------------------------------------------------------------------------------------------
	apagaFicheiro: function(id) {

		var table = $('table_' + id);
		var icone = $('icone_' + id).src;
		var ficheiro = $('a_' + id).getProperty('file');
		var nome = ficheiro.substr(ficheiro.lastIndexOf('/') + 1);

		msgr.show({
			tipo: 'confirm',
			html: 'Apagar este documento?<br />' +
				'<table width="300" border="0" cellspacing="0" cellpadding="1" style="padding-top: 5px; padding-bottom: 5px;">' +
					'<tr>' +
						'<td width="40"><img src="' + icone + '" width="24" height="24" alt="" /></td>' +
						'<td>' + nome + '</td>' +
					'</tr>' +
				'</table>',
			onComplete: function(ok) {
				if(ok) {
					var FicheiroRequest = new Class({
						Extends: Request.HTML,
						table: table,
						ficheiro: ficheiro
					});
					var request = new FicheiroRequest({
						url: 'modulos/ficheiros/apaga_ficheiro.ajax.php',
						onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript) {
							$(this.table).destroy();
						}
					});
					request.get({ficheiro: request.ficheiro});
				}
			}
		});

	},

	// ----------------------------------------------------------------------------------------------------
	mostraImg: function(img) {

		new Request.JSON(
			{
				url: 'modulos/ficheiros/get_img_info.ajax.php',
				onComplete: function(imgInfo) {
					var width = imgInfo.width;
					var height = imgInfo.height;
					new Asset.image(
						img,
						{
							onload: function() {
								msgr.show({
									tipo: 'img',
									largura: parseInt(width),
									img: {
										src: img,
										width: width,
										height: height
									}
								});								
							}
						}
					);
				}
			}
		).get({img: img});

	}

});
