
var MAX_ELEMS_TO_SHOW = 5;

function seekScroll(){
	var size = $j(".scrollableAccomodations").data("scrollable").getSize();
	var children = document.getElementById('scrollableAccomodationsItems').getElementsByTagName('div');
	var selectedIndex = 0;
	for(var i=0;i<children.length;i++){
		if(children[i].className=="selected"){
			selectedIndex = i;
		}
	}
	var seekIndex = getScroolSeekTo(selectedIndex);
	$j(".scrollableAccomodations").data("scrollable").seekTo(seekIndex);
}

function getScroolSeekTo(selectedIndex){
	var seekFrom = 0;
	var seekTo = (MAX_ELEMS_TO_SHOW/2);
	var size = $j(".scrollableAccomodations").data("scrollable").getSize();
	if(selectedIndex<=seekTo){
		return seekFrom;
	} else {
		while((seekTo<size) && (!isInSet(seekFrom,seekTo,selectedIndex))){
			seekFrom++;
			seekTo++;
		}
		return seekFrom;
	}
}

function isInSet(start,end,index){
	return (index>start) && (index<=end);
}

function nextAccomodations(){
	var size = $j(".scrollableAccomodations").data("scrollable").getSize();
	var index = $j(".scrollableAccomodations").data("scrollable").getIndex();	
	$j(".scrollableAccomodations").data("scrollable").next();			
	updateNaviEnablement();
}

function prevAccomodations(){
	var size = $j(".scrollableAccomodations").data("scrollable").getSize();
	var index = $j(".scrollableAccomodations").data("scrollable").getIndex();
	$j(".scrollableAccomodations").data("scrollable").prev();			
	updateNaviEnablement();
}

function updateNaviEnablement(){
	var size = $j(".scrollableAccomodations").data("scrollable").getSize();
	var index = $j(".scrollableAccomodations").data("scrollable").getIndex();
	
	if( (size-index)<=(MAX_ELEMS_TO_SHOW)){
		document.getElementById("nextAccomodationsScroll").style.visibility="hidden";
	} else {
		document.getElementById("nextAccomodationsScroll").style.visibility="visible";
	}

	if(index>0){
		document.getElementById("prevAccomodationsScroll").style.visibility="visible";
	} else {
		document.getElementById("prevAccomodationsScroll").style.visibility="hidden";
	}
	
}
