/* Number of news that are shown */
var limit=12;
/* Offset for the transition */
var pxOffset = 588;
/* News that is currently at the topIdx */
var topIdx=0;
/* News that is currently at the bottom */
var newsArr = new Array();
/* News Array length */
var newsArrLength = 0;
/* Sliding observer */
var sliding = false;

Event.observe(window, 'load', function() {
	//Check if a news list plugin is set
	if(checkAvailability()){
		init();
		//Check if scroller is needed
		handleBtns();
		
		//Register scrollUp Event on the button
		Event.observe('scrollUp', 'click', function(e) {
			if(!sliding){
				//Play scrollUp effect 
				scrollUpFx();
				//Check if another scrollUp is possible
				handleBtns();
			}
		});
	
		//Register scrollDown Event on the button
		Event.observe('scrollDown', 'click', function(e) {
			if (!sliding) {
				//Play scrollDown effect 
				scrollDownFx();
				//Check if another scrollUp is possible
				handleBtns();
			}
		});
	}
});

/**
 * Init globals vars
 */
function init(){
	newsArr = $('border-content').select('p.news-list-item');
	newsArrLength = newsArr.length;
	topIdx = 0;
}

/**
 * Handle Button's visibility
 */
function handleBtns(){	
	//List has reached the top
	if(topIdx==0){
		$('scrollUp').setStyle({
			visibility: 'hidden' 
		});	
	}else{
		$('scrollUp').setStyle({
			visibility: 'visible' 
		});
	}
	//List has reached the bottom
	if((topIdx+limit) >= (newsArrLength)){
		$('scrollDown').setStyle({
			visibility: 'hidden' 
		});
	}else{
		$('scrollDown').setStyle({
			visibility: 'visible' 
		});
	}
}

/**
 * Play scrollUp effect
 */
function scrollUpFx(){
	new Effect.Move($('internal-news-scroll'), {
		x:0,
		y: pxOffset,
		duration:0.8,
		transition: Effect.Transitions.sinoidal,
		beforeStart: setSliding,
		afterFinish: clearSliding
	});	
	topIdx-=limit;
}

/**
 * Play scrollDown effect
 */
function scrollDownFx(){
	new Effect.Move($('internal-news-scroll'),{
		x:0,
		y: -pxOffset,
		duration:0.8,
		transition: Effect.Transitions.sinoidal,
		beforeStart: setSliding,
		afterFinish: clearSliding
	});	
	topIdx+=limit;
}

/**
 * Check if a news list plugin is set
 */
function checkAvailability(){
	if($('scrollUp') && $('scrollDown')){
		return true;
	}else{
		return false;
	}
}

/**
 * Sets the sliding control-bool to false (Method is normally called after the slide event is finished)
 */
function clearSliding(){
	sliding = false;
}

/**
 * Sets the sliding control-bool to true (Method is normally called before the slide event begins)
 */
function setSliding(){
	sliding = true;
}

