$(document).ready(function() {
	$('#menu_left li:last').css('border-bottom', 'none');
	$('#header_left ul li:last').css('background', 'none');
	
	//Проверяем ширину контента и по необходимости выводим стрелки
	var heightPadding = $('.height_js').height(),
		heightContent = $('#middle_block').height();
	
	if (heightPadding > heightContent - 90)
	{
		//Создаем навигационные элементы и обертку для прокручивания
		var html = $('.height_js').html(),
			bitrixPath = '/bitrix/templates/different/';
		
		$('#middle_block').append('<div id="navigation"><img src="' + bitrixPath + 'images/arrow_top.png" alt="" id="arrow_top" /><img src="' + bitrixPath + 'images/arrow_bottom_active.png" alt="" id="arrow_bottom" /></div>');
		$('.height_js').html('<div id="overflow_block"><div id="slider_block">' + html + '</div></div>');
		
		//Скроллер
		var heightSlider = $('#slider_block').height(),
			heightWindow = $('#overflow_block').height(),
			speed = 350,
			positionTop = 0,
			stepAnimate = 0,
			step = 163;
			
		arrowBottom = function()
		{
			$('#arrow_top').unbind('click');
			$('#arrow_bottom').unbind('click');
			
			if ((positionTop + step) > (heightSlider - heightWindow))
			{
				stepAnimate = Math.abs(positionTop - (heightSlider - heightWindow));
				arrowsSlide(1, 0);
			}
			else
			{
				stepAnimate = step;
				arrowsSlide(1, 1);
			}
			
			$('#slider_block').animate({
				'top' : '-=' + stepAnimate + 'px'
			}, speed);
			
			positionTop += stepAnimate;
		}
		
		arrowTop = function()
		{
			$('#arrow_top').unbind('click');
			$('#arrow_bottom').unbind('click');
			
			if ((positionTop - step) <= 0)
			{
				stepAnimate = Math.abs(positionTop);
				arrowsSlide(0, 1);
			}
			else
			{
				stepAnimate = step;
				arrowsSlide(1, 1);
			}
			
			$('#slider_block').animate({
				'top' : '+=' + stepAnimate + 'px'
			}, speed);
			
			positionTop -= stepAnimate;
		}
		
		$('#arrow_bottom').bind('click', arrowBottom);
		
		
		//СКРОЛЛЕР
		var stepSlide = 40, //Шаг смещения
			current_ipp = 0, //Позиция смещаемого блока
			heightScroll = $('#slider_block').height(), //Высота смещаемого блока
			heightBlockOverflow = $('#overflow_block').height(); //Высота полосы скроллинга
		
		$("#slider_block").bind("mousewheel", function(event, delta){
			$('#arrow_top').unbind('click');
			$('#arrow_bottom').unbind('click');
			current_ipp = parseInt($(this).css('top'));
			
			if (delta >= 0.1)
			{
				if ((current_ipp + stepSlide) >= 0)
				{
					current_ipp = 0;
					$(this).css('top', (current_ipp) + 'px');
					arrowsSlide(0, 1);
				}
				else
				{
					$(this).css('top', (current_ipp + stepSlide) + 'px');
					arrowsSlide(1, 1);
				}
			}
			else
			{
				if ((current_ipp - stepSlide) < ((heightScroll - heightBlockOverflow) * (-1)))
				{
					current_ipp = (heightScroll - heightBlockOverflow) * -1;
					$(this).css('top', (current_ipp) + 'px');
					arrowsSlide(1, 0);
				}
				else
				{
					$(this).css('top', (current_ipp - stepSlide) + 'px');
					arrowsSlide(1, 1);
				}
			}
			
			current_ipp = parseInt($(this).css('top'));
			positionTop = current_ipp * -1;
			return false;
		});
		
		function arrowsSlide(top, bottom)
		{
			if (top)
			{
				$('#arrow_top').attr('src', bitrixPath + 'images/arrow_top_active.png');
				$('#arrow_top').bind('click', arrowTop);
			}
			else
				$('#arrow_top').attr('src', bitrixPath + 'images/arrow_top.png');
				
			if (bottom)
			{
				$('#arrow_bottom').attr('src', bitrixPath + 'images/arrow_bottom_active.png');
				$('#arrow_bottom').bind('click', arrowBottom);
			}
			else
				$('#arrow_bottom').attr('src', bitrixPath + 'images/arrow_bottom.png');
		}
		
	}
	
	
});
