
function send_report(button){
	if (button && (ReportManager.status == 'hidden')) {
		ReportManager.dialog = new Dialog(button);
		ReportManager.status = 'form_displayed';
		ReportManager.sendReport();
	}
}

function reportReturn(result){
	ReportManager.status = 'processed';
	ReportManager.dialog.displayResult(result);
}

function closeReportForm(){
	ReportManager.status = 'hidden';
	ReportManager.dialog.close();
}

function MissingSetReportManager(game_id, language){
	this.dialog = null;
	this.gameID = game_id;
	this.language = language;
	this.status = 'hidden';

}

MissingSetReportManager.prototype.sendReport = function(){
	if (this.dialog) {
		this.status = 'waiting';
		var missing_set_name = '';
		var set_name_area = $('set_name_area_id');
		if (set_name_area) {
			missing_set_name = set_name_area.value;
		}
		if (missing_set_name) {
			var report = new MissingSetReport(this.gameID, missing_set_name);
			x_report_missing_set(report, reportReturn);
			this.dialog.showMessage(Language.getLabel('sending'));
		} else {
			this.dialog.showMessage(Language.getLabel('enter_name'), true);
		}
	}

}

function MissingSetReport(game_id, series_name){
	this.gameID = game_id;
	this.seriesName = series_name;
}

function Dialog(element){
	this.width = 200;
	this.pane = null;
	var element_height = 20;
	var element_width = 40;
	var position = Position.positionedOffset(element);
	this.pane_top = position[1]+element_height;
	this.pane_left = position[0]+element_width-this.width;
}

Dialog.prototype.showMessage = function(msg, include_close_button){
	if (this.buildPane()) {
		$A(this.pane.childNodes).each(function(item) {
			// Inside the loop there is no 'this' (better said, 'this' refers to Window)
			ReportManager.dialog.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,'missing_set_report_button_area');
			this.pane.appendChild(close_button_area);
		}
	}
}

Dialog.prototype.buildPane = function(){
	if (!this.pane) {
		this.pane = document.createElement('div');
		Element.setStyle(this.pane, {position: 'absolute', top: this.pane_top, left: this.pane_left, width: this.width+'px'});
		Element.addClassName(this.pane, 'missing_set_report_pane');
		this.pane.id = 'missing_set_pane_id';
		var parent = $('missing_set_form_id');
		parent.appendChild(this.pane);
	}
	return true;
}

Dialog.prototype.displayResult = function(result){
	var msg_code = 'success_msg';
	var msg = Language.getLabel(msg_code);
	this.showMessage(msg, true);

}

Dialog.prototype.close = function(){
	$('missing_set_form_id').removeChild(this.pane);
	this.pane=null;
}


function MissingSetReportLanguage(){
	this.labels = [];
	this.init();
}

MissingSetReportLanguage.prototype.init = function(){
	this.labels[1] = {
		close: 'Close',
		enter_name: 'Please enter the name of the set we are missing.',
		sending: 'Sending your report...',
		success_msg: 'Your report has been successfully registered. We will work on adding that set as soon as possible. Thanks a lot for your help.'
	}
	this.labels[2] = {
		close: 'Cerrar',
		enter_name: 'Por favor introduce el nombre de la expansión que nos falta.',
		sending: 'Enviando tu informe...',
		success_msg: 'Se ha registrado correctamente tu informe. Intentaremos añadir la expansión que nos comentas a la mayor brevedad posible. Muchas gracias por tu ayuda.'
	}
}
MissingSetReportLanguage.prototype.getLabel = function(id){
	return this.labels[ReportManager.language][id];
}

var Language = new MissingSetReportLanguage();
