$(function() {
	
	init_dropMenu();
	
	$("#birka_1, #birka_2")
	.mouseenter(function() {
		$(this).animate({top: "0px"}, 100, function() { $(this).clearQueue().css("top", "0px");  });
	}) 
	.mouseleave(function() {
		$(this).animate({top: "-8px"}, 100, function() { }); 
	});
	
	if (collage) 
	{
		preload(collage);
		collageAnim();
	}
	
	$("#content ol").each(function() {
		var i = 1;
		$("li", this).css("list-style", "none").each(function() {	
			$(this).prepend('<div><img src="/addons/themes/holzplast_1/img/li' + i + '.png" class="ol_num" alt="' + i + '" /></div>');
			i++;
		});
	});
	
	
	$("a.zoom").fancybox({
		'titlePosition'	: 'inside'
	});
	
	$("a.zoom_no_style").fancybox({
		'titlePosition'	: 'inside'
	});
	
	//menu modal
	$("#gallery_menu_c div").each(function() {
		var altImg = this.childNodes[0].childNodes[0].alt;
		$(this).prepend('<img src="/addons/themes/holzplast_1/img/menu_modal/'+altImg+'.png" alt="" class="menu_catalog_modal" />');
	});
	
	//awards
	awardAnimate();
	
	$("#gallery_menu_c a")
	.mouseleave(function() { $(".menu_catalog_modal", this.parentNode).clearQueue().hide(); })
	.mouseenter(function() { $(".menu_catalog_modal", this.parentNode).delay(50).fadeIn('slow'); });
	
	$(".spoiler_link").click(function() {
		if (!$(this.parentNode).hasClass("act")) 
		{
			$(".spoiler_content", this.parentNode).slideDown();
			$(this.parentNode).addClass("act");
		}
		else
		{
			$(".spoiler_content", this.parentNode).slideUp();
			$(this.parentNode).removeClass("act");
		}
	});
	
	init_slides();
	
});

function init_slides()
{
	$(".slides").each(function() {
	
		var obj = this;
		obj.items = 0;
		obj.current = 0;
		$(this).find(".slide").each(function() {
			if (obj.items != 0) $(this).css("opacity", 0);
			$(this).attr("rev", obj.items);
			obj.items++;
		});
		obj.last_current = obj.items - 1;
		sliders_animate(obj);
		
	});
}

function sliders_animate(obj)
{
	$(obj).find(".slide").eq(obj.current).css("z-index", 5);
	$(obj).find(".slide").eq(obj.last_current).css("z-index", 1);
	
	$("a.slide[rev="+obj.current+"]").animate({"opacity": 1}, 400, function() {

			$("a.slide[rev="+obj.last_current+"]").css("opacity", 0).clearQueue();
			$("a.slide[rev="+obj.current+"]").clearQueue();
			
			obj.last_current = obj.current;
			if (obj.current + 1 >= obj.items) obj.current = 0; else obj.current++;
			
			$("body").delay(3000).animate({"display": "block"}, 0, function() { sliders_animate(obj); });
			
	});
	
}


function awardAnimate()
{
	$("#award_1").addClass("act");
	setTimeout('$("#award_1").removeClass("act")', 70);
	
	setTimeout('$("#award_2").addClass("act")', 70);
	setTimeout('$("#award_2").removeClass("act")', 140);
	
	setTimeout('$("#award_3").addClass("act")', 140);
	setTimeout('$("#award_3").removeClass("act")', 210);
	
	setTimeout('$("#award_4").addClass("act")', 210);
	setTimeout('$("#award_4").removeClass("act")', 280);
	
	setTimeout('awardAnimate()', 3000);
}

var collage = [
		'/addons/themes/holzplast_1/img/header/1.jpg',
		'/addons/themes/holzplast_1/img/header/2.jpg',
		'/addons/themes/holzplast_1/img/header/3.jpg',
		'/addons/themes/holzplast_1/img/header/4.jpg',
		'/addons/themes/holzplast_1/img/header/5.jpg'
	];

var collageImgIndex = 0;

function collageAnim()
{
	$(".header_img").removeClass("header_img").addClass("header_old_img");
	$("#header_right").prepend('<div class="header_img" style="background-image: url(' + collage[collageImgIndex] + ')">&nbsp;</div>');
	$(".header_old_img").fadeOut(800, function() {
		$(this).remove();
		if (!collage[collageImgIndex + 1]) collageImgIndex = 0;
		else collageImgIndex++;
		setTimeout("collageAnim()", 3000);
	});
}

// предзагрузка изображений
function preload(images) {
    if (typeof document.body == "undefined") return;
    try {
        var div = document.createElement("div"); 
        var s = div.style;
        s.position = "absolute";
        s.top = s.left = 0;
        s.visibility = "hidden";
        document.body.appendChild(div);
        div.innerHTML = "<img src=\"" + images.join("\" /><img src=\"") + "\" />";
    } catch(e) {
        // Error. Do nothing.
    }
}


function init_dropMenu()
{
	$("#menu ul li")
		.mouseenter(function() { $(this).find(".level_1").css("left", $(this).position().left + "px").slideDown(200); })
		.mouseleave(function() { $(this).find(".level_1").slideUp(200, function() { $(this).hide(); }); });
}
