/**
 * @author orange
 */
var SearchForm = Class.create();
var CarManufacturerSelect = Class.create();
var CarBrandSelect = Class.create();
var CarYearSelect = Class.create();
var CarTransmissionSelect = Class.create();

CarManufacturerSelect.prototype={
	initialize: function(element, options){
		this.element=element;
		element.observe("change", this.update.bind(this));
		
	},
	update:function(){
		new Ajax.Request('/ajax.php', {
			parameters:"op=get_car_brands&manufacturer="+this.element.value, 
			onComplete: function(result){
				object=eval(result.responseText);
				if(object){
					if(object.error){
						
					}
					else{
						SearchForm.brandSelect.clear();
						$A(object).each(function(brand){
							SearchForm.brandSelect.add(brand);
						}.bind(this));
						SearchForm.brandSelect.update();
					}
				}
			}.bind(this)
		});
	}
}
CarBrandSelect.prototype={
	initialize: function(element, options){
		this.element=element;
		element.observe("change", this.update.bind(this));
		
	},
	update:function(){
		new Ajax.Request('/ajax.php', {
			parameters:"op=get_car_years&brand="+this.element.value, 
			onComplete: function(result){
				object=eval(result.responseText);
				if(object){
					if(object.error){
						
					}
					else{
						
						SearchForm.yearSelect.clear();
						$A(object).each(function(brand){
							SearchForm.yearSelect.add(brand);
						}.bind(this));
						SearchForm.yearSelect.update();
					}
				}
			}.bind(this)
		});
	},
	add:function(brand){
		this.element.insert(new Element("option",{value:brand.id}).update(brand.name));
		
	},
	clear:function(){
		this.element.options.length=0;
		SearchForm.yearSelect.clear();
	}
}
CarYearSelect.prototype={
	initialize: function(element, options){
		this.element=element;
		element.observe("change", this.update.bind(this));
		
	},
	update:function(){
		new Ajax.Request('/ajax.php', {
			parameters:"op=get_car_transmissions&brand="+SearchForm.brandSelect.element.value+"&year="+this.element.value, 
			onComplete: function(result){
				object=eval(result.responseText);
				if(object){
					if(object.error){
						
					}
					else{
						
						SearchForm.transmissionSelect.clear();
						$A(object).each(function(brand){
							SearchForm.transmissionSelect.add(brand);
						}.bind(this));
						SearchForm.transmissionSelect.update();
					}
				}
			}.bind(this)
		});
	},
	add:function(brand){
		this.element.insert(new Element("option",{value:brand}).update(brand));
		
	},
	clear:function(){
		this.element.options.length=0;
		SearchForm.transmissionSelect.clear();
	}
}
CarTransmissionSelect.prototype={
	initialize: function(element, options){
		this.element=element;
	},
	add:function(brand){
		this.element.insert(new Element("option",{value:brand}).update(brand));
		
	},
	clear:function(){
		this.element.options.length=0;
	}
}
document.observe('dom:loaded', function () {
	SearchForm.manufacturerSelect=new CarManufacturerSelect($("car_manufacturer"));
	SearchForm.brandSelect=new CarBrandSelect($("car_brand"));
	SearchForm.yearSelect=new CarYearSelect($("car_year"));
	SearchForm.transmissionSelect=new CarTransmissionSelect($("car_transmission"));
	SearchForm.manufacturerSelect.update();
});
