var comments_per_page = 10;
var use_google_style = true;
var show_google_nav = false;

function loadPage(page_num,nav_div)
{
	/* function is only used when navigating "google" style */
	
	/* "paginateNUM" div is child of "paginate" div */
	current_page = nav_div.parentNode;
	
	/* obtain comment array */
	var comments = getPageComments(current_page);
	var start_position = page_num;
	var end_position = start_position + comments_per_page;

	/* make sure boundary conditions are valid */
	if (start_position < 0){
		start_position = 0;
		end_position = start_position + comments_per_page;
	}
	if (start_position > comments.length){
		start_position = comments.length - comments_per_page;
		end_position = start_position + comments_per_page;
	}
	if (end_position > comments.length){
		end_position = comments.length;
	}
	
	/* hide all comments, then show the desired ones */
	hideAllComments(comments);
	for(var i = start_position; i < end_position; i++){
		comments[i].style.display = 'block';
	}
	
	/* update the nav and position div */
	setPageCounter(start_position,end_position,current_page);
}

function advancePage(current_page)
{
	/* which comment is visible, that's our current position */
	var current_position = getPagePosition(current_page);
	var comments = getPageComments(current_page);
	
	/* increment the page position */
	var start_position = current_position + comments_per_page;
	var end_position = start_position + comments_per_page;
	
	/* make sure boundary conditions are valid */
	if (start_position > comments.length){
		return;
	}
	if (end_position > comments.length){
		end_position = comments.length;
	}
	
	/* hide all comments, then show the desired ones */
	hideAllComments(comments);
	for(var i = start_position; i < end_position; i++){
		comments[i].style.display = 'block';
	}
	
	setPageCounter(start_position,end_position,current_page);
}

function rewindPage(current_page)
{
	var current_position = getPagePosition(current_page);
	var comments = getPageComments(current_page);
	
	/* decrement the position */
	var start_position = current_position - comments_per_page;
	var end_position = current_position;
	
	/* make sure boundary conditions are valid */
	if (start_position < 0){
		start_position = 0;
		end_position = start_position + comments_per_page;
	}
	if (end_position < 0){
		end_position = comments_per_page;
	}
	if (end_position > comments.lenth){
		end_position = comments.length;
	}
	
	/* hide all comments, then show the desired ones */
	hideAllComments(comments);
	for(var i = start_position; i < end_position; i++){
		comments[i].style.display = 'block';
	}
	
	setPageCounter(start_position,end_position,current_page);
}

function getPagePosition(active_page)
{
	/* function finds the first visible comment */
	
	var comments = getPageComments(active_page);
	
	for( var i = 0; i < comments.length; i++ )
	{
		if (comments[i].style.display == 'block'){
			return i;
		}
	}

	return 0;
}

function getPageComments(active_page)
{
	/* function finds all valid comment container divs and return them as an array */
	for (var i = 0; i < active_page.childNodes.length; i++)
	{
		var comments = new Array();
		
		if (active_page.childNodes[i].className == 'paginateITEMS')
		{
			for (var j = 0; j < active_page.childNodes[i].childNodes.length; j++)
			{
				if (active_page.childNodes[i].childNodes[j].className == ''){
					comments.push(active_page.childNodes[i].childNodes[j]);
				}
			}
			return comments;
		}
	}
	
	/* didn't find any, returns false (NOT USED) */
	return false;
}

function setPageCounter(start,end,active_page)
{
	/* sets the navigation and status div */
	
	var comments = getPageComments(active_page);
	for (var i = 0; i < active_page.childNodes.length; i++)
	{
		if (active_page.childNodes[i].className == 'paginateNUM'){
			break;
		}
	}
	
	/* find the current position */
	var current_position = getPagePosition(active_page);
	
	/* use google-style navigation? i.e. [1][2][3][4].....etc */
	if (use_google_style)
	{
		var s = '';
		var k = 0;

		/* j is page number, k is comment number, k is all we really care about */
		for(var j = 0; j < comments.length; j = j + comments_per_page)
		{
			k++;
			if ((current_position >= j) && (current_position < (j + comments_per_page))){
				s = s + ' ' + k + ' ';	// SETS NONLINK TEXT - '[' + k + ']'
			}
			else{
				s = s + ' <a onclick="loadPage(' + j + ',this.parentNode)">' + k + '</a> ';	// SETS LINK TEXT
			}
		}
		active_page.childNodes[i].innerHTML = s;
	}
	else{
		 active_page.childNodes[i].innerHTML = (start +1) + '-' + end + '/' + comments.length;
	}

	/* and fix nav direction divs, if necessary */
	for (var i = 0; i < active_page.childNodes.length; i++)
	{
		if (show_google_nav)
		{
			if (active_page.childNodes[i].className == 'paginatePREV'){
				if (current_position < comments_per_page){
					active_page.childNodes[i].style.display = 'none';
				}
				else{
					active_page.childNodes[i].style.display = 'block';
				}
			}
			
			if (active_page.childNodes[i].className == 'paginateNEXT'){
				if (current_position > (comments.length - comments_per_page)){
					active_page.childNodes[i].style.display = 'none';
				}
				else{
					active_page.childNodes[i].style.display = 'block';
				}
			}
		}
		else
		{
			if (active_page.childNodes[i].className == 'paginatePREV'){
				active_page.childNodes[i].style.display = 'none';
			}
			if (active_page.childNodes[i].className == 'paginateNEXT'){
				active_page.childNodes[i].style.display = 'none';
			}
		}
	}
}

function hideAllComments(comments)
{
	/* hides every comment */
	
	for(var i = 0; i < comments.length; i++){
		comments[i].style.display = 'none';
	}
}

function paginateInitialize()
{
	/* function hides all comments but the first page */

	var document_divs = document.getElementsByTagName('div');
	
	for(var i = 0; i < document_divs.length; i++)
	{
		if (document_divs[i].className == 'paginate')
		{
			/* initialize the page counter/nav as well */
			setPageCounter(0,comments_per_page,document_divs[i]);
			
			var comments = getPageComments(document_divs[i]);
			
			for(var j = 0; j < comments.length; j++)
			{
				if (j < comments_per_page){
					comments[j].style.display = 'block';
				}
				else{
					comments[j].style.display = 'none';
				}
			}
		}
	}
}
