//анимация, которая плавно показывает все элементы страницы, после загрузки флешки
function showElements() {
	$('html').css('background-image', 'url(/themes/ram/img/bg.jpg)')
	$('#main-column, .footer, .sidebar').css('opacity', '0');
	$('#main-column, .footer, .sidebar').show();
	$('#main-column, .footer').animate({
		opacity: '1'
	}, 400)
	$('#content-unit').hide();
}

//анимация, которая раскрывает контентную область
function showContent(){
	$('html').css('background-image', 'url(/themes/ram/img/bg.jpg)')
	$('#content-unit').show()
	$('#sbMenu li').children().css('opacity', '0');
	var contentHeight = $('#content').outerHeight(true);
	$('#content-unit').css('height', contentHeight)
	$('#content').css('height', '0')
	$('#content').css('opacity','1');
	$('#content').animate({
		height: contentHeight
	}, 1000);
}

//анимация, которая скрывает контентную область
function hideContent(){
	$('#sbMenu li').children().css('opacity', '0');
	$('#content').animate({
		height: 0
	}, 1000);
	$('.content').children().hide();
}

//анимация, которая показывает пункты меню
function animationFigure(i){
	$('.sidebar').css('opacity', '1');
	$('#sbMenu li').children().css('opacity', '1');
	$('#sbMenu li a').css('opacity', '0');
	$('.sidebar-menu_item'+i).animate({
		right: 0,
		opacity: 1
	}, 400).nextAll().delay(75-i*3);
}

//анимация, которая прячет пункты меню до перезагрузки страницы
function hideAnimation(i){
	$('.sidebar-menu_item'+i).animate({
		left: 200,
		opacity: 0
	}, 400).nextAll().delay(75-i*3);
}

function initAnimation(){
	var menuItemsLength = $('#sbMenu li a').length;
	for (i=0; i<=menuItemsLength; i++) {
		animationFigure(i);
	}
}

//анимация для ховера меню
function hoverFigure(){
	
	$('#sbMenu li a').css('opacity', '0');
	
	$('#sbMenu li a').mouseover(function(){
		$(this).siblings('div').stop().animate({
			opacity: 0
		}, 200);
		$(this).siblings('span').stop().addClass('textColor');
		$(this).stop().animate({
			opacity: 1
		}, 200);
	})
	
	$('#sbMenu li a').mouseout(function(){
		$(this).siblings('div').stop().animate({
			opacity: 1
		}, 200);
		$(this).siblings('span').stop().removeClass('textColor');
		$(this).stop().animate({
			opacity: 0
		}, 200);
	})
}

//анимация, которая прячет меню после чего перезагружается страница
function hideMenu(){

	$('#sbMenu li a, #mainmenu a').click(function(event){
		
		if($(this).parent('li.analyticsCheckBox').size()){
			return false;
		}
		//переходим по ссылке только после окончания анимации
		event.preventDefault();
		var menuItemsLength = $('#sbMenu li a').length;
		var delay = 0;
		for (i=0; i<=menuItemsLength; i++) {
			hideAnimation(i);
			delay+=75-i*3;
		}

		setTimeout('hideContent();', delay+400)

		var url = $(this).attr('href');
		setTimeout(function(){
			document.location.href=url
		}, delay+1400);	
	});
};

//запускаем анимацию, используя необходимые проверки
function startAnimation(){
	$('#sbMenu li').css('right', '-150px')
	$('#sbMenu li').each(function(i){
		$(this).addClass('sidebar-menu_item'+i)
		$(this).css('opacity', 0);
	})
	
	//проверяем на наличие класса hasFlash, если его нет, запускаем анимацию
	if($('body').hasClass('hasFlash')){
		return false;
	} else {
		hoverFigure();
		showContent();
		setTimeout('initAnimation()', 1000);
	}
	
	hideMenu();
	
};

//центрируем текст в пунктах меню
function textLineheight(){
	$('#sbMenu span').each(function(i){
		var menuItemHeight = $(this).height();
		var menuParentHeight = $(this).parent().height();
		$(this).css('top', menuParentHeight/2);
		$(this).css('margin-top', -menuItemHeight/2 - 1);
		$(this).height(menuItemHeight + 5);
	})
}

function tagCloudShow(){
	$('#blog-search').animate({height: 'toggle'}, 200, function(){
		var contentHeight = $('#content').outerHeight();
		var tagBlockHeight = $('#blog-search').outerHeight();
		//console.log(tagBlockHeight);
		$('#content-unit, #content').css('height', 'auto');
	});	
}

$(window).load(function(){
	
	textLineheight();
	startAnimation();

})
