$(document).ready(function() {
	var cur = 0;
	var tableWidth = $('#menu_center table').width();
	$('.menu_align').each(function() {
		var widthElem = $(this).width();
		if (widthElem > cur) {
			cur = widthElem;
		}
	});
	
	var posLeft = (tableWidth - cur) / 2;
	$('.menu_align').parent('li').css({'width': (cur + 3) + 'px', 'padding-left': posLeft + 'px'});
	
	var hrLength = $('#menu_top_padding hr').size();
		
	//Листалка салонов
	var pos = 1;
	var countImg = $('#big img').length;
	
	clickRight = function() {
		$('#strelka_right').unbind('click');
		$('#strelka_left').unbind('click');
		
		pos += 1;
		$('#big').animate({
			'left': '-=791px'
		}, 600, function() {
			if (pos > 1 && pos < countImg) {
				$('#strelka_left').show();
				$('#strelka_right').show();
				$('#strelka_left').bind('click', clickLeft);
				$('#strelka_right').bind('click', clickRight);
			}
			else if (pos == countImg) {
				$('#strelka_left').show();
				$('#strelka_right').hide();
				$('#strelka_left').bind('click', clickLeft);
			}
		});
	}
	
	clickLeft = function() {
		$('#strelka_right').unbind('click');
		$('#strelka_left').unbind('click');
		
		pos -= 1;
		$('#big').animate({
			'left': '+=791px'
		}, 600, function() {
			if (pos > 1 && pos < countImg) {
				$('#strelka_left').show();
				$('#strelka_right').show();
				$('#strelka_left').bind('click', clickLeft);
				$('#strelka_right').bind('click', clickRight);
			}
			else if (pos == 1) {
				$('#strelka_left').hide();
				$('#strelka_right').show();
				$('#strelka_right').bind('click', clickRight);
			}
		});
	}
	
	$('#strelka_right').bind('click', clickRight);
	//Конец листалки салонов
	
    if ($('.overflow').attr("class")) {
        var offset = $('.overflow').offset();
    	var heightSite = 700;
    	var newHeight = heightSite - offset.top;
    	$('.overflow').css('height', newHeight + 'px');
    }

	//Скроллинг
	var ipp = 40; //Количество пикселей для смещения
	var current_ipp = 0; //Положение, на сколько смещен плавающий блок
	var heightScroll = $('#scroll_abs').height(); //Высота смещаемого блока
	var heightBlockOverflow = $('.overflow').height(); //Высота полосы скроллинга
	var proportion; //Задаем пустую переменную
	var heightScroller = 63; //Высота ползунка для скроллера
	var scrollerTopPosition = 0; //Значение "top" у ползунка скроллера
	$('#scroll_abs').css('height', heightScroll + 'px');
	
    if ($('.overflow').attr("class")) {
    	if ((heightScroll + offset.top) > heightSite) {
    		$('#scroller').show();
    		$('.overflow').css('border-right', '4px solid #ccc');
    		wheelFunc = function(event, delta) {
    			if (delta >= 0) {
    				current_ipp = parseInt($(this).css('top'));
    				
    				if ((current_ipp + ipp) >= 0) {
    					current_ipp = 0;
    					$(this).css('top', (current_ipp) + 'px');
    				}
    				else {
    					$(this).css('top', (current_ipp + ipp) + 'px');
    				}
    				
    				current_ipp = parseInt($(this).css('top'));
    				proportion = (-1 * current_ipp * (heightBlockOverflow - heightScroller) / (heightScroll - heightBlockOverflow)) + scrollerTopPosition;
    				$('#scroller').css('top', proportion + 'px');
    			}
    			else {
    				current_ipp = parseInt($(this).css('top'));
    				if ((current_ipp - ipp) < ((heightScroll - heightBlockOverflow) * (-1))) {
    					current_ipp = (heightScroll - heightBlockOverflow) * -1;
    					$(this).css('top', (current_ipp) + 'px');
    				}
    				else {
    					$(this).css('top', (current_ipp - ipp) + 'px');
    				}
    				
    				current_ipp = parseInt($(this).css('top'));
    				proportion = (-1 * current_ipp * (heightBlockOverflow - heightScroller) / (heightScroll - heightBlockOverflow)) + scrollerTopPosition;
    				
    				
    				$('#scroller').css('top', proportion + 'px');
    			}
    			return false;
    		}
			$("#scroll_abs").bind('mousewheel', wheelFunc);
			
			
			var startScroller1 = 0;
			var pageStartScroller1 = 0;
			var scroller1ClickObject = 0;
			var positionScroller1;
			var newPosScroller1;

			scrollerMouseDown = function(e) {
				window.getSelection().removeAllRanges(); 
				pageStartScroller1 = e.pageY;
				startScroller1 = parseInt($('#scroller').css('top'));        
				scroller1ClickObject = (e.pageY + 38) - startScroller1;
				$('#scroller').unbind('mousedown');
				$(document).bind('mousemove', scrollerMouseMove1);
				$(document).bind('mouseup', scrollerMouseUp1);
			}

			scrollerMouseMove1 = function(e) {
				window.getSelection().removeAllRanges(); 
				positionScroller1 = e.pageY - pageStartScroller1;
				newPosScroller1 = positionScroller1 + startScroller1;
				
				if (newPosScroller1 >= scrollerTopPosition && newPosScroller1 <= (heightBlockOverflow - heightScroller + scrollerTopPosition)) {
					$('#scroller').css('top', newPosScroller1 + 'px');
					var scroller1Ostatok = newPosScroller1 - scrollerTopPosition;
					var proportion1RightBlock = -1 * ((heightScroll - heightBlockOverflow) * scroller1Ostatok) / (heightBlockOverflow - heightScroller);
					$('#scroll_abs').css('top', proportion1RightBlock + 'px');
				}
				else {
					if (newPosScroller1 < scrollerTopPosition) {
						$('#scroller').css('top', scrollerTopPosition + 'px');
					}
					else {
						$('#scroller').css('top', heightBlockOverflow - heightScroller + scrollerTopPosition + 'px');
					}
				}
			}

			scrollerMouseUp1 = function(e) {
				$('#scroller').bind('mousedown', scrollerMouseDown);
				$(document).unbind('mousemove');
				$(document).unbind('mouseup');
			}

			$('#scroller').bind('mousedown', scrollerMouseDown);
    	}
     }
	//Конец (Скроллинг)
	
    
});

