JSAuthors = new Class({

	l: null,
	strings: {
		mostrarEntradasDesteAutor: {
			pt: 'Mostrar entradas escritas por este autor',
			en: 'Show entries written by this author'
		},
		ocultarEntradasDesteAutor: {
			pt: 'Ocultar entradas escritas por este autor',
			en: 'Hide entries written by this author'
		}
	},

	// ----------------------------------------------------------------------------------------------------
	initialize: function(l) {

		this.l = l;

	},

	// ----------------------------------------------------------------------------------------------------
	verifica: function(form) {

		var errorArray = new Array();

		// Nome
		var nome = $('nome').value = $('nome').value.clean();
		if(nome == '') errorArray.push('N&atilde;o escreveu um nome<br /><span class="nota">Escreva um nome com um caractere ou mais</span>');
		else {
			var autorRequest = new Request.HTML({
				url: 'modulos/authors/verificar_nome_autor.ajax.php',
				async: false,
				onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript) {
					var maxCount = form == $('form_adicionar') ? 0 : 1;
					if(responseHTML.clean() > maxCount) this.errorArray.push('J&aacute; existe um autor com este nome');
				}
			});
			autorRequest.errorArray = errorArray;
			autorRequest.get({nome: nome});
		}

		// Se houver erros mostra-los, caso contrario enviar o formulario
		if(errorArray.length) utils.mostraErrorArray(errorArray, 'salvar');
		else {
			if(form.get('name') == 'form_editar') {
				var id = $('id').get('value');
				form.set('action', 'authors/' + id);
			}
			form.submit();
		}

	},

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

		$('tr_' + id).setStyle('background', '#FFFFFF');
		$('div_comandos_' + id).setStyle('display', 'block');

	},

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

		$('tr_' + id).setStyle('background', 'none');
		$('div_comandos_' + id).setStyle('display', 'none');

	},

	// ----------------------------------------------------------------------------------------------------
	apaga: function(id, nome, porAjax) {

		// Verificar se ha alguma entrada escrita por este autor
		new Request.HTML({
			url: 'modulos/authors/verificar_apagar.ajax.php',
			onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript) {
				var numEntradas = responseHTML.clean();
				if(numEntradas == 1) {
					msgr.show({
						tipo: 'alert',
						titulo: 'Este autor n&atilde;o pode ser apagado',
						html: 'Este autor n&atilde;o pode ser apagado porque existe uma entrada escrita por ele na Sacarop&eacute;dia ou na Sucropedia.<br /><span class="nota">Apague-a ou altere o autor; de seguida tente apagar o autor novamente.</span>',
					});
					return;
				} else if(numEntradas > 1) {
					msgr.show({
						tipo: 'alert',
						titulo: 'Este autor n&atilde;o pode ser apagado',
						html: 'Este autor n&atilde;o pode ser apagado porque existem ' + numEntradas + ' entradas escritas por ele na Sacarop&eacute;dia ou na Sucropedia.<br /><span class="nota">Apague-as ou altere o autor; de seguida tente apagar o autor novamente.</span>',
					});
					return;
				}
				msgr.show({
					tipo: 'confirm',
					titulo: 'Apagar?',
					html: porAjax ? 'Apagar o autor <strong>' + nome + '</strong>?' : 'Apagar este autor?',
					onComplete: function(ok) {
						if(!ok) return;
						if(porAjax) {
							new Request.HTML({
								url: 'modulos/authors/apagar.ajax.php',
								onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript) {
									// Retirar a tr do autor
									$('tr_' + id).dispose();
								}
							}).get({id: id});
						} else document.location.href = 'authors/apagar/' + id;
					}
				});
			}
		}).get({id: id});

	},

	// ----------------------------------------------------------------------------------------------------
	mostraEntradasDesteAutor: function(mostra) {

		if(mostra) {
			$('div_entries').setStyle('display', 'block');
			$('a_entries').innerHTML = this.strings.ocultarEntradasDesteAutor[this.l];
			$('a_entries').href = 'javascript:authors.mostraEntradasDesteAutor(false);';
		} else {
			$('div_entries').setStyle('display', 'none');
			$('a_entries').innerHTML = this.strings.mostrarEntradasDesteAutor[this.l];
			$('a_entries').href = 'javascript:authors.mostraEntradasDesteAutor(true);';
		}

	}

});

