function galleryStepTo (pos) {
	pos -= 1;
	galstep = ($('.main_score .holder li:first').height()+4);
	galheight = $('.main_score .holder li:not(.more_score_info li)').size()*($('.main_score .holder li:first').height()+4);
	if ((((-1)*(parseInt($('.main_score .holder').css('margin-top'))-galstep*pos)+parseInt($('.main_score .frame').height())) <= galheight)) {
		$('.main_score .holder').animate({marginTop: (parseInt($('.main_score .holder').css('margin-top'))-galstep*pos)}, 300);
	}
}
$(document).ready(function(){
	$('div.promo .promo_holder').galleryScroll({
		step:1,
		duration:300,
		circleSlide:true
	});
	var i = 1;
	$('.championship .paging li').each(function(){
		$(this).find('a').attr('href','tab'+i);
		$(this).parents('.championship').find('table').eq($(this).index()).addClass('tab'+i);
		i ++;
	})
	$('.championship .paging a').click(function(){
		$(this).parents('.paging').find('.active').removeClass('active');
		$(this).parent().addClass('active');
		$(this).parents('.championship').find('.t_holder').text($(this).attr('title'));
		$(this).parents('.championship').find('table').hide();
		$(this).parents('.championship').find('.'+$(this).attr('href')).show();
		return false;
	});
	$('.photo_list').makeGallery({
		gallery_item: 'li'
	});
	popupBg ();
	$('.photo_list a').click(function(){
		return false;
	})
	$('.main_score').scrollGallery();
	$('.text_box .expanded_block h2 a').click(function(){
		
		if ($('.text_box .expanded_block').length == 1){
			return false;
		}
		
		$(this).parents('.expanded_block').find('.expanded').slideToggle(600, function(){
			$(this).parent().toggleClass('expanded_active');
		})
		return false;
	});
	galleryStepTo(4);
	$('.more').click(function(){
		$(this).parent().find('.years_menu').fadeIn(200);
		$('body').bind('click',function(){
			$('.years_menu').fadeOut(200);
			$(this).unbind('click');
		});
		return false;
	});
	$('.years_menu').click(function(){
		return false;
	});
	$('.years_menu a').click(function(){
		alert('какой-то обработчик');
		$('.years_menu').fadeOut(200);
		$('body').unbind('click');
		return false;
	});
	$('.popular_video .popular_video_list').css('width',$('.popular_video .popular_video_list li').size()*154);
	$('.news_photo .popular_video_list').css('width',$('.news_photo .popular_video_list li').size()*155);
	
});
(function($) {
	$.fn.scrollGallery = function(o) {
		o = $.extend( {
			speed : 300, /* скорость перемещения 1000 = 1секунда */
			gallery_height : '.frame',
			gallery_frame : '.main_score',
			gallery_holder : 'ul.holder',
			gallery_item : 'li'
		}, o || {});
		var steps = 1;
		$(this).each(
				function() {
					phase = 0;
					main_holder = $(this);
					var Fheight = main_holder.find(o.gallery_item+':not(.more_score_info li)').size()*(main_holder.find(o.gallery_item+':first').height()+4);
					var step = ($(this).find(o.gallery_item+':first').height()+4) * steps;
					main_holder.find('.up').bind('click',function(){
						if (((parseInt($(this).parents(o.gallery_frame).find(o.gallery_holder).css('margin-top'))+step) <= 0) && (phase != 1) ) {
							phase = 1;
							$(this).parents(o.gallery_frame).find(o.gallery_holder).animate({marginTop: (parseInt($(this).parents(o.gallery_frame).find(o.gallery_holder).css('margin-top'))+step)}, o.speed, function(){
								phase = 0;
							});
						}
						return false;
					});
					main_holder.find('.down').bind('click',function(){
						if ((((-1)*(parseInt($(this).parents(o.gallery_frame).find(o.gallery_holder).css('margin-top'))-step)+parseInt($(this).parents(o.gallery_frame).find(o.gallery_height).height())) <= Fheight) && (phase != 1)) {
							phase = 1;
							$(this).parents(o.gallery_frame).find(o.gallery_holder).animate({marginTop: (parseInt($(this).parents(o.gallery_frame).find(o.gallery_holder).css('margin-top'))-step)}, o.speed, function(){
								phase = 0;
							});
						}
						return false;
					});
				});
	};
})(jQuery);

(function($) {
	$.fn.makeGallery = function(o) {
		o = $.extend( {
			gallery_item : 'li'
		}, o || {});
		$(this).each(
				function() {
					console.log(1);
					$('body').append('<div class="photo_popup"><div class="bg"></div><div class="holder"><div class="gallery"><div class="info"><h3></h3><p></p></div><a class="btn prev" href="#">Prev</a><a class="btn next" href="#">Next</a><div class="visual"><img src="images/none.gif" /></div></div><span class="from"><span></span></span></div></div>');
					var main_holder = $(this);
					var index = main_holder.find('.selected').index();
					main_holder.find(o.gallery_item+' a').click(function(){
					
						$(this).parent().addClass('selected');
						$(this).parents('li').addClass('selected');
						$('.photo_popup .holder').css('left','50%').css('margin-left','-100px').css('margin-top',$(window).scrollTop()+40);
						$('.photo_popup .holder .visual').css('width','200px').css('height','200px');
						$('.photo_popup .holder img').attr('src',$(this).attr('href')).css('position','absolute').css('left','-9999px');
						$('.photo_popup .holder .info h3').text($(this).attr('title'));
						$('.photo_popup .holder .info p').html($(this).find('img').attr('alt'));
						$('.photo_popup .holder .from span').text($(this).parent().index()+1+' из '+main_holder.find(o.gallery_item).size());
						$('.photo_popup .info').hide();
						$('.photo_popup .from span').hide();
						$('.photo_popup .prev').hide();
						$('.photo_popup .next').hide();
						var newImg = new Image();
						
						newImg.onload = function(){
							$('.photo_popup').show();
							var moveLeft = (-100) + (-1)*(newImg.width/2 - 100);
							$('.photo_popup .holder .visual').animate({height:newImg.height},500, function(){
								$('.photo_popup .holder').animate({marginLeft:moveLeft},500);
								if (newImg.width > 320) {
									newWidth = newImg.width;
								} else {
									newWidth = 320;
								}
								$(this).animate({width:newWidth},500, function(){
									if (main_holder.find(o.gallery_item).size() > 1) {
										if ((index+1) > 1) {
											$('.photo_popup .prev').fadeIn(500);
										};
										if ((index+1) < main_holder.find(o.gallery_item).size()) {
											$('.photo_popup .next').fadeIn(500);
										};
										$('.photo_popup .next').bind('click',function(){
											if ((index+1) < main_holder.find(o.gallery_item).size()) {
												$('.photo_popup .info').slideUp(100);
												$('.photo_popup .from span').hide();
												$('.photo_popup .prev').hide();
												$('.photo_popup .next').hide();
												$('.photo_popup .holder .info h3').text($(main_holder).find('.selected').next().find('a').attr('title'));
												$('.photo_popup .holder .info p').html($(main_holder).find('.selected').next().find('img').attr('alt'));
												$('.photo_popup .holder .from span').text($(main_holder).find('.selected').next().index()+1+' из '+main_holder.find(o.gallery_item).size());
												$('.photo_popup .holder img').fadeOut(300, function(){
													$(this).attr('src', $(main_holder).find('.selected').next().find('a').attr('href')).css('position', 'absolute').css('left', '-9999px').show();
													var newImg = new Image();
													newImg.onload = function(){
														if (newImg.width > 320) {
															newWidth = newImg.width;
														} else {
															newWidth = 320;
														}
														$(main_holder).find('.selected').removeClass('selected').next().addClass('selected');
														index++;
														$('.photo_popup .holder .visual').animate({
															height: newImg.height
														}, 300, function(){
															var moveLeft = (-100) + (-1) * (newImg.width / 2 - 100);
															$('.photo_popup .holder').animate({
																marginLeft: moveLeft
															}, 500);
															$(this).animate({
																width: newWidth
															}, 500, function(){
																$('.photo_popup .prev').fadeIn(500);
																if ((index+1) < main_holder.find(o.gallery_item).size()) {
																	$('.photo_popup .next').fadeIn(500);
																};
																$('.photo_popup .close').show();
																$('.photo_popup .holder img').css('position', 'static').hide().fadeIn(500, function(){
																	$('.photo_popup .info').css('width', $('.photo_popup .visual').width());
																	$('.photo_popup .info').slideDown(300);
																	$('.photo_popup .from span').show();
																});
															});
														});
													};
													if (newImg.src != $(main_holder).find('.selected').next().find('a').attr('href')) {
														newImg.src = $(main_holder).find('.selected').next().find('a').attr('href');
													}
												});
											};
											return false;
										});
										$('.photo_popup .prev').bind('click',function(){
											if (index > 0) {
												$('.photo_popup .info').slideUp(100);
												$('.photo_popup .from span').hide();
												$('.photo_popup .prev').hide();
												$('.photo_popup .next').hide();
												$('.photo_popup .holder .info h3').text($(main_holder).find('.selected').prev().find('a').attr('title'));
												$('.photo_popup .holder .info p').html($(main_holder).find('.selected').prev().find('img').attr('alt'));
												$('.photo_popup .holder .from').html($(main_holder).find('.selected').prev().index()+1+' из '+main_holder.find(o.gallery_item).size());
												$('.photo_popup .holder img').fadeOut(300, function(){
													$(this).attr('src', $(main_holder).find('.selected').prev().find('a').attr('href')).css('position', 'absolute').css('left', '-9999px').show();
													var newImg = new Image();
													newImg.onload = function(){
														if (newImg.width > 320) {
															newWidth = newImg.width;
														} else {
															newWidth = 320;
														}
														$(main_holder).find('.selected').removeClass('selected').prev().addClass('selected');
														index--;
														$('.photo_popup .holder .visual').animate({
															height: newImg.height
														}, 300, function(){
															var moveLeft = (-100) + (-1) * (newImg.width / 2 - 100);
															$('.photo_popup .holder').animate({
																marginLeft: moveLeft
															}, 500);
															$(this).animate({
																width: newWidth
															}, 500, function(){
																if ((index+1) > 1) {
																	$('.photo_popup .prev').fadeIn(500);
																};
																$('.photo_popup .next').fadeIn(500);
																$('.photo_popup .holder img').css('position', 'static').hide().fadeIn(500, function(){
																	$('.photo_popup .info').css('width', $('.photo_popup .visual').width());
																	$('.photo_popup .info').slideDown(300);
																	$('.photo_popup .from span').show();
																});
															});
														});
													};
													if (newImg.src != $(main_holder).find('.selected').prev().find('a').attr('href')) {
														newImg.src = $(main_holder).find('.selected').prev().find('a').attr('href');
													}
												});
											}
											return false;
										});
									} else {
										$('.photo_popup .info em').hide();
									};
									$('.photo_popup .holder img').css('position','static').hide().fadeIn(500, function(){
										$('.photo_popup .info').css('width',$('.photo_popup .visual').width());
										$('.photo_popup .info').slideDown(300);
										$('.photo_popup .holder .from span').show();
									});
								});
							});
						}
						
						if (newImg.src != $(this).attr('href')) {
							newImg.src = $(this).attr('href');
						}
						$('.photo_popup .bg').bind('click',function(){
							$('.photo_popup').fadeOut(200);
							main_holder.find('.selected').removeClass('selected');
						});
						return false;
					});
				});
	};
})(jQuery);

function popupBg () {
	if ($(window).height() > $('.wrapper').height()) {
		$('.photo_popup .bg').css('height',$(window).height());
	} else {
		$('.photo_popup .bg').css('height',$('.wrapper').height())
	}
}
jQuery.fn.galleryScroll = function(_options){
	// defaults options	
	var _options = jQuery.extend({
		btPrev: 'a.prev',
		btNext: 'a.next',
		holderList: 'div.frame',
		scrollElParent: 'ul',
		scrollEl: 'li',
		slideNum: false,
		duration : 1000,
		step: false,
		circleSlide: true,
		disableClass: 'disable',
		funcOnclick: null,
		autoSlide:false,
		innerMargin:0,
		stepWidth:false
	},_options);

	return this.each(function(){
		var _this = jQuery(this);

		var _holderBlock = jQuery(_options.holderList,_this);
		var _gWidth = _holderBlock.width();
		var _animatedBlock = jQuery(_options.scrollElParent,_holderBlock);
		var _liWidth = jQuery(_options.scrollEl,_animatedBlock).outerWidth(true);
		var _liSum = jQuery(_options.scrollEl,_animatedBlock).length * _liWidth;
		var _margin = -_options.innerMargin;
		var f = 0;
		var _step = 0;
		var _autoSlide = _options.autoSlide;
		var _timerSlide = null;
		if (!_options.step) _step = _gWidth; else _step = _options.step*_liWidth;
		if (_options.stepWidth) _step = _options.stepWidth;
		
		if (!_options.circleSlide) {
			if (_options.innerMargin == _margin)
				jQuery(_options.btPrev,_this).addClass('prev-'+_options.disableClass);
		}
		if (_options.slideNum && !_options.step) {
			var _lastSection = 0;
			var _sectionWidth = 0;
			while(_sectionWidth < _liSum)
			{
				_sectionWidth = _sectionWidth + _gWidth;
				if(_sectionWidth > _liSum) {
				       _lastSection = _sectionWidth - _liSum;
				}
			}
		}
		if (_autoSlide) {
				_timerSlide = setTimeout(function(){
					autoSlide(_autoSlide);
				}, _autoSlide);
			_animatedBlock.hover(function(){
				clearTimeout(_timerSlide);
			}, function(){
				_timerSlide = setTimeout(function(){
					autoSlide(_autoSlide)
				}, _autoSlide);
			});
		}
	
		// click button 'Next'
		jQuery(_options.btNext,_this).bind('click',function(){
			jQuery(_options.btPrev,_this).removeClass('prev-'+_options.disableClass);
			if (!_options.circleSlide) {
				if (_margin + _step  > _liSum - _gWidth - _options.innerMargin) {
					if (_margin != _liSum - _gWidth - _options.innerMargin) {
						_margin = _liSum - _gWidth  + _options.innerMargin;
						jQuery(_options.btNext,_this).addClass('next-'+_options.disableClass);
						_f2 = 0;
					} 
				} else {
					_margin = _margin + _step;
					if (_margin == _liSum - _gWidth - _options.innerMargin) {
						jQuery(_options.btNext,_this).addClass('next-'+_options.disableClass);_f2 = 0;
					} 					
				}
			} else {
				if (_margin + _step  > _liSum - _gWidth + _options.innerMargin) {
					if (_margin != _liSum - _gWidth + _options.innerMargin) {
						_margin = _liSum - _gWidth  + _options.innerMargin;
					} else {
						_f2 = 1;
						_margin = -_options.innerMargin;
					}
				} else {
					_margin = _margin + _step;
					_f2 = 0;
				}
			} 
			
			_animatedBlock.animate({marginLeft: -_margin+"px"}, {queue:false,duration: _options.duration });
			
			if (_timerSlide) {
				clearTimeout(_timerSlide);
				_timerSlide = setTimeout(function(){
					autoSlide(_options.autoSlide)
				}, _options.autoSlide);
			}
			
			if (_options.slideNum && !_options.step) jQuery.fn.galleryScroll.numListActive(_margin,jQuery(_options.slideNum, _this),_gWidth,_lastSection);		
			if (jQuery.isFunction(_options.funcOnclick)) {
				_options.funcOnclick.apply(_this);
			}
			return false;
		});
		// click button 'Prev'
		var _f2 = 1;
		jQuery(_options.btPrev, _this).bind('click',function(){
			jQuery(_options.btNext,_this).removeClass('next-'+_options.disableClass);
			if (_margin - _step >= -_step - _options.innerMargin && _margin - _step <= -_options.innerMargin) {
				if (_f2 != 1) {
					_margin = -_options.innerMargin;
					_f2 = 1;
				} else {
					if (_options.circleSlide) {
						_margin = _liSum - _gWidth  + _options.innerMargin;
						f=1;_f2=0;
					} else {
						_margin = -_options.innerMargin
					}
				}
			} else if (_margin - _step < -_step + _options.innerMargin) {
				_margin = _margin - _step;
				f=0;
			}
			else {_margin = _margin - _step;f=0;};
			
			if (!_options.circleSlide && _margin == _options.innerMargin) {
				jQuery(this).addClass('prev-'+_options.disableClass);
				_f2=0;
			}
			
			if (!_options.circleSlide && _margin == -_options.innerMargin) jQuery(this).addClass('prev-'+_options.disableClass);
			_animatedBlock.animate({marginLeft: -_margin + "px"}, {queue:false, duration: _options.duration});
			
			if (_options.slideNum && !_options.step) jQuery.fn.galleryScroll.numListActive(_margin,jQuery(_options.slideNum, _this),_gWidth,_lastSection);
			
			if (_timerSlide) {
				clearTimeout(_timerSlide);
				_timerSlide = setTimeout(function(){
					autoSlide(_options.autoSlide)
				}, _options.autoSlide);
			}
			
			if (jQuery.isFunction(_options.funcOnclick)) {
				_options.funcOnclick.apply(_this);
			}
			return false;
		});
		
		if (_liSum <= _gWidth) {
			jQuery(_options.btPrev,_this).addClass('prev-'+_options.disableClass).unbind('click');
			jQuery(_options.btNext,_this).addClass('next-'+_options.disableClass).unbind('click');
		}
		// auto slide
		function autoSlide(autoSlideDuration){
			//if (_options.circleSlide) {
				jQuery(_options.btNext,_this).trigger('click');
			//}
		};
		// Number list
		jQuery.fn.galleryScroll.numListCreate = function(_elNumList, _liSumWidth, _width, _section){
			var _numListElC = '';
			var _num = 1;
			var _difference = _liSumWidth + _section;
			while(_difference > 0)
			{
				_numListElC += '<li><a href="">'+_num+'</a></li>';
				_num++;
				_difference = _difference - _width;
			}
			jQuery(_elNumList).html('<ul>'+_numListElC+'</ul>');
		};
		jQuery.fn.galleryScroll.numListActive = function(_marginEl, _slideNum, _width, _section){
			if (_slideNum) {
				jQuery('a',_slideNum).removeClass('active');
				var _activeRange = _width - _section-1;
				var _n = 0;
				if (_marginEl != 0) {
					while (_marginEl > _activeRange) {
						_activeRange = (_n * _width) -_section-1 + _options.innerMargin;
						_n++;
					}
				}
				var _a  = (_activeRange+_section+1 + _options.innerMargin)/_width - 1;
				jQuery('a',_slideNum).eq(_a).addClass('active');
			}
		};
		if (_options.slideNum && !_options.step) {
			jQuery.fn.galleryScroll.numListCreate(jQuery(_options.slideNum, _this), _liSum, _gWidth,_lastSection);
			jQuery.fn.galleryScroll.numListActive(_margin, jQuery(_options.slideNum, _this),_gWidth,_lastSection);
			numClick();
		};
		function numClick() {
			jQuery(_options.slideNum, _this).find('a').click(function(){
				jQuery(_options.btPrev,_this).removeClass('prev-'+_options.disableClass);
				jQuery(_options.btNext,_this).removeClass('next-'+_options.disableClass);
				
				var _indexNum = jQuery(_options.slideNum, _this).find('a').index(jQuery(this));
				_margin = (_step*_indexNum) - _options.innerMargin;
				f=0; _f2=0;
				if (_indexNum == 0) _f2=1;
				if (_margin + _step > _liSum) {
					_margin = _margin - (_margin - _liSum) - _step + _options.innerMargin;
					if (!_options.circleSlide) jQuery(_options.btNext, _this).addClass('next-'+_options.disableClass);
				}
				_animatedBlock.animate({marginLeft: -_margin + "px"}, {queue:false, duration: _options.duration});
				
				if (!_options.circleSlide && _margin==0) jQuery(_options.btPrev,_this).addClass('prev-'+_options.disableClass);
				jQuery.fn.galleryScroll.numListActive(_margin, jQuery(_options.slideNum, _this),_gWidth,_lastSection);
				
				if (_timerSlide) {
					clearTimeout(_timerSlide);
					_timerSlide = setTimeout(function(){
						autoSlide(_options.autoSlide)
					}, _options.autoSlide);
				}
				return false;
			});
		};
		jQuery(window).resize(function(){
			_gWidth = _holderBlock.width();
			_liWidth = jQuery(_options.scrollEl,_animatedBlock).outerWidth(true);
			_liSum = jQuery(_options.scrollEl,_animatedBlock).length * _liWidth;
			if (!_options.step) _step = _gWidth; else _step = _options.step*_liWidth;
			if (_options.slideNum && !_options.step) {
				var _lastSection = 0;
				var _sectionWidth = 0;
				while(_sectionWidth < _liSum)
				{
					_sectionWidth = _sectionWidth + _gWidth;
					if(_sectionWidth > _liSum) {
					       _lastSection = _sectionWidth - _liSum;
					}
				};
				jQuery.fn.galleryScroll.numListCreate(jQuery(_options.slideNum, _this), _liSum, _gWidth,_lastSection);
				jQuery.fn.galleryScroll.numListActive(_margin, jQuery(_options.slideNum, _this),_gWidth,_lastSection);
				numClick();
			};
			//if (_margin == _options.innerMargin) jQuery(this).addClass(_options.disableClass);
			if (_liSum - _gWidth  < _margin - _options.innerMargin) {
				if (!_options.circleSlide) jQuery(_options.btNext, _this).addClass('next-'+_options.disableClass);
				_animatedBlock.animate({marginLeft: -(_liSum - _gWidth + _options.innerMargin)}, {queue:false, duration: _options.duration});
			};
		});
	});
}

jQuery.fn.lightBox = function(){
	$('body').append('<div class="photo_lightbox"><div class="bg"></div><div class="holder"><div class="gallery"><div class="visual"><img src="images/none.gif" /></div></div></div></div>');
	popupBg();
	main_holder = $(this);
	main_holder.find('a').click(function(){
		$('.photo_lightbox .holder').css('left','50%').css('margin-left','-100px').css('margin-top',$(window).scrollTop()+40);
		$('.photo_lightbox .holder .visual').css('width','200px').css('height','200px');
		$('.photo_lightbox .holder img').attr('src',$(this).attr('href')).css('position','absolute').css('left','-9999px');
		var newImg = new Image();
		newImg.onload = function(){
			$('.photo_lightbox').show();
			var moveLeft = (-100) + (-1)*(newImg.width/2 - 100);
			$('.photo_lightbox .holder .visual').animate({height:newImg.height},500, function(){
				$('.photo_lightbox .holder').css('padding-bottom', 15).animate({marginLeft:moveLeft},500);
				if (newImg.width > 320) {
					newWidth = newImg.width;
				} else {
					newWidth = 320;
				}
				$(this).animate({width:newWidth},500, function(){
					$('.photo_lightbox .holder img').css('position','static').hide().fadeIn(500, function(){
						
					});
				});
			});
		}
		
		if (newImg.src != $(this).attr('href')) {
			newImg.src = $(this).attr('href');
		}
		
		$('.photo_lightbox').bind('click',function(){ // .bg
			$('.photo_lightbox').fadeOut(200);
		});
		return false;
	});
}
