$(document).ready(function(){

	// ----------------------------
	// Двигатель лоадера
	
	if(!($.browser.msie && $.browser.version == "6.0")) {
	
		$(document).mousemove(function(e){
			$("#loader").css({top: e.pageY - 30, left: e.pageX + 5});
		});
	
	}
	
	// ----------------------------
	// Сообщение, что браузер юзера - гавно

	if($.browser.msie && ($.browser.version == "6.0" || $.browser.version == "7.0")) {
		var closed = getCookie("browserMessageClosed");
		if(!closed) {
			$("#browserMessage .version").append($.browser.version);
			$("#browserMessage").show();
		}
	}
	
	// ----------------------------
	// Обозреватель галереи - карусель

	$("#gallery").jcarousel({
		vertical: true,
		scroll: 2
	});
	
	// ----------------------------
	// Саб меню в промо разделе
	
	if($.browser.msie && $.browser.version == "6.0") {
		$(".topSubMenu li.parent").hover(
			function(){
				$(".topSubMenu li.parent .sub").show();
			},
			function(){
				$(".topSubMenu li.parent .sub").hide();
			}
		);
	}
	
	// Затемнение промо блоков на глагне
	
	$(".faded").css({
		opacity: "0.2"
	});
	
	// Слайдер вверху

	$(".topMenu.promo table").animate({
		opacity: 0.2
	}, 500);

	$(".topMenu .sub").hover(
		function(){
			var subList = $(this).children("ul");
			if($(subList).css("display") == "none")
				$(subList).slideDown(250);
		},
		function(){
			var subList = $(this).children("ul");
			if($(subList).css("display") != "none")
				$(subList).slideUp(250);
		}
	);
	
	// анимация
	
	var topMenuBgPos = -275;

	// если IE
	if($.browser.msie) {
		$(".topMenu").css({
			backgroundPosition: "50% " + topMenuBgPos + "px"
		}, 500);
	}
	else {
		$(".topMenu").animate({
			backgroundPosition: "(50% " + topMenuBgPos + "px)"
		}, 500);
	}
	
	$(".topMenu").hover(
		function(){
			
			if(!($.browser.msie && $.browser.version == "6.0")) {
			
				var slideTo = $(this).height() - topMenuBgHeight;
				$(".topMenu").animate({
					backgroundPosition: "(50% bottom)"//" + slideTo + "px
				}, 300);
				/*setTimeout(function() {
					$(".topMenu").css({backgroundPosition: "50% bottom"});
				}, 120);*/
			
			}
			
			$(".topMenu.promo table").animate({
				opacity: 1
			}, 300);
		},
		function(){
			
			if(!($.browser.msie && $.browser.version == "6.0")) {
			
				$(".topMenu").stop().animate({
					backgroundPosition: "(50% " + topMenuBgPos + "px)"
				}, 300);
				
			}
				
			$(".topMenu.promo table").animate({
				opacity: 0.2
			}, 300);
		}
	);
	
	// ----------------------------
	// Диалоговое окно
	
	$('#dialog').css({
		top: 0
	});
	
	$('#dialog .close').click(function() {
		dialogHide();
	});
	
	$('#overlay').click(function() {
		dialogHide();
	});
	
	if($.browser.msie && $.browser.version == "6.0") {
		$(window).scroll(function() {

			var dialogTopMargin = ($(this).height() / 2 - dialogWatchLastHeight / 2 + document.documentElement.scrollTop) + "px";
			var topMargin = document.documentElement.scrollTop + "px";

			$("#dialog").css({
				top: dialogTopMargin
			});

			$("#overlay").css({
				top: topMargin
			});

		});
	}
	
	$('.thumbnail img').click(function(){
		var height = $(this).parent().attr('rel');
		if(height > 600)
			height = 600;
		$("#dialog .inner").html("<center><img src=\"" + $(this).parent().attr('href') + "\" height=\"" + height + "\" /></center>");
		dialogShow();
		$("#dialog center").click(function() {
			dialogHide();
		});
		return false;
	});
	
	$('.galleryAnimate img').click(function(){
		$("#dialog .inner").html("<center><img src=\"" + $(this).parent().attr('href') + "\" /></center>");
		dialogShow();
		$("#dialog center").click(function() {
			dialogHide();
		});
		return false;
	});

});

// ----------------------------
// Всякий функционал

// Закрываем сообщение об ущербости браузера пользователя

function browserMessageClose() {
	$("#browserMessage").slideUp(300);
	setCookie("browserMessageClosed", "1", 1, "/");
}

// Функции отображения промо блоков на глагне

var topMenuBgHeight = 300;

function showPromo(id) {
	$("#" + id).animate({
		opacity: "1"
	}, 1000);
}

// Это, чтобы окошко браузера чотко работало

var dialogWatchLastHeight = 0;
var dialogWatchCurHeight = 0;
var dialogWatchKey = 1;

function dialogWatch() {
	dialogWatchCurHeight = $('#dialog').height();
	if(dialogWatchCurHeight != dialogWatchLastHeight) {
		dialogWatchLastHeight = dialogWatchCurHeight;
		var top = 0;
		if($.browser.msie && $.browser.version == "6.0")
			top = ($(window).height() / 2 - dialogWatchCurHeight / 2 + $(document).scrollTop());
		else
			top = $(window).height() / 2 - dialogWatchCurHeight / 2;
		$('#dialog').animate({ 
			top: top + "px"
		}, 300 );
	}
	if(dialogWatchKey) {
		setTimeout(function() {
			dialogWatch();
		}, 300);
	}
	return false;
}

function dialogShow() {
	if($.browser.msie && $.browser.version == "6.0")
		$('select.styled').hide();
	$('#dialog').fadeIn();
	$('#overlay').fadeIn().fadeTo(200, 0.7);
	dialogWatchKey = 1;
	dialogWatch();
}

function dialogHide() {
	$('#dialog').fadeOut();
	$('#overlay').fadeOut();
	dialogWatchKey = 0;
	if($.browser.msie && $.browser.version == "6.0")
		$('select.styled').show();
}
