$(function() {

	var self=this,

		$lentaCont = $('.fotogal .fotos_cont'),
		$fotoLenta = $('.fotogal .fotos_cont .more div'),
		$lenta = $('.fotogal .fotos_cont .more'),
		$curB = $('.fotogal .fotos_cont .more div:first'),
		curBnum = 0,
		$firstB = $('.fotogal .fotos_cont  .more div:first'),		
		$lastB = $('.fotogal .fotos_cont  .more div:last'),
		$linkNext = $('.fotogal .page_through .next'),
		$linkPrev = $('.fotogal .page_through .prev'),
		bX = 0,
		bW = 126,
		bH = 95,
		curP,
			
		$linkAll = $('.fotogal .all_f a');


		$linkPrev.hide();
		$linkAll.each(function() {$(this).html("View all <span>"+$(this).parent().parent().parent().find('.fotos_cont .more div').length+"</span>");}); 

		this.activeBlock=function($block){ 
			$myparent=$block.parent().parent().parent();
			$lenta2=$myparent.find('.fotos_cont .more'); 
			$linkNext2=$myparent.find('.page_through .next');
			$linkPrev2=$myparent.find('.page_through .prev');				
			$fotoLenta2=$myparent.find('.fotos_cont .more div');
			$firstB2 = $myparent.find('.fotos_cont  .more div:first');
			$lastB2 = $myparent.find('.fotos_cont  .more div:last'); 
			
			if ( ($fotoLenta2.index($block) + 5 ) > $fotoLenta2.index($lastB2)){
				$linkNext2.hide();
			} else {
				$linkNext2.show();
			}

			if ( $fotoLenta2.index($block) == $fotoLenta2.index($firstB2)){
				$linkPrev2.hide();			
			} else {
				$linkPrev2.show();
			}

			curP = parseInt($block.prevAll().length * bW - bX); 
			$lenta2.stop().animate({left: -curP}, 250);
		}



		$linkNext.click(function() {
			if(!$('*:animated').length){
				curBnum=parseInt($(this).parent().attr('curBnum'));
				if(!curBnum)curBnum=0;
				curBnum = curBnum + 5;
				$(this).parent().attr('curBnum',curBnum);
//				$curB = $('.fotogal .more div:eq('+curBnum+')');
				$curB = $(this).parent().parent().find('.more div:eq('+curBnum+')');
				self.activeBlock($curB);
			}
			return false; 
		});

		$linkPrev.click(function() {
			if(!$('*:animated').length){
				curBnum=parseInt($(this).parent().attr('curBnum'));
				if(!curBnum)curBnum=0;				
				curBnum = curBnum - 5;
				$(this).parent().attr('curBnum',curBnum);				
//				$curB = $('.fotogal .more div:eq('+curBnum+')'); 
				$curB = $(this).parent().parent().find('.more div:eq('+curBnum+')');
				self.activeBlock($curB);
			}
			return false;
		});


/*показать все фотки*/
		$linkAll.click(function() {
			if(!$('*:animated').length){
//dron				
//				$lenta.css({left: 0 });
				$myparent=$(this).parent().parent().parent();
				$lenta2=$myparent.find('.fotos_cont .more');
				$lentaCont2 =$myparent.find('.fotos_cont');
				$linkNext2=$myparent.find('.page_through .next');
				$linkPrev2=$myparent.find('.page_through .prev');				
				$fotoLenta2=$myparent.find('.fotos_cont .more div');
				
				$lenta2.css({left: 0 });
				$curB = $firstB,
				curBnum = 0;

				if(!$(this).hasClass('show')){
//					$lentaCont.stop().animate({height: $lenta.height()}, 500);
					$lentaCont2.stop().animate({height: $lenta2.height()}, 500);
					$(this).addClass('show');
//					$linkNext.hide();$linkPrev.hide();
					$linkNext2.hide();
					$linkPrev2.hide();
					$(this).html("Hide");

				} else { 
//					$lentaCont.stop().animate({height: bH}, 500);
					$lentaCont2.stop().animate({height: bH}, 500);					
					$(this).removeClass('show');
//					$linkNext.show();
					$linkNext2.show();
					$(this).html("View all <span>"+$fotoLenta2.length+"</span>");
				}
			}
			return false;
		});


})

