// nick_toTop_smooth.js

//a script that, when run, makes a page scroll smoothly back to a set vertical location in the page


//Stores the type of DOM used by the current browser. -1 means it hasn't been set yet.
var domType = -1;
//When the function has not been run yet, runInterval is 0. When the function has been run, runInterval is 1.
var runInterval = 0;

//The desired top location (pixels) to scroll to
var topY = 0;

function scroll_timer() {
	//We need to set the current top-most position of the user's screen (curY, in pixels)
	var curY = 0;
	if (domType == 1){
		curY = document.documentElement.scrollTop;
	}
	else{
		curY = document.body.scrollTop;
	}
	//alert(curY);
	//Used to define the desired scroll speed (a lower number makes it scroll faster)
	var moveStep = 15;
	
	//update the value of curY based on moveStep
	if(curY >= topY){
		curY -= Math.ceil(curY * moveStep / 100);
	}
	else{
		curY=topY;
	}
	//now set the document's scrollTop to the current value of curY
	if(domType == 1){
		document.documentElement.scrollTop = curY;
	}
	else{
		document.body.scrollTop = curY;
	}
	//If the window has scrolled to the top, end the interval
	if (curY == topY) {
	clearInterval(runInterval);
	runInterval = 0;
	}
	
}

function scroll_to_top(){
	if(runInterval == 0){
		//The DOM type will affect how document object parameters are called. We need to find out which kind of DOM is being used.
		if(document.documentElement && document.documentElement.scrollTop){
			domType = 1;
		}
		else if(document.body && document.body.scrollTop){
			domType = 2;
		}
		else{
			domType = 0;
		}
		if (domType > 0){
			runInterval = setInterval('scroll_timer()', 25);
		}
	}
}

// This function will hide the "Back to top" link if the page height is less than given number of pixels
function manageTopLink(){
	
	//this var holds the minimum page length that required a back to top link
	var minToTop = 1500;
	//this var will hold the page height
	var pageHeight = -1;
	
	//Set page height, dependent on type of DOM being used by browser
	if(document.documentElement && document.documentElement.scrollHeight){
		pageHeight = document.documentElement.scrollHeight;
		//alert(pageHeight);
	}
	else if(document.body && document.body.scrollHeight){
		pageHeight = document.body.scrollHeight;
		//alert(pageHeight);
	}
	else{
		pageHeight = 0;
	}
	
	//if pageHeight is greater than 0 and less than minToTop, hide "back to top" link
	if(pageHeight > 0 && pageHeight < minToTop){
		document.getElementById("toTop").style.visibility = "hidden";
	}
	
}