var animationEnabled = true;

schedule("window", pagingInit);
window.onresize = pagingInit;




function getBrowserHeight()
{
	if (window.innerWidth)
	{
		return window.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight != 0)
	{
		return document.documentElement.clientHeight;
	}
	else if (document.body)
	{
		return document.body.clientHeight;
	}
	
	return 0;
}




function getBrowserWidth()
{
	if (window.innerWidth)
	{
		return window.innerWidth;
	}
	else if (document.documentElement && document.documentElement.clientWidth != 0)
	{
		return document.documentElement.clientWidth;
	}
	else if (document.body)
	{
		return document.body.clientWidth;
	}
	
	return 0;
}




function pagingInit()
{
	new pageBrowser("contentScroll");

	return true;
}




function pageBrowser(parentID)
{
	if (document.getElementById("pageBrowser") != null)
	{
		document.getElementsByTagName("body")[0].removeChild(document.getElementById("pageBrowser"));
		document.getElementsByTagName("body")[0].removeChild(document.getElementById("fadeIn"));
	}
	
	this.currOffsetY = 0;
	this.currPage = 1;
	this.currVelocity = 0;
	this.goalY = 0;
	this.theBrowser = null;
	this.theContent = document.getElementById(parentID);
	this.theContentHeight = 450;
	this.theContentViewHeight = 450;
	this.theContentViewWidth = this.theContent.scrollWidth;
	this.theFadeIn = null;
	this.totalPages = 1;
	
	this.theContent.style.top = "";
		
	this.setContentWidth();
	this.setContentHeight();
	
	var theBody = document.getElementsByTagName("body")[0];
	
	if (this.theContentHeight > this.theContentViewHeight - 150)
	{
		this.theBrowser = document.createElement("div");		
		this.theBrowser.associatedObject = this;		
		this.theBrowser.id = "pageBrowser";
		
		this.theFadeIn = document.createElement("div");
		
		this.theFadeIn.id = "fadeIn";
		this.theFadeIn.className = "top";
		this.theFadeIn.style.width = this.theContentViewWidth + "px";
		this.theFadeIn.style.marginTop = -parseInt(this.theContentViewHeight / 2) - 1 + "px";
		theBody.appendChild(this.theFadeIn);
		
		this.pageNext = document.createElement("a");
		this.pageNext.id = "pageBrowserNext";
		this.pagePrevious = document.createElement("a");
		this.pagePrevious.id = "pageBrowserPrevious";
		this.pageNumber = document.createElement("div");
		this.pageNumber.id = "pageBrowserNumber";
			
		this.pageNext.href = "#";
		this.pageNext.title = "View the next page"
		this.pageNext.onclick = pageClicked;
			
		this.pagePrevious.href = "#";
		this.pagePrevious.title = "View the previous page"
		this.pagePrevious.onclick = pageClicked;
		
		this.totalPages = parseInt(this.theContentHeight / (this.theContentViewHeight - 150) + 1)
		this.pageNumber.appendChild(document.createTextNode("Page 1 / " + this.totalPages));
		
		this.theBrowser.appendChild(this.pageNext);
		this.theBrowser.appendChild(this.pageNumber);
		this.theBrowser.appendChild(this.pagePrevious);
		
		this.theBrowser.style.width = this.theContentViewWidth + "px";
		theBody.appendChild(this.theBrowser);
		this.theBrowser.style.marginTop = parseInt(this.theContentViewHeight / 2) - this.theBrowser.scrollHeight + 1 + "px";
	}
	
	this.gotoPage(1);
	
	return true;
}




pageBrowser.prototype.gotoPage = function(pageMove)
{
	var theContentHeight = this.theContent.scrollHeight;
	var theContentViewWidth = this.theContent.clientWidth;

	if (typeof(pageMove) == "number")
	{
		this.currPage = pageMove;
	}
	
	if (pageMove == "next" && this.currPage < this.totalPages)
	{
		this.currPage++;
	}
	else if (pageMove == "previous" && this.currPage > 1)
	{
		this.currPage--;
	}
	
	this.goalY = -(this.theContentViewHeight - 150) * (this.currPage - 1);
	
	if (this.theContent.style.top == "")
	{
		this.theContent.style.top = "0";
	}
	
	if (this.currPage > 1)
	{
		this.pagePrevious.className = this.pagePrevious.className.removeClass("off");
		this.theFadeIn.className = this.theFadeIn.className.removeClass("top");

		if (this.currPage >= this.totalPages)
		{
			this.pageNext.className = this.pageNext.className.addClass("off");
		}
		else
		{
			this.pageNext.className = this.pageNext.className.removeClass("off");
		}
	}
	else
	{
		this.pagePrevious.className = this.pagePrevious.className.addClass("off");
		this.theFadeIn.className = this.theFadeIn.className.removeClass("top");
	}
	
	this.pageNumber.childNodes[0].nodeValue = "Page " + this.currPage + " / " + this.totalPages;
	
	this.scrollPage();
	
	return true;
}




pageBrowser.prototype.scrollPage = function()
{
	var incrementX = 0;
	var self = this;
	
	if (animationEnabled)
	{
		incrementX = (this.goalY - this.currOffsetY) / 10;
	}
	else
	{
		incrementX = this.goalY - this.currOffsetY;
	}
	
	/* Decelerate on reversed velocity */
	if (incrementX * this.currVelocity < -1)
	{
		incrementX = this.currVelocity / 2;
	}
	
	this.currVelocity = incrementX;
	
	if (Math.abs(incrementX) > 0.005)
	{
		this.currOffsetY = this.currOffsetY + incrementX;
		this.theContent.style.top = parseInt(this.currOffsetY) + "px ";
		
		setTimeout(function()
			{
				self.scrollPage();

				return true;
			}
		, 25);
	}
	
	if (this.goalY == 0 && Math.abs(this.currOffsetY) < 30)
	{
		this.theFadeIn.className = this.theFadeIn.className.addClass("top");
	}
	
	return true;
}




pageBrowser.prototype.setContentHeight = function()
{
	this.theContent.style.height = "auto";
	this.theContentHeight = this.theContent.scrollHeight;

	var theBrowserHeight = getBrowserHeight();
	var theHeight = 450;
	
	if (theBrowserHeight > 600)
	{
		theHeight = theBrowserHeight - 150;
	}
		
	if (theHeight > 650)
	{
		theHeight = 650;
	}

	this.theContent.style.height = theHeight + "px";
	this.theContent.parentNode.style.height = theHeight + "px";
	this.theContent.parentNode.parentNode.style.height = theHeight + "px";
	this.theContent.parentNode.parentNode.parentNode.style.height = theHeight + "px";
	this.theContent.parentNode.parentNode.parentNode.parentNode.style.height = theHeight + "px";
	this.theContent.parentNode.parentNode.parentNode.parentNode.style.marginTop = -parseInt(theHeight / 2) + "px";
	this.theContentViewHeight = theHeight;
	
	return true;	
}




pageBrowser.prototype.setContentWidth = function()
{
	var theContentViewWidth = this.theContent.scrollWidth;
	var theContentInner = this.theContent.parentNode.parentNode.parentNode;
	
	if (theContentViewWidth > 356)
	{
		theContentInner.style.width = "450px";
	}
	else
	{
		theContentInner.style.width = "auto";
	}
	
	return false;
}




function pageClicked()
{
	var browserObject = this.parentNode.associatedObject;
	
	if (this.id == "pageBrowserNext")
	{	
		browserObject.gotoPage("next");
	}
	else
	{
		browserObject.gotoPage("previous");
	}
	
	this.blur();
	
	return false;
}




/* Add a class to a string */
String.prototype.addClass = function(theClass)
{
	if (this != "")
	{
		if (!this.classExists(theClass))
		{
			return this + " " + theClass;
		}
	}
	else
	{
		return theClass;
	}
	
	return this;
}




/* Check if a class exists in a string */
String.prototype.classExists = function(theClass)
{
	var regString = "(^| )" + theClass + "\W*";
	var regExpression = new RegExp(regString);
	
	if (regExpression.test(this))
	{
		return true;
	}
	
	return false;
}




/* Remove a class from a string */
String.prototype.removeClass = function(theClass)
{
	var regString = "(^| )" + theClass + "\W*";
	var regExpression = new RegExp(regString);
	
	return this.replace(regExpression, "");
}
