$(document).ready(function()
{
	$("#search-box").focus(function()
	{
		$(this).val("");
	});

	// the banners
	var banners = $("#banner ul li");

	// fade banners only when there are more than two
	if(banners.length >= 2)
	{
		// index of currently visible banner
		var current = 0;

		var speed = 1000;

		// the function that fades the banners
		var fadeBanner = function(direction, next)
		{
			// fade out currently visible banner
			$(banners.get(current)).fadeOut(speed);

			if(next == -1)
			{
				// compute index of next banner
				if(direction > 0)
				{
					next = current + 1;
				}
				else
				{
					next = current - 1;
				}

				// circular
				if(next >= banners.length)
				{
					next = 0;
				}
				if(next < 0)
				{
					next = banners.length - 1;
				}
			}

			// fade in next banner
			$(banners.get(next)).fadeIn(speed);
			current = next;

			// update circles
			$("#banner .buttons a img").attr("src", "images/circle.png");
			$("#banner .buttons a.b" + (current + 1) + " img").attr("src", "images/circle2.png");
		};

		// right arrow
		$("#banner .arrow.right").click(function(event)
		{
			event.preventDefault();
			fadeBanner(1, -1);
		});

		// left arrow
		$("#banner .arrow.left").click(function(event)
		{
			event.preventDefault();
			fadeBanner(-1, -1);
		});

		// circles
		$("#banner .buttons a").click(function(event)
		{
			event.preventDefault();
			var next = parseInt($(this).attr("class").substring(8), 10) - 1;
			if(next >= 0 && next < banners.length)
			{
				fadeBanner(0, next);
			}
		});

		// timer
		setInterval(function()
		{
			fadeBanner(1, -1);
		}, 8000);
	}

	$("#gift-message a.close").click(function(event)
	{
		event.preventDefault();
		$("#gift-message").fadeOut();
	});

	$("#gift .checkbox").change(function(event)
	{
		if($(this).attr("checked"))
		{
			$("#gift-message").fadeIn();
		}
	});

	$("#categories li").hover(function() {
		$(this).find("ul").fadeIn();
	}, function() {
		$(this).find("ul").fadeOut();
	});

	/*$("#banner").jCarouselLite(
	{
		visible: 1,
		start: 0,
		circular: true,
		speed: 1000,
		auto: 4000,
		btnGo:
		[
			".button.b1", ".button.b2", ".button.b3", ".button.b4", ".button.b5"
		],
		btnNext: ".arrow.right",
		btnPrev: ".arrow.left",
		afterEnd: function(x)
		{
			var banners = $("#banner .buttons img");
			for(var i = 0; i < banners.size(); i++)
			{
				$(banners.get(i)).attr("src", "images/circle.png");

			}

			$("#banner .buttons a." + $(x).attr("class") + " img").attr("src", "images/circle2.png");
		}
	});*/

	$("#content .categories a.category").click(function(event)
	{
		var list = $(this).parent().find("ul")

		$("#content .categories ul li ul").removeClass("ignore");
		list.addClass("ignore");

		$("#content .categories ul li ul").each(function()
		{
			if(!$(this).hasClass("ignore"))
			{
				$(this).slideUp();
			}
		});
		list.slideDown();

		//event.preventDefault();
	});

	$("#content .gallery .thumbs a").click(function(event)
	{
		$("#content .gallery .large").attr("src", $(this).find("img").attr("src"));

		event.preventDefault();
	});
});
