$(function()
{	
	var lang = $("#header").attr("class");
	build();
	
	$("#nimg li").each(function(){inews($(this));$(this).hide();});
	inews($("#nimg li").eq(0));
	//newsSel($("#nlist ul li").eq(0));
	map();
	newsHeight();
	paddingBoc();
	topImage();
	events();
	openMenu();
	
	
	/* IE */
	if ($.support.opacity == false)
	{
		$("#newsblock").prepend('<p id="tl"/><p id="tr"/>').after('<p id="bl"/><p id="br"/>');
	}
	/* IE */
	
	$("#3d1").everyTime(5000, function(i)
	{	
		if ($("#3d1").css("display") == "none") $("#3d1").show();else $("#3d1").hide();
		if ($("#3d2").css("display") == "none") $("#3d2").show();else $("#3d2").hide();
	});
	
});

$(window).resize(function()
{
	newsHeight();
	radiusTopImage();
	//paddingBoc();
});

$(window).load(function()
{
	radiusImg();
	var sdv = 13;
	if ($.browser.version == '7.0') sdv = 9;
	
	newsSel($("#nlist ul li").eq(0));
	$("#nimg li").each(function(){inews($(this));$(this).hide();});
	inews($("#nimg li").eq(0));
});

function radiusImg()
{
	//$(".container img, .iro img, .rimg img, #ndesc img, #nimg img")
	$(".article img, .iro img")
		.not("#sshow img, .mapuniq img, #newsm im-g")
		.before('<i class="i1"/><i class="i2"/><i class="i3"/><i class="i4"/>')
		.not(".iro img, #ndesc img").each(function()
		{
			var w = $(this).width()  - 13 + "px";
			var h = $(this).height() - 13 + "px";		
			$(this)
				.prev().css("margin", h + " 0 0 " + w)
				.prev().css("margin-top", h)
				.prev().css("margin-left", w);
		});
}

function events()
{
	$("#statvideo a").click(function(){$("#cl").remove();$("#statvideo").before('<embed id="cl" width="426px" height="320px" allowscriptaccess="always" allowfullscreen="true" src="/resource/player.swf" type="application/x-shockwave-flash" flashvars="file=' + $(this).attr('rel') +'"/>');});
	$("a.kvideo, a.photo").fancybox({"frameWidth":640,"frameHeight":520,"centerOnScroll":false});
	
	// Перелистывание "Знаете ли вы"
	//$(".bol div span").click(function(){know(this);});
	
	$("#nlist li").hover(function(){newsSel(this);});
	$("#act span").click(function(){act(this);});
	
	$("#nimg span").click(function()
	{
		$(this).parent().hide();
		if ($(this).hasClass("n"))
		{
			inews($(this).parent().next());
		}
		else inews($(this).parent().prev());
	});
}

function radiusTopImage()
{
	$("#header .i2, #header .i4").css("margin-left", $("#timg").width() - 9 + "px");
}


function topImage()
{
	ar = $("#timg").attr("class").split('-');
	var ins = '';
	for(var key in ar)
	{
		ins += '<img src="/ufiles/top/' + ar[key] + '.jpg"/>\n';
	}
	
	$("#timg").append('<div id="topi">' + ins + '</div>');
	$("#topi").nivoSlider(
	{
		//'sliceDown,sliceDownLeft,sliceUp,sliceUpLeft,sliceUpDown,sliceUpDownLeft,fold,fade,random,slideInRight,slideInLeft'
		effect:'fade',
		animSpeed:1500,
		pauseTime:9000,
		controlNav:false
		//startSlide: 4
	});
}
function openMenu()
{
	// Открытие меню
	$("#nav > li").hover(function()
	{
		var margin = 0;
		$(this).children("a").addClass("selmenu");
		var ul = $(this).children("ul");
		$("#nav > li:lt(" + $("#nav > li").index(this) + ")").each(function()
		{
			margin += $(this).width();
		});
		
		clone = ul.clone().appendTo(this).css("left", 0);
		all = margin + clone.width() - $(".cont").width();
		clone.empty();
		if (all > 0) ul.css("margin-left", "-" + all + "px");
		ul.slideDown(300);
	},
	function()
	{
		$(this).find("ul").slideUp(300);
		$(this).children("a").removeClass("selmenu");
	});
}

function build()
{
	$("#timg").before('<p class="clear"><i class="i1"/><i class="i2"/><i class="i3"/><i class="i4"/></p>');
	radiusTopImage();
	
	
	$(".article table[border=0]").css("border", 0).find("td, th").css({border:0, background:0});
	
	$("#nav > li span:not(.noli) ").prepend("<em/>").find("em").css("margin-left", $("#nav span").width() / 2 - 3);
	$("#nav > li").first().css("border-left", 0);
	$("#nav > li").last().css("border-right", 0);

	$("#nav ul li").css("background", "#b2c4e3 url('/i/menu-sub.png') repeat-x");
	// Удаление лишних бордеров
	$("#nav ul").each(function()
	{
		$(this).find("a, span").first().css("border-left", 0);
		$(this).find("a, span").last().css("border-right", 0);
	});
	
	$("#nimg li").prepend('<p class="alpha"/>');
	
	// Список новостей или блоги добавить маркер
	$(".ndate, .npdate, .boc ul li").prepend("<i/>");
	
	// Добавление скруглений саб-меню
	$("#nav ul").append('<li class="submenu_r"/>').prepend('<li class="submenu_l"/>');
	
	// Переключатель новостей
	$("#nlist li").prepend("<span/><em/>");
	
	// Листать акции
	//$("#act, .bol div, #nimg li").prepend("<span class='p'/><span class='n'/>");
	$("#act, #nimg li").prepend("<span class='p'/><span class='n'/>");
	
	$("#act .p").hide();
	if ($("#act p").length < 2) $("#act .n").hide();
	$("#act p").eq(0).show();
	mt = 24 - $("#act div").height()/2;
	if (mt < 0) mt = 0;
	$("#act div").css("margin-top", mt);
	
	var infob = $("#infoblocks h2");
	infob.prepend('<span/>');
	infob.eq(0).children("span").css("background-position","0 -68px");
	infob.eq(1).children("span").css("background-position","-24px -44px");
	infob.eq(3).children("span").css("background-position","-24px -68px");
	
	// Меню слева
	$("#menu li, .arr li").prepend("<i/>");

	// Поиск
	$("#header input:text").focus(function(){if ($(this).val() == "Поиск по сайту...") $(this).val("");});
	$("#header form").submit(function(){location.href = $("#header").attr("class") + "/search/" + $(this).find("input:text").val();return false;});
}

//Карта
function map()
{
	$("#map p").click(function()
	{
		var lab = $("#map div").length;
		var el = $(this);
		if (lab == 0)
		{
			$.get("/ajax/mapinfo", {lang: "w" + $("#header").attr("class")}, 
			function(data)
			{
				data = data.replace('<p>', '').replace('</p>', '<br/>');
				//alert(data);
				$("#map").append(data).find("span a").fancybox({"frameWidth":640,"frameHeight":520,"centerOnScroll":false});
				mapShow(el);
			});
		}
		else mapShow($(this));
	}).eq(1).css({margin:"295px 0 0 10px", width:"160px"});
	
	// Закрыть плашку с картой 
	$("#map em").live("click", function(){$(this).parent().fadeOut();});
}

function mapShow(el)
{
	$("#map div").eq($("#map p").index(el)).fadeIn();
}

//Картинка в новости
function inews(el)
{
	el.show();
	if (el.next().length == 0) el.children(".n").hide();else el.children(".n").show();
	if (el.prev().length == 0) el.children(".p").hide();else el.children(".p").show();
	//alert(9);
	var dh = el.find("div").height() + 5;
	var ih = el.find("img").height();
	
	if (ih > 60)
	{
		el.children(".alpha").css({height: dh, marginTop: ih - dh + "px"});
		el.children("div").css({"margin-top": ih - dh + "px", visibility:"visible"});
		el.find(".i1, .i2").css("margin-top", 0);
		el.find(".i3, .i4").css("margin-top", ih - 14 + "px");
	}
}

// Расстановка паддингов у блоков
function paddingBoc()
{
	s = Math.floor(($(".cont").width() - 990) / 6.6);
	//alert(s);
	if (s < 5) s = 4 + s;
	if (s > 20) s = 20;
	
	$(".boc div:gt(0)").css("padding", "17px " + s + "px");
	blogs = $(".boc div").eq(0);
	blogs.css("width", blogs.width() + s*2 + "px");
}
	
// Выбор новости
function newsSel(el)
{
	$(el).children("span").css("margin-top", $(el).height() / 2 - 6 + "px");
	$(el).parent().find("li").removeClass("nsel");
	$(el).addClass("nsel");
	var e = $("#ndesc div").hide().eq($("#nlist ul li").index(el));
	e.show().find(".i3, .i4").css("margin-top", e.find("img").height() - 15 + "px");
	newsHeight();
}

// Высота блока новостей
function newsHeight()
{
	var ndesc = $("#ndesc").height();
	var nlist = $("#nlist").height();
	var newsblock = (ndesc > nlist) ? ndesc : nlist;
	$("#newsblock").height(newsblock + 30);	
}

// Акции (отврат. переделать)
function act(el)
{
	var parent = $(el).parent();
	var span = parent.children("span");
	var p = parent.find("p");
	var pos = p.index(parent.find("p:visible"));
	var tmp = 1;
	
	p.hide();
	span.hide();
	
	// Если нажата кнопка "Вперед"
	if ($(el).hasClass("n"))
	{
		pos = pos + 2;
		tmp = 0;
	}
	
	if (pos < p.length) parent.children(".n").show();
	if (pos > tmp) parent.children(".p").show();	
	p.eq(pos - 1).show();
	mt = 24 - $("#act div").height()/2;
	if (mt < 0) mt = 0;
	parent.children("div").css("margin-top", mt);
}

// Знаете ли вы
function know(el)
{
	local = $("#header form").attr("class");
	//alert(local);
	var par = $(el).parent();
	var met = 'n';
	if ($(el).hasClass("p")) met = 'p';
	$.getJSON("/ajax/know/?id=" + par.attr("class") + "&local=" + local + "&met=" + met, function(data)
	{
		if (data.id > 0)
		{
			par.attr("class", data.id).find("img").attr("src", "/ufiles/know/" + data.id + "_th.jpg");
			par.find("em a").attr("href", "/know/" + data.id);
			par.next().html(data.text);
		}
	});	
}
