JSGestorNoticias = new Class({

	autorCount: 0,
	tituloPtCount: 1,
	request: null,

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

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

		new Request.HTML(
			{
				url: 'modulos/noticias/mostrar_todas.ajax.php',
				encoding: 'iso-8859-1',
				onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript) {
					$('noticias').innerHTML = responseHTML;
				}
			}
		).get({l: l});

	},

	// ----------------------------------------------------------------------------------------------------
	verificaEdicao: function(tipo, id, l) {

		new Request.HTML(
			{
				url: 'modulos/seguranca/verificar_bloqueio_edicao.ajax.php',
				onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript) {
					if(responseHTML != '') {
						msgr.show({
							tipo: 'alert',
							html: responseHTML
						});
					}
				}
			}
		).get({
			tipo: tipo,
			id: id,
			l: l
		});

	},

	// ----------------------------------------------------------------------------------------------------
	edita: function(tipo, id, l) {

		new Request.HTML(
			{
				url: 'modulos/seguranca/verificar_bloqueio_edicao.ajax.php',
				onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript) {
					if(responseHTML != '') {
						var confirmStr = responseHTML + '<strong>Queres edit&aacute;-la na mesma?</strong>';
						msgr.show({
							tipo: 'confirm',
							html: confirmStr,
							onComplete: function(ok) {
								if(ok) window.location.href = '?p=noticias&a=editar&id=' + id + '&l=' + l;
							}
						});
					} else window.location.href = '?p=noticias&a=editar&id=' + id + '&l=' + l;
				}
			}
		).get({
			tipo: tipo,
			id: id,
			l: l
		});

	},

	// ----------------------------------------------------------------------------------------------------
	cancelaEdicao: function(tipo, id, l, paginaAnterior) {

		new Request.HTML(
			{
				url: 'modulos/seguranca/desbloquear_edicao.ajax.php',
				onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript) {
					window.location.href = paginaAnterior;
				}
			}
		).get({
			tipo: tipo,
			id: id,
			l: l
		});

	},

	// ----------------------------------------------------------------------------------------------------
	verificaNova: function(tipo, paginaAnterior) {

		// Espera
		$('btn_submit').value = "Espera...";

		// Validar
		var texto_pt = __xinhas[0].getEditorContent().trim();
		var texto_en = __xinhas[1].getEditorContent().trim();
		if(!texto_pt.length && !texto_en.length) {
			utils.mostraErrorArray(['N&atilde;o escreveste nenhum texto.<br /><span class="nota">Escreve texto pelo menos numa vers&atilde;o</span>'], 'salvar');
			$('btn_submit').value = "Salvar";
			return;
		}

		// Enviar por AJAX
		new Request.HTML(
			{
				url: 'modulos/noticias/gravar_nova.ajax.php',
				onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript) {
					document.location.href = paginaAnterior;
					/*$('noticias').innerHTML = responseHTML;
					msgr.show({
						tipo: 'info',
						html: 'A not&iacute;cia foi salva.'
					});
					$('btn_submit').value = "Salvar";
					gestorNoticias.mostraTodas($('l').value);*/
				}
			}
		).post(
			{
				l: $('l').value,
				dia: $('dia').value,
				mes: $('mes').value,
				ano: $('ano').value,
				icone: $('icone').value,
				texto_pt: texto_pt,
				texto_en: texto_en
			}
		);

	},

	// ----------------------------------------------------------------------------------------------------
	verificaEditada: function(tipo, paginaAnterior) {

		// Espera
		$('btn_submit').value = "Espera...";

		// Validar
		var texto_pt = __xinhas[0].getEditorContent().trim();
		var texto_en = __xinhas[1].getEditorContent().trim();
		if(!texto_pt.length && !texto_en.length) {
			utils.mostraErrorArray(['N&atilde;o escreveste nenhum texto.<br /><span class="nota">Escreve texto pelo menos numa vers&atilde;o</span>'], 'salvar');
			$('btn_submit').value = "Salvar";
			return;
		}

		// Enviar por AJAX
		new Request.HTML(
			{
				url: 'modulos/noticias/gravar_editada.ajax.php',
				async: true,
				onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript) {
					document.location.href = paginaAnterior;
					/*$('noticias').innerHTML = responseHTML;
					msgr.show({
						tipo: 'info',
						html: 'A not&iacute;cia foi salva.'
					});
					$('btn_submit').value = "Salvar";
					gestorNoticias.mostraTodas($('l').value);*/
				}
			}
		).post({
			id: $('id').value,
			l: $('l').value,
			dia: $('dia').value,
			mes: $('mes').value,
			ano: $('ano').value,
			icone: $('icone').value,
			texto_pt: texto_pt,
			texto_en: texto_en
		});

		// Desbloquear a edicao
		new Request.HTML(
			{
				url: 'modulos/seguranca/desbloquear_edicao.ajax.php',
				onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript) {
					window.location.href = paginaAnterior;
				}
			}
		).get({
			tipo: tipo,
			id: $('id').value,
			l: $('l').value
		});

	},

	// ----------------------------------------------------------------------------------------------------
	apaga: function(tipo, id, numVersoes, l) {

		// Verificar bloqueio
		new Request.HTML(
			{
				url: 'modulos/seguranca/verificar_bloqueio_edicao.ajax.php',
				onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript) {
					// Se estiver a ser editado
					if(responseHTML != '') {
						var confirmStr = responseHTML;
						confirmStr += "<strong>Queres apag&aacute;-la na mesma?</strong><br />";
						if(numVersoes > 1) {
							confirmStr += '<br /><span class="nota">Ser&atilde;o apagadas ambas as vers&otilde;es da not&iacute;cia.<br />';
							confirmStr += 'Se s&oacute; pretendes apagar uma das vers&otilde;es, edita a not&iacute;cia e apaga o texto da vers&atilde;o que queres apagar.</span>';
						}
						msgr.show({
							tipo: 'confirm',
							html: confirmStr,
							onComplete: function(ok) {
								if(ok) new Request.HTML(
									{
										url: 'modulos/noticias/apagar.ajax.php',
										onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript) {
											$('noticia_' + id).dispose();
										}
									}
								).get({id: id});
							}
						});
					// Caso contrario
					} else {
						var confirmStr = '<h1>Apagar esta not&iacute;cia?</h1>'
						if(numVersoes > 1) {
							confirmStr += 'Ser&atilde;o apagadas ambas as vers&otilde;es da not&iacute;cia.<br />';
							confirmStr += 'Se s&oacute; pretendes apagar uma das vers&otilde;es, edita a not&iacute;cia e apaga o texto da vers&atilde;o que queres apagar.';
						}
						msgr.show({
							tipo: 'confirm',
							html: confirmStr,
							onComplete: function(ok) {
								if(ok) new Request.HTML(
									{
										url: 'modulos/noticias/apagar.ajax.php',
										onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript) {
											$('noticia_' + id).dispose();
										}
									}
								).get({id: id});
							}
						});
					}
				}
			}
		).get({
			tipo: tipo,
			id: id,
			l: l
		});

	},

	// ----------------------------------------------------------------------------------------------------
	criaCalendario: function(data) {

		Calendar.setup({
			flat         : 'calendario',					// ID of the parent element
			flatCallback : function(calendar) {				// our callback function
				// Beware that this function is called even if the end-user only
				// changed the month/year.  In order to determine if a date was
				// clicked you can use the dateClicked property of the calendar:
				if(calendar.dateClicked) {
					var d = calendar.date.getDate();     	 // integer, 1..31
					var m = 1 + calendar.date.getMonth();    // integer, 0..11
					var a = calendar.date.getFullYear();
					$('dia').value = d;
					$('mes').value = m;
					$('ano').value = a;
				}
			},
			daFormat     : "%d-%m-%Y",
			date         : data
		});

	}

});

