// JavaScript 
// provides simple pagination functions
// requieres : jquery-1.2.3.js
// usage  setPagination(<#page[pageNum]>)  (defaults to #page1)
// pages are divs with id=page[pagenumber] and class=page
// pages should be initially set to display:none 	
// additional styles can be set for pagination using:
//		id = 'pagination'   - The pagination div itself
// 		class = 'paginationLink' - the links to the  pages
// 				'currentPage' - the current page span tag	
// G.Braendle 2008


	function setPagination(currpageId) {
		if (arguments.length == 0) {
			currpageId = "#page1";	
		}
		var currpage=$(currpageId);
		var pagenum=parseInt(currpageId.substr(5));
		var numpages=$(".page").length;
		currpage.css("display","block");
		if ( !currpage.hasClass("currpage") ) {
			currpage.addClass("currpage");
		}
		var pager ="<div id='pagination' style='position:absolute;left:0;width:"+currpage.parent().width()+"px;bottom:0px;'><span style='display:block;font-size:110%;text-align:center;' >";
		var prev = "&lt;&nbsp;"
		var next = "&nbsp;&gt;"
		if (pagenum == 1) {
			pager += "<span>"+ prev +"</span>";
		} else {
			pager += '<a href="#page'+(pagenum - 1)+'" class="paginationLink" >'+prev+'</a>';  	
		}
		for (var page=1;page<=numpages;page++) {
			if (page != pagenum) {
				pager += '<a href="#page'+ page +'" class="paginationLink" >&nbsp;'+ page +'&nbsp;</a>';
			} else {
				pager += '<span class="currentPage">&nbsp;'+page+'&nbsp;</span>';
			}
		}
		if (pagenum == numpages) {
			pager += '<span>'+ next +'</span>';
		} else {
			pager += '<a href="#page'+(pagenum + 1)+'" class="paginationLink" >'+next+'</a>';  	
		}
		pager += '</span></div>';
		currpage.append(pager);
		$("#pagination .paginationLink").css( {textDecoration:"none"});
		$("#pagination .paginationLink").click(paginationClick);
	}	
	

	// Pagination click event handler
	//
	function paginationClick(e) {
			var pageSelector = $(this).attr("href");
			// stupid IE... 
			pageSelector=pageSelector.substr(pageSelector.lastIndexOf("#"));
			var currpage =$(".currpage");
			$("#pagination",currpage).remove();
			currpage.css("display","none");
			if ( currpage.hasClass("currpage") ) {
				currpage.removeClass("currpage")
			}
			setPagination(pageSelector);
	}

/*
	Initialize when (css fall-back) static 'pager' class divs
*/

$(document).ready(function() {
	if ( $(".pager").length > 0) {	
		$(".pager").remove();	// remove the static pager divs
		$(".spacing").remove();
		$("#content_area").css("height", "400px");	// small cosmetic
		$(".page").css("display","none");	 
		setPagination("#page1");		// initialize pagination
	}
});
	
