$(document).ready(function(){
	var path = document.location.pathname;		
	if ( path.charAt(path.length-1) != '/' ) path = path+'/';
	var epos = document.location.pathname.indexOf('/',1) == -1 ? document.location.pathname.length : document.location.pathname.indexOf('/',1)-1;
	var section = document.location.pathname.substr(1, epos );
	
	try {
		document.execCommand("BackgroundImageCache", false, true);
	} catch(err) {}
	
	// FONT -------------------------------------------------------------------
	Cufon.replace($('.title-js'));

	var buf = new Image();
	// GALLERY ------------------------------------------------------------------
	function scrolling(idx) {
		
		//if(idx-2 > 0 && idx+2 < $(".gallery-preview a").length) {
			$(".gallery-preview a.active").removeClass("active");
			$("#preview-line").stop();
			$("#preview-line").animate({scrollLeft : (idx*91) + "px"}, 600);
			$(".gallery-preview a").eq(idx).addClass("active");
			
			$("#big_img").attr("src", "/images/blank.gif");
			
			buf.onload = function() {
				$("#big_img").attr("src", this.src);
				if($(".gallery-navi a.pointers[href='#play']").hasClass("play")) {
					if(idx < ($(".gallery-preview a").length-1) ) {
						window.setTimeout(function() {
							scrolling(idx+1);
						}, 3000);
					}
					else {window.setTimeout(function() {
							scrolling(0);
						}, 3000);
					}
				}
			}
			buf.src = $(".gallery-preview a").eq(idx).attr("href");
			
		//}
	}
	scrolling(0);
	
	$(".gallery-preview").width($(".gallery-preview a").length * 91);

	$(".gallery-preview a").click( function(){
		var idx = $(".gallery-preview a").index($(this));
		scrolling(idx);
		return false;
	});
	$(".gallery-navi a.pointers[href='#back1']").click(function(){
		var idx = $(".gallery-preview a").index($(".gallery-preview a.active"));
		if(idx) {
			idx--;
			scrolling(idx);
		}
		else {
			scrolling($(".gallery-preview a").length-1);
		}
		return false;
	});
	$(".gallery-navi a.pointers[href='#next1']").click(function(){
		var idx = $(".gallery-preview a").index($(".gallery-preview a.active"));
		if(idx <  ($(".gallery-preview a").length-1)) {
			idx++;
			scrolling(idx);
		}
		else {
			scrolling(0);
		}
		return false;
	});
	$(".gallery-navi a.pointers[href='#back2']").click(function(){
		scrolling(0);
		return false;	
	});
	$(".gallery-navi a.pointers[href='#next2']").click(function(){
		scrolling($(".gallery-preview a").length-1);
		return false;
	});
	$(".gallery-navi a.pointers[href='#play']").click(function(){
		var idx = $(".gallery-preview a").index($(".gallery-preview a.active"));
		
		//alert(idx);
		if ($(this).hasClass("play")) {
			// отключаем
			$(this).removeClass("play");
			$(this).find("img").attr("src", "/images/content/play.png")
		}
		else {
			//включаем
			$(this).addClass("play");
			$(this).find("img").attr("src", "/images/content/pause.png")
			scrolling(idx);
		}
		return false;
	});
	
	function slideShow(idx) {
		if(idx <  ($(".gallery-preview a").length-1)) {
			idx++;
			
			scrolling(idx);
		}
		else {
			clearInterval(inter);
			//scrolling(0);
		}
	}
	
	
	// CONTENT GALLERY --------------------------------------------------------
	$(".in-scroll").each(function() {
		$(this).width(($(this).find(".album-block").length * 916));
	});
	
	$(".out-scroll").scrollLeft(0);
	$(".open-page").click(function() {
		$("#pager-" + $(this).attr("rel")).find("a.active").removeClass("active");
		$(this).addClass("active");
		
		var page = parseInt($(this).html());
		var scroll = (page-1)*916;
		//alert(scroll);
		$( "#scroll-block-" + $(this).attr("rel") ).animate({scrollLeft : scroll + 'px'}, 500);
		//alert($( "#scroll-block-" + $(this).attr("rel") ).scrollLeft());
	});
	
	
	// MENU -------------------------------------------------------------------
	$(".menu-div").hover(function(){
		$(this).find("div.hover-block").show();
		$(this).find("a.menu-link").addClass("hover-link");
	}, function(){
		$(this).find("div.hover-block").hide();
		$(this).find("a.menu-link").removeClass("hover-link");
	});
	
});
