function CarBodyField(divID, selectID) {
	
	this.click = function(obj, key, event) {

		if($(obj).hasClass("selected")) {
			this.select.attr("selectedIndex", 0);
			$(obj).removeClass("selected");
			
			if(this.makeModelField) {
				this.makeModelField.filterGrbodyMake('');
				this.makeModelField.filterGrbodyModel('');
			}
			
			return;
		}

		this.select.attr("selectedIndex", key);
		this.div.children(".selected").removeClass("selected");
		$(obj).addClass("selected");
		
		if(this.makeModelField) {
			this.makeModelField.filterGrbodyMake(this.select.attr("value"));
			this.makeModelField.filterGrbodyModel(this.select.attr("value"));
		}
	};
	
	var THIS = this;

	this.select = $("#" + this.prepareID(selectID));
	if(!this.select) {
		return;
	}

	this.div = $("#" + this.prepareID(divID));
	if(!this.div) {
		this.div = this.select.parent();
	}

	this.select.hide();
	this.select.children("option").each(function(key, value) {
		if(key == 0) {
			return;
		}

		var id = selectID + "_" + key;
		THIS.div.append("<div align=\"center\" id=\"" + id + "\" style=\"background-image: url(/i/auto/body_" + key + ".png);\" title=\"" + $(value).html() + "\" class=\"cn\"></div>");
		$("#" + THIS.prepareID(id)).click(function(event) {THIS.click(this, key, event); return false;});
		
		if(key == THIS.select.attr("selectedIndex")) {
			$("#" + THIS.prepareID(id)).trigger("click");
		}
	});

	this.makeModelField = null;
    //this.div.children("div").each(function() {this.style.width = this.offsetWidth < 80 ? '80px' : 'auto';});
}

CarBodyField.prototype = new Field();



// options: makeSelectID, makeUrlPrefix, modelSelectID, modelUrlPrefix, popMakeID, popModelsID, ajaxURL, grbodySelectID
function CarMakeModelFields(options) {
	var THIS = this;
	
	this.change = function(event) {
		var THIS = this;

		if(!this.modelSelect) {
			return true;
		}

		if(this.makeSelect.attr("value")) {
			
			this.modelSelect.html("<option>загружаю список...</option>");
			this.modelSelect.attr("disabled", true);

			if(this.popModels) {
				this.popModels.html('Загружаю список популярных...');
			}
	
			$.get(this.ajaxURL, {id: this.makeSelect.attr("value")}, function(data, textStatus, XMLHttpRequest) {
				var pos = data.search(/<hr\/>/);
				THIS.onload(data.substring(0, pos), textStatus, XMLHttpRequest);
				if(THIS.popModels) {
					THIS.onloadList(data.substring(pos + 5), textStatus, XMLHttpRequest);
				}
			});

		} else {

			this.modelSelect.html("<option>любая</option>");
			
			if(this.popModels) {
				this.popModels.html('');
			}
		}
	};
	
	this.onload = function(data, textStatus, XMLHttpRequest) {
		this.modelSelect.html(data);
		this.modelSelect.attr("disabled", false);
		this.filterGrbodyMake(this.grbodySelect.attr("value"));
	};
	
	this.onloadList = function(data, textStatus, XMLHttpRequest) {
		this.popModels.html(data.length > 0 ? data : 'Все модели хороши, выбирай любую ;)');
		this.initModels();
		this.filterGrbodyModel(this.grbodySelect.attr("value"));
	};
	
	this.makeClick = function(link) {

		if($(link).hasClass("active")) {
			$(link).removeClass("active");
			this.makeSelect.attr("selectedIndex", 0);
			this.change();
			return;
		}
		
		var THIS = this;
		var pos = link.href.indexOf(this.makeUrlPrefix + "=");
		if(pos >= 0) {
			var id = link.href.substring(pos + this.makeUrlPrefix.length + 1);
                        if(id.indexOf("&") > 0) {
                            id = id.substring(0, id.indexOf("&"));
                        }

			if(id != null) {
				this.popMake.children("a.active").removeClass("active");
				$(link).addClass("active");
				this.makeSelect.children("option").each(function(key) {if(this.value == id) THIS.makeSelect.attr("selectedIndex", key);});
				this.change();
			}
		}
	};
	
	this.initModels = function() {
		this.popModels.children("a").click(function(event) {THIS.modelClick(this); return false;});
	};
	
	this.modelClick = function(link) {

		if($(link).hasClass("gray")) {
			return;
		}

		if($(link).hasClass("active")) {
			$(link).removeClass("active");
			this.modelSelect.attr("selectedIndex", 0);
			return;
		}
		
		var THIS = this;
		var pos = link.href.indexOf(this.modelUrlPrefix + "=");
		if(pos >= 0) {
			var id = 1 * link.href.substring(pos + this.modelUrlPrefix.length + 1);
			if(id > 0) {
				this.popModels.children("a.active").removeClass("active");
				$(link).addClass("active");
				this.modelSelect.children("option").each(function(key) {if(this.value == id) THIS.modelSelect.attr("selectedIndex", key);});
			}
		}
	};

	this.filterGrbodyMake = function(grbody) {
		grbody = grbody + "";
		var rexp = new RegExp(grbody);
		this.modelSelect.children("option").each(function() {
			if(grbody.length > 0 && $(this).attr("grbody") && $(this).attr("grbody").search(rexp) == -1) {
				this.disabled = true;
				this.className += "";
			} else {
				this.disabled = false;
				this.className += "";
			}
		});
	};

	this.filterGrbodyModel = function(grbody) {
		grbody = grbody + "";
		var rexp = new RegExp(grbody);
		this.popModels.children("a").each(function() {
			if(grbody.length > 0 && $(this).attr("grbody") && $(this).attr("grbody").search(rexp) == -1) {
				if($(this).hasClass("active")) {
					$(this).trigger("click");
				}
				$(this).addClass("gray");
			} else {
				$(this).removeClass("gray");
			}
		});
	};
	
	this.ajaxURL = options.ajaxURL ? options.ajaxURL : "/ajax/models";
	this.makeSelect = this.get(options.makeSelectID);
	this.makeUrlPrefix = options.makeUrlPrefix;
	this.modelUrlPrefix = options.modelUrlPrefix;
	this.grbodySelect = this.get(options.grbodySelectID);
	if(options.modelSelectID) {
		this.modelSelect = this.get(options.modelSelectID);
		this.popModels = this.get(options.popModelsID);
		this.filterGrbodyMake(this.grbodySelect.attr("value"));
		this.filterGrbodyModel(this.grbodySelect.attr("value"));
	}
	this.popMake = this.get(options.popMakeID);

	this.makeSelect.change(function(event) {THIS.change(event);});
	
	if(this.popMake) {
		this.popMake.children("a").click(function(event) {THIS.makeClick(this); return false;});
	}
	
	if(this.popModels) {
		this.initModels();
	}
}

CarMakeModelFields.prototype = new Field();



function CarYearField(yearLID, yearHID, hintID) {
	var THIS = this;
	
	this.click = function(from, till) {
		this.yearH.attr("value", this.year - from);
		if(till > 0) {
			this.yearL.attr("value", this.year - till);
		} else {
			this.yearL.attr("value", "");
		}
	}

	this.year = new Date().getFullYear();

	this.yearL = this.get(yearLID);
	this.yearH = this.get(yearHID);
	this.hint = this.get(hintID);
	if(!this.yearL || !this.yearH || !this.hint) {
		return;
	}

	this.get(hintID).children("A").css("visibility", "visible");

	$("#year_lt3").click(function(event) {THIS.click(0, 3); return false;});
	$("#year_3_7").click(function(event) {THIS.click(3, 7); return false;});
	$("#year_gt7").click(function(event) {THIS.click(7); return false;});
}

CarYearField.prototype = new Field();



function RegionField(elementID, divID, regex) {
	var THIS = this;
	
	this.click = function(link) {

		if($(link).hasClass("active")) {
			$(link).removeClass("active");
			this.element.attr("value", "");
			return;
		}
		
		var THIS = this;
		var tokens = link.href.match(this.regex);
		if(tokens && tokens.length > 1) {
			this.div.find("a.active").removeClass("active");
			$(link).addClass("active");
			this.element.attr("value", decodeURI(tokens[1]));
		}
	};
	
	this.element = this.get(elementID);
	this.div = this.get(divID);
	this.regex = regex;

	this.div.find("a").click(function(event) {THIS.click(this); return false;});
}

RegionField.prototype = new Field();