$(function() {
	var defaultQueryText = "Search for porn...";
	var nextAnimation = -1;
	
	$query = $("#query");
	$searchForm = $query.parents('form');
	
	// Preload images
	var preload1 = new Image();
	preload1.src = '/art/searchButtonHover.png';
	var preload2 = new Image();
	preload2.src = '/art/buttonBGHover.png';
	
	$('#logo').hover(function() {
		$(this).fadeTo(200, 0.7);
	}, function() {
		$(this).fadeTo(200, 1.0);
	});
	
	
	if ($query && !isNaN($query.val()) && $query.val().length < 1) {
		$query.val(defaultQueryText);
	}
	$query.focus(function() {
		if ($query.val() == defaultQueryText) {
			$query.val("");
		}
	});
	$query.blur(function() {
		if ($query.val().length < 1) {
			$query.val(defaultQueryText);
		}
	});
	
	$query.autocomplete({
		source: "http://eveknows.com/suggest.php?json=2",
		minLength: 2
	});
	
	$('#queryButton').hover(function() {
		$(this).attr('src', '/art/searchButtonHover.png');
	}, function() {
		$(this).attr('src', '/art/searchButton.png');
	});
	
	$('#searchControls input').change(function() {
		if ($query.val() != defaultQueryText) {
			$searchForm.submit();
		}
	});
	
	$('#searchControls select').change(function() {
		if ($query.val() != defaultQueryText) {
			$searchForm.submit();
		}
	});
	
	$(".result_summary img").hover(function() {
		$(this).fadeTo('fast', 0.8);
	}, function() {
		$(this).fadeTo('fast', 1.0);
	});
	
	$('.galleryDetails').hide();
	
	$(".searchImages ul li").hover(function() {
		$(this).parent().find('.galleryDetails').hide();
		animate_gallery_start($(this));
		$(this).find(".galleryDetails").show().position({
			my: 'top',
			at: 'bottom',
			of: $(this),
			offset: "0 -10"
		}).find(".galleryMoreInfo").hide();
		$(this).find(".galleryMoreInfoButton").show();
	}, function() {
		animate_gallery_stop($(this));
		$(this).find(".galleryDetails").hide();
	});
	
	$('.galleryMoreInfoButton').click(function() {
		var $button = $(this);
		$(this).parent().find('.galleryMoreInfo').slideDown('fast', function() {
			$button.hide();
		});
	});
	
	$(".searchImages ul li.frontPage").hover(function() {
		$(this).find("img").fadeTo('fast', 0.7);
	}, function() {
		$(this).find("img").fadeTo('fast', 1.0);
	});
	
	function animate_gallery_start($gallery) {
		$gallery.data('animate', '1');
		if (nextAnimation > -1) {
			clearTimeout(nextAnimation);
			nextAnimation = -1;
		}
		
		if ($gallery.data('thumbs-loaded') != '1') {
			var $link = $gallery.find('.galleryLink');
			var thumb1 = $gallery.data('thumb-src-1');
			var thumb2 = $gallery.data('thumb-src-2');
			var thumb3 = $gallery.data('thumb-src-3');
		
			$link.html('<img src="' + thumb1 + '" alt="" class="galleryThumb1" /><img src="' + thumb2 + '" alt="" class="galleryThumb2" /><img src="' + thumb3 + '" alt="" class="galleryThumb3" />');
		
			$gallery.data('thumbs-loaded', '1');
		}
		
		animate_gallery($gallery);
	}
	
	function animate_gallery($gallery) {
		var animate = $gallery.data('animate');
		if (animate == '1') {
			if (nextAnimation > -1) {
				clearTimeout(nextAnimation);
				nextAnimation = -1;
			}
			
			var thumb = $gallery.data('thumb');
			if (typeof thumb === 'undefined') {
				return;
			}
			var nextThumb = thumb + 1;
			if (nextThumb > 3) {
				nextThumb = 1;
			}
			var lastThumb = thumb - 1;
			if (lastThumb < 1) {
				lastThumb = 3;
			}
			$gallery.find(".galleryThumb" + lastThumb).css("z-index", "1");
			$gallery.find(".galleryThumb" + thumb).css("z-index", "2");
			$gallery.find(".galleryThumb" + nextThumb).hide().css("z-index", "3").slideDown('fast', function() {
				$gallery.data('thumb', nextThumb);
				nextAnimation = setTimeout(function() {animate_gallery($gallery);}, 1200);
			});
		}
	}
	
	function animate_gallery_stop($gallery) {
		$gallery.data('animate', '0');
	}
});


