$(document).ready(function(){
	
	$("#search_options").click(function () {
		
		//$(this).css("border-bottom","0");
		//border-bottom:1px dashed #8b8b8b;
		//$(this).css("color","#000");
		
		$("#search_options_box").css("display","");
		$("#search_box").css("height","122px");
		
		return false;
	});
	
	$("#requests_steps_vix").click(function () {
		if ($(this).attr("status") == "visible"){
			$(".requests_steps_vix").css("display","none");
			$(this).html("Показать заполненную информацию");
			$(this).attr("status","hidden");
		} else {
			$(".requests_steps_vix").css("display","");
			$(this).html("Скрыть заполненную информацию");
			$(this).attr("status","visible");
		}
		
		return false;
	});
	
	$("#changeactual").click(function () {
		if ($(this).attr("status") == "visible"){
			$("#changeactualp").css("display","none");
			$(this).attr("status","hidden");
		} else {
			$("#changeactualp").css("display","");
			$(this).attr("status","visible");
		}
		
		return false;
	});
	
	$("#changeactual_go").click(function () {
		$.post("/requests/change_act/", { date: $("#datepicker").val(), uid:$(this).attr("link") },
		  function(data){
			if (data != ''){
				$("#changeactual").html(data);
				public_act = 1;
			}
		  });
		
		return false;
	});
	
	$(".new_fac_list").click(function () {
		if ($(this).attr("checked")){
			$("#new_fac_list_summ").html($("#new_fac_list_summ").html() * 1 + $(this).attr("price") * 1);
		}else if (!$(this).attr("checked")){
			$("#new_fac_list_summ").html($("#new_fac_list_summ").html() * 1 - $(this).attr("price") * 1);
		}
	});
	$("#filter_button").click(function () {
		if ($(this).attr("visible") == 'false'){
	      $("#filter").show("slow");
	      $(this).attr("visible","true")
	    }
	    else{
	      $("#filter").hide("slow");
	      $(this).attr("visible","false")
	    }
	      return false;
	});
	$("#filter_c_p").change(function () {
		document.location.href='?show_type_comment='+$(this).val();
	});
	$("#ch_country").change(function () {
		$.post("/catalogauto/get_citys/", { country: $(this).val() },
		  function(data){
			$("#ch_citys").html(data);
		  });
	});
	$("#ch_country1").change(function () {
		$.post("/catalogauto/get_citys/", { country: $(this).val() },
		  function(data){
			$("#ch_citys1").html(data);
		  });
	});
	$("#ch_country2").change(function () {
		$.post("/catalogauto/get_citys/", { country: $(this).val() },
		  function(data){
			$("#ch_citys2").html(data);
		  });
	});
	$("#ch_country3").change(function () {
		$.post("/catalogauto/get_citys/", { country: $(this).val() },
		  function(data){
			$("#ch_citys3").html(data);
		  });
	});
	//Указание доставка в о компании
	$("#d_delivery_ch").change(function () {
		if ($(this).val() == '2'){
			//Страна
			$("#d_delivery_country_b").css("display","");
			$("#d_delivery_city_b").css("display","none");
		} else if ($(this).val() == '3'){
			//Города
			$("#d_delivery_country_b").css("display","");
			$("#d_delivery_city_b").css("display","");
		} else{
			$("#d_delivery_country_b").css("display","none");
			$("#d_delivery_city_b").css("display","none");
		}
	});
	//Указание доставка в о компании
	$("#ch_dels").change(function () {
		if ($(this).val() == '2'){
			//Страна
			$("#ch_country_div_dealers").css("display","");
			$("#ch_citys_div_dealers").css("display","none");
		} else if ($(this).val() == '3'){
			//Города
			$("#ch_country_div_dealers").css("display","");
			$("#ch_citys_div_dealers").css("display","");
		} else{
			$("#ch_country_div_dealers").css("display","none");
			$("#ch_citys_div_dealers").css("display","none");
		}
	});
	
	
	$(".rate_now").click(function () {
		$.post("/articles/rate_now/", { rate: $(this).val(), uid: $(this).attr("link") },
			function(data){
				$("#rating_p").css("display","none");
			  });
	});
	
	$(".rate_now_blog").click(function () {
		$.post("/blogs/rate_now/", { rate: $(this).val(), uid: $(this).attr("link") },
			function(data){
				$("#rating_blog").css("display","none");
			  });
	});
	
	$(".rate_now_news").click(function () {
		$.post("/news/rate_now/", { rate: $(this).val(), uid: $(this).attr("link") },
			function(data){
				$("#rating_news").css("display","none");
			  });
	});
	
	$(".rate_now_product").click(function () {
		$.post("/manufacturers/rate_now_product/", { rate: $(this).val(), uid: $(this).attr("link") },
			function(data){
				$("#rating_product").css("display","none");
			  });
	});
	
	$(".rate_now_producer").click(function () {
		$.post("/manufacturers/rate_now_producer/", { rate: $(this).val(), uid: $(this).attr("link") },
			function(data){
				$("#rating_producer").css("display","none");
			  });
	});
	
	/**************************************Голосование**************************************/
	var current_vote = 0;
	$(".main_votes_answers_i_voteanswer").click(function () {
		current_vote = $(this).val();
	});
	$(".votes___").click(function () { 
		if ($(this).attr("id") == 'vote_res'){
			$.post("/vote/results/", { 
				vote_id: $(this).attr("vote_id")},
			  	function(data){
					t = data.split("|");
					if (t[0] == 1){
						futu_alert("Опрос", data, false, 'error')
					} else if(t[0] == 0){
						$("#resultsvote").html("");
						$("#vote_res").html("Смотреть ответы");
						$("#vote_res").attr("id","vote_ans");
						t1 = t[1].split("&:&");
						for (i = 0 ; i < t1.length ; i ++){
							if (t1[i] == '') 
								continue;
							else {
								t2 = t1[i].split("&=&");
								$("#resultsvote").append("<li style='padding-left:20px;'><label>"+t2[0]+" ("+t2[2]+"%)</label></li>");
							}
						}
						
					}
			  	}
			);
		} else if ($(this).attr("id") == 'vote_ans'){
			$.post("/vote/answers/", { 
				vote_id: $(this).attr("vote_id")},
			  	function(data){
					t = data.split("|");
					if (t[0] == 1){
						futu_alert("Опрос", data, false, 'error')
					} else if(t[0] == 0){
						$("#resultsvote").html("");
						$("#vote_ans").html("Смотреть результаты");
						$("#vote_ans").attr("id","vote_res");
						t1 = t[1].split("&:&");
						for (i = 0 ; i < t1.length ; i ++){
							if (t1[i] == '') 
								continue;
							else {
								t2 = t1[i].split("&=&");
								$("#resultsvote").append('<li><input type="radio" name="voteanswer" value="'+t2[1]+'" id="f1" class="main_votes_answers_i_voteanswer"><label for="f1">'+t2[0]+'</label></li>');
							}
						}
						
					}
			  	}
			);
			
		}
		return false;
	});
	$("#vote_sbt").click(function () { 
		if (current_vote == 0){
			futu_alert("Опрос", "Для голосования в опросе необходимо выбрать ответ!", false, 'error')
		} else {
			$.post("/vote/vote", { 
				vote_id: $(this).attr("vote_id"),
				vote_answer: current_vote},
			  	function(data){
					if (data == 1){
						futu_alert("Опрос", "Ваш голос учтен! Спасибо!", false, 'save')
						$("#vote_res").click();
					} else {
						futu_alert("Опрос", data, false, 'error')
					}
			  	}
			);
		}
	});
	
	
	/**************************************Почта**************************************/
	
	/**			Перемещение писем		**/
	$("#msg_move").change(function () {
		if (confirm('Вы уверены?')){
			ids = '';
			f_dest = '';
			$(".msg_move_check").each( function(){
				if ($(this).attr("checked") == true){
					if (ids != '')
						ids+='_';
					ids+=$(this).val();
				}
			});
			if (ids == ''){
				showerror("Необходимо выбрать хотя бы одно сообщение!");
			} else {
				document.f_move.msgs.value = ids;
				document.f_move.referer.value = document.location.href;
				document.f_move.submit();
			}
		}
	});
	
	
	/**************************************Новая заявка**************************************/
	
	/**			Загрузка шаблона автомобиля		**/
	
	$("#tpl_auto").change(function () {
		if ($(this).val() == '-1')
			return ;
		$(".avtoriz_text").each( function(){
			var key=$(this).attr("name");
			key = key.replace(/auto\[/, "");
			key = key.replace(/spares\[/, "");
			key = key.replace(/tyres\[/, "");
			key = key.replace(/disks\[/, "");
			key = key.replace(/\]/, "");
			/**
			 * Заполняем форму
			 */
			$(this).val($("#tpl_auto option:selected").attr(key));
			if (key == 'p_marka'){
				marka = $("#tpl_auto option:selected").attr(key);
			}
			if (key == 'p_model'){
				model = $("#tpl_auto option:selected").attr(key);
			}
			
			
		});
		$(".avtoriz_text1").each( function(){
			var key=$(this).attr("name");
			key = key.replace(/auto\[/, "");
			key = key.replace(/spares\[/, "");
			key = key.replace(/tyres\[/, "");
			key = key.replace(/disks\[/, "");
			key = key.replace(/\]/, "");
			/**
			 * Заполняем форму
			 */
			$(this).val($("#tpl_auto option:selected").attr(key));
			if (key == 'p_marka'){
				marka = $("#tpl_auto option:selected").attr(key);
			}
			if (key == 'p_model'){
				model = $("#tpl_auto option:selected").attr(key);
			}
			
			
		});
		$(".avtoriz_text11").each( function(){
			var key=$(this).attr("name");
			key = key.replace(/auto\[/, "");
			key = key.replace(/spares\[/, "");
			key = key.replace(/tyres\[/, "");
			key = key.replace(/disks\[/, "");
			key = key.replace(/\]/, "");
			/**
			 * Заполняем форму
			 */
			$(this).val($("#tpl_auto option:selected").attr(key));
			if (key == 'p_marka'){
				marka = $("#tpl_auto option:selected").attr(key);
			}
			if (key == 'p_model'){
				model = $("#tpl_auto option:selected").attr(key);
			}
			
			
		});
		if (auto_main == ''){
			//Подгружаем чекбоксы для шаблона авто
			$.post("/catalogauto/get_alts/", { tpl: $("#tpl_auto option:selected").attr("id") },
				  function(data){
					t = data.split("|");
					for ( i = 0; i < t.length; i ++ ){
						if (t[i] != ''){
							$("#alts"+t[i]).attr("checked","checked");
						}
					}
				  });
		}
		if (marka!=0){
			$("#change_marka").change();
		}
		$("#tpl_auto").val("-1");
	});
	
	/**************************************Работа с каталого авто**************************************/
	var auto_main=0;
	var marka = 0;
	var model = 0;
	var body = 0;
	/**			Загрузка моделей		**/
	$("#change_marka").change(function () {
		$.post("/catalogauto/get_models/", { marka: $(this).val() },
		  function(data){
			$("#change_model").html(data);
			if (model != 0){
				$("#change_model option[value='"+model+"']").attr('selected', 'selected');
				$("#change_model").change();
			}
		  });
	});
	
		
});

function RatingOver(num){
  for (i=1;i<=num;i++){
    document.getElementById("stars"+i).childNodes[0].src = "/img/rate_full.gif";
  }
}
function Rate(num){
  document.formRating.estim.value=num;
  document.formRating.submit();
}

function setHome(A,B){
	A.style.behavior="url(#default#homepage)";
	A.setHomePage(B);
	return false
};


$(document).ready(function(){
	$("a[class^='comgal']").prettyPhoto();
});