/** Move the pagetext scrollcontainer forward or back by 1 page */
var lastSubpage=null;
function movePageTextScrollContainer(dir, absolute) {
	setPageTextScrollContainerInnerHeight();
	var el=$('pageTextScrollContainer');
	if (!el) return;
	var height=el.getHeight();
	var scrollHeight=el.getScrollHeight();
	var scrollTop=el.getScrollTop();
	var nrOfSubpages=Math.floor((scrollHeight+1)/height);
	var currentSubpage=Math.round(scrollTop/height);
	var newSubpage=Math.max(0, absolute ? dir : Math.min(nrOfSubpages-1, currentSubpage+dir));
	scrollTop=height*newSubpage;
	el.scrollTop=scrollTop;
	updatePageTextScrollButtons();
	lastSubpage=newSubpage;
	return;
}

/** Disable the prev/next buttons that control the pagetext scrollcontainer
	 if we are at the beginning or end of the text, and enable them otherwise */
function updatePageTextScrollButtons() {
	setPageTextScrollContainerInnerHeight();
	var el=$('pageTextScrollContainer');
	if (!el) return;
	var height=el.getHeight();
	var scrollHeight=el.getScrollHeight();
	var scrollTop=el.getScrollTop();
	var nrOfSubpages=Math.floor((scrollHeight+1)/height);
	var currentSubpage=Math.round(scrollTop/height);
	var prev=$('pageTextScrollPrevButton');
	if (prev) {
		//setButtonSelected(prev, height*Math.max(0,currentSubpage-1)<scrollTop);
		
		if(height*Math.max(0,currentSubpage-1)<scrollTop){
			prev.style.display = "block";
		} else {
			prev.style.display = "none";
		}
	}
	var next=$('pageTextScrollNextButton');
	if (next) {
		//alert(height*Math.min(nrOfSubpages-1,currentSubpage+1)>scrollTop);
		if(height*Math.min(nrOfSubpages-1,currentSubpage+1)>scrollTop){
			next.style.display = "block";
		} else {
			next.style.display = "none";
		}
		//setButtonSelected(next, height*Math.min(nrOfSubpages-1,currentSubpage+1)>scrollTop);
	}
}

/** Ensure that the pagetext scrollcontainer contents occupies a whole number of pages */
function setPageTextScrollContainerInnerHeight() {
	var el=$('pageTextScrollContainer');
	if (!el) return;
	var el2=$(el.firstChild);
	var height=el.getHeight();
	var contentsHeight=el2.getHeight();
	var scrollHeight=el.getScrollHeight();
	var targetScrollHeight=height*Math.ceil(scrollHeight/height);
	while (scrollHeight<targetScrollHeight-1) {
		el2.setStyle('height', (contentsHeight+targetScrollHeight-scrollHeight)+'px');
		contentsHeight=el2.getHeight();
		scrollHeight=el.getScrollHeight();
	}
	var scrollTop=el.getScrollTop();
//		el.scrollTop=height*Math.ceil(scrollTop/height);

}

function resetGuestbookBox() {
   $('guestbooksectionform').getElements('input, textarea', true).each(
		function(el) {
         if (!el.name || el.disabled || el.type=='submit' || el.type=='reset') return;
			el.value='';
		}
	);
	return false;
}

var lastId;
function toggleTip(id, state){
	if (state=="show"){
		selectButtonsByValue('tip', id);
	} else {
		selectButtonsByValue('tip', '');
	}
	return false;
}



var lastSubpage=null;
function moveTextScrollContainer(id, dir, absolute) {
	setTextScrollContainerInnerHeight(id);
	var el=$('textScrollContainer'+id);
	if (!el) return;
	var height=el.getHeight();
	var scrollHeight=el.getScrollHeight();
	var scrollTop=el.getScrollTop();
	var nrOfSubpages=Math.floor((scrollHeight+1)/height);
	var currentSubpage=Math.round(scrollTop/height);
	var newSubpage=Math.max(0, absolute ? dir : Math.min(nrOfSubpages-1, currentSubpage+dir));
	scrollTop=height*newSubpage;
	el.scrollTop=scrollTop;
	updateTextScrollButtons(id);
	lastSubpage=newSubpage;
	return;
}

/** Disable the prev/next buttons that control the pagetext scrollcontainer
	 if we are at the beginning or end of the text, and enable them otherwise */
function updateTextScrollButtons(id) {
	setTextScrollContainerInnerHeight(id);
	var el=$('textScrollContainer'+id);
	if (!el) return;
	var height=el.getHeight();
	var scrollHeight=el.getScrollHeight();
	var scrollTop=el.getScrollTop();
	var nrOfSubpages=Math.floor((scrollHeight+1)/height);
	var currentSubpage=Math.round(scrollTop/height);
	var prev=$('textScrollPrevButton'+id);
	if (prev) {
		//setButtonSelected(prev, height*Math.max(0,currentSubpage-1)<scrollTop);
		
		if(height*Math.max(0,currentSubpage-1)<scrollTop){
			prev.style.display = "block";
		} else {
			prev.style.display = "none";
		}
	}
	var next=$('textScrollNextButton'+id);
	if (next) {
		//alert(height*Math.min(nrOfSubpages-1,currentSubpage+1)>scrollTop);
		if(height*Math.min(nrOfSubpages-1,currentSubpage+1)>scrollTop){
			next.style.display = "block";
		} else {
			next.style.display = "none";
		}
		//setButtonSelected(next, height*Math.min(nrOfSubpages-1,currentSubpage+1)>scrollTop);
	}
}

/** Ensure that the pagetext scrollcontainer contents occupies a whole number of pages */
function setTextScrollContainerInnerHeight(id) {
	var el=$('textScrollContainer'+id);
	if (!el) return;
	var el2=$(el.firstChild);
	var height=el.getHeight();
	var contentsHeight=el2.getHeight();
	var scrollHeight=el.getScrollHeight();
	var targetScrollHeight=height*Math.ceil(scrollHeight/height);
	while (scrollHeight<targetScrollHeight-1) {
		el2.setStyle('height', (contentsHeight+targetScrollHeight-scrollHeight)+'px');
		contentsHeight=el2.getHeight();
		scrollHeight=el.getScrollHeight();
	}
	var scrollTop=el.getScrollTop();
//		el.scrollTop=height*Math.ceil(scrollTop/height);

}


function prevNextItems(direction, typeName, limit){
	
	var first;
	if(direction=="next"){
		first = Number($(typeName+"sShown").value)+1;
	} else if (direction=="prev"){
		first = Number($(typeName+"sShown").value)-1;
	}
	showPrevNextLinks(first, typeName, limit);
	
	if(first>=1 && first<=limit){
		var itemsShown = new Array(first, first+1, first+2, first+3, first+4, first+5, first+6);
		selectButtonsByValue(typeName+"sShown", itemsShown);
		$(typeName+"sShown").value = first;
	}
}

function showPrevNextLinks(requestedItem, typeName, limit){
	if((requestedItem==1) && ((requestedItem+6)<limit)){
		$(typeName+"prevlink").style.display = "none";
		$(typeName+"nextlink").style.display = "block";
	} else if ((requestedItem+6)>=limit && (limit>7)) {
		$(typeName+"prevlink").style.display = "block";
		$(typeName+"nextlink").style.display = "none";
	} else if (requestedItem>1 && requestedItem<limit){
		$(typeName+"prevlink").style.display = "block";
		$(typeName+"nextlink").style.display = "block";
	} else {
		$(typeName+"prevlink").style.display = "none";
		$(typeName+"nextlink").style.display = "none";
	}
}

function goToCurrentItems(currentItem, typeName, limit){
	var itemsShown = new Array(currentItem, currentItem+1, currentItem+2, currentItem+3, currentItem+4, currentItem+5, currentItem+6);
	selectButtonsByValue(typeName+"sShown", itemsShown);
	showPrevNextLinks(currentItem, typeName, limit);
}
