jQuery.fn.recentnews = function(settings) {
	settings = jQuery.extend({
				delay: 5500,
				exe: 'getJSON',
				//url: 'js/news.json'
				url: 'http://' + window.location.hostname + '/js/news.json'
			}, settings);
	
	var news = null;
	var newsArray = [];
	var current = -1;
	var timer = null;
	var inprogress = false;
	
	var addevents = function(obj) {
		timer = setTimeout(function() {
			fetchnext(obj);
			addevents(obj);
		}, settings.delay);
	};
	
	var run = function(obj) {
		for (var i in news) //create the links
			newsArray[i] = $('<a>').attr({id: i, href: 'http://' + window.location.hostname + '/' + news[i].link}).css({position: 'relative', fontWeight: 'normal'}).html((parseInt(i) + 1) + '. ' + news[i].desc + ', ' + news[i].date);
		
		fetchnext(obj);
		addevents(obj);
	};
	
	var buttons = function(obj) {
		var _top = ($.browser.msie && $.browser.version >= 8) || $.browser.opera ? '6px' : ($.browser.mozilla ? '-2px' : '1px');
		//var prev = $('<button class="prev" />').css({backgroundImage: 'url(img/arrow-left.png)', top: _top});
		var prev = $('<button class="prev" />').css({backgroundImage: 'url(http://' + window.location.hostname + '/img/arrow-left.png)', top: _top});
		//var next = $('<button class="next" />').css({backgroundImage: 'url(img/arrow-right.png)', top: _top});
		var next = $('<button class="next" />').css({backgroundImage: 'url(http://' + window.location.hostname + '/img/arrow-right.png)', top: _top});
		
		prev.bind('click', function() {
			$(this).blur();
			
			if (!inprogress) {
				inprogress = true;
				clearTimeout(timer);
				fetchprev(obj);
				addevents(obj);
			}
		});
		
		next.bind('click', function() {
			$(this).blur();
			
			if (!inprogress) {
				inprogress = true;
				clearTimeout(timer);
				fetchnext(obj);
				addevents(obj);
			}
		});
		
		$(obj).before(prev);
		$(obj).before(next);
	};
	
	var fetchprev = function(obj) {
		current--;
		show(obj);
	};
	
	var fetchnext = function(obj) {
		current++;
		show(obj);
	};
	
	var clear = function(obj) {
		if ($(obj).children().length > 0)
			$(obj).children().fadeTo(250, 0.1, function() {
				$(this).remove();
				$(document).dequeue('display');
				inprogress = false;
			});
		else
			$(document).dequeue('display');
	};
	
	var show = function(obj) {
		$(document).queue('display', function() {
			if (current < 0)
				current = newsArray.length * 10 - 1;
			
			$(obj).append(newsArray[current % newsArray.length].fadeTo(250, 1));
		});
		clear(obj);
	};
	
	$(this).each(function() {
		var that = this;
		
		$(document).queue(settings.exe, function() {
			run(that);
			buttons(that);
		});
		
		//initialize runs automatically because of ajax
		var init = function(d) {
			/*
			$.get(settings.url, 
				function(data, textStatus) {
					news = data;
					d.dequeue(settings.exe);
				}, "json");
				*/
			$.getJSON(settings.url, 
				function(data, textStatus) {
					news = data;
					d.dequeue(settings.exe);
			});
		}($(document));
	});
};//recentnews

