$(function(){

	//--- Bind the events
	$("#newstrigger-down").hover( news_scrolldown, news_scrollstop ).css("cursor", "pointer");
	$("#newstrigger-up").hover( news_scrollup, news_scrollstop ).css("cursor", "pointer");

	//--- Fix the height of the news items to be whatever the currently rendered height is
	$(".newsitems-wrap").css("height", $(".newsitems-wrap").height() );

	//--- Set the margin-top to 0 so we can move it later
	$("ul.newsitems").css("margin-top", "0px");
	
});


var news_scrolldir = 0;
var news_scrollstep = 2;
var news_scrolldelay = 20;
var news_scrolltimer = 0;
var news_scrollloading = false;
var news_scrollmaxidx = false;

function news_scrolldown() {
	news_scrollstop();
	news_scrolldir = -1;
	news_scrollfunc();
}
function news_scrollup() {
	news_scrollstop();
	news_scrolldir = 1;
	news_scrollfunc();
}

//--- Appends a new news item onto the bottom of the list
function news_addnew() {
	//--- Return immediately if we've already fetched the last available item
	if (news_scrollmaxidx) return;
	
	news_scrollloading = true;
	var item = $(".newsitem.template").eq(0).clone();
	
	var idx = $("li.newsitem").length

	item.removeClass("template");
	item.addClass("loading");
	item.load(  $(".newsitems").attr("feeder") +"?idx="+idx, {}, news_addnew_callback );
	item.css("display","none");

	$(".newsitems").append( item );

	item.fadeIn();
}

function news_addnew_callback(responseText, textStatus, XMLHttpRequest) {
	news_scrollloading = false;
	$(this).removeClass("loading");
	
	if( responseText.replace(/^\s+|\s+$/g,"") ==""){ 
		news_scrollmaxidx = true;
		$(this).remove();
	}	
}


//--- Stops the currently runing animation
function news_scrollstop() {
	clearTimeout(news_scrolltimer);
}

//--- Function that actually moves the news items.
function news_scrollfunc() {
	var winheight, itemsheight, offset;
	var newsitemob = $("ul.newsitems");
	var nowtop = parseInt(newsitemob.css("margin-top"));

	//-- Calculate the new position
	var newtop = (nowtop + news_scrolldir * news_scrollstep);
	if( newtop > 0 ) newtop = 0;

	//-- Do we need to add a new item?  Only if scrolling up and the entire last item is visible
	winheight = $(".newsitems-wrap").height();
	itemsheight = $("ul.newsitems").height();
	offset = parseInt( $("ul.newsitems").css("margin-top") );

	//--- If we're scrolling down and we are starting to show an item that doesn't yet exist, load one.
	if( news_scrolldir == -1 && ( offset + itemsheight <= winheight - 15 ) && !news_scrollloading ) {
		news_addnew();
	}

	//--- If we're going downwards, don't move if we're at the bottom of the last
	//		entry and it's loading.  (but call this func again so we can resume scrolling once item is loaded)
	if( news_scrolldir == -1 && (offset + itemsheight <= winheight) && ( news_scrollmaxidx || $(".newsitem:last-child").hasClass("loading") ) ) {
		news_scrolltimer = setTimeout(news_scrollfunc, news_scrolldelay);
		return;
	}

	//-- Actually move the entire unordered list (But only if the bottommost item is loaded)
	newsitemob.css("margin-top", newtop + "px" );

	//-- Call this again shortly
	news_scrolltimer = setTimeout(news_scrollfunc, news_scrolldelay);
}
