/* 
Author: Juha Valvanne, Smilehouse Oy
Description: This JavaScript code prints page numbers and links
to create Google style advanced paging links on produft list and search result pages.
Refer to manual on how to pass the stripped url variable to this script.
*/

function paging(url,thisPage,lastPage) {
	var i = 0;
	var j = 0;

	// print div around the paging
	document.write('<div class="paging">');
	
	// print Previous page link if there are previous pages
	if (thisPage != 1) {
		var previousPage = thisPage - 1;
		// print link to first page if more than one previous pages available
		if (previousPage > 5) {
			document.write('<a href="' + url + '&pagenr=1' + '">&laquo; Alkuun</a>');
		}
		document.write('<a href="' + url + '&pagenr=' + previousPage + '">&laquo; Edellinen</a>');
		// print dots if more than one previous pages available
		if (previousPage > 5) {
			document.write('<span class="dots">...</span>');
		}
	}

	// print max 5 previous page numbers as links
	i = thisPage-5;
	while (i < thisPage) {
		if (i > 0) {
			document.write('<a href="' + url + '&pagenr=' + i + '">' + i + '</a>');
		}
		i++;
	}

	// print nothing if there is only one page
	if (lastPage != 1) {
		document.write('<span class="thisPage">' + thisPage + '</span>');
	}

	// print max 5 next page numbers as links
	i = thisPage+1;
	j = 0;
	while (i <= lastPage && j < 5) {
		document.write('<a href="' + url + '&pagenr=' + i + '">' + i + '</a>');
		i++;
		j++;
	}

	// print Next page link if there are previous pages
	if (thisPage != lastPage) {
		var nextPage = thisPage + 1;
		// print dots if more than more than one next page
		if ((lastPage - nextPage) > 5) {
			document.write('<span class="dots">...</span>');
		}
		document.write('<a href="' + url + '&pagenr=' + nextPage + '">Seuraava &raquo;</a>');
		// print link to last page if more than more than one next page
		if ((lastPage - nextPage) > 5) {
			document.write('<a href="' + url + '&pagenr=' + lastPage + '">Loppuun &raquo;</a>');
		}
	}

	// close the wrapping div
	document.write('</div>');
}
