$(document).ready(function()
	{
		if (!questionLodaded) {
			setTimeout(function(){
				//$("#current .yellowFrame").show("medium");
				$("#current .question").show("slow");
				$("#current .answers").show("slow");
			}, 500);

			setTimeout(function(){
				$(".searching").hide();
				$(".results").slideDown("slow");
			}, 1000);
		}

		$(".type .name a").click(function(e){
			if (disableAnimation)
				return;

			e.preventDefault();
			var linkHref = $(this).attr("href");
			var typeField = $(this).parents(".type");
			var questionField = typeField.children(".yellowFrame").children(".m");
			var typeID = $(this).attr('rel');
			var url = "/ajax/question/?p="+typeID+"&url="+curentURL;

			$("#current .question").hide("slow");
			$("#current .answers").hide("slow", function(){
				$("#current").attr("id", "null");
				typeField.attr("id", "current");

				$.getJSON(url, function(data) {
					if (data.status != 'OK'){
						alert("AJAX error");
						location.href = linkHref;
						return;
					}
					questionField.html('<div class="question" style="display:none"><h2>'+data.question+'</h2></div>');
					var answers = document.createElement('ul');
					$(answers).attr('class', 'answers');
					$(answers).attr('style', 'display:none');
					$.each(data.answers, function(){
						$(answers).append('<li><a href="'+this.link+'">'+this.label+'</a></li>');
					});
					questionField.append(answers);
					typeField.children(".name, .select").hide("slow");
					questionField.children(".question").show("slow");
					questionField.children(".answers").show("slow");
				});
			});
		});

		$(".type .answers li a").live('click', function(e){
			if (disableAnimation)
				return;

			e.preventDefault();
			var answerText = $(this).text();
			var answerLink = $(this).attr("href");
			var Current = $("#current");
			var oldText = $("#current .name").text();
			var isBlank = Current.hasClass("blank");

			setTimeout(function(){
				location.href = answerLink;
			}, 500);

			setTimeout(function(){
				Current.attr("id", "current");
				$(".question", Current).show("slow");
				$(".answers", Current).show("slow");
				$("#current .name").text(oldText);
				if (isBlank)
					Current.addClass("blank");
			}, 4000);

			$("#current .question").hide("slow");
			$("#current .answers").hide("slow", function(){
				$(".name", Current).text(answerText);
				$(".name_field", Current).hide();
				$("#current").attr("id", "null");
				Current.removeClass("blank");
				$(".name", Current).css('font-weight', 'bold');
				$(".name_field", Current).show("slow", function(){
					//location.href = answerLink;
					});
			});
		});

		$(".pagination .big").click(function(){
			location.href = $("a", $(this)).attr("href");
		});

		var annotationTimeout = null;
		$(".gifts .gift").live('mouseenter', function(){

			if (annotationTimeout)
				clearTimeout(annotationTimeout);

			$("a.name", $(this)).addClass("linkOver");
			var Text = "<strong>" + $("a.name", $(this)).text() + "</strong><br/>" + $("img", $(this)).attr("alt");

			if (!Text)
				return;

			var elem = this;
            
			annotationTimeout = setTimeout(function(){
				$(".annotation .text").html(Text);

				var y = 0, x = 0;

				while (elem && $(elem).attr("class") != "main") {
					y += elem.offsetTop;
					x += elem.offsetLeft;
					elem = elem.offsetParent;
				}

				x -= 270;
                
				$(".annotation").css("top", y+"px").css("left", x+"px");
				$(".annotation").fadeIn("slow");
				annotationTimeout = null;
			}, annotAnimationTime);
		});

		$(".gifts .gift").live('mouseleave', function(){
			if (annotationTimeout)
				clearTimeout(annotationTimeout);
			else
				$(".annotation").fadeOut("slow");
		});

		$(".gifts .gift").click(function(){
			location.href = $("a.name", $(this)).attr("href");
		});

		//$(".buyBtn").click(function(){
		//location.href = $("a", $(this)).attr("href");
		//});

		$(".gifts .gift .image").live('mouseleave', function(){
			$("a.name", $(this).parent()).removeClass("linkOver");
		});

		$(".blockImage .image").live('mouseover', function(){
			$(".block1 .gift .name a").addClass("linkOver");
		});

		$(".blockImage .image").live('mouseout', function(){
			$(".block1 .gift .name a").removeClass("linkOver");
		});

		$(".special .close").click(function(){
			var exdate = new Date();
			exdate.setDate(exdate.getDate()+1);
			document.cookie="specialHide=1; path=/; expires=" + exdate.toGMTString();
			$(this).parents(".mainGift").hide("slow");
		});

		var Animation = {};
		var curentIndex = 0;
		var curentRequest = null;
		var curentTimeout = null;
		var currentElem = 0;

		$(".gifts .gift").live('mouseenter', function(){
			$(".label", $(this)).show();

			if (!giftAnimationTime)
				return;

			stopAnimation();

			var elem_id = $(this).attr("id");
			var first_gift = $(".name", $(this)).attr("id").slice(4);
			currentElem = elem_id;
			var curImg = $("img", $(this));

			curImg.animate({
				opacity: 1
			}, 200, function() {
				if (currentElem != elem_id)
					return;
			
				//curImg.animate({opacity: 0.5}, 250).animate({opacity: 1}, 250, function(){

				if ( elem_id in Animation ) {
					curentTimeout = setTimeout(function(){
						startAnimation(elem_id);
					}, giftAnimationTime-200);
					return;
				}

				curentRequest = $.getJSON("/ajax/gifts/?group="+elem_id.slice(1), function(data) {
					if (!data || data.status != 'OK'){
						//alert("AJAX error");
						return;
					}

					Animation[elem_id] = [first_gift];

					$.each(data['gifts'], function(i, val) {
						if (val.id != first_gift)
							Animation[elem_id].push(val);
					});

					curentTimeout = setTimeout(function(){
						startAnimation(elem_id);
					}, giftAnimationTime-200);
				});
			//});
			});
		});

		$(".gifts .gift").live('mouseleave', function(){
			$(".label", $(this)).hide();
			stopAnimation();
			currentElem = 0;
			if ($(this).attr("id") in Animation)
				changeImage($(this).attr("id"), Animation[$(this).attr("id")][0]);
		});

		function stopAnimation() {
			if (curentRequest)
				curentRequest.abort();
			if (curentTimeout)
				clearTimeout(curentTimeout);
			curentIndex = 0;
		}

		function startAnimation(elem_id) {
			if (Animation[elem_id].length < 2)
				return;

			if (currentElem != elem_id)
				return;

			curentIndex++;
			if (curentIndex >= Animation[elem_id].length)
				curentIndex = 0;

			var image = document.createElement('img');
			image.onload = function () {
				changeImage(elem_id, Animation[elem_id][curentIndex]);
				curentTimeout = setTimeout(function(){
					startAnimation(elem_id);
				}, giftAnimationTime);
			};
			image.src = "/images/small/" + Animation[elem_id][curentIndex] + ".jpg";
		}

		function changeImage(elem_id, gift_id) {
			var curGift = $(".gifts .gift#"+elem_id);
			if ( $(".name", curGift).attr("id") == gift_id )
				return;
		
			$("img", curGift).fadeOut("fast", function(){
				$("img", curGift).attr("src", "/images/small/"+gift_id+".jpg");
				var link = $(".name", curGift).attr("href");
				link = link.replace(/gift=([0-9]+)/, "gift="+gift_id);
				$("a", curGift).attr("href", link);
				$(".name", curGift).attr("id", gift_id);
				$("img", curGift).fadeIn("fast");
			});
		}

		var aP_cache = {};

		$(".gifts").each(function(){
			var letter = $(this).attr("id");
			aP_cache[letter+'1'] = $(this).html();
		});

		function aPchange(letter, page)
		{
			if (aP[letter][2])
				aP[letter][2].abort();
			
			var props = [aP[letter][0]].concat(selected);
			var gpanel = $(".gifts#"+letter);
			var h = parseInt(gpanel.css("height"));
			$(gpanel).animate({opacity: 0}, 400, function() {
				gpanel.html('<div style="text-align:center;" class="pAwaiting"><img style="margin:'+Math.round((h-32)/2)+'px 0;" aling="center" alt="Идёт загрузка..." src="/images/ajax-loader.gif" /></div>');
				$(gpanel).animate({opacity: 1}, 0, function() {
					if ( letter+page in aP_cache )
						$(gpanel).animate({opacity: 0}, 0, function() {
							gpanel.html(aP_cache[letter+page]);
						}).animate({opacity: 1}, 500);
					else
						aP[letter][2] = $.ajax({
							url: '/ajax/aP/?l='+aP_size+'&p='+letter+page+'&s='+props.join(','),
							success: function(data){
								aP[letter][2] = false;
								aP_cache[letter+page] = data;
								$(gpanel).animate({opacity: 0}, 0, function() {
									gpanel.html(data);
								}).animate({opacity: 1}, 500);
						}});
				});
			});
		}

		$(".aP .page a").live('click', function(){
			var page = $(this).attr("href").slice(2);
			var letter = $(this).attr("href").slice(1,2);
			aPchange(letter, page);
			var max_page = Math.ceil(aP[letter][1] / aP_size);
			var pages = $(this).parents(".internal");
			pages.html('');
			var f_p = Math.max(1, page*1-3);
			var l_p = Math.min(max_page, page*1+3);

			while (f_p <= l_p) {
				pages.append('<li class="page'+(f_p==page ? ' selected' : '')+'"><a class="'+letter+f_p+'" href="#'+letter+f_p+'">'+f_p+'</a></li> ');
				f_p++;
			}

			var np = page*1+1;
			if (np <= l_p)
				pages.append('<li class="page"><a class="'+letter+np+'" href="#'+letter+np+'">Следующие &#62;</a></li>');
				
		});
	});

var giftAnimationTime = 0;
var annotAnimationTime = 0;
var disableAnimation = 0;