$(document).ready(function(){
	//включаем fancybox
	$('a.fancy').fancybox({
		'hideOnContentClick':true, //прячем картинку по тыку
		'zoomSpeedIn':0, 	//показываем без анимации
		'zoomSpeedOut':0, //прячем без анимации
		'overlayShow':true	//серенький фон :)
	});
	
	//Выбираем город
	$('#city-button').click(function(){
		$(this).siblings('ul').toggle();
		return false;
	});
	
	$('#citys li a').click(function(){
		var city_name = $(this).html();
		$(this).parents('ul').siblings('span').html(city_name);
		$(this).parents('ul').hide();
		//top.location.href='?'+$(this).val();
	});
	
	//tooltip
	$('#laeder-header').hover(function(){
		$(this).children('div').show();		
	},
	function(){
		$(this).children('div').hide();
	});
	
	//Развернутый ответ
	$('.ans-quest').click(function(){	
		$(this).parents('.more-opinions').next('.answer').slideToggle();
		return false;
	});
	
	//Обработчик элементов форм
	$('input').each(function(){
		var input_type = $(this).attr('type');
		$(this).addClass('input-'+input_type);
	});
	
	//ссылка "полностью" в отзывах
	$('.amore').click(function(){
		tmp = $(this).parents('.more-opinions').children(".zoom1");
		if ($(this).html() == 'полностью') {
			//tmp.children(".dtext").fadeOut();
			//tmp.children(".dmore").fadeIn();
			$(this).html("скрыть");
		} else {
			//tmp.children(".dtext").fadeIn();
			//tmp.children(".dmore").fadeOut();
			$(this).html("полностью");
		}
		tmp.children(".dtext").toggle();
		tmp.children(".dmore").toggle();
		return false;
	});
	
	//написать модератору
	$("a.modmsg").click(function(){
		$("div.modmsg").slideToggle();
		return false;
	});
	
	//добавить новость
	$("a.modnews").click(function(){
		$("div.modnews").slideToggle();
		return false;
	});
	
	//изменить текст о компании
	$("a.modtext").click(function(){
		$("a.modtext").hide();
		$("div.modtext").hide();
		$("div.modtext2").show();
		//#TODO: актуальный текст передавать на редактирование
		return false;
	});
	
	//отмена изменений текста о компании
	$("input.modtextcancel").click(function(){
		$("a.modtext").show();
		$("div.modtext").show();
		$("div.modtext2").hide();
		return false;
	});
	
	//отмена изменений текста о компании
	$("input.modtextcancel").click(function(){
		//#TODO:
	});
	
	//ответ в гостевую
	$("a.modanswer").click(function(){
		$("div.modanswer").toggle();
		return false;
	});
	
	//rating_switch
	$(".rating_switch a").click(function(){
		if (!$(this).hasClass("current")) {
			$(".rating_switch a").removeClass("current");
			$(this).addClass("current");
			if ($(this).hasClass("rating_all")) { //общий рейтинг
				$("#leaders").hide();
				$("#rating").show();
				$("#laeder-header").html("рейтинг компаний");
			} else { //рейтинг по отраслям
				$("#leaders").show();
				$("#rating").hide();
				$("#laeder-header").html("лидеры отраслей");
			}
			return false;
		}
	});
	
	//динамика день/неделя/месяц
	$("p.dyn_switch a").click(function(){
		$("p.dyn_switch a").removeClass("current");
		$(this).addClass("current");
		$("div.dyn").hide();
		$("div."+$(this).attr("name")).show();
		return false;
	});
	
	//"предложить свой раздел"
	$("select[name='razd']").change(function(){
		if ($(this).val() == -1) {
			$(".razdtitle").show();
		} else {
			$(".razdtitle").hide();
			
			//выбрали отрасль, предлагаем список компаний
			$("#comp_id").show();
		}
	});
	
	//ОБЩАЯ СТРАНИЦА ДОБАВЛЕНИЯ ОТЗЫВА
	//
	$("select[name='catid']").change(function(){
		if ($(this).val() == -1) {
			//"предложить свою
			$("#comp_id").hide();
		} else {
			//выбрали отрасль, предлагаем список компаний
			$("#ajaxgif").show();
			$.post(
				'gb',
				{
					id: $(this).val()
				},
				function(xml){
					str = '<option value="0">ВЫБЕРИТЕ ОРГАНИЗАЦИЮ</option>';
					$("item", xml).each(function(i) {
						texty = this.childNodes[0].data;
						str += '<option value="'+$(this).attr("id")+'">'+texty+'</option>';
					});
					str += '<option value="-1">- Добавить организацию - - - - - - - </option>';
					$("#comp_id_select").html(str);
					$("#comp_id").show();
					$("#ajaxgif").hide();
				}
			);
		}
	});
	$("#comp_id_select").change(function(){
		if ($(this).val() == -1) { //добавить организацию
			$("#compdiv").show();
		} else {
			$("#compdiv").hide();
			$("#of_comp").val(""); //выбрали организацию - очищаем инпут от "добавить организацию"
		}
	});
	
});