var filtered_brand = null;

$(document).ready(function () {
	if ($('#brand').attr('type') == 'hidden') {
		// $("select#model").attr("disabled", "disabled");
		// vtwin = true;
		filtered_brand = $("#brand").val();
	}
	
	xhr = null;
	xhr2 = null;
	$("#year").change(function () {
		if( this.value != 0 ){
			if (xhr) xhr.abort();
			if (filtered_brand != null) { // V-Twin page: brand preset to H-D
				$("select#model").html("<option>loading...</option>");
				$("#brand").change();
			} else {
				$("select#brand").html("<option>loading...</option>");
				$("select#model").attr("disabled", "disabled");
				$("select#product").attr("disabled", "disabled");
				var section = $("#section").val();
				xhr = $.getJSON("/motorcycle/search/by_motorcycle_ajax/" + section + "/" + this.value + "/", function(json){
					update_selects(json);
					xhr = null;
				});
			}
		}
	});
	
	$("#brand").change(function () {
		if( this.value != 0 ){
			if (xhr) xhr.abort();
			$("select#model").html("<option>loading...</option>");
			$("select#product").attr("disabled", "disabled");
			var year = $("#year").val();
			var section = $("#section").val();
			xhr = $.getJSON("/motorcycle/search/by_motorcycle_ajax/" + section + "/" + year + "/" + this.value + "/", function(json){
				update_selects(json);
				xhr = null;
			});
		}
	});
	
	$("#model").change(function () {
		if( this.value != 0  && !check_selects_and_submit() ){
			if (xhr) xhr.abort();
			$("select#product").html("<option>loading...</option>");
			var year = $("#year").val();
			var brand = $("#brand").val();
			var section = $("#section").val();
			
			xhr = $.getJSON("/motorcycle/search/by_motorcycle_ajax/" + section + "/" + year + "/" + brand + "/" + this.value + "/", function(json){
				update_selects(json);
				xhr = null;
			});
		}
		
	});
	$("#product").change(function () {
		check_selects_and_submit();
	});	
	
	
	
	
	$("#narrowed_distr").change(function () {
		window.location = 'http://' + location.hostname + '/motorcycle/search/by_motorcycle/' + 
			$("#fd_section").val() +'/' +
			$("#fd_year").val() +'/' +
			$("#fd_brand").val() +'/' +
			$("#fd_model").val() +'/' +
			$("#fd_product").val() +'/' +
			$("#narrowed_distr").val().replace(/&/g,"_").replace(/,/g,"_").replace(/\./g,"_").replace(/ /g,"_") +'/';
	});
});

function update_selects(lists) {
	var options="";
	if (lists.brand && filtered_brand == null) {
		var last_index;
		jQuery.each(lists.brand, function(i, me) {
			if (typeof me == 'object') {
				options += '<option value="' + i + '" selected>' + me['selected'] + '</option>';
			} else {
				options += '<option value="' + i + '">' + this + '</option>';
			}
			last_index = this+"";
		});
		$("select#brand").html(options);
		$("select#brand").removeAttr("disabled");
	}
	var options="";
	if (lists.model) {
		var last_index;
		jQuery.each(lists.model, function(i, me) {
			if (typeof me == 'object') {
				//console.log(me);
				options += '<option value="' + i + '" selected>' + me['selected'] + '</option>';
			} else {
				options += '<option value="' + i + '">' + this + '</option>';
			}
			last_index = this+"";
		});
		$("select#model").html(options);
		$("select#model").removeAttr("disabled");
	}
	var options="";
	if (lists.product) {
		jQuery.each(lists.product, function(i, me) {
			if (typeof me == 'object') {
				options += '<option value="' + i + '" selected>' + me['selected'] + '</option>';
			} else {
				options += '<option value="' + i + '">' + this + '</option>';
			}
		});
		$("select#product").html(options);
		$("select#product").removeAttr("disabled");
	}
	check_selects_and_submit();
}

function check_selects_and_submit() {
	var ready = true;
	$(".rst").each(function(i){
		if (this.value == 0) ready = false;
	});
	if (ready) {
		if (filtered_brand == null) {
			var section_param = $("#section").val();
		} else {
			if (filtered_brand == 'HD') {
				var section_param = 'American_V-Twin';
			} else {
				var section_param = filtered_brand;
			}
		}
		var url = 'http://' + location.hostname + '/motorcycle/search/by_motorcycle/' + 
			section_param +'/' +
			$("#year").val() +'/' +
			$("#brand").val().replace(/&/g,"_").replace(/ /g,"_") +'/' +
			$("#model").val().replace(/&/g,"_").replace(/ /g,"_").replace(/\//g,"|") +'/';
			
		if ($("#product").val()) {
			url += $("#product").val().replace(/&/g,"_").replace(/ /g,"_") +'/';
		} else {
			url += 'all/';
		}
	window.location = url;
	} else {
		return false;
	}
}

