var speed = 8000 // sec timer interval
var line, curr_index, this_index, lineLength, timer, oldNewsItem, newNewsItem;
var isIE7_8= (jQuery.browser.msie && (jQuery.browser.version == '7.0' || jQuery.browser.version == '8.0'))?true:false;


function goForward(){
	curr_index= line.children().index(line.find('li.current'));			
	this_index = curr_index+1;
	if (this_index >(lineLength-1)) {
		this_index = 0;
	}
	setCurrent(curr_index, this_index);
}

function goBackward(){
	curr_index=line.children().index(line.find('li.current'));			
	this_index = curr_index-1;
	if (this_index <0) {
		this_index = lineLength-1;
	}
	setCurrent(curr_index, this_index);
}

function setCurrent(oldCurr, newCurr){
	clearInterval(timer);
	
	oldNewsItem = line.find('li:eq('+oldCurr+')');
	oldNewsItem.removeClass('current');
	if (isIE7_8) {oldNewsItem.css({display:'none'});}
	else {oldNewsItem.stop().fadeTo('normal',0).css({display:'none'});}
	
	newNewsItem = line.find('li:eq('+newCurr+')');
	newNewsItem.addClass('current');
	if (isIE7_8) {newNewsItem.css({display:'block'});}
	else {newNewsItem.stop().css({display:'block', opacity:0}).fadeTo('normal',1);}
	
	timer = setInterval(goForward,speed);
} 

		
jQuery(document).ready(function($) {
		line = $('#news-output .news-output-list ul');
		lineLength = line.find('li').length;
		prevButton = $("#news-output a#news-output-button-prev");
		nextButton = $("#news-output a#news-output-button-next");
		
		if(lineLength>1){				
			line.find("li").css('display','none');			
			rand = Math.round(Math.random()*(lineLength-1));  
			
  			setCurrent(0, rand);			
			
			prevButton.bind('click', function(){
				goBackward();
				return false;
			})
			nextButton.bind('click', function(){
				goForward();
				return false;
			})
		} else {
			prevButton.hide();
			nextButton.hide();
		}
})
