$(window).resize( function() {
	popup();
});

$(document).ready( function() {
	$(function() {
		$('.scroll-pane').jScrollPane({
			showArrows:true, 
			scrollbarWidth:11, 
			dragMaxHeight:50
		});
	});
	if ($('div.about div.picsprev').length) {
		$('div.picsprev').jScrollPane( {
			showArrows :true,
			scrollbarWidth :11,
			dragMaxHeight :50
		});			
	}
	popup();
	thumbs_scroll();
	collect_select_new();

});


function test_func(mt){
	  alert(mt);
	}

function thumbs_scroll() {
	$(".collect_scroll .scrollcont .thumb>a").hover( function() {
		$(this).toggleClass("thhov");
	}, function() {
		$(this).removeClass("thhov");
	});

}

function collect_select_new(){
	if($(".collect_scroll").attr("class")=='collect_scroll text_block') { pic_width =179; pic_numb=3;}
	if($(".collect_scroll").attr("class")=='collect_scroll text_block cs_about') { pic_width =134; pic_numb=4;}
	if($(".collect_scroll").attr("class")=='collect_scroll') { pic_width =137; pic_numb=4;}
	
	$(".collect_scroll div.scrollcont2 .pics a").css("width",pic_width+"px");
	$(".collect_scroll div.scrollcont2 .pics a").css("overflow","hidden");
	$(".collect_scroll div.scrollcont2 .pics a").css("float","left");
	
	lmrg = $(".collect_scroll div.scrollcont2").css('margin-left').replace("px", "");
	lmrg = lmrg.replace("-", "")*1;	
		
	var pics_numb = $('div.scrollcont2 img').length;	
	cwidth = pic_width*pics_numb;
	//rmrg = cwidth-pic_width*4;
	rmrg = cwidth-pic_width*pic_numb;	
	$('div.scrollcont2').css("width",cwidth+"px");
	
	var pic_act = null;
  // Если выбрана фотка
	if($(".collect_scroll .scrollcont2 a.thact").length>0){
			pic_act = $(".collect_scroll .scrollcont2 a.thact img").attr("class");
			pic_act = pic_act.replace("th", "")*1;		
			//if((pics_numb-pic_act)<4) lmrg = (pics_numb-4)*pic_width;
			if((pics_numb-pic_act)<pic_numb) lmrg = (pics_numb-pic_numb)*pic_width;			
			else lmrg = pic_act*pic_width;
		}
	//alert(lmrg);
	//alert(pics_numb);
	//alert(pic_act);
	
	$(".collect_scroll div.scrollcont2").css('margin-left',-lmrg+'px');
	
	var nxt = 0;
	var prv = 0;	
	if(pic_act) {
		  	//if((pics_numb - pic_act)>4) nxt=1;
			//if((pic_act>0)&&(pics_numb>4)) prv=1;
		  	if((pics_numb - pic_act)>pic_numb) nxt=1;
			if((pic_act>0)&&(pics_numb>pic_numb)) prv=1;						
		}
	//else if(pics_numb>3) nxt = 1;
	else if(pics_numb>(pic_numb-1)) nxt = 1;	
		
	if(nxt==1) $(".collect_scroll div.next a").css('display', 'block'); else $(".collect_scroll div.next a").css('display', 'none');
	if(prv==1) $(".collect_scroll div.prev a").css('display', 'block'); else $(".collect_scroll div.prev a").css('display', 'none');
		
	$(".collect_scroll .prev a").click(function(){
			lmrg = lmrg - pic_width;			
			$(".collect_scroll div.scrollcont2").css('margin-left',-lmrg+'px');
			if(lmrg>0) $(".collect_scroll div.prev a").css('display', 'block'); else $(".collect_scroll div.prev a").css('display', 'none');
			if(lmrg<rmrg) $(".collect_scroll div.next a").css('display', 'block'); else $(".collect_scroll div.next a").css('display', 'none');
			
			//alert(lmrg);
		});
	$(".collect_scroll .next a").click(function(){
			lmrg = lmrg + pic_width;
			$(".collect_scroll div.scrollcont2").css('margin-left',-lmrg+'px');
			if(lmrg>0) $(".collect_scroll div.prev a").css('display', 'block'); else $(".collect_scroll div.prev a").css('display', 'none');
			if(lmrg<rmrg) $(".collect_scroll div.next a").css('display', 'block'); else $(".collect_scroll div.next a").css('display', 'none');
			
			//alert(lmrg);
		});	
	
		
	}

function popup() {
	if ($(".layer").length > 0) {
		t = ($(window).height() - $(".popup").height()) / 2;
		w = $(window).width();
		st = $(window).scrollTop();
		t = t + st;

		if (t < 0)
			t = 0;

		if (w < $(".main").width())
			w = $(".main").width();
		$(".popup").css("margin-top", t + "px");
		$(".layer").css("height", $(document).height() + "px");
		$(".layer").css("width", w + "px");

		$(".popup .close a").click( function() {
			$(".layer").css("display", "none");
		});
	}
}
