// JavaScript Document

var nScrollSpeed = 10;

var nScrollLength = 0;
var nCurrentOffset = 0;
var nCurrentTop = 0;

var ScrollInterval;

var nRealContentActualPage;
var nRealContentMaxPage;

var nRealContentOffsetHeight;
var nRealContentRealHeight;

////////////////////////////////////////////////////
// helpers

function ScrollInit()
{
	nRealContentActualPage = 1;
	nRealContentMaxPage = 0;
	nActualScrollDefect = 0;
	
	nRealContentOffsetHeight = document.getElementById('page_real_content').offsetHeight;
	nRealContentRealHeight = document.getElementById('page_real_content').scrollHeight;
	
	CalculateNumberOfPages();
	SetActualPage();
	SetMaxPage();
	SetPageInput();
	CheckScrollArrowsState();
}

function CalculateNumberOfPages()
{
	var n = 0;
	
	n = Math.floor(nRealContentRealHeight / nRealContentOffsetHeight);
	
	if((n * nRealContentOffsetHeight) < nRealContentRealHeight)
		nRealContentMaxPage = n + 1;
	else if((n * nRealContentOffsetHeight) == nRealContentRealHeight)
		nRealContentMaxPage = n;
}

function SetActualPage()
{	
	document.getElementById('RealContentActualPage').innerHTML = nRealContentActualPage;
}
function SetMaxPage()
{	
	document.getElementById('RealContentMaxPage').innerHTML = nRealContentMaxPage;
}
function SetPageInput()
{
	document.getElementById('RealContentPageInput').value = nRealContentActualPage;
}
//////////////////////////////////////////////////////

function ScrollRealContentUp()
{
	//if we are on the start, nothing need to happen
	if(nRealContentActualPage == 1)
		return;

	nRealContentActualPage--;

	ScrollRealContent('up');

	SetActualPage();
	SetPageInput();
	
	CheckScrollArrowsState();
}

function ScrollRealContentDown()
{
	//if we are on the end, nothing need to happen
	if(nRealContentActualPage == nRealContentMaxPage)
		return;
	
	nRealContentActualPage++;
	
	ScrollRealContent('down');
	
	SetActualPage();
	SetPageInput();
	
	CheckScrollArrowsState();
}

function ScrollRealContentBottom()
{
	nRealContentActualPage = nRealContentMaxPage;
	
	SetActualPage();
	SetPageInput();
	
	CheckScrollArrowsState();
}

function ScrollRealContent(direction)
{
	ScrollWholePage();
	
	var myDiv = document.getElementById('page_real_content');
	nCurrentTop = myDiv.scrollTop;
	
	var nPendingTop = (nRealContentActualPage - 1) * nRealContentOffsetHeight;
	
	if((nPendingTop + nRealContentOffsetHeight) > nRealContentRealHeight)
		nPendingTop = nRealContentRealHeight - nRealContentOffsetHeight;
		
	if(direction == 'none')
	{
		if(nPendingTop > nCurrentTop)
			direction = 'down';
		else if(nPendingTop < nCurrentTop)
			direction = 'up';
		else if(nPendingTop == nCurrentTop)
			return;
	}	
		
	//calculate next scroll lenght
	nScrollLength = Math.abs(nPendingTop - nCurrentTop);
	
	nCurrentOffset = 0;
	
	// scroll page content
	clearInterval(ScrollInterval); 
	ScrollInterval = setInterval("ScrollFunc('" + direction + "')", 10); 
}

function ScrollFunc(direction)
{
	//calculate step
	var nStep = Math.ceil((nScrollLength - nCurrentOffset) / nScrollSpeed);
	
	nCurrentOffset += nStep;
	
	if(direction == 'down')
		document.getElementById('page_real_content').scrollTop = nCurrentTop + nCurrentOffset;
	else if(direction == 'up')
		document.getElementById('page_real_content').scrollTop = nCurrentTop - nCurrentOffset;
		
	if(nCurrentOffset >= nScrollLength)
		clearInterval(ScrollInterval); 
}

function CheckScrollArrowsState()
{
	var UpArrowActive = '<img src="images/pagging_up.gif" alt="up" style="cursor:pointer" onclick="javascript:ScrollRealContentUp();" />';
	var UpArrowInActive = '<img src="images/pagging_up.gif" alt="up" />';
	
	var DownArrowActive = '<img src="images/pagging_down.gif" alt="down" style="cursor:pointer" onclick="javascript:ScrollRealContentDown();" />';
	var DownArrowInActive = '<img src="images/pagging_down.gif" alt="down" />';
	
	if(nRealContentActualPage == 1)
		document.getElementById('RealContentUpArrow').innerHTML = UpArrowInActive;
	else
		document.getElementById('RealContentUpArrow').innerHTML = UpArrowActive;
		
	if(nRealContentActualPage == nRealContentMaxPage)
		document.getElementById('RealContentDownArrow').innerHTML = DownArrowInActive;
	else
		document.getElementById('RealContentDownArrow').innerHTML = DownArrowActive;
}

function CheckPageInput()
{
	if(document.getElementById('RealContentPageInput').value == '')
		return;
		
	var nValue = parseInt(document.getElementById('RealContentPageInput').value, 10);
	
	if(nValue <= 0 || isNaN(nValue))
	{
		nRealContentActualPage = 1;
		SetPageInput();
	}
	else if(nValue > nRealContentMaxPage)
	{
		nRealContentActualPage = nRealContentMaxPage;
		SetPageInput();
	}
	else
		nRealContentActualPage = nValue;
		
	ScrollRealContent('none');
	SetActualPage();
	CheckScrollArrowsState();
}

//////////////////////////////////////////////////////////////////////////////////
// ScrollWholePage
var WPScrollInterval;
var nWPCurrentTop;
var nWPScrollLength;
var nWPCurrentScrollDelta;
var nWPScrollSpeed = 5;

function ScrollWholePage()
{
	var nAnchorY = WPGetAnchorY(document.getElementById('scroll_page_here'));
	nWPCurrentTop = WPScrollTop();
	
	nWPScrollLength = nWPCurrentTop - nAnchorY;
	
	// if page top is visible
	if(nWPScrollLength <= 0)
		return;
	
	nWPCurrentScrollDelta = 0;
	
	clearInterval(WPScrollInterval); 
	
	WPScrollInterval = setInterval("WPScroll()", 10); 
}

function WPScroll()
{
	if(nWPScrollLength <= 0)
		clearInterval(WPScrollInterval);
		
	//calculate step
	var nStep = Math.ceil((nWPScrollLength - nWPCurrentScrollDelta) / nWPScrollSpeed);
	if(nStep <= 0)
		return;
	
	nWPCurrentScrollDelta += nStep;
	
	
	window.scrollTo(0, nWPCurrentTop - nWPCurrentScrollDelta);
	
	if(nWPCurrentScrollDelta >= nWPScrollLength)
		clearInterval(WPScrollInterval); 
}

function WPGetAnchorY(WorkAnchor)
{
	var gy = WorkAnchor.offsetTop
	if (WorkAnchor.offsetParent) 
	{
		while (WorkAnchor = WorkAnchor.offsetParent) 
			gy += WorkAnchor.offsetTop;
	}
	return gy
}

function WPScrollTop()
{
	body = document.body;
	d = document.documentElement;
	
	if(body && body.scrollTop) 
		return body.scrollTop;
	
	if(d && d.scrollTop) 
		return d.scrollTop;
	
	if(window.pageYOffset) 
		return window.pageYOffset;
		
	return 0;
}






