
function report_content(image, user_id, anchor_name){
	if (image && (ReportManager.status == 'hidden')) {
		ReportManager.form = new ContentReportForm(user_id, anchor_name);
		ReportManager.form.display(image);
		ReportManager.status = 'form_displayed';
	}
}

function replyReport(result){
	ReportManager.status = 'processed';
	ReportManager.form.displayResult(result);
}

function closeReportForm(){
	ReportManager.status = 'hidden';
	ReportManager.form.close();
}

function acceptReport(){
	ReportManager.acceptReport();
}

function ContentReportManager(section, base_url, language){
	this.form = null;
	this.section = section;
	this.baseURL = base_url;
	this.language = language;
	this.status = 'hidden';

}

ContentReportManager.prototype.acceptReport = function(){
	if (this.form) {
		this.status = 'waiting';
		var comments = '';
		var text = '';
		var comment_area = $('comment_area_id');
		if (comment_area) {
			comments = comment_area.value;
		}
		var text_div = $(this.form.anchorName);
		if (text_div) {
			var text_div_clone = text_div.cloneNode(true);
			// Remove div element containing the report form
			if (text_div_clone.lastChild && text_div_clone.lastChild.nodeName=='DIV') {
				text_div_clone.removeChild(text_div_clone.lastChild);
			}
			text = text_div_clone.innerHTML;
		}
		var report = new ContentReport(this.form.offenderID, this.baseURL+'#'+this.form.anchorName, this.section, comments, text);
		x_report_content(report, replyReport);
//		alert(this.form.offenderID+' - '+this.baseURL+'#'+this.form.anchorName+' - '+this.section+' - '+comments+' - '+text);
		this.form.showMessage(Language.getLabel('sending'));
	}

}

function ContentReport(offender_id, url, section, comments, content_text){
	this.offenderID = offender_id;
	this.url = url;
	this.section = section;
	this.comments = comments;
	this.contentText = content_text;
}

function ContentReportForm(user_id, anchor_name){
	this.offenderID = user_id;
	this.anchorName = anchor_name;
	this.width = 200;
	this.pane = null;
}

ContentReportForm.prototype.showMessage = function(msg, include_close_button){
	if (this.pane) {
		$A(this.pane.childNodes).each(function(item) {
			// Inside the loop there is no 'this' (better said, 'this' refers to Window)
			ReportManager.form.pane.removeChild(item);
		});
		this.pane.appendChild(document.createTextNode(msg));
		if (include_close_button) {
			this.pane.appendChild(document.createElement('br'));
			var close_button = document.createElement('input');
			close_button.type = 'button';
			close_button.value = Language.getLabel('close');
			Event.observe(close_button, 'click', closeReportForm, false);
			var close_button_area = document.createElement('div');
			close_button_area.appendChild(close_button);
			Element.addClassName(close_button_area,'content_report_button_area');
			this.pane.appendChild(close_button_area);
		}
	}
}

ContentReportForm.prototype.displayResult = function(result){
	var msg_code = 'success_msg';
	if (result) {
		msg_code = result;
	}
	if (result==600) {
		msg_code = 'error_not_logged_in';
	}
	if (result==601) {
		msg_code = 'error_unknown';
	}
	if (result==602) {
		msg_code = 'error_too_many_reports';
	}
	if (result==603) {
		msg_code = 'error_already_reported';
	}
	var msg = Language.getLabel(msg_code);
	this.showMessage(msg, true);

}

ContentReportForm.prototype.close = function(){
	$(this.anchorName).removeChild(this.pane);
}

ContentReportForm.prototype.display = function(image){
	if (!this.pane) {
		var icon_height = 20;
		var icon_width = 20;
		var position = Position.positionedOffset(image);
		var pane_top = position[1]+icon_height;
		var pane_left = position[0]+icon_width-this.width;
		this.pane = document.createElement('div');
		this.pane.appendChild(document.createTextNode(Language.getLabel('prompt')));
		var comment_area = document.createElement('textarea');
		comment_area.id = 'comment_area_id';
		comment_area.cols = 25;
		comment_area.rows = 2;
		this.pane.appendChild(document.createElement('br'));
		this.pane.appendChild(comment_area);
		this.pane.appendChild(document.createElement('br'));
		var accept_button = document.createElement('input');
		accept_button.type = 'button';
		accept_button.value = Language.getLabel('accept');
		var cancel_button = document.createElement('input');
		cancel_button.type = 'button';
		cancel_button.value = Language.getLabel('cancel');
		Event.observe(accept_button, 'click', acceptReport, false);
		Event.observe(cancel_button, 'click', closeReportForm, false);
		Element.addClassName(accept_button,'content_report_button');
		Element.addClassName(cancel_button,'content_report_button');
		var button_area = document.createElement('div');
		button_area.appendChild(accept_button);
		button_area.appendChild(cancel_button);
		Element.addClassName(button_area,'content_report_button_area');
		this.pane.appendChild(button_area);
		Element.setStyle(this.pane, {position: 'absolute', top: pane_top, left: pane_left, width: this.width+'px'});
		Element.addClassName(this.pane, 'content_report_pane');
	//		this.pane.setAttribute('height', 200);
		this.pane.id = 'content_form_pane';
		var parent = $(this.anchorName);
		parent.appendChild(this.pane);
	}
}

function ContentReportLanguage(){
	this.labels = [];
	this.init();
}

ContentReportLanguage.prototype.init = function(){
	this.labels[1] = {
		prompt: 'Confirm that you want to report this content as unacceptable or inappropriate. You may include an optional comment:',
		accept: 'Accept',
		cancel: 'Cancel',
		close: 'Close',
		sending: 'Sending your report...',
		success_msg: 'Your report has been successfully registered. It will be reviewed and the offending content removed from the site. Thanks a lot for your help.',
		error_not_logged_in: 'You are not logged in. Please log in and try again.',
		error_unknown: 'There was a problem submitting your report. Please contact the webmaster.',
		error_too_many_reports: 'To prevent the abuse of this system, you are not allowed to create more than 5 reports in the same day, or more than 20 in the same week.',
		error_already_reported: 'You have already issued a report about this content. Thanks a lot.',
		self_report: 'You can not report a content you have created yourself. Please edit it if you think it is not correct.'
	}
	this.labels[2] = {
		prompt: 'Confirma que quieres informar de este contenido como inaceptable o inapropiado. Puedes incluir un comentario opcional:',
		accept: 'Aceptar',
		cancel: 'Cancelar',
		close: 'Cerrar',
		sending: 'Enviando tu informe...',
		success_msg: 'Se ha registrado correctamente tu informe. Será revisado y se eliminará de la web el contenido ofensivo. Muchas gracias por tu ayuda.',
		error_not_logged_in: 'No te has identificado. Por favor haz login e inténtalo de nuevo.',
		error_unknown: 'Hubo un error al procesar tu informe. Por favor contacta con el webmaster.',
		error_too_many_reports: 'Para prevenir el abuso de este sistema, no se permite la creación de más de 5 informes en el mismo día, ni de más de 20 en la misma semana.',
		error_already_reported: 'Ya has creado un informe sobre este contenido. Muchas gracias.',
		self_report: 'No puedes reportar un contenido que t&uacute; mismo has creado. Por favor modif&iacute;calo si crees que no es correcto.'
	}
}
ContentReportLanguage.prototype.getLabel = function(id){
	return this.labels[ReportManager.language][id];
}

var ReportManager = new ContentReportManager('', '', 1);
var Language = new ContentReportLanguage();
