Site.initQueue.push(function(){
	this.ajaxLoader = function(selector){
		$(selector).html(this.loader);
	};
	this.bindModel = function(){
		var func = this;
		$('#car-model').unbind();
		$('#car-model').change(function(){
			func.loadYears(this.value);
		});
	};
	this.bindYear = function(model_id){
		var func = this;
		$('#car-year').unbind();
		$('#car-year').change(function(){
			func.ajaxLoader('#car-types');
			if(this.value!==''){
				$('#car-types').load('/applications/view_types/'+model_id+'/'+this.value,{},function(){
					if($('#car-types').children('select').length>0){
						$('#car-search').hide();
						func.loadTypes();
					}else{
						$('#car-search').show();
					}
				});
			}else{
				$('#car-search').hide();
			}
		});
	};
	this.loadModels = function(make_id){
		if(make_id!=''){
			this.ajaxLoader('#car-models');
			$('#car-years').empty();
			$('#car-types').empty();
			var func = this;
			$('#car-models').load('/car_models/view_by_make/'+make_id,{},function(){
				func.bindModel();
			});
		}
	};
	this.loadYears = function(model_id){
		var func = this;
		if(model_id!=''){
			func.ajaxLoader('#car-years');
			$('#car-types').empty();
			$('#car-years').load('/applications/view_years_by_model/'+model_id,{},function(){
				func.bindYear(model_id);
			});
		}
	};
	this.loadTypes = function(){
		$('#car-type').unbind();
		$('#car-type').change(function(){
			if(this.value!==''){
				$('#car-search').show();
			}else{
				$('#car-search').hide();
			}
		});
	};
	var func = this;
	$('#spec-app-type').change(function(){
		if(this.value==='standard'){
			$('#spec-your-car-search').show();
		}else if(this.value==='conversion'){
			tb_show('Conversion Definition','/conversion_definition?height=200&width=400&inlineId=def-container');
			/*$('<div id="def-container"></div>').appendTo('body').load('/conversion_definition',function(data){
				tb_show('Conversion Definition','#TB_inline?height=200&width=400&inlineId=def-container');
				$('#def-container').remove();
				$('#option-ok').click(function(){
					window.location = '/conversions';
				});
			});*/
		}
	});
	$('#car-make').change(function(){
		func.loadModels(this.value);
	});
	$('#spec-your-car-search').submit(function(){
		var msg = '';
		var error = false;
		if($('#car-make').val()==''){
			$('#car-search').hide();
			error = true;
			msg = 'Please select a Make!';
		}else if($('#car-model').val()==''){
			$('#car-search').hide();
			error = true;
			msg = 'Please select a Model!';
		}else if($('#car-year').val()==''){
			$('#car-search').hide();
			error = true;
			msg = 'Please select a Year!';
		}else if($('#car-type').val()==''){
			$('#car-search').hide();
			error = true;
			msg = 'Please select a Type!';
		}
		if(error){
			$('#car-error').html(msg).show();
			return false;
		}
		var action = '/cars/'+
			Site.getSelectedText($('#car-make')[0])+'/'+
			Site.getSelectedText($('#car-model')[0])+'/'+
			Site.getSelectedText($('#car-year')[0])+'/'+
			Site.getSelectedText($('#car-type')[0]);
		window.location = action;
		return false;
	});
	this.bindModel();
	this.bindYear($('#car-model').val());
	this.loader = new Image();
	this.loader.src = '/img/ajax-loader.gif';
});