// 2010.06.16 KJD: Document created based from DC.com
Types = new Array("Convertible","2WD","4WD","RWD","FWD","AWD","2DR","4DR","Coupe","Limited","Turbo");

// 2010.10.15 KJD: Create makes and models
function createmakemodels(){
	if( $( 'select.makes' ).length ){
		$( 'select.makes' )
			.change(function(){
				var thisMake	= $(this).val();
				
				if( thisMake != 'Other' ){
					$( this ).next().fadeOut(500);
				} else {
					$( this ).next().fadeIn(500);
				}
				
				$( 'select.models' )
					.change(function(){
						if( $(this).val() != 'Other' ){
							$( this ).next().fadeOut(500);
						} else {
							$( this ).next().fadeIn(500);
						}			
					})
					.CreateModels( thisMake );
			})
			.CreateMakes();
	
		$( 'select.types' ).CreateTypes();
	}
}

$.fn.CreateMakes	= function(){
	// 2010.10.11 KJD: Get makes
	var Makes	= new Array();
		$.ajax({
			type: 'GET',
			url: (typeof homefilename !='undefined' )?homefilename:thisfilename,
			data: {'page':'get_makes'},
			async: false,
			success: function(thisResponse){
				var myObject = eval('(' + thisResponse + ')');

				for( var i in myObject ){
					Makes[Makes.length] = myObject[i]['name'];
				}
			}
		});
	
	var thisHtml	= "<option value=''>-- select make --</option>"
					+ "<option value='All'>All / Any</option>";
	for( var i in Makes ){
		thisHtml += "<option value=\"" + Makes[i] + "\">" + Makes[i] + "</option>";
	}

	$( this ).html( thisHtml );
	
	// 2010.10.10 KJD: Check rel
	var thisRel	= $( this ).attr( 'rel' );
	if( thisRel ){
		$( this ).find("option[value='" + thisRel + "']").attr('SELECTED',true);
		
		$('select.models').CreateModels( thisRel );
	}
};

$.fn.CreateModels	= function( thisMake ) {
	var thisHtml	= "";

	var Models	= new Object();
	$.ajax({
		type: 'POST',
		url: (typeof homefilename !='undefined' )?homefilename:thisfilename,
		data: {
			'page':'get_models',
			'make' : thisMake
			},
		async: false,
		success: function(thisResponse){
			var myObject = eval('(' + thisResponse + ')');

			for( var i in myObject ){
				Models[ myObject[i]['id'] ] = myObject[i]['name'];
			}
		}
	});
	

	
	if ( thisMake == 'All' ){
		thisHtml+= "<option value='All'>All / Any</option>";
	} else if( thisMake != 'Other' ){
		thisHtml+="<option value=''>-- select a model --</option>"
				+ "<option value='All'>All / Any</option>";
		for( var i in Models ){
			//thisHtml += "<option value='" + i + "'>" + Models[i] + "</option>";
			thisHtml += "<option value=\"" + Models[i] + "\">" + Models[i] + "</option>";
		}
		
		thisHtml += "<option>Other</option>";
	} else if( thisMake == 'Other'){
		thisHtml+= "<option value='All'>All / Any</option>"
				+ "<option>Other</option>";
	}

	$( this ).html( thisHtml );
	
	// 2010.10.10 KJD: Check rel
	var thisRel	= $( this ).attr( 'rel' );
	if( thisRel ){
		$( this ).find("option[value='" + thisRel + "']").attr('SELECTED',true);
	}
};

$.fn.CreateTypes	= function( thisType ){
	$( this ).html( "<option value=''>-- select type --</option>"
					+ "<option value='All'>All / Any</option>"
					+ "<option>" + Types.join("</option><option>") + "</option>" );
	
	var thisRel	= $( this ).attr( 'rel' );
	if( thisRel ){
		$( this ).find("option[value='" + thisRel + "']").attr('SELECTED',true);
	}
};


