var SVZPhotoGalCurrentImg = 0;
var SVZPhotoGalIsPBShow = false;
var SVZPhotoGalIsNBShow = false;

function SVZPhotoGalInit()
{
	
	if((typeof(PhotosNum)!=='undefined')  && PhotosNum > 1)
	{	
		jQuery('.imgView img').load(function(){
			var heigth = jQuery(this).height();
			if(heigth > 0 ) jQuery('.imgView').animate({height:heigth+"px"} ,100);
		})
		
		jQuery('.imgView img').attr('src',jQuery('.imgView img').attr('src'));
		
		jQuery(".articleImages .previewBlock li").each(function(){
			if(jQuery(this).hasClass('active')) return ;
			var img = jQuery('<img style="display:none">');	
			img.attr('src',jQuery('a', this).attr('src'));
			jQuery(".articleImages .imgView").append(img);
		})
		
		
		jQuery('.articleImages .previewBlock a').each(function(i, obj){
			jQuery(this).click(function(){
			SVZPhotoGalShowPic((i));
			
			return false;
		});
			});
			
			if(jQuery('.articleImages .previewBlock a').size() > 1)
			{
				SVZPhotoGalShowNextBT();
			}
			
			
			jQuery("#prevButton").click(function(){
				SVZPhotoGalShowPic((SVZPhotoGalCurrentImg-1));
				return false;
			});
			
			jQuery("#nextButton").click(function(){
				SVZPhotoGalShowPic((SVZPhotoGalCurrentImg+1));
				return false;
			});
			
	} else {
		
		jQuery('.imgView img').load(function(){
			var heigth = jQuery(this).height();
			if(heigth > 0 ) jQuery('.imgView').animate({height:heigth+"px"} ,100);
		})
		jQuery('.imgView img').attr('src',jQuery('.imgView img').attr('src'));
	}
	
	
}

function SVZPhotoGalShowNextBT(){
	if(SVZPhotoGalIsNBShow != true) 
	{
		SVZPhotoGalIsNBShow = true
		jQuery('.articleImages .imgView .nextButton').animate({right:'+=30'},500)
	}
}

function SVZPhotoGalHideNextBT(){ 
	if(SVZPhotoGalIsNBShow == true ) 
	{
		SVZPhotoGalIsNBShow = false
		jQuery('.articleImages .imgView .nextButton').animate({right:'-=30'},500)
	}		
}

function SVZPhotoGalShowPrevBT(){
	
	if(SVZPhotoGalIsPBShow != true ) 
	{
		SVZPhotoGalIsPBShow=  true;
		jQuery('.articleImages .imgView .prevButton').animate({left:'+=30'},500)
	}
}	
	
function SVZPhotoGalHidePrevBT(){ 
	
	if(SVZPhotoGalIsPBShow == true ) 
	{
		SVZPhotoGalIsPBShow = false;
		jQuery('.articleImages .imgView .prevButton').animate({left:'-=30'},500)
	}
}

function SVZPhotoGalShowPic(PicNum)
{	
	// console.log(PicNum);
	if(PicNum >= (PhotosNum)) return;
	
	SVZPhotoGalCurrentImg = PicNum; 
	var heigth = jQuery('.imgView img').eq(PicNum).height();
	
	if(heigth > 0 ) jQuery('.imgView').animate({height:heigth+"px"} ,100);
	jQuery(".imgView img:visible").fadeOut(100);
	jQuery('.imgView img').eq(PicNum).fadeIn(100);
	
	
	// jQuery(" .imgView img#PostView").attr('src',jQuery('.articleImages .previewBlock a').eq(PicNum).attr("src"));
	
	jQuery('.articleImages .previewBlock li.active').removeClass("active");
	jQuery('.articleImages .previewBlock li').eq(PicNum).addClass("active");
	
	if(PicNum > 0) { SVZPhotoGalShowPrevBT() ; SVZPhotoGalShowNextBT(); }
	if(PicNum == (PhotosNum -1)) {SVZPhotoGalShowPrevBT()  ; SVZPhotoGalHideNextBT();}
	if(PicNum == 0) { SVZPhotoGalHidePrevBT() ; SVZPhotoGalShowNextBT(); }
	
	return false;
}

jQuery(function(){
	
	SVZPhotoGalInit();
	
	

	
	
});

