$(document).ready(function()
{
	$("#makes").change(function(){
		//$("#app_text").hide("slow");
		//reset the current selection
		$("#model").html("<option value='0'>Please Wait</option>");		
		
		$.getJSON("getModels.php", {makeID: $(this).val()}, function(j){													   
			var options = "";
			for(var i=0; i<j.length; i++){
				options += '<option value="'+ j[i].optionValue +'">' + j[i].optionDisplay +'</option>';
			}
			$("#model").html(options).attr('disabled', '');
		});
		$("#engine").html("<option value='0'></option>").attr('disabled', 'disabled');
		$("#part").html("<option value='0'></option>").attr('disabled', 'disabled');
	});
	$("#model").change(function(){
		//reset the current selection
		$("#engine").html("<option value='0'>Please Wait</option>");
		
		$.getJSON("getEngine.php", {makeID: $("#makes").val(), modelID: $(this).val()}, function(j){
			var options = "";
			for( var i=0; i<j.length; i++ ){
				options += '<option value="'+ j[i].optionValue +'">' + j[i].optionDisplay +'</option>';
			}
			$("#engine").html(options).attr('disabled', '');
		});
		$("#part").html("<option value='0'></option>").attr('disabled', 'disabled');
	});

	$("#engine").change(function(){
		//reset the current selection
		$("#part").html("<option value='0'>Please Wait</option>");
		
		$.getJSON("getParts.php", {makeID: $("#makes").val(), modelID: $("#model").val(), engine: $(this).val()}, function(j){													   
			var options = "";
			for(var i=0; i<j.length; i++){
				options += '<option value="'+ j[i].optionValue +'">' + j[i].optionDisplay +'</option>';
			}
			$("#part").html(options).attr('disabled', '');
		});
	});

	$("#part").change(function(){
		$("#resultsFrame").attr("src",'catalogue_results.php?makeID=' + $("#makes").val() + '&modelID=' + $("#model").val() + '&engine=' + $("#engine").val() + '&type=' + $("#part").val());
	});
	
	//Keyword search
	$("#keyword_search").click(function(){
		var keyword = $("#keyword").val();
		if(keyword.length < 3){
			alert("Please enter a keyword more than 3 charactors");
		}else{
			$("#resultsFrame").attr("src", "catalogue_results.php?keyword="+keyword);
		}
	});
	
	//sorting
	$(".sort").click(function(){
		var filter = $(this).attr("href").replace("#", "");
		var src = $("#resultsFrame").attr("src");
		$("#resultsFrame").attr("src", src + "&order=" + filter);
	});
});	   