	$(document).ready(function() {
		$("li.subsection > a").click(function() {
			$(this).parent().toggleClass("selected");
			return false;
		});
		$(".tree a.plus").live("click", function() {
			$(this).attr("class", "node minus").nextAll("ul").show();
			$.fancybox.center();
		});
		$(".tree a.minus").live("click", function() {
			$(this).attr("class", "node plus").nextAll("ul").hide();
			$.fancybox.center();
		});

		$("li.parent").children('a').hover(function(){
			var a = $(this).next('ul');
			//fallover(a);
		});
		
		$("ul.child1 li a").hover(function(){
			var a = $(this).next('ul');
			fallover(a);
		});
	});
	
	function fallover(elem)
{
    var docViewTop = $(window).scrollTop();
    var docViewBottom = docViewTop + $(window).height();

    var elemTop = $(elem).offset().top;
    var elemBottom = elemTop + $(elem).height();

	
    //return (elemBottom >= docViewTop) && (elemTop <= docViewBottom);
	if( elemBottom > docViewBottom )
	{
		elem.css('margin-top', (docViewBottom-elemBottom-25) + 'px');
	}
    //  && (elemBottom <= docViewBottom) &&  (elemTop >= docViewTop) );
	}


	// возвращает список разделов
	function sections(parent) {
		var list = '';
		var active;
		for (var title in catalog[parent]) {
			id = catalog[parent][title];
			if (id > 0) {
				active = (id == breadcrumbs[parent]) ? ' class="selected"' : "";
				list += '<a href="/' + cur_lang + '/catalog/' + id + '"' + active + '>' + title + '</a>';
			}
		}
		return list;
	}

	// отображает элемент для выбора раздела
	function section(parent, current) {
		var id;
		var active;
		var select = '<select onchange="subsection($(this));">';
		for (var title in catalog[parent]) {
			id = catalog[parent][title];
			active = (id == current) ? ' selected="selected"' : "";
			select += '<option value="' + id + '"' + active + '>' + title + '</option>';
		}
		return select += '</select>';
	}

	// обрабатывает выбор раздела
	function subsection(select) {
		var id = select.val();
		// убираем неактуальные
		select.nextAll("select").remove();
		// прячем форму и загрузку
		$(".response").hide();
		$(".form-content").hide();
		if (id > 0) {
			if (catalog[id]) {
				select.removeAttr("name");
				// показываем подкатегории
				$(".catalog").append(section(id));
			} else {
				load_template(select);
			}
		}
	}

	/*function load_template(select) {
		select.attr("name", "catalog");
		select.hide();
		select.show();
		// подгружаем форму
		$(".catalog > select").attr("disabled", "disabled");
		$(".catalog > select").hide();
		$(".form-loader").show();
		$(".form-content").load("/offer-form/" + select.val(), function() {
			// после загрузки
			$(this).find("a.fancybox").fancybox();
			$(".form-loader").hide();
			$(".form-content").fadeIn();
			$(".catalog > select").removeAttr("disabled");
			$(".catalog > select").show();
		});
	}*/
