function garaj_setOverlay() {
	$("#overlay").click(function(){
		$(".modal").fadeOut(500, function(){
			$("#overlay").fadeOut(500, function(){$(this).remove()});
		});
	});

}

function closeDiv(div) {
	$("#"+div).fadeOut(500, function() {
		$("#overlay").fadeOut(500, function(){$(this).remove()});
	});

}

function garaj_changeModel(div) {
	var doc_height = $(document).height();

	$('<div id="overlay"></div>')
	.appendTo("body")
	.height(doc_height)
	.css({
		'display': 'block',
		'position': 'fixed',
		'width': '100%',
		'background': '#000',
		'opacity': '0',
		'top': 0,
		'left': 0,
		'z-index': 9999
	});

	$("#overlay").animate({opacity: 0.5}, 500, function() {
		garaj_setOverlay();
		$("#"+div).fadeIn(500);
	});
}

function garaj_selectModels(_this) {
	$.ajax({
		type: "GET",
		url: "/classes/Ajaxer/select_model.php",
		data: "brand_id="+_this.value,
		success: function(html) {
			$("#top_model").html(html);
            $("#top_model").attr("disabled", false);
		}
	});
}

function garaj_selectYears(_this) {
	$.ajax({
		type: "GET",
		url: "/classes/Ajaxer/select_years.php",
		data: "model="+_this.value,
		success: function(html){
			$("#top_year").html(html);
			$("#top_year").attr("disabled", false);
		}
	});
}

function garaj_realChangeModel(select) {
	var new_model = document.getElementById(select).value;
	if (new_model == "" || new_model == "select") {
		return false;
	}

	$.ajax({
		type: "GET",
		url: "/classes/Ajaxer/xml_car.php",
		data: "id="+new_model,
		success: function(xml) {
			var obj = $(xml).find('car');
			var id = obj.find('id').text();
			var brand = obj.find('brand').text();
			var model = obj.find('model').text();
			var years = obj.find('years').text();
			var name = brand + ' ' + model + ' (' + years + ')';
			var photo = obj.find('photo').text();

			$(".car_garaj_name").html(name);
			$("img.car_garaj_img").attr("src", photo);
            $("a#opinions").attr("href", '/opinions/model/'+id+'/');
            $("a#zakaz_top_garaj").attr("href", '/pages/zakaz/'+id+'/');
            $("a#add_opinion").attr("href", '/opinions/add/?select='+id+'#head');
            $("a#add_link").attr("href", "/auth/visitorsinfo/garaj/addcar/?select="+id);
            $("select#brand_cars option#null").attr("selected", "selected");
            closeDiv('change_model');
            closeDiv('change_model_only');
		}
	});

	$.ajax({
		type: "GET",
		url: "/classes/Ajaxer/refresh_selector.php",
		data: "model="+new_model,
		success: function(html){
			$("select#brand_cars").html(html);
		}
	});
}
