
$(document).ready(function() {

	/*///////////////////////// RESULTS SECTION /////////////*/ 
	
	// Select box links
	$("#orderByView").change( function() { 
		optionValue = $(this).children("option:selected").attr("value"); 
		if(optionValue) {
			location.href = optionValue;
		}
	} );

	// Make wheelresult div a link
	$(".wheelResult").click(function() {
		location.href = $(this).children("a").attr("href"); 
	});

	// Hover effect for wheelresult div
	$(".wheelResult").hover(function() {
		$(this).addClass("hover");
	}, function() {
		$(this).removeClass("hover");
	});
	
	
	/*////////////////////////// SEARCH SECTION /////////////*/ 
	
	// Slide out advanced box
	$("#advancedLink").click(function() {
		$("#advancedBox").slideDown("slow");
		$("#advancedLink").hide();
		checkStudDetails();
	});
	
	// Hide advanced box
	$("#advancedClose").click(function() {
		$("#advancedBox").slideUp("slow");
		$("#advancedLink").slideDown("fast");
		$("#studNotice").hide("slow");
	});
	
	
	// Activate check for stud details on change of stud details
	$(".studDetails").change(function() {
		checkStudDetails();
	});
	
    // Make make/model sublist dependent
    makeSublist('make','model',false);    

});

// Check for stud details
function checkStudDetails() { 
	var studInputs = []; 
	studInputs[0] = $("select[@name=studSpacing]").val();
	studInputs[1] = $("select[@name=studNum]").val();
	studInputs[2] = $("input[@name=min_offset]").val();
	studInputs[3] = $("input[@name=max_offset]").val();
	var showError = 0;
	for (var i=0; i<studInputs.length; i++) {
		if ( (studInputs[i] == "0") || (studInputs[i] == "") ) {
		  // TODO
		} else {
		  showError = 1;
		}
	}
	if(showError) {
		$("#studNotice").show("slow");
	} else {
		$("#studNotice").hide("slow");
	}
}

// Create dependent select boxes
function makeSublist(parent,child,isSubselectOptional)
{
    $("body").append("<select style='display:none' id='"+parent+child+"'></select>");
    $('#'+parent+child).html($("#"+child+" option"));
    $('#'+child).html("<option value='0'>Any Model</option>");
    $('#'+parent).change(
        function()
        {
            var parentValue = $('#'+parent).attr('value');
            $('#'+child).html($("#"+parent+child+" .sub_"+parentValue).clone());
            if(isSubselectOptional) $('#'+child).prepend("<option> — Select — </option>");
        });
}

