var speed = 9000;			
	
function set_permalink ( file ) {
$("#thefortune a").attr("href","?"+file);
}

function load_fortune( recursion ) {
if (speed == 0) return;
$.post("get_fortune.php",{ seed:Math.random() },function(data) {
	$("#thefortune").fadeOut("slow", function() {
		$(this).css("background-image","url(f3/"+data+".jpg)").delay(600).fadeIn("slow");
		$(this).data("slideid",data);
	});
	set_permalink( data );
	if (speed && recursion) setTimeout("load_fortune(true)",speed);
});
}

function set_heights() {
$("#fortuneoutside").height( $(window).height() );
$("#thefortune").css("top", (( $(window).height()-350)/2) + "px");
}


$(document).ready(function() {

set_heights(); 
$(window).resize(function() {
	set_heights();
});

if (window.location.search.length) {
	file = window.location.search.substr(1);
	$("#thefortune").css("background-image","url(f3/"+file+".jpg)").delay(800).fadeIn("slow");
	set_permalink( file );
	speed = 0;
	$("ul.speeds a").removeClass("current");
	$("a#s-0").addClass("current");					
} else {
  load_fortune( true );
}

$("#thefortune").hover(function(){
	$(this).children().fadeIn("fast");
}, function(){
	$(this).children().fadeOut("fast");
});

$("ul.speeds a").click(function() {
	val = $(this).attr("id").substr(2);
	oldspeed = speed;					
	if (val > 0) speed = val * 1000 + 1000;
	else speed = 0;
	$("ul.speeds a").removeClass("current");
	$(this).addClass("current");
	if (oldspeed == 0 && speed > 0) load_fortune(true);					
});

$("#thefortune a").click(function() {
	speed = 0;
	$("ul.speeds a").removeClass("current");
	$("a#s-0").addClass("current");
	window.location.search =  "?" + $("#thefortune").data('slideid');
})

});

