// ********** Variables **********
var getInfo = '/getinfo.php?';
var selectYear = {'0':'Select a Year'};
var selectMake = {'0':'Select a Make'};
var selectModel = {'0':'Select a Model'};
var selectBody = {'0':'Select a Body Style'};
var selectWindow = {'0':'Select a Window'};
var selectLoading = {'0':'Loading...'};
var selectBlank = {'0':'-'};


// ********** System Load **********
$(document).ready(function() {
	// ********** Add Change Events **********
	// set the year change event: load the makes and set other options to select make
	$('#year').change(function() {
		selectPrevious('make',selectBlank);
		selectPrevious('model',selectBlank);
		selectPrevious('body',selectBlank);
		selectPrevious('part',selectBlank);
		$('#submit').attr('disabled','disabled');
		setCookie('autoglassYear',$(this).val(),30);
		if($(this).val()!=0) {
			$('#make').addOption(selectLoading);
			loadMakes($(this).val());
		}
	});

	// set the make change event: load the model and set other options to select model
	$('#make').change(function() {
		selectPrevious('model',selectBlank);
		selectPrevious('body',selectBlank);
		selectPrevious('part',selectBlank);
		$('#submit').attr('disabled','disabled');
		setCookie('autoglassMake',$(this).val(),30);
		if($(this).val()!=0) {
			$('#model').addOption(selectLoading);
			loadModels($('#year').val(), $(this).val());
		}
	});
		
	// set the model change event: load the body and set other options to select body
	$('#model').change(function() {
		selectPrevious('body',selectBlank);
		selectPrevious('part',selectBlank);
		$('#submit').attr('disabled','disabled');
		setCookie('autoglassModel',$(this).val(),30);
		if($(this).val()!=0) {
			$('#body').addOption(selectLoading);
			loadBodys($('#year').val(), $('#make').val(), $(this).val());
		}
	});

	// set the model change event: load the body and set other options to select body
	$('#body').change(function() {
		selectPrevious('part',selectBlank);
		$('#submit').attr('disabled','disabled');
		setCookie('autoglassBody',$(this).val(),30);
		if($(this).val()!=0) {
			$('#part').addOption(selectLoading);
			loadParts($(this).val());
		}
	});

	// set the auto image to display the window selected and enable the submit
	$('#part').change(function() {
		$('#submit').attr('disabled','disabled');
		setCookie('autoglassPart',$(this).val(), 30);
		if($(this).val()!=0) {
			$('#submit').removeAttr('disabled');

			// change the car image
			var valueText = $(this).selectedTexts();
			if(valueText[0]) highlightWindow(valueText[0]);
		}
	});

	$('#part').keyup(function() {
		// change the car image
		var valueText = $(this).selectedTexts();
		if(valueText[0]) highlightWindow(valueText[0]);
	});

	$('#part').mousemove(function() {
		// change the car image
		var valueText = $(this).selectedTexts();
		if(valueText[0]) highlightWindow(valueText[0]);
	});

		
	// ********** Initialize the Year **********
	// set year to loading and disable other selects
	$('#year').addOption(selectLoading);
	selectPrevious('make',selectBlank);
	selectPrevious('model',selectBlank);
	selectPrevious('body',selectBlank);
	selectPrevious('part',selectBlank);
	$('#submit').attr('disabled','disabled');
	loadYears();
});
	


// ********** FUNCTIONS TO LOAD THE SELECT BOXES **********
function selectPrevious(selectid, option) {
	// clear the options
	$('#'+selectid).empty();
	// set the option
	$('#'+selectid).addOption(option);
	// disable the select
	$('#'+selectid).attr('disabled','disabled');
}
	
function loadYears() {
	$.getJSON(getInfo,
		function(json) {
			// disable the select
			$('#year').attr('disabled', 'disabled');
			// clear the options
			$('#year').empty();
			// add a "choose year" option
			$('#year').addOption(selectYear);
			// add the options
			$('#year').addOption(json.years, false);
			// enable the select
			$('#year').removeAttr('disabled');
			// if cookie, select the year and load the makes
			if(getCookie('autoglassYear') && $('#year').containsOption(getCookie('autoglassYear')) && (getCookie('autoglassYear')!=0)) {
				$('#year').selectOptions(getCookie('autoglassYear'),true);
				loadMakes($('#year').val());
			}
		}
	);
}

function loadMakes(year) {
	$.getJSON(getInfo,
		{year: year},
		function(json) {
			// disable the select
			$('#make').attr('disabled', 'disabled');
			// clear the options
			$('#make').empty();
			// add a "choose make" option
			$('#make').addOption(selectMake);
			// add the options
			$('#make').addOption(json.makes, false);
			// enable the select
			$('#make').removeAttr('disabled');
			// if cookie, select the make and load the models 
			if(getCookie('autoglassMake') && $('#make').containsOption(getCookie('autoglassMake')) && (getCookie('autoglassMake')!=0)) {
				$('#make').selectOptions(getCookie('autoglassMake'),true);
				loadModels($('#year').val(), $('#make').val());
			}
		}
	);
}

function loadModels(year, make) {
	$.getJSON(getInfo,
		{year: year, make: make},
		function(json) {
			// disable the select
			$('#model').attr('disabled', 'disabled');
			// clear the options
			$('#model').empty();
			// add a "choose model" option
			$('#model').addOption(selectModel);
			// add the options
			$('#model').addOption(json.models, false);
			// enable the select
			$('#model').removeAttr('disabled');
			// if cookie, select the model and load the bodys 
			if(getCookie('autoglassModel') && $('#model').containsOption(getCookie('autoglassModel')) && (getCookie('autoglassModel')!=0)) {
				$('#model').selectOptions(getCookie('autoglassModel'),true);
				loadBodys($('#year').val(), $('#make').val(), $('#model').val());
			}
		}
	);
}

function loadBodys(year, make, model) {
	$.getJSON(getInfo,
		{year: year, make: make, model: model},
		function(json) {
			// disable the select
			$('#body').attr('disabled', 'disabled');
			// clear the options
			$('#body').empty();
			// add a "choose body" option
			$('#body').addOption(selectBody);
			// add the options
			$('#body').addOption(json.bodys, false);
			// enable the select
			$('#body').removeAttr('disabled');
			// if cookie, select the body and load the parts 
			if(getCookie('autoglassBody') && $('#body').containsOption(getCookie('autoglassBody')) && (getCookie('autoglassBody')!=0)) {
				$('#body').selectOptions(getCookie('autoglassBody'),true);
				loadParts($('#body').val());
			}
		}
	);
}

function loadParts(carid) {
	$.getJSON(getInfo,
		{carid: carid},
		function(json) {
			// disable the select
			$('#part').attr('disabled', 'disabled');
			// clear the options
			$('#part').empty();
			// add a "choose part" option
			$('#part').addOption(selectWindow);
			// add the options
			$('#part').addOption(json.parts, false);
			// enable the select
			$('#part').removeAttr('disabled');
			// if cookie, select the part 
			if(getCookie('autoglassPart') && $('#part').containsOption(getCookie('autoglassPart')) && (getCookie('autoglassPart')!=0)) {
				$('#part').selectOptions(getCookie('autoglassPart'),true);
				$('#submit').removeAttr('disabled');
			}
		}
	);
}

function highlightWindow(valueText) {
	if(valueText.match(/^Windshield/i)) {
		$('#auto').attr('src', 'images/windshield.jpg');
	} else if(valueText.match(/^Vent - Front/i)) {
		$('#auto').attr('src', 'images/vent-front.jpg');
	} else if(valueText.match(/^Door - Front/i)) {
		$('#auto').attr('src', 'images/door-front.jpg');
	} else if(valueText.match(/^Door/i)) {
		$('#auto').attr('src', 'images/door.jpg');
	} else if(valueText.match(/^Vent/i)) {
		$('#auto').attr('src', 'images/vent.jpg');
	} else if(valueText.match(/^Quarter/i)) {
		$('#auto').attr('src', 'images/quarter.jpg');
	} else {
		$('#auto').attr('src', 'images/auto.jpg');
	}
}

