$(document).ready(function(){
	$("#overlay, .popup_close").bind('click',function(){
		$("#overlay").hide();
		$(".form-popup").hide();
	});

	$("ul.sitemap li:last").bind('click',function(event){
		event.preventDefault();
		$("#overlay").show();
		$("#feedback").show();
	});

	$("dl.video a.register").bind('click',function(event){
		event.preventDefault();
		$("#overlay").show();
		$("#registration").show();
	});

	$('img[src*="suscr"]').parent().bind('click',function(event){
		event.preventDefault();
		$("#overlay").show();
		$("#subscription").show();
	});

	$('#header_news_subscription').bind('click',function(event){
		event.preventDefault();
		$("#overlay").show();
		$("#subscription").show();
	});
});

$("dl.form-popup form").bind('submit',function(event){
	event.preventDefault();

	// Деактивируем кнопку сабмита
    $(this).find(":submit").attr("disabled","disabled");

	// Собираем все поля
    dataForSend = new Object();
   	$(this).find("input,textarea").each(function(index,elem){
   		if($(elem).attr('name') != "") dataForSend[$(elem).attr('name')] = $(elem).attr('value');
	})

	$.post($(event.target).attr('action'),dataForSend,function(data){
		popup = $(event.target).parents("dl");

		if(data.STATUS == 1)
		{
			// Сворачиваем форму
			$(popup).find("dd").slideUp(500);

			// Заменяем заголовок
			$(popup).find("dt").fadeOut(200,function(){
				$(this).html(data.MESSAGE);
				$(this).fadeIn(200);
			})

		} else { alert(data.MESSAGE); }
		$(popup).find(":submit").removeAttr("disabled");
	},"json")
});
