
// constants

var CONFRONTOBOX_MAX_ROWS_COUNT = 20;
var CONFRONTOBOX_SILENT = false;

// functions

function confrontoBox_addRow(mar, mod, all, aa, mm, marcaLabel, descrLabel, callRemote) {
	if (confrontoBox_getRowsCount() >= CONFRONTOBOX_MAX_ROWS_COUNT) {
		alert('Raggiunto il numero massimo di confronti contemporanei.');
		return;
	}
	
	// row data
	var data = {
		"mar": mar,
		"mod": mod,
		"all": all,
		"aa": aa,
		"mm": mm,
		"marcaLabel": marcaLabel,
		"descrLabel": descrLabel
	};
	
	// remote call
	callRemote = typeof(callRemote) != 'undefined' ? callRemote : true;
	var remoteOk = true;
	
	if (callRemote) {
		$.ajax({
			url: "/listino/ConfrontoBoxProxy.cfm",
			type: "post",
			dataType: "json",
			data: $.extend({}, data, { "method": "AddCar" }),
			async: false,
			success: function (data) { remoteOk = data; },
			error: function (request) { alert(request.responseText); remoteOk = false; }
		});
	}
	if (!remoteOk)
		return;
	
	// function code
	var $box = $("#confrontoBox");
	var $contents = $("div.boxSxContent", $box);
	
	var $row = $('<div class="boxSxRiga">').data("data", data);
	
	var $marcaTxt = $('<p class="pMarca"><a href="/listino/listamodelli.cfm?mar=' + mar + '"></a></p>');
	var $descrTxt = $('<p class="descrizione_auto"></p>');
	var $closeBut = $('<div class="pul_chiudi"><a class="deleteLink"><img src="/asset/img/nuovo/img_pul_chiudi.png" width="13" height="13" alt="cancella"></a></div>');
	
	$("a", $marcaTxt).html(marcaLabel);
	$descrTxt.html(descrLabel);
	$("a", $closeBut).click(function() { confrontoBox_removeRow($row); });
	
	$('<div class="boxSxTxt_auto">').append($marcaTxt).append($descrTxt).appendTo($row);
	$closeBut.appendTo($row);
	
	$('<input type="hidden" />').
		attr("name", "allestimento").
		attr("value", all + "_" + aa + "_" + mm).
		appendTo($row);
	
	//$row.prependTo($contents);
	$row.insertBefore($(".boxSxRiga_finale", $contents));
	confrontoBox_updateLayout();
	
	if (!CONFRONTOBOX_SILENT) {
		alert("Auto aggiunta e pronta per il confronto.");
	}
}

function confrontoBox_getRowsCount() {
	return $("#confrontoBox div.boxSxContent div.boxSxRiga").length;
}

function confrontoBox_removeRow($row, data, callRemote) {
	callRemote = typeof(callRemote) != 'undefined' ? callRemote : true;
	var remoteOk = true;
	
	if (callRemote) {
		$.ajax({
			url: "/listino/ConfrontoBoxProxy.cfm",
			type: "post",
			dataType: "json",
			data: $.extend({}, $row.data("data"), { "method": "RemoveCar" }),
			async: false,
			success: function (data) { remoteOk = data; },
			error: function (request) { alert(request.responseText); remoteOk = false; }
		});
	}
	if (!remoteOk)
		return;
	
	// function code
	$row.remove();
	confrontoBox_updateLayout();
}

function confrontoBox_removeAll(callRemote) {
	callRemote = typeof(callRemote) != 'undefined' ? callRemote : true;
	var remoteOk = true;
	
	if (callRemote) {
		$.ajax({
			url: "/listino/ConfrontoBoxProxy.cfm",
			type: "post",
			dataType: "json",
			data: { "method": "RemoveAll" },
			async: false,
			success: function (data) { remoteOk = data; },
			error: function (request) { alert(request.responseText); remoteOk = false; }
		});
	}
	if (!remoteOk)
		return;
	
	// function code
	var $box = $("#confrontoBox");
	
	$("div.boxSxContent div.boxSxRiga", $box).remove();
	confrontoBox_updateLayout();
}

function confrontoBox_submit() {
	$('input[name="allestimento"]:first').attr("name", "modelloBase");
	document.confrontoForm.submit();
}

function confrontoBox_updateLayout() {
	var count = confrontoBox_getRowsCount();
	
	$deleteAllDisabled = $("#confrontoBox .deleteAllDisabled");
	$deleteAll = $("#confrontoBox .deleteAll").parent();
	$submit = $("#confrontoBox .submit");
	
	if (count > 1) {
		$deleteAllDisabled.hide();
		$deleteAll.show();
		$submit.show();
	} else if (count == 1) {
		$deleteAllDisabled.hide();
		$deleteAll.show();
		$submit.hide();
	} else {
		$deleteAllDisabled.show();
		$deleteAll.hide();
		$submit.hide();
	}
	
	if (count > 0) {
		$("#confrontoBox .carsCounterText").text("(" + count + ")");
	} else {
		$("#confrontoBox .carsCounterText").text("");
	}
}

