$(document).ready(function() {
//popup base for popup menu
   	var height = 0;
	$(".popupmenu").each(
		function(){
			if (height < $(this).height()) height = $(this).height()+3;
		}
	);
	$("#popupbase_for_menu").css('height',height);
	$("#menu").hover(
    	function () {
        	$("#popupbase_for_menu").css('display','block');
    	}, 
    	function () {
       		$("#popupbase_for_menu").css('display','none');
    	}
    );
	$("#menu li.firstlevel").hover(
    	function () {
        	$(this).children('ul').css('display','block');
    	}, 
    	function () {
       		$(this).children('ul').css('display','none');
    	}
    );			 
//gallery schroll
	function scrollleft(){
		$("#gallery ul").prepend($("#gallery ul li:last-child"));
	}
	function scrollright(){
		$("#gallery ul").append($("#gallery ul li:first-child"));
	}
	var intervalID ;
	$('#leftbtn').bind({
		mousedown: function(){
			intervalID = setInterval(scrollleft, 10)
		}
		,
		mouseup: function(){
			clearInterval(intervalID);
		}
	});
	$('#rightbtn').bind({
		mousedown: function(){
			intervalID = setInterval(scrollright, 10)
		}
		,
		mouseup: function(){
			clearInterval(intervalID);
		}
	});
	$("#gallery li").bind('click', function (){
		$("#gallery li").removeClass("current");
		$(this).addClass("current");
	});
//popupFAG
	$("#deflist dt").bind("click", function (){
		if ($(this).next().css('display') == 'block'){ $(this).next().removeClass("opened");$(this).removeClass("opened")}
		else {$(this).addClass("opened"); $(this).next().addClass("opened")}
	});
//popupmenu in rightcolumn
	$(".popuprightmenu .mainindex").bind("click", function (){
		if ($(this).siblings("li:last-child").css('display') == 'block') $(this).siblings("li").not(":first-child").css("display","none")
		else $(this).siblings("li").css("display","block")
	});
//FAQ popupmenu in rightcolumn
	$(".faq_popupmenu .mainindex").bind("click", function (){
		if ($(this).siblings("li:last-child").css('display') == 'block') $(this).addClass("faq_subheader")
		else $(this).removeClass("faq_subheader")
	});
	
	
//Main Banner ROTATOR	 
    var rotateinteravalID;
	function rotate(rotateit) {
		// Берем первую картинку
		var current = ($('div#rotator ul#rotator_imgs li.show')?  $('div#rotator ul#rotator_imgs li.show') : $('div#rotator ul#rotator_imgs li:first'));
		//$("#consol").html(current.text());
		//если пользователь нажал сменить картику
		if (rotateit !== "noid") {
			var next = $('div#rotator ul#rotator_imgs li:nth-child('+rotateit+')');
		}
		else {
			// Берем следующую картинку, когда дойдем до последней начинаем с начала
			var next = ((current.next().length) ? ((current.next().hasClass('show')) ? $('div#rotator ul#rotator_imgs li:first') :current.next()) : $('div#rotator ul#rotator_imgs li:first'));	
		 
			// Показ картинок в случайном порядке
			// var sibs = current.siblings();
			// var rndNum = Math.floor(Math.random() * sibs.length );
			// var next = $( sibs[ rndNum ] );
		}
		
		// Подключаем эффект растворения/затухания для показа картинок, css-класс show имеет больший z-index
		next.css({opacity: 0.0})
		.addClass('show')
		.animate({opacity: 1.0}, 1000);
	 
		// Прячем текущую картинку
		current.animate({opacity: 0.0}, 1000)
		.removeClass('show');
		
		//выделяем кнопку соответствующую картинке
		function nextbutton(){
			$("#rotator #rotator_btns li").removeClass("current");
			$("#rotator #rotator_btns li:nth-child("+(next.index()+1)+")").addClass('current');
		}
		setTimeout(nextbutton,1000);
	};
	function theRotator() {
		// Устанавливаем прозрачность всех картинок в 0
		$('div#rotator ul#rotator_imgs li').css({opacity: 0.0});
	 
		// Берем первую картинку и показываем ее (по пути включаем полную видимость)
		$('div#rotator ul#rotator_imgs li:first').css({opacity: 1.0});
	 
		// Вызываем функцию rotate для запуска слайдшоу, 5000 = смена картинок происходит раз в 5 секунд
		rotateinteravalID = setInterval(function(){rotate("noid")},5000);
	}	 
	// Запускаем слайдшоу
	theRotator();
	
	//add Rotate buttons
	$("#rotator").append('<ul id="rotator_btns"></ul>');
	$("#rotator #rotator_imgs li").each(function(){
		$("#rotator #rotator_btns").append("<li>");
	});
	$("#rotator #rotator_btns li:first-child").addClass('current');
	$("#rotator #rotator_btns li").bind('click',function(){
		if (!$(this).hasClass("current")){
			$("#rotator #rotator_btns li").removeClass("current");
			$(this).addClass("current");
			clearInterval(rotateinteravalID);
			rotate($(this).index()+1);
			rotateinteravalID = setInterval(function(){rotate("noid")},5000);
		}
	});
	

}); 

