function list_tyre_ratio() {
	var tyre_width = jQuery("#tyre_width").val();
	if(tyre_width != '') {
		jQuery.ajax({
			type: "POST",
			url: "/index.php/search/list_tyre_ratio",
			data: "width=" + tyre_width,
			dataType: "html",
			success: function(result){
				jQuery('#tyre_ratio').removeAttr('disabled'); 
				jQuery("#tyre_ratio").html(result);
			}
		});
	}
}
function list_tyre_diameter() {	
	var tyre_width = jQuery("#tyre_width").val();
	var tyre_ratio = jQuery("#tyre_ratio").val();
	if(tyre_width != '' && tyre_ratio != '') {
		jQuery.ajax({
			type: "POST",
			url: "/index.php/search/list_tyre_diameter",
			data: "width=" + tyre_width + "&ratio=" + tyre_ratio,
			dataType: "html",
			success: function(result){
				jQuery('#tyre_diameter').removeAttr('disabled'); 
				jQuery("#tyre_diameter").html(result);
			}
		});
	}
}
function list_tyre_brand() {	
	var tyre_width = jQuery("#tyre_width").val();
	var tyre_ratio = jQuery("#tyre_ratio").val();
	var tyre_diameter = jQuery("#tyre_diameter").val();
	if(tyre_width != '' && tyre_ratio != '' && tyre_diameter != '') {
		jQuery.ajax({
			type: "POST",
			url: "/index.php/search/list_tyre_brand",
			data: "width=" + tyre_width + "&ratio=" + tyre_ratio + "&diameter=" + tyre_diameter,
			dataType: "html",
			success: function(result){
				jQuery('#tyre_brand').removeAttr('disabled'); 
				jQuery("#tyre_brand").html(result);
			}
		});
	}
}


function list_wheel_width() {
	var wheel_diameter = jQuery("#wheel_diameter").val();
	if(wheel_diameter != '') {
		jQuery.ajax({
			type: "POST",
			url: "/index.php/search/list_wheel_width",
			data: "diameter=" + wheel_diameter,
			dataType: "html",
			success: function(result){
				jQuery('#wheel_width').removeAttr('disabled'); 
				jQuery("#wheel_width").html(result);
			}
		});
	}
}
function list_wheel_brand() {
	var wheel_width = jQuery("#wheel_width").val();
	var wheel_diameter = jQuery("#wheel_diameter").val();
	if(wheel_width != '' && wheel_diameter != '') {
		jQuery.ajax({
			type: "POST",
			url: "/index.php/search/list_wheel_brand",
			data: "width=" + wheel_width + "&diameter=" + wheel_diameter,
			dataType: "html",
			success: function(result){
				jQuery('#wheel_brand').removeAttr('disabled'); 
				jQuery("#wheel_brand").html(result);
			}
		});
	}
}

function find_tyres() {
	if(jQuery("#product_selector .links a.tyres:first").hasClass("selected")) {
		jQuery("#product_selector .links a.tyres:first").removeClass("selected");
		jQuery("#product_selector img:first").removeClass("active");
		if(!jQuery('#tyre_selector').hasClass('hide')) {
			jQuery('#tyre_selector').addClass('hide');
		}
		if(!jQuery('#wheel_selector').hasClass('hide')) {
			jQuery('#wheel_selector').addClass('hide');
		}		
	} else {
		jQuery("#product_selector .links a.tyres:first").addClass("selected");
		jQuery("#product_selector img:first").addClass("active");
		if(jQuery('#tyre_selector').hasClass('hide')) {
			jQuery('#tyre_selector').removeClass('hide');
		}
		if(!jQuery('#wheel_selector').hasClass('hide')) {
			jQuery('#wheel_selector').addClass('hide');
		}
	}
	if(jQuery("#product_selector .links a.wheels:first").hasClass("selected")) {
		jQuery("#product_selector .links a.wheels:first").removeClass("selected");		
	}
	return false;
}

function find_wheels() {
	if(jQuery("#product_selector .links a.wheels:first").hasClass("selected")) {
		jQuery("#product_selector .links a.wheels:first").removeClass("selected");
		jQuery("#product_selector img:first").removeClass("active");
		if(!jQuery('#wheel_selector').hasClass('hide')) {
			jQuery('#wheel_selector').addClass('hide');
		}
		if(!jQuery('#tyre_selector').hasClass('hide')) {
			jQuery('#tyre_selector').addClass('hide');
		}
	} else {
		jQuery("#product_selector .links a.wheels:first").addClass("selected");
		jQuery("#product_selector img:first").addClass("active");
		if(jQuery('#wheel_selector').hasClass('hide')) {
			jQuery('#wheel_selector').removeClass('hide');
		}
		if(!jQuery('#tyre_selector').hasClass('hide')) {
			jQuery('#tyre_selector').addClass('hide');
		}
	}
	if(jQuery("#product_selector .links a.tyres:first").hasClass("selected")) {
		jQuery("#product_selector .links a.tyres:first").removeClass("selected");		
	}
	return false;
}

function search_tyres() {
	var width = jQuery('#tyre_width').val();
	var ratio = jQuery('#tyre_ratio').val();
	var diameter = jQuery('#tyre_diameter').val();
	var brand = jQuery('#tyre_brand').val();
	if(width == '') {
		alert('Please select width');
		return false;
	} else if(ratio == '') {
		alert('Please select ratio');
		return false;
	} else if(diameter == '') {
		alert('Please select diameter');
		return false;
	} else {
		jQuery('#tyre_selector_go').attr('href', '/index.php/summary/tyres/search/w_' + width + '/r_' + ratio + '/d_' + diameter + '/b_' + brand + '/p_1');
		return true;
	}	
}

function search_wheels() {
	var width = jQuery('#wheel_width').val();
	var diameter = jQuery('#wheel_diameter').val();
	var brand = jQuery('#wheel_brand').val();
	if(diameter == '') {
		alert('Please select diameter');
		return false;
	} else if(width == '') {
		alert('Please select width');
		return false;
	} else {
		jQuery('#wheel_selector_go').attr('href', '/index.php/summary/wheels/search/w_' + width + '/d_' + diameter + '/b_' + brand + '/p_1');
		return true;
	}	
}

function show_all_sizes() {
	jQuery("a.all_sizes").hide(0);
	jQuery("table#partial").addClass("hide");
	jQuery("table#all").removeClass("hide");
	return false;
}



jQuery(document).ready(function() {
	jQuery("#tyre_width").change(function() {
		jQuery("#tyre_ratio").html('');
		jQuery('#tyre_ratio').attr('disabled', 'disabled'); 
		jQuery("#tyre_diameter").html('');
		jQuery('#tyre_diameter').attr('disabled', 'disabled'); 
		jQuery("#tyre_brand").html('');
		jQuery('#tyre_brand').attr('disabled', 'disabled'); 
		list_tyre_ratio();
	});
	jQuery("#tyre_ratio").change(function() {		
		jQuery("#tyre_diameter").html('');
		jQuery('#tyre_diameter').attr('disabled', 'disabled'); 
		jQuery("#tyre_brand").html('');
		jQuery('#tyre_brand').attr('disabled', 'disabled'); 
		list_tyre_diameter();
	});
	jQuery("#tyre_diameter").change(function() {
		jQuery("#tyre_brand").html('');
		jQuery('#tyre_brand').attr('disabled', 'disabled'); 
		list_tyre_brand();
	});
	jQuery("#wheel_diameter").change(function() {
		jQuery("#wheel_width").html('');
		jQuery('#wheel_width').attr('disabled', 'disabled'); 
		jQuery("#wheel_brand").html('');
		jQuery('#wheel_brand').attr('disabled', 'disabled'); 
		list_wheel_width();
		
	});
	jQuery("#wheel_width").change(function() {
		jQuery("#wheel_brand").html('');
		jQuery('#wheel_brand').attr('disabled', 'disabled'); 
		list_wheel_brand();
	});
	
	jQuery("table.summary tr").bind("click", function(e) {
		window.location.href = jQuery("a:first", jQuery(this)).attr("href");		
	});
	jQuery("#product_bottom table tr:not(:first) input:checkbox").bind("click", function(e) {
		e.stopPropagation();
	});
	jQuery("#product_bottom table tr:not(:first)").bind("click", function(e) {
		if(jQuery("input:checkbox", jQuery(this)).is(":checked")) {
			jQuery("input:checkbox", jQuery(this)).attr("checked", false);
		} else {
			jQuery("input:checkbox", jQuery(this)).attr("checked", true);
		}
	});
	jQuery("ul.applications li").bind("click", function(e) {
		window.location.href = jQuery("a.btn", jQuery(this)).attr("href");
		return false;
	});
	
	jQuery("a.request_a_quote").bind("click", function(e) {
		var query = '';
		
		if(jQuery("body").hasClass("tyres_product") || jQuery("body").hasClass("wheels_product")) {							
			if(jQuery("#product_bottom table:not(.hide) tr input:checkbox").length == 1) {
				jQuery("#product_bottom table:not(.hide) tr input:checkbox").attr("checked", true);
			}

			if(jQuery("#product_bottom table:not(.hide) tr input:checkbox:checked").length == 0) {
				alert("Please pick the product(s)");
				return false;				
			} else {
				query = jQuery("#product_bottom table:not(.hide)").closest("form").serialize();
			}
		} else if(jQuery("body").hasClass("packages_product")) {			
			query = jQuery("form#package_info").serialize();					
		}

		jQuery.ajax({
			type: "POST",
			url: "/index.php/quote",
			data: query,
			success: function(msg){
				window.location.href = jQuery("a.request_a_quote").attr("href");
			}
		});
		return false;
	});

	jQuery(document).pngFix(); 	
});
