$(document).ready(function(){
	var selnum = $('div#programm li').size()/3;
	var selmaxnum2=5;
	var $sli2 = $('div#programm li:eq(0)');
	var sli_height2 = $sli2.outerHeight(true);
	var sli_width2 = $sli2.outerWidth(true);
	var scurrel2 = selnum+Math.ceil(selmaxnum2/2)-1;
	$('div#programm,div#programm ul').height(sli_height2);
	$('div#programm').width(selmaxnum2*sli_width2-8);
	$('div#programm ul').width(3*selnum*sli_width2).css({left:1-(scurrel2-Math.ceil(selmaxnum2/2))*sli_width2});

	setCurrent(scurrel2);
	
	$('#bigfoto_left').click(function(){
		 smove(1);
	});
	
	$('#bigfoto_right').click(function(){
		smove(-1);
	});
	
	$('div#programm li').click(function(){
		var el = $(this);
		var index = $('div#programm li').index(el)+1;
		if(index == scurrel2) return;
		smove(scurrel2-index);
	});
	
	$('div#programm li').hover(function(){if($(this).hasClass('current')) return;$(this).stop(true, false).animate({opacity:1},500);}, function(){if($(this).hasClass('current')) return;$(this).stop(true, false).animate({opacity:0.5},500);});
	
	function smove(dir){
		if($('div#programm ul:animated').size() > 0) return;
		//цикличность 2 - лево
		$('#programm li:eq('+(scurrel2-1)+')').stop(true,false).animate({opacity:0.5},500).removeClass('current');
		if(  scurrel2-dir < Math.ceil(selmaxnum2/2)){
			scurrel2+=selnum;
			$('div#programm ul').css({left:1-(scurrel2-Math.ceil(selmaxnum2/2))*sli_width2});
		}
		//цикличность 2 - право
		if(scurrel2-dir >= 3*selnum-Math.floor(selmaxnum2/2)){
			scurrel2-=selnum;
			$('div#programm ul').css({left:1-(scurrel2-Math.ceil(selmaxnum2/2))*sli_width2});
		}
		scurrel2-=dir;
		$('div#programm ul').animate({left:1-(scurrel2-Math.ceil(selmaxnum2/2))*sli_width2},500);
		setCurrent(scurrel2);
	}
	
	$('#middle_img').load(function(){
		$(this).stop(true, false).animate({opacity:1},500);
	});
	
	function setCurrent(current){
		var size = gallery.length;
		$('#programm li:eq('+(current-1)+')').stop(true,false).animate({opacity:1},500).addClass('current');
		if(current > size) current-=selnum;
		if(current > size) current-=selnum;
		$('#middle_img').stop(true, false).animate({opacity:0},500, function(){$(this).attr({src:gallery_path+gallery[current-1]});});
	}
});
