var mouse_is_inside = false;

$(document).ready(function () {

	$(document).keyup(function (e) {
		if (e.keyCode == 27) {
			hideResultsNow();
		} // esc
	});
	
	$('#faq-post-a').click(function(){
		$('#faq-post').show();
		$('#faq-list').hide();
		$('#faq-list-a').removeClass('selected');
		$(this).addClass('selected');
		return false;
	});
	
	$('#faq-list-a').click(function(){
		$('#faq-post').hide();
		$('#faq-list').show();
		$('#faq-post-a').removeClass('selected');
		$(this).addClass('selected');
		return false;
	});

	$('#gfForm').submit(function () {
		var str = $(this).serialize();
		$("#gfE").css('color', '#ccc');

		$.post('/includes/_gf.php', str, function (json) {
			if (json.status == 'ok') {
				$("#gfE").css('color', '#23871D');
				$(".mail .tail").toggle();
				$('#gfEmail').fadeOut(1000);
				$("#gfE").attr('value', '');
			} else {
				$("#gfE").css('color', '#E2302F');
			}
		}, "json");

		return false;
	});

	$("a.f").fancybox({
		'hideOnContentClick': true
	});
	$(".city-container").click(function () {
		$(".city-container .cselect .items").toggle();
	});

	$("#gf").click(function () {
		$(this).parent().toggleClass('selected');
		$(".mail .tail").toggle();
		$("#gfEmail").slideToggle("fast");
	});

	$("#close").click(function () {
		$("#overlay").hide();
		$(".mapwin").hide();
	});

	$("#overlay").click(function () {
		$(this).hide();
		$(".mapwin").hide();
	});

	$(".address").click(function () {
		var shop = $(this).attr('shop');
		var name = $(this).attr('name');
		var google = $(this).attr('google');

		$("#overlay").show();
		$(".mapwin").show();
		$("#aName").html(name);
		$("#aShop").html(shop);

		$.post('/includes/_google.php', {
			id: google
		}, function (data) {
			$("#aGoogle").html(data);
		});
	});


	$('.catalog, #menuOn').hover(function () {
		mouse_is_inside = true;
	}, function () {
		mouse_is_inside = false;
	});

	$("body").mouseup(function () {
		if (!mouse_is_inside && $('.catalog').css('display') == 'block') {
			$('#menuOn').removeClass('active');
			$('.catalog').slideToggle();
		}
	});


	$(".menuCatalog").toggle(function () {
		$(this).next().slideToggle('fast');
	}, function () {
		$(this).next().slideToggle('fast');
	});

	$("div.colors .color").mouseover(function () {
		$(this).children('.popup').show();
	}).mouseout(function () {
		$(this).children('.popup').hide();
	});

	$("#menuOn").toggle(
		function () {
			$(this).addClass('active');
			$('.catalog').slideToggle();
		}, function () {
			$(this).removeClass('active');
			$('.catalog').slideToggle();
	});
	
	$("#menuOn").mouseover(function(){
		//if ($(this).hasClass('avtive')) return;
		
		$(this).toggleClass('active');
		$('.catalog').slideToggle();		
	});
		
	$("ul.view li a").click(function () {
		var how = $(this).attr('how');

		$("ul.view li").removeClass("active");
		$(this).parent().addClass("active");

		$.get('/init.php', {
			show: how
		});

		if (how == 'list') {
			$(".wrap3 .container").addClass("view-list");
		} else {
			$(".wrap3 .container").removeClass("view-list");
		}

		return false;
	});

	$('#search').focus(function(){
		$(this).animate({width: 125}, 400);
	});
	$("#search").blur(function () {
		$(this).animate({width: 35}, 400);
		if (timeout) clearTimeout(timeout);
		//$(".suggest").html('');
		//timeout = setTimeout(hideResultsNow, 200);
	});


	$("#search").keydown(function () {
		$(".suggest .all").css("background", "url(/s/ajax-loader.gif) no-repeat 95% center");
		setTimeout(function () {

			var str = document.s.search.value; // $("#s").serialize();
			if (str.length > 1) {
				$.post('/includes/_search.php', {
					search: str
				}, function (data) {
					$(".suggest").html(data);
					$(".suggest").highlight(str);
				});
			} else {
				$(".suggest").html('');
			}
		}, 500);

	});

	//wish
	//при загрузке
	wish('');

	//добавление
	$('#wForm,.wForm').submit(function () {
		var str = $(this).serialize();
		var art = $(this).children('input[name=art_id]').val();
		
		$("#f" + art).hide();
		$("#b" + art).show();

		wish(str);
		return false;
	});

	//удаление
	$(".wClose").live("click", function () {
		var art = $(this).attr('art');
		$(this).parent().hide();
		$("#f" + art).show();
		$("#b" + art).hide();
		wish('art=' + art);

		return false;
	});

	$(".addWish").click(function () {
		var art = $(this).attr('art');
		$(this).html('');
		wish('art_id=' + art);

		return false;
	});
	
	$('.right address P').hide();
	$('.right address H3').click(function(){
		$(this).toggleClass('selected').next().slideToggle('fast');
	});

});

function other(id) {
	$.post('/includes/_other.php', {
		art_id: id
	}, function (data) {
		$("#addit").html(data);
	});
}

function wish(str) {
	$.post('/includes/_wish.php', str, function (data) {
		$(".wishlist").html(data);
	});
}
var timeout = null;

function hideResultsNow() {
	$(".suggest").html('');
}

function videoPlay() {
	$('.header.big .l,.header.big .r,.header.big .back').animate({
		opacity: 'toggle'
	}, 400);
	$('.header.big .txt,.header.big .play').animate({
		marginLeft: "-1200px"
	}, 400);
	$('.headmenu,.catalog,.logo').animate({
		marginTop: "-600px"
	}, 400);

	var flash = document.getElementById('flash');

	if (flash.tagName == 'DIV') {
		swfobject.embedSWF('/css/swf/player.swf', 'flash', '900', '520', '10.0.0', '/css/swf/expressInstall.swf', null, null, {
			wmode: "opaque"
		});
	} else {
		swfobject.getObjectById('flash').play();
	}
}

function videoPause() {
	// возвращаем все на место
	$('.headmenu,.catalog,.logo').animate({
		marginTop: 0
	}, 400);
	$('.header.big .txt,.header.big .play').animate({
		marginLeft: 0
	}, 400);
	$('.header.big .l,.header.big .r,.header.big .back').animate({
		opacity: 'toggle'
	}, 400);
}
