var autoCacheOptions = {
	marcheUrl: '/res/auto/marche.json',
	baseModelloUrl: '/res/auto/'
};

var motoCacheOptions = {
	marcheUrl: '/res/moto/marche.json',
	baseModelloUrl: '/res/moto/'
};

/*
	Class AutoCache
*/
var AutoCache = Class.extend({
	// configuration
	marcheUrl: null,
	baseModelloUrl: null,
	
	// fields
	formSel: null,
	marcaSelectNames: [], 
	modelloSelectNames: [], 
	initValues: [],
	
	// internal fields
	marcheDone: false,
	initMarcheDone: false,
	modelliCache: null,

	// methods
	init: function (options) {
		$.extend(this, options);
		this.modelliCache = {};
	},

	prepare: function (options) {
		$.extend(this, options);
		
		for (var i = 0; i < this.marcaSelectNames.length; i++) {
			$(this.formSel).find("select[name='" + this.marcaSelectNames[i] + "']").change(this.onMarcaChange.bind(this, this.marcaSelectNames[i], this.modelloSelectNames[i], null));
		}
		
		this.enableMarche();
	},
	
	disableMarche: function () {
		for (var i = 0; i < this.modelloSelectNames.length; i++) {
			$(this.formSel).find("select[name='" + this.modelloSelectNames[i] + "']").attr("disabled", "disabled");
		}
		for (var i = 0; i < this.marcaSelectNames.length; i++) {
			$(this.formSel).find("select[name='" + this.marcaSelectNames[i] + "']").attr("disabled", "disabled");
		}
	},
	
	enableMarche: function () {
		if (!this.marcheDone) {
			$.ajax({
				url: this.marcheUrl,
				success: function (data) {
					eval(data); // defines marcheList
					for (var i = 0; i < marcheList.length; i++) {
						for (var j = 0; j < this.marcaSelectNames.length; j++) {
							$(this.formSel).find("select[name='" + this.marcaSelectNames[j] + "']").append("<option value=\"" + marcheList[i].value + "\">" + marcheList[i].text + "</option>");
						}
					}
					this.marcheDone = true;
					this.enableMarche_onSuccess();
					if (!this.initMarcheDone) {
						this.initializeMarcheSelect();
					}
				}.bind(this)
			});
		} else {
			this.enableMarche_onSuccess();
		}
	},
	
	enableMarche_onSuccess: function () {
		for (var i = 0; i < this.marcaSelectNames.length; i++) {
			$(this.formSel).find("select[name='" + this.marcaSelectNames[i] + "']").attr("disabled", "");
		}
		for (var i = 0; i < this.modelloSelectNames.length; i++) {
			$(this.formSel).find("select[name='" + this.modelloSelectNames[i] + "']").attr("disabled", "");
		}
	},
	
	onMarcaChange: function (marcaSelect, modelloSelect, modelloInitValue) {
		var marcaValue = $(this.formSel).find("select[name='" + marcaSelect + "']").val();
		$(this.formSel).find("select[name='" + modelloSelect + "']").html("<option selected=\"selected\" value=\"\">Selezionare un modello</option>");
		if (marcaValue != '') {
			if (this.modelliCache[marcaValue] == undefined) {
				$.ajax({
					url: this.baseModelloUrl + marcaValue + ".json",
					success: function (data) {
						eval(data); // defines modelliList
						this.modelliCache[marcaValue] = modelliList;
						this.onMarcaChange_onSuccess(modelloSelect, marcaValue, modelloInitValue);
					}.bind(this)
				});
			} else {
				this.onMarcaChange_onSuccess(modelloSelect, marcaValue, modelloInitValue);
			}
		}
	},
	
	onMarcaChange_onSuccess: function (modelloSelect, marcaValue, initValue) {
		var values = this.modelliCache[marcaValue];
		for (var i = 0; i < values.length; i++) {
			$(this.formSel).find("select[name='" + modelloSelect + "']").append("<option value=\"" + values[i].value + "\">" + values[i].text + "</option>");
		}
		if (initValue != null) {
			$(this.formSel).find("select[name='" + modelloSelect + "']").val(initValue);
		}
	},
	
	initializeMarcheSelect: function () {
		if (this.initMarcheDone) {
			return;			
		}

		for (var i = 0; i < this.initValues.length; i++) {
			if (this.initValues[i] == '') {
				continue;
			}
			
			var splitIndex = this.initValues[i].indexOf('_');
			var marcaValue = splitIndex >= 0 ? this.initValues[i].substring(0, splitIndex) : this.initValues[i];
			
			$(this.formSel).find("select[name='" + this.marcaSelectNames[i] + "']").val(marcaValue);
			this.onMarcaChange(this.marcaSelectNames[i], this.modelloSelectNames[i], this.initValues[i]);
		}
		
		this.initMarcheDone = true;
	}
});

var autoCache = new AutoCache(autoCacheOptions);
var motoCache = new AutoCache(motoCacheOptions);

/*
	Class AutoNameCache
*/
var AutoNameCache = Class.extend({
	// configuration
	marcheUrl: null,
	baseModelloUrl: null,
	
	// internal fields
	marcheDone: false,
	marcheCache: null,
	modelliCache: null,
	toDo: null,

	// methods
	init: function (options) {
		$.extend(this, options);
		this.marcheCache = {};
		this.modelliCache = {};
		this.toDo = new Array();
	},
	
	initMarche: function () {
		$.ajax({
			url: this.marcheUrl,
			success: function (data) {
				eval(data); // defines marcheList
				for (var i = 0; i < marcheList.length; i++) {
					this.marcheCache[marcheList[i].value] = marcheList[i].text;
				}
				this.marcheDone = true;
				this.processToDo();
			}.bind(this)
		});
	},
	
	get: function (where, what) {
		var splitIndex = what.indexOf('_');
		if (splitIndex < 0) {
			return;
		}
		
		if (!this.marcheDone) {
			this.initMarche();
		}
		
		var marcaValue = what.substring(0, splitIndex);
		var modelloValue = what.substring(splitIndex + 1);
		
		this.toDo.push({ where: where, marca: marcaValue, modello: modelloValue, done: false });

		if (this.modelliCache[marcaValue] == undefined) {
			this.modelliCache[marcaValue] = "x";
			$.ajax({
				url: this.baseModelloUrl + marcaValue + ".json",
				success: function (data) {
					eval(data); // defines modelliList
					this.modelliCache[marcaValue] = modelliList;
					this.processToDo();
				}.bind(this)
			});
		} else if (this.modelliCache[marcaValue] == "x") {
			// richiesta gia' in corso.
		} else {
			this.processToDo();
		}
	},

	processToDo: function () {
		for (var j = 0; j < this.toDo.length; j++) {
			if (!this.toDo[j].done) {
				if (this.modelliCache[this.toDo[j].marca] == undefined || this.modelliCache[this.toDo[j].marca] == "x") {
					continue;
				}
				var values = this.modelliCache[this.toDo[j].marca];
				
				this.toDo[j].done = true;
				var key = this.toDo[j].marca + "_" + this.toDo[j].modello;
				var name = "";
				for (var i = 0; i < values.length; i++) {
					if (values[i].value == key) {
						name = values[i].text;
					}
				}
				if (name.length > 0) {
					if ($(this.toDo[j].where).html().length > 0) {
						$(this.toDo[j].where).append(", ");
					}
					$(this.toDo[j].where).append(this.marcheCache[this.toDo[j].marca] + " " + name);
				}
			}
		}
	}
});

var autoNameCache = new AutoNameCache(autoCacheOptions);
var motoNameCache = new AutoNameCache(motoCacheOptions);

