$(document).ready(function(){
	$('.gallery').scrollGallery();
	$('.gallery .visual img').addClass('active');
	$('.gallery .visual').css('position','relative');
	$('.gallery .visual img').before('<img src="images/none.gif" class="next" width="'+$('.gallery .visual img').width()+'" height="'+$('.gallery .visual img').height()+'" alt="" style="display:none; position:absolute; top:1px; left:1px; " />');
	$('.gallery .gallery-frame a').click(function(){
		if (!$(this).parent().hasClass('active')) {
			$(this).parents('ul').find('.active').removeClass('active');
			$(this).parent().addClass('active');
			$(this).parents('.gallery').find('.visual .next').attr('src', $(this).attr('href'));
			$(this).parents('.gallery').find('.visual .next').fadeIn(300, function(){
				$(this).parent().find('.active').attr('src', $(this).attr('src'));
				$(this).fadeOut(100);
			});
		}
		return false;
	});
});
(function($) {
	$.fn.scrollGallery = function(o) {
		o = $.extend( {
			speed : 300, /* скорость перемещения 1000 = 1секунда */
			gallery_height : '.gallery-frame',
			gallery_frame : '.gallery-holder',
			gallery_holder : 'ul',
			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').outerHeight());
					var step = ($(this).find(o.gallery_item+':first').outerHeight()) * steps;
					main_holder.find('.btn-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('.btn-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);
