var scrolling = true;
var obScroll = null;
var rsTimeOutScroll = null;
var iRecursionCount = 0;
var lenta_photos1 = null;
var lentaml_photos1 = null;
var lentaln_photos1 = null;

var iSortYearValue = null;
var iSortNameValue = null;
var iGroupValue = '';
var iPageID = null;
var iAlfaValue = '';
var sCurPageComponent = '';

function onScrollPrev()
{
	if(!lenta_photos1 && !lentaml_photos1)
	{
		lenta_photos1 = jQuery('.lenta', jQuery(obScroll).parent());
		lentaml_photos1 = parseInt(lenta_photos1.css('margin-left'));
		if(!lentaml_photos1 || lentaml_photos1>0) lentaml_photos1 = 0;
	}
	
	if (lentaml_photos1 < 0)
	{
		lentaml_photos1 += 70;
		
		lenta_photos1.stop().animate({marginLeft: lentaml_photos1+'px'},500, function(){
			if(scrolling){
				setTimeout("onScrollPrev();", 20);
			}
		});
	}
}

function onScrollNext()
{
	if(!lenta_photos1 && !lentaml_photos1)
	{
		lenta_photos1 = jQuery('.lenta', jQuery(obScroll).parent());
		lentaml_photos1 = parseInt(lenta_photos1.css('margin-left'));
		if(!lentaml_photos1 || lentaml_photos1>0) lentaml_photos1 = 0;
	}
	
	if(!lentaln_photos1) lentaln = jQuery('img', lenta_photos1).length;
	
	lentaml_photos1 -= 70;
	
	lenta_photos1.stop().animate({marginLeft: lentaml_photos1+'px'},500, function(){
		if(scrolling){
			setTimeout("onScrollNext();", 20);
		}
	});
}
	
jQuery.noConflict();
jQuery(document).ready(function($){	
	function stopScrolling(value)
	{
		scrolling = value;
		jQuery(document).unbind('mousemove', function(){stopScrolling(scrolling);});
	}
	
	function initSliderEvents()
	{
		/**
		 * Слайдер фоток 1 - Назад
		 */
		$('#photos1 .lenta_cont .prev').hover(function() {
				scrolling = true;
				obScroll = this;
				onScrollPrev();
			},
			function(e){
				if(scrolling)
				{
					scrolling = false;
					e.stopPropagation();
					e.cancelBubble = true;
				}
				else
				{
					scrolling = true;
				}
				
				jQuery(document).bind('mousemove', function(){stopScrolling(scrolling);});
			}
		);
		
		/**
		 * Слайдер фоток 1 - Вперед
		 */
		$('#photos1 .lenta_cont .next').hover(function() {
				scrolling = true;
				obScroll = this;
				onScrollNext();
			},
			function(e){
				if(scrolling)
				{
					scrolling = false;
					e.stopPropagation();
					e.cancelBubble = true;
				}
				else
				{
					scrolling = true;
				}
				
				jQuery(document).bind('mousemove', function(){stopScrolling(scrolling);});
			}
		);
		
		
		$('#photos2 .prev').hide();
		if($('#photos2 .lenta_cont .lenta img').length <= 5)
		{
			$('#photos2 .next').hide();
		}
		
		/**
		 * Слайдер фоток 2 - Назад
		 */
		$('#photos2 .prev').click(function() {
			var lenta = $('.lenta_cont .lenta', $(this).parent());
			var lentaml = parseInt(lenta.css('margin-left'));
			if(!lentaml) lentaml = 0;
			if (lentaml < 0) {
				lenta.animate({marginLeft: '+=112px'});
				if(lentaml == -112) $(this).hide();
				$('.next', $(this).parent()).show();
			}
			else
			{
				if($(this).css('display') == 'none') $(this).show();
			}
		});
		
		/**
		 * Слайдер фоток 2 - Вперед
		 */
		$('#photos2 .next').click(function() {
			var lenta = $('.lenta_cont .lenta', $(this).parent());
			var lentaml = parseInt(lenta.css('margin-left'));
			if(!lentaml) lentaml = 0;
			var lentaln = $('img', lenta).length;
			if (lentaml > (lentaln - 5) * -112) {
				lenta.animate({marginLeft: '-=112px'});
				if(-lentaml >= (lentaln - 6) * 112) $(this).hide(); //???
				$('.prev', $(this).parent()).show();
			}
			else
			{
				if($(this).css('display') == 'none') $(this).show();
			}
		});
		
		$('#photos2 .lenta_cont .lenta .f').click(function(){
			$('#photos2 .big img').attr("src", $(this).attr("href"));
			$('.f.act', $(this).parent()).removeClass("act");
			$(this).addClass("act");
			$('#photos2 .txt').html($(this).attr("caption").replace('&quot;','"'));
		});
	}
	
	function initMainMenuEvents()
	{
		/**
		 * Главное всплывающее меню - при наведении
		 */
		$(".menu1 ul li.down").hover(function(){
			
			var obSubmenu = $(".submenu", this);
			if(obSubmenu != null && (!obSubmenu.parent().css("z-index") || obSubmenu.parent().css("z-index") == 0))
			{
				obSubmenu.parent().css("z-index","1");
				obSubmenu.slideDown(300);
			}
		}, function(e){
			
			var obSubmenu = $(".submenu", this);
			if(obSubmenu != null && obSubmenu.parent().css("z-index") == 1)
			{
				obSubmenu.slideUp(300, function(){
					obSubmenu.parent().css("z-index","0");			
				});
			}
	
		});
	}
	
	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 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(
				"/feedback.php?ajax=1",
				arData,
				function(data){ onLoadFeedBack(data) },
				"html"
			);
			
			return false;
		});
	}
	
	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();
				}
			}
		});
		
		/**
		 * Скрытие лэйблов для subscribe
		 */
		$(".subscribe 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();
				}
			}
		});
		
		/**
		 * Показ лэйблов для subscribe
		 */
		$(".subscribe 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 onLoadFeedBack(data)
	{
		$(".feedback").html(data);
		//alert(data);
		initFeedBackFormEvents();
	}
	
	function initFilterNavigation()
	{
		if(document.URL.indexOf("sortyear=2")>0)
		{
			iSortYearValue = 2;
		}
		else
		{
			iSortYearValue = 1;
		}
		
		if(document.URL.indexOf("sortname=2")>0)
		{
			iSortNameValue = 2;
		}
		else
		{
			iSortNameValue = 1;
		}
		
		if(document.URL.indexOf("group=")>0 && (document.URL.indexOf("sportsman")>0 || document.URL.indexOf("documents")>0))
		{
			var arRequest = document.URL.split("?");
			
			if(arRequest[1].indexOf("&")>0)
			{
				var arParams = arRequest[1].split("&");
				for(var i=0; i<arParams.length; i++)
				{
					if(arParams[i].indexOf("group=")>=0)
					{
						var arGroupParam = arParams[i].split("=");
						iGroupValue = arGroupParam[1];
						break;
					}
				}
			}
			else
			{
				if(arRequest[1].indexOf("group=")>=0)
				{
					var arGroupParam = arRequest[1].split("=");
					iGroupValue = arGroupParam[1];
				}
			}
		}
		
		if(document.URL.indexOf("alfa=")>0 && (document.URL.indexOf("sportsman")>0 || document.URL.indexOf("documents")>0))
		{
			var arRequest = document.URL.split("?");
			
			if(arRequest[1].indexOf("&")>0)
			{
				var arParams = arRequest[1].split("&");
				for(var i=0; i<arParams.length; i++)
				{
					if(arParams[i].indexOf("alfa=")>=0)
					{
						var arAlfaParam = arParams[i].split("=");
						iAlfaValue = arAlfaParam[1];
						break;
					}
				}
			}
			else
			{
				if(arRequest[1].indexOf("alfa=")>=0)
				{
					var arAlfaParam = arRequest[1].split("=");
					iAlfaValue = arAlfaParam[1];
				}
			}
		}
		
		if(document.URL.indexOf("/page/")>0 && (document.URL.indexOf("sportsman")>0 || document.URL.indexOf("documents")>0))
		{
			var arRequest = document.URL.split("/page/");
			
			if(arRequest[1].indexOf("/")>0)
			{
				var arParams = arRequest[1].split("/");
				iPageID = parseInt(arParams[0]);
			}
		}
		
		if(document.URL.indexOf("sportsman")>0)
		{
			sCurPageComponent = "sportsman";
		}
		
		if(document.URL.indexOf("documents")>0)
		{
			sCurPageComponent = "documents";
		}
		
		if(sCurPageComponent.length>0)
		{
			$('.filling .sort span').click(function(){
				var obParent = $(this).parent();
				var iCurEl = $('.filling .sort span').index($(this));
				var obElementsContainer = $('.filling .elements_list');
				
				if(obParent.hasClass('up'))
				{
					$('.up', obParent.parent()).removeClass('up');
					$('.down', obParent.parent()).removeClass('down');
					
					obParent.addClass('down');
					
					if(!iCurEl)
					{
						iSortYearValue = 2;
						iSortNameValue = null;
					}
					else
					{
						iSortYearValue = null;
						iSortNameValue = 2;
					}
				}
				else
				{
					$('.up', obParent.parent()).removeClass('up');
					$('.down', obParent.parent()).removeClass('down');
					
					obParent.addClass('up');
					
					if(!iCurEl)
					{
						iSortYearValue = 1;
						iSortNameValue = null;
					}
					else
					{
						iSortYearValue = null;
						iSortNameValue = 1;
					}
				}
				
				$.get("/"+sCurPageComponent+"/ajax.php", { ajax: "1", group: iGroupValue, PAGEN_1:iPageID, alfa: iAlfaValue, sortyear: iSortYearValue, sortname: iSortNameValue }, function(data){
					obElementsContainer.html(data);
				});
			});
			
			$('#select_sporttype_7').change(function(){
				var iSportType = $('option:selected', this).val();
				document.location.href = "/"+sCurPageComponent+"/?group="+iSportType;
			});
			
			$('#select_doctype_6').change(function(){
				var iSportType = $('option:selected', this).val();
				document.location.href = "/"+sCurPageComponent+"/?group="+iSportType;
			});
		}
	}

	
	/*---------------------------------------------------*/
	initSliderEvents();
	
	initMainMenuEvents();
	
	initFeedBackFormEvents();
	
	initFormLabelsShow();
	
	initFilterNavigation();
	
	$("a.print").click(function(){
		window.print();
		
		return false;
	});
	
	/**
	 * Показ-скрытие формы обратной связи
	 */
	$(".link_feedback").click(function(){
		var obFeedBack = $(".feedback");
		if(obFeedBack != null)
		{
			if(obFeedBack.css("display") == "none") obFeedBack.slideDown(500);
			else obFeedBack.slideUp(500);
		}
	});

	$(".feedback .close").click(function(){
		var obFeedBack = $(".feedback");
		obFeedBack.slideUp(500);
	});
	
	$("#flash li a").click(function(){
		$("li.act", $(this).parent().parent()).removeClass('act');
		$(this).parent().addClass('act');
		
		if($("#flash li").index($(this).parent()) == 1)
		{
			$("#container").removeClass("modern");
			$("#container .flash_lenta_modern").animate({height: '0px'}, 400, null);
			$("#container .flash_lenta_retro").animate({height: '89px'}, 400, null);
		}
		else
		{
			if(!$("#container").hasClass("modern")) 
			{
				$("#container").addClass("modern");
				$("#container .flash_lenta_retro").animate({height: '0px'}, 400, null);
				$("#container .flash_lenta_modern").animate({height: '89px'}, 400, null);
			}
		}
	});
});