maxWidth = 200;
picCount = 4;

animateDelay = 300;
hoverDelay = 150;

function initPics(resized)
{
	var head = $(".news-title a#link"+1).html();
	$(".head-news h1 a").text(head);

	var blockWidth = $(".img-line").width()-1;
	/*var spacer =  Math.floor(blockWidth*7/100/picCount);*/
	var spacer =  15;
	var partImgWidth = Math.floor((blockWidth - maxWidth - picCount*spacer)/(picCount-1));

	if (partImgWidth > maxWidth)
	{
		partImgWidth = maxWidth;
		/*spacer = Math.floor((blockWidth - picCount*maxWidth)/picCount);*/
	}

	$(".img-line div").width(partImgWidth);
	/*$(".img-line p.spacer").width(spacer);*/



	if (resized == 1)  $(lastBlock).css({width: maxWidth+"px"}, { queue:false, duration:animateDelay});
	else $(lastBlock).animate({width: maxWidth+"px"}, { queue:false, duration:animateDelay});

	$(lastBlock).find("ins").addClass("block");
}

function animateTo(targetN)
{

			target = $(".img-line div#img"+targetN);
			if($(lastBlock).attr('id') == $(target).attr('id')) return;

			var head = $(".news-title a#link"+targetN).html();
			var head_alt = $(".news-title a#link"+targetN).attr("title");
			var head_href = $(".news-title a#link"+targetN).attr("href");
			$(".head-news h1 a").text(head);
			$(".head-news h1 a").attr('title',head_alt);
			$(".head-news h1 a").attr('href',head_href);

			var blockWidth = $(".img-line").width()-1;
			/*var spacer =  Math.floor(blockWidth*7/100/picCount);*/
			var spacer = 15;
			var partImgWidth = Math.floor((blockWidth - maxWidth - picCount*spacer)/(picCount-1));
			if (partImgWidth > maxWidth)
			{
				partImgWidth = maxWidth;
				/*spacer = Math.floor((blockWidth - picCount*maxWidth)/picCount);*/
			}

			/*$(".img-line p.spacer").animate({queue:false, width: spacer+"px", duration:animateDelay/2});*/

			$(lastBlock).animate({width: partImgWidth+"px"}, { queue:false, duration:animateDelay });
			$(target).animate({width: maxWidth+"px"}, { queue:false, duration:animateDelay});

			targetLink = $("#link" + targetN);
			targetMark = $("#mark" + targetN);
			$(".block-new a").removeClass("active");
			$(targetLink).addClass("active");
			$(".block-new > ins").removeClass("visible");
			$("#mark" + targetN).addClass("visible");


			lastBlock = target;
}

var timeoutId;
var pannedId;

function plannedAnimate()
{
	timeoutId=0;
	animateTo(plannedId);
}

function planAnimateTo(targetN)
{
	plannedId = targetN;
	if (timeoutId!=0) window.clearTimeout(timeoutId);
	timeoutId = window.setTimeout(plannedAnimate, hoverDelay);
}

function clearPlannedAnimate()
{
	if (timeoutId!=0) window.clearTimeout(timeoutId);
	timeoutId = 0;
}

 $(document).ready(function(){
	$("#close-sub a.root").click(function() { 	 $("#open-sub").slideToggle("slow");  });
	$(".with-sub").click(function() { 	 $("#open-navig").slideToggle("slow");  });

  	lastBlock = $("#img1");
	initPics(0);


	$(window).resize(function(){initPics(1);});

    $(".img-line div").hover(
		function()
		{
			id = $(this).attr('id');
			var n = id.substr(3,1) * 1;
			planAnimateTo(n);
		}, clearPlannedAnimate
	);

	$(".block-new a").hover(
		function()
		{
			id = $(this).attr('id');
			var n = id.substr(4,1) * 1;
			planAnimateTo(n);
		}, clearPlannedAnimate
	)
//
	$('#subscribe').click(function() {
		var res = $('#subscript_mail').val();
		//alert('res'+res);
		if(res  == 'Ваш e-mail' || res == ''){
			alert('Введите ваш e-mail');
		}else{
			$.ajax({
			type: "POST",
	    	url: "/bitrix/templates/newsperm_1024/subscribe_back.php",
	    	data: {
	    		'mail': $('#subscript_mail').val()
	    	},
	    	error: function (){
				alert('Ошибка соединения');
	    	},
	    	success: function(data) {
				$('#subsres').html(data);
	  		}

		});
		}
		return false;
	});

 //
	$('.okclose').click(function(){
		$('.message-background').css('display','none');
	})
	$('.redirect2list').click(function(){
		$('.message-background').css('display','none');
		return false;
	})

//
	$('#unsubscribe').click(function() {
		var res = $('#subscript_mail').val();
		//alert('res'+res);
		if(res  == 'Ваш e-mail' || res == ''){
			alert('Введите ваш e-mail');
		}else{
			$.ajax({
			type: "POST",
	    	url: "/bitrix/templates/newsperm_1024/subscribe_back.php",
	    	data: {
	    		'mail': $('#subscript_mail').val()
	    	},
	    	error: function (){
				alert('Ошибка соединения');
	    	},
	    	success: function(data) {
				$('#subsres').html(data);
	  		}

		});
		}
		return false;
	});
//

  });

function view_sub(vis_layer)
{
   vis_layer_new = document.getElementById(vis_layer);
    vis_layer_new.style.visibility='visible';vis_layer_new.style.display='block';

}

function close_sub(invis_layer)
{
    invis_layer_new = document.getElementById(invis_layer);
   invis_layer_new.style.visibility='hidden';
   invis_layer_new.style.display='none';
}


function anichange (N) {
name_layer = $('#msg'+N);
if ( $(name_layer).is(':hidden') ) {
$(name_layer).show();
} else {
$(name_layer).hide();
}
}

