var Expocamp = {};
Expocamp.jsonResult = false;
Expocamp.registry = new Array();

Expocamp.loadData = function() {
	if(!Expocamp.jsonResult) {
		new Ajax.Request("/include/services/dataService.php", {
			method: "post",
			parameters: {
				mode: "ajax"
			},
			onComplete: function(transport) {
				Expocamp.jsonResult = transport.responseJSON;
				
				document.fire("data:ready");
			}
		});
	}else
		document.fire("data:ready");	
}

Expocamp.getVendors = function(isUsed, vehicleType) {
	Expocamp.registry.isUsed = isUsed;
	Expocamp.registry.vehicleType = vehicleType;
	
	document.observe("data:ready", Expocamp.getVendorCallBack);
	
	Expocamp.loadData();
}

Expocamp.getRanges = function(vendorId) {
	var rangeToOptions = Array();
	
	Expocamp.jsonResult.data.findAll(function(item) {
		if(item.value.vendorId == vendorId) {
			rangeToOptions.push(item.value.rangeId);
		}				
	});
	
	$("rangeChooser").update("");
	$("rangeChooser").insert(
		new Element("option", {
			value: ""
		}).update(translationAll)
	); 
		
	rangeToOptions.uniq().each(function(rangeId) {
		$("rangeChooser").insert(
			new Element("option", {
				value: rangeId
			}).update(Expocamp.jsonResult.ranges[rangeId])
		); 
	});
}

Expocamp.getVendorCallBack = function() {
	var isUsed = (Expocamp.registry.isUsed == 1) ? false : true;
	var vehicleType = Expocamp.registry.vehicleType;
	
	Expocamp.jsonResult.data = $H(Expocamp.jsonResult.data);

	var vendorToOptions = Array();
	
	Expocamp.jsonResult.data.findAll(function(item) {
		if(item.value.used == isUsed && item.value.type == vehicleType) {
			vendorToOptions.push(item.value.vendorId);
		}				
	});
	
	$("vendorChooser").update("");
	$("vendorChooser").insert(
		new Element("option", {
			value: ""
		}).update(translationAll)
	); 
		
	vendorToOptions.uniq().each(function(vendorId) {
		$("vendorChooser").insert(
			new Element("option", {
				value: vendorId
			}).update(Expocamp.jsonResult.vendors[vendorId])
		); 
	});
	
	document.stopObserving("data:ready", Expocamp.getVendorCallBack);
	Expocamp.registry.isUsed = null;
	Expocamp.registry.vehicleType = null;
}

function disableQuickSearchForm() {
	$$(
		"#quickSearch input[type=text]", 
		"#quickSearch input[type=checkbox]",
		"#quickSearch select"
	).invoke("disable");
}

document.observe("dom:loaded", function() {
	// Tabhandling für Quicksearch
	var tabs = $$("#quickSearchTabs a");
	var vehicleStatus = false;
			
	tabs.each(function(tab) {
		tab.observe("click", function() {
			tabs.invoke("removeClassName", "active");
			tab.addClassName("active");
			
			$("quickSearchForm").reset();

			vehicleStatus = tab.readAttribute("rel");
			$("vehicleStatus").setValue(vehicleStatus);
			
			disableQuickSearchForm();
		});
	});
	
	disableQuickSearchForm();
	
	// Aktiv setzen der Form Elemente nach Auswahl des Fahrzeugtyps
	$$("#quickSearch .vehicleTypeChooser").each(function(vehicleTypeChooser) {
		vehicleTypeChooser.observe("click", function() {
			$("quickSearchForm").enable();
			
			if(this.readAttribute("value") == "Reisemobil") {
				$("vehicleWeight").hide();
				$("vehicleProfile").show();
			} else {
				$("vehicleProfile").hide();
				$("vehicleWeight").show();
			}
			
			Expocamp.getVendors($F("vehicleStatus"), this.getValue());
		});
	});
	
	$("vendorChooser").observe("change", function() {
		Expocamp.getRanges(this.getValue());
	});
});