var fortune = new fortuneObj();

$(document).ready(function() {
	$("#fortuneContent .content").html( fortune.getNext() );
	$("#fortuneContent .loading").fadeOut(500);

	$("#fortunePrevious").click(function() {
		$("#fortuneContent .loading").show();
		$("#fortuneContent .content").html( fortune.getPrevious() );
		$("#fortuneContent .loading").fadeOut(500);
	});
	$("#fortuneNext").click(function() {
		$("#fortuneContent .loading").show();
		$("#fortuneContent .content").html( fortune.getNext() );
		$("#fortuneContent .loading").fadeOut(500);
	});
});

function fortuneObj() {
	var current = 0;
	var list = new Array();

	var getNewFortune = function() {
		var newFortune = "";

		$.ajax({
			type: "POST",
			url: "/ajax/misc/fortune.html",
			async: false,
			success: function(data) {
				newFortune = data;
			},
			error: function() {
				newFortune = "Error retreiving fortune.";
			}
		});

		return newFortune;
	}

	this.getCurrent = function() {
		return list[current];
	}

	this.getPrevious = function() {
		if (current > 0) {
			current--;
		} else {
			list.unshift( getNewFortune() );
			current = 0;
		}
		return list[current];
	}
	this.getNext = function() {
		if (current < list.length - 1) {
			current++;
		} else {
			current = list.push( getNewFortune() ) - 1;
		}
		return list[current];
	}
}


