$(function () {

	var data;

	$.getJSON('ticker.php', function(stuff) {
		data = stuff;
		runIt();
	});

	$.fn.wait = function(time, type) 
	{
		time = time || 1000;
		type = type || "fx";
		return this.queue(type, function() {
			var self = this;
			setTimeout(function() {
				$(self).dequeue();
			}, time);
		});
	};

	function runIt()
	{
		$.each(data, function(i, item) {
			$('#curtain').fadeIn(1000, function() {$('#ticker').text(item.title).wrapInner('<a href="' + item.link + '" target="' + item.target + '"></a>')})
			.fadeOut(1000)
			.wait(5000)
			.fadeIn(1000, function() {
				if (i == data.length - 1) {
					runIt();
				}
			});
		});
	}
});