$(function(){

	var self = this;

	var $anonstvBlock = $('.anonstv_block');
	var $lenta = $('.lenta', $anonstvBlock);
	var $blocks = $('> .block', $lenta);
	var $anons = $('.anons', $blocks);

	var $dots = $('> ul', $anonstvBlock);

	var bW = 420;

	var manual = false;

	$dots.html('');
	$blocks.each(function(i){
		var k = i+1;
		$dots.append('<li>'+k+'</li>');
		var $dot = $dots.find('> li:last');
		$dot.click(function(){
			manual = true;
			$dots.find('> li.act').removeClass('act');
			$anons.hide();
			$dot.addClass('act');
			$lenta.stop().animate({ left:-i*bW }, 500, function(){
				$anons.eq(i).fadeIn(500);
			});
		});
	});
	$dots.find('> li:first').addClass('act');
	$anons.eq(0).show();


	this.nextPlay = function(){
		if(manual){
			manual = false;
		} else {
			var $lis = $dots.find('> li');
			var k = $lis.index($dots.find('> li.act'));
			if(k+1 == $lis.length){
				k = 0;
			} else {
				k++;
			}
			$dots.find('> li.act').removeClass('act');
			$anons.hide();
			$lis.eq(k).addClass('act');
			$lenta.stop().animate({ left:-k*bW }, 1000, function(){
				$anons.eq(k).fadeIn(300);
			});
		}
		setTimeout(self.nextPlay, 8000);
	}

	setTimeout(self.nextPlay, 8000);

});

function initSliderEvents()
{
	$('.video_block .prev').hide();
	if($('.video_block .movie_block .movie_list div').length <= 2)
	{
		$('.video_block .next').hide();
	}
	
	/**
	* Слайдер фоток 1 - Назад
	*/
	$('.video_block .prev').click(function() {
		var lenta = $('.movie_block .movie_list', $(this).parent());
		var lentaml = parseInt(lenta.css('margin-left'));
		if(!lentaml) lentaml = 0;
		if (lentaml < 0) {
			lenta.animate({marginLeft: '+=86px'});
			if(lentaml == -86) $(this).hide();
			$('.next', $(this).parent()).show();
		}
		else
		{
			if($(this).css('display') == 'none') $(this).show();
		}
	});
	
	/**
	 * Слайдер фоток 1 - Вперед
	 */
	$('.video_block .next').click(function() {
		var lenta = $('.movie_block .movie_list', $(this).parent());
		var lentaml = parseInt(lenta.css('margin-left'));
		if(!lentaml) lentaml = 0;
		var lentaln = $('div', lenta).length;
		if (lentaml > (lentaln - 2) * -86) {
			lenta.animate({marginLeft: '-=86px'});
			if(-lentaml >= (lentaln - 3) * 86) $(this).hide();
			$('.prev', $(this).parent()).show();
		}
		else
		{
			if($(this).css('display') == 'none') $(this).show();
		}
	});
	
	
	$('.telecast_list .txt').click(function() {
		document.location.href = $("a", $(this).parent()).attr('href');
	});
	
	$('.telecast_list .prev').hide();
	if($('.telecast_list .lenta img').length <= 2)
	{
		$('.telecast_list .next').hide();
	}
	
	/**
	* Слайдер фоток 2 - Назад
	*/
	$('.telecast_list .prev').click(function() {
		var lenta = $('.lenta', $(this).parent());
		var lentaml = parseInt(lenta.css('margin-left'));
		if(!lentaml) lentaml = 0;
		if (lentaml < 0) {
			lenta.animate({marginLeft: '+=317px'});
			if(lentaml == -317) $(this).hide();
			$('.next', $(this).parent()).show();
		}
		else
		{
			if($(this).css('display') == 'none') $(this).show();
		}
	});
	
	/**
	 * Слайдер фоток 2 - Вперед
	 */
	$('.telecast_list .next').click(function() {
		var lenta = $('.lenta', $(this).parent());
		var lentaml = parseInt(lenta.css('margin-left'));
		if(!lentaml) lentaml = 0;
		var lentaln = $('img', lenta).length;
		if (lentaml >= (lentaln - 2) * -317) {
			lenta.animate({marginLeft: '-=317px'});
			if(-lentaml >= (lentaln - 3) * 317) $(this).hide();
			$('.prev', $(this).parent()).show();
		}
		else
		{
			if($(this).css('display') == 'none') $(this).show();
		}
	});
	
	
	$('.telecast_list2 .prev').hide();
	if($('.telecast_list2 .lenta img').length <= 2)
	{
		$('.telecast_list2 .next').hide();
	}
	
	/**
	* Слайдер фоток 3 - Назад
	*/
	$('.telecast_list2 .prev').click(function() {
		var lenta = $('.lenta', $(this).parent());
		var lentaml = parseInt(lenta.css('margin-left'));
		if(!lentaml) lentaml = 0;
		if (lentaml < 0) {
			lenta.animate({marginLeft: '+=317px'});
			if(lentaml == -317) $(this).hide();
			$('.next', $(this).parent()).show();
		}
		else
		{
			if($(this).css('display') == 'none') $(this).show();
		}
	});
	
	/**
	 * Слайдер фоток 3 - Вперед
	 */
	$('.telecast_list2 .next').click(function() {
		var lenta = $('.lenta', $(this).parent());
		var lentaml = parseInt(lenta.css('margin-left'));
		if(!lentaml) lentaml = 0;
		var lentaln = $('img', lenta).length;
		if (lentaml > (lentaln - 2) * -317) {
			lenta.animate({marginLeft: '-=317px'});
			if(-lentaml >= (lentaln - 3) * 317) $(this).hide();
			$('.prev', $(this).parent()).show();
		}
		else
		{
			if($(this).css('display') == 'none') $(this).show();
		}
	});
	
	$('.info_foto .lenta_cont img').click(function(){
		$('.info_foto .big img').attr("src", $(this).attr("src"));
		$('.act', $(this).parent()).removeClass("act");
		$(this).addClass("act");
		$('.info_foto .txt').html($(this).attr("caption"));
	});
	
	/*$('.video_block .movie_block .movie_list span').click(function(){
		var obFlash = $(".flash", $(this).parent().parent().parent().parent());
		$("object param[name=flashvars]", obFlash).attr('value',"imageURL="+$("img", this).attr('src')+"&videoURL="+$(this).attr('href'));
	});*/
}

function initMainMenuEvents()
{
	/**
	 * Главное всплывающее меню - при наведении
	 */
	var $rootMenuItems = $(".menu > li");
	$rootMenuItems.mouseover(function(){
		var obSubmenu = $(".submenu", this);
		var menuCount = $rootMenuItems.length;
		var curIndex = $rootMenuItems.index($(this))
		if(obSubmenu != null && obSubmenu.length)
		{
			//obSubmenu.parent().css("z-index","1");
			if(curIndex == menuCount - 1) {
				$(this).addClass('last');
			}
			
			obSubmenu.show();
		}
	});
	
	/**
	 * Главное всплывающее меню - при отведении
	 */
	$rootMenuItems.mouseout(function(){
		var obSubmenu = $(".submenu", this);
		if(obSubmenu != null && obSubmenu.length)
		{
			//obSubmenu.parent().css("z-index","0");
			obSubmenu.hide();
		}
	});
	
	/*$(".menu li").click(function(){
		var obSubmenu = $(".submenu", this);
		if(obSubmenu != null && obSubmenu.length)
		{
			return false;
		}
	});*/
	
	/**
	 * Жанровое всплывающее меню - при наведении
	 */
	$(".menu2 li").mouseover(function(){
		var obSubmenu = $(".submenu", this);
		if(obSubmenu != null && obSubmenu.length)
		{
			$(this).css("z-index","2");
			obSubmenu.show();
		}
	});
	
	/**
	 * Жанровое всплывающее меню - при отведении
	 */
	$(".menu2 li").mouseout(function(){
		var obSubmenu = $(".submenu", this);
		if(obSubmenu != null && obSubmenu.length)
		{
			$(this).css("z-index","0");
			obSubmenu.hide();
		}
	});
}

function initFeedBackFormEvents()
{
	var obFldFeedBackContainer = $(".feedback input");
	
	if(obFldFeedBackContainer != null)
	{
		for(var i=0; i < obFldFeedBackContainer.length; i++)
		{
			/**
			 * Скрытие лэйблов для FeedBack - input
			 */
			$($(".feedback input").get(i)).bind('focus', function(){
				var obFldLabel = null;
				var sFldType = $(this).attr('type');
				if(sFldType != null && sFldType !== 'submit' && sFldType !== 'image' && sFldType !== 'hidden')
				{
					obFldLabel = $("label[for=" + $(this).attr("id") + "]", $(this).parent());
					if(obFldLabel != null)
					{
						obFldLabel.hide();
					}
				}
			});
			
			/**
			 * Показ лэйблов для FeedBack - input
			 */
			$($(".feedback input").get(i)).bind('blur', function(){
				var obFldLabel = null;
				var sFldType = $(this).attr('type');
				if(sFldType != null && sFldType !== 'submit' && sFldType !== 'image' && sFldType !== 'hidden')
				{
					obFldLabel = $("label[for=" + $(this).attr("id") + "]", $(this).parent());
					if(obFldLabel != null && !$(this).val().length)
					{
						obFldLabel.show();
					}
				}
			});
		}
	}
	
	var obFldFeedBackContainer = $(".feedback textarea");
	
	if(obFldFeedBackContainer != null)
	{
		for(var i=0; i < obFldFeedBackContainer.length; i++)
		{
			/**
			 * Скрытие лэйблов для FeedBack - textarea
			 */
			$($(".feedback textarea").get(i)).bind('focus', function(){
				var obFldLabel = null;
				obFldLabel = $("label[for=" + $(this).attr("id") + "]", $(this).parent());
				if(obFldLabel != null)
				{
					obFldLabel.hide();
				}
			});
			
			/**
			 * Показ лэйблов для FeedBack - textarea
			 */
			$($(".feedback textarea").get(i)).bind('blur', function(){
				var obFldLabel = null;
				obFldLabel = $("label[for=" + $(this).attr("id") + "]", $(this).parent());
				if(obFldLabel != null && !$(this).val().length)
				{
					obFldLabel.show();
				}
			});
		}
	}
	
	$(".feedback input[type=submit]").bind("click", function(){
		var arData = new Object();
		var sFldType = null;
		var sFldName;
		var sAjaxFeedBackPath = $(".feedback input[name=web_form_ajax_load_path]").val();
		
		var obFldContainer = $(".feedback input");
		var obItem = null;
		if(obFldContainer != null)
		{
			for(i=0; i<obFldContainer.length; i++)
			{
				obItem = obFldContainer.get(i);
				sFldType = $(obItem).attr('type');
				if(sFldType != null)// && sFldType !== 'submit' && sFldType !== 'image' && sFldType !== 'hidden')
				{
					sFldName = $(obItem).attr("name");
					arData[sFldName] = $(obItem).val();
				}
			}
		}
		
		obFldContainer = $(".feedback textarea");
		obItem = null;
		if(obFldContainer != null)
		{
			for(i=0; i<obFldContainer.length; i++)
			{
				obItem = obFldContainer.get(i);
				sFldName = $(obItem).attr("name");
				arData[sFldName] = $(obItem).val();
			}
		}
		
		$.post(
			sAjaxFeedBackPath,
			arData,
			function(data){ onLoadFeedBack(data) },
			"html"
		);
		
		return false;
	});
}

function onLoadFeedBack(data)
{
	$(".feedback").html(data);
	//alert(data);
	initFeedBackFormEvents();
}

function initFormLabelsShow()
{
	/**
	 * Скрытие лэйблов для search
	 */
	$(".search input").bind('focus', function(){
		var obFldLabel = null;
		var sFldType = $(this).attr('type');
		if(sFldType != null && sFldType !== 'submit' && sFldType !== 'image' && sFldType !== 'hidden')
		{
			obFldLabel = $("label[for=" + $(this).attr("id") + "]", $(this).parent());
			if(obFldLabel != null)
			{
				obFldLabel.hide();
			}
		}
	});
	
	/**
	 * Показ лэйблов для search
	 */
	$(".search input").bind('blur', function(){
		var obFldLabel = null;
		var sFldType = $(this).attr('type');
		if(sFldType != null && sFldType !== 'submit' && sFldType !== 'image' && sFldType !== 'hidden')
		{
			obFldLabel = $("label[for=" + $(this).attr("id") + "]", $(this).parent());
			if(obFldLabel != null && !$(this).val().length)
			{
				obFldLabel.show();
			}
		}
	});
}

function initHideShowRubrics()
{
	$(".rubrics span.h").click(function(){
		$(".rubrics ul").show();
	});
	
	$(".rubrics div").mouseover(function(){
		$(".rubrics ul").show();
	});
	$(".rubrics div").mouseout(function(){
		$(".rubrics ul").hide();
	});
	
	$(".telecast_anons .h .sort span").click(function(){
		var rubricswitcher = $("ul", $(this).parent());
		if(rubricswitcher.css('display') == 'none')
		{
			rubricswitcher.show();
		}
		else
		{
			rubricswitcher.hide();
			if(rubricswitcher.hasClass("sub"))
			{
				$("li", $(this).parent().parent().parent()).show();
			}
		}
	});
}


$(document).ready(function(){
	initSliderEvents();
	
	initMainMenuEvents();
	
	initFeedBackFormEvents()
	
	initFormLabelsShow();
	
	initHideShowRubrics();
	
	/**
	 * Показ-скрытие формы обратной связи
	 */
	$("#mail .link_feedback").click(function(){
		var obFeedBack = $(".popup.write_mail");
		if(obFeedBack != null)
		{
			if(obFeedBack.css("display") == "none")
			{
				$('.popup.add_blog').hide();
				obFeedBack.show();
			}
			else obFeedBack.hide();
		}
		return false;
	});
	$('.popup.write_mail .close').click(function(){
		$('.popup.write_mail').hide();
	});
	
	/**
	 * Показ-скрытие формы Вставить в блог
	 */
	$('#inblog span').live('click', function(){
		$('.popup.write_mail').hide();
	 	$('.popup.add_blog').toggle();
	});
	$('.popup.add_blog .close').click(function(){
		$('.popup.add_blog').hide();
	});
	
	$("#print a").click(function(){
		window.print();
		
		return false;
	});
	
	$(".bl_v").mouseover(function(){
		$(".inf", $(this)).show();
	});
	
	$(".bl_v").mouseout(function(){
		$(".inf", $(this)).hide();
	});
	
	$(".bl_v .inf").click(function(){
		document.location.href = $("a", $(this).parent()).attr('href');
	});
	
	$(".telecast_list .lenta .bl").mouseover(function(){
		$(".txt", $(this)).show();
	});
	
	$(".telecast_list .lenta .bl").mouseout(function(){
		$(".txt", $(this)).hide();
	});
	
	$(".telecast_anons .short .name").click(function(){
		var allswitcher = $(".all", $(this).parent().parent());
		if(allswitcher.css('display') == 'none')
		{
			allswitcher.show();
			$(this).parent().parent().addClass('show');
		}
		else
		{
			allswitcher.hide();
			$(this).parent().parent().removeClass('show');
		}
	});
	
	$(".telecast_anons .h .sort ul.sub li").click(function(){
		var allcasts = $("li", $(this).parent().parent().parent().parent());
		var RubricRlassName = $(this).attr('class');
		allcasts.each(function(i){
			if(!$(this).hasClass('h') && !$(this).parent().hasClass('sub'))
			{
				$(this).hide();
				if($(this).hasClass(RubricRlassName))
				{
					$(this).show();
				}
			}
		});
		$(this).parent().hide();
	});
	
	$(".anonstv_block .lenta .block").click(function(){
		document.location.href = $(this).attr('href');
	});
	
	
	if($("#mid2 .pager2 a.prev").attr("href") == '#') $("#mid2 .pager2 a.prev").hide();
	if($("#mid2 .pager2 a.next").attr("href") == '#') $("#mid2 .pager2 a.next").hide();
	$("#mid2 .pager2 a").click(function(){
		if($(this).attr("href") == '#')
		{
			return false;
		}
	});
	
	$('.logos_block .prev').hide();
	if($('.logos_block .lenta a').length <= 5)
	{
		$('.logos_block .next').hide();
	}
	
	/**
	* Слайдер логотипов - Назад
	*/
	$('.logos_block .prev').click(function() {
		var lenta = $('.lenta', $(this).parent());
		var lentaml = parseInt(lenta.css('margin-left'));
		if(!lentaml) lentaml = 0;
		if (lentaml < 0) {
			lenta.animate({marginLeft: '+=103px'});
			if(lentaml == -103) $(this).hide();
			$('.next', $(this).parent()).show();
		}
		else
		{
			if($(this).css('display') == 'none') $(this).show();
		}
	});
	
	/**
	 * Слайдер логотипов - Вперед
	 */
	$('.logos_block .next').click(function() {
		var lenta = $('.lenta', $(this).parent());
		var lentaml = parseInt(lenta.css('margin-left'));
		if(!lentaml) lentaml = 0;
		var lentaln = $('img', lenta).length;
		if (lentaml >= (lentaln - 5) * -103) {
			lenta.animate({marginLeft: '-=103px'});
			if(-lentaml >= (lentaln - 6) * 103) $(this).hide();
			$('.prev', $(this).parent()).show();
		}
		else
		{
			if($(this).css('display') == 'none') $(this).show();
		}
	});
});