// 2010.10.24 KJD: document created
$( document ).ready(function(){
	
	// 2010.11.14 KJD: check for makes selected
	if( $( 'select.make' ).attr("rel") ){
		GetModels( $( 'select.make' ).attr("rel"), $( 'select.year' ).attr("rel") );
	}
	
	// 2010.11.14 KJD: Check for make selected
	$( 'select.make' ).change(function(){
		var thisYear	= $( this ).parents('form').find('select.year').val();
		
		GetModels( $( this ).val(), thisYear );
	});
	
	$( 'select.year' ).each(function(){
		var thisDate	= new Date();
		var lowYear		= 1948;
		var highYear	= thisDate.getFullYear();
		var thisHtml	= "";
		
		for( var i = highYear; i >= lowYear; i--){
			thisHtml	+= "<option value='"+i+"'>" + i + "</option>";
		}
		
		$( this ).append( thisHtml );
	});
	
	$( '.findform' ).attr('action', 'index.php');
	
	$( '.colorbox' ).colorbox();

	$('.cart').validate();
	
	// 2010.10.27 KJD: Set selected year,make
	$( '.year, .make, .model').each(function(){
		if( $( this ).attr('rel') ){
			$( this ).val( $( this ).attr('rel') );
		}
	});

	// 2010.10.27 KJD: Contact form
	$( 'form.contactform').validate({
		submitHandler: function( form ){
			$.ajax({
				type: 'POST',
				url: "index.php",
				data: $(form).serialize(),
				success: function(thisResponse){
					alert( thisResponse );
					$( form ).find('.reset').click();
				}
			});		
			
			return false;
		}
	});
	
	$( 'form.additionalshipping').validate();
	
	// $( '.corner').corner();
});


// 2010.11.14 KJD: get models
function GetModels( thisMake, thisYear ){

	$.ajax({
		type: 'POST',
		url: "index.php",
		data: {
			'page': 'models',
			'json': 'true',
			'make': thisMake,
			'year': thisYear
		},
		success: function(thisResponse){
			var theseModels	= eval("("+ thisResponse +")");
			var thisHtml	= "<option value=''>Any</option>";
			for( var i in theseModels ){
				thisHtml	+= "<option value='" + theseModels[i]['model'] + "'>" + theseModels[i]['model'] + "</option>";
			}
			
			$( 'select.model').html( thisHtml );
		}
	});	
}
