
var tablePaginater = (function () {
	var tableInfo = {}, uniqueID = 0, text = ["First Page", "Previous Page (Page %p)", "Next Page (Page %p)", "Last Page (Page %t)", "Page %p of %t"];
	var addClass = function (e, c) {
		if (new RegExp("(^|\\s)" + c + "(\\s|$)").test(e.className)) {
			return;
		}
		e.className += (e.className ? " " : "") + c;
	};
	var removeClass = function (e, c) {
		e.className = !c ? "" : (e.className || "").replace(new RegExp("(^|\\s)" + c + "(\\s|$)"), " ").replace(/^\s\s*/, "").replace(/\s\s*$/, "");
	};
	var addEvent = function (obj, type, fn) {
		if (obj.attachEvent) {
			obj["e" + type + fn] = fn;
			obj[type + fn] = function () {
				obj["e" + type + fn](window.event);
			};
			obj.attachEvent("on" + type, obj[type + fn]);
		} else {
			obj.addEventListener(type, fn, true);
		}
	};
	var removeEvent = function (obj, type, fn) {
		try {
			if (obj.detachEvent) {
				obj.detachEvent("on" + type, obj[type + fn]);
				obj[type + fn] = null;
			} else {
				obj.removeEventListener(type, fn, true);
			}
		}
		catch (err) {
		}
	};
	var stopEvent = function (e) {
		e = e || window.event;
		if (e.stopPropagation) {
			e.stopPropagation();
			e.preventDefault();
		}
		/*@cc_on@*/
		/*@if(@_win32)
                e.cancelBubble = true;
                e.returnValue  = false;
                /*@end@*/
		return false;
	};
	var init = function (dataGridId, rowsPerPage, currentPage, totalRows, numPages) {
		var maxPages = 7;
		if (numPages <= maxPages) {
			maxPages = numPages;
		}
		tableInfo[dataGridId] = {rowsPerPage:rowsPerPage, currentPage:currentPage, totalRows:totalRows, maxPages:maxPages, numPages:numPages};
		showPage(dataGridId);
	};
	var createButton = function (details, ul, pseudo) {
		var li = document.createElement("li"), but = document.createElement(pseudo ? "div" : "a"), span = document.createElement("span");
		if (!pseudo) {
			but.href = "javascript:window.finder['83423d'].jumpTo.bind(window.finder['83423d'])(2)";
			but.title = details.title;
		}
		but.className = details.className;
		ul.appendChild(li);
		li.appendChild(but);
		but.appendChild(span);
		span.appendChild(document.createTextNode(details.text));
		if (!pseudo) {
			li.onclick = but.onclick = buttonClick;
			if (details.id) {
				but.id = details.id;
			}
		}
		li = but = span = null;
	};
	var removePagination = function (tableId) {
		var wrapT = document.getElementById(tableId + "-fdtablePaginaterWrapTop"), wrapB = document.getElementById(tableId + "-fdtablePaginaterWrapBottom");
		if (wrapT) {
			wrapT.parentNode.removeChild(wrapT);
		}
		if (wrapB) {
			wrapB.parentNode.removeChild(wrapB);
		}
	};
	var buildPagination = function (tblId) {
		if (!(tblId in tableInfo)) {
			return;
		}
		removePagination(tblId);
		var details = tableInfo[tblId];
		if (details.numPages < 2) {
			return;
		}
		function resolveText(txt, curr) {
			curr = curr || details.currentPage;
			return txt.replace("%p", curr).replace("%t", details.numPages);
		}
		if (details.maxPages) {
			findex = Math.max(0, Math.floor(Number(details.currentPage - 1) - (Number(details.maxPages - 1) / 2)));
			lindex = findex + Number(details.maxPages);
			if (lindex > details.numPages) {
				lindex = details.numPages;
				findex = Math.max(0, details.numPages - Number(details.maxPages));
			}
		} else {
			findex = 0;
			lindex = details.numPages;
		}
		var wrapT = document.createElement("div");
		wrapT.className = "fdtablePaginaterWrap";
		wrapT.id = tblId + "-fdtablePaginaterWrapTop";
		var wrapB = document.createElement("div");
		wrapB.className = "fdtablePaginaterWrap";
		wrapB.id = tblId + "-fdtablePaginaterWrapBottom";

                // Create list scaffold
		var ulT = document.createElement("ul");
		ulT.id = tblId + "-tablePaginater";
		var ulB = document.createElement("ul");
		ulB.id = tblId + "-tablePaginaterClone";
		ulT.className = ulB.className = "fdtablePaginater";

                // Add to the wrapper DIVs
		wrapT.appendChild(ulT);
		wrapB.appendChild(ulB);

				
                // FIRST (only created if maxPages set)
		if (details.maxPages) {
                        //createButton({title:text[0], className:"first-page", text:"u00ab"}, ulT, !findex);
			createButton({title:text[0], className:"first-page", text:"\xab"}, ulB, !findex);
		}
                
                // PREVIOUS (only created if there are more than two pages)
		if (details.numPages > 2) {
                        //createButton({title:resolveText(text[1], details.currentPage-1), className:"previous-page", text:"u2039", id:tblId+"-previousPage"}, ulT, details.currentPage == 1);
			createButton({title:resolveText(text[1], details.currentPage - 1), className:"previous-page", text:"\u2039", id:tblId + "-previousPageC"}, ulB, details.currentPage == 1);
		}
                
                // NUMBERED
		for (var i = findex; i < lindex; i++) {
                        //createButton({title:resolveText(text[4], i+1), className:i != (details.currentPage-1) ? "page-"+(i+1) : "currentPage page-"+(i+1), text:(i+1), id:i == (details.currentPage-1) ? tblId + "-currentPage" : ""}, ulT);
			createButton({title:resolveText(text[4], i + 1), className:i != (details.currentPage - 1) ? "page-" + (i + 1) : "currentPage page-" + (i + 1), text:(i + 1), id:i == (details.currentPage - 1) ? tblId + "-currentPageC" : ""}, ulB);
		}

                // NEXT (only created if there are more than two pages)
		if (details.numPages > 2) {
                        //createButton({title:resolveText(text[2], details.currentPage + 1), className:"next-page", text:"u203a", id:tblId+"-nextPage"}, ulT, details.currentPage == details.numPages);
			createButton({title:resolveText(text[2], details.currentPage + 1), className:"next-page", text:"\u203a", id:tblId + "-nextPageC"}, ulB, details.currentPage == details.numPages);
		}
                
                // LAST (only created if maxPages set)
		if (details.maxPages) {
                        //createButton({title:resolveText(text[3], details.numPages), className:"last-page", text:"u00bb"}, ulT, lindex == details.numPages);
			createButton({title:resolveText(text[3], details.numPages), className:"last-page", text:"\xbb"}, ulB, lindex == details.numPages);
		}
                
                // DOM inject wrapper DIVs (FireFox 2.x Bug: this has to be done here if you use display:table)
		if (document.getElementById(tblId + "-paginationListWrapTop")) {
			document.getElementById(tblId + "-paginationListWrapTop").appendChild(wrapT);
		} else {
			document.getElementById(tblId).parentNode.insertBefore(wrapT, document.getElementById(tblId));
		}
		if (document.getElementById(tblId + "-paginationListWrapBottom")) {
			document.getElementById(tblId + "-paginationListWrapBottom").appendChild(wrapB);
		} else {
			document.getElementById(tblId).parentNode.insertBefore(wrapB, document.getElementById(tblId).nextSibling);
		}
	};
	var showPage = function (tblId, pageNum) {
		if (!(tblId in tableInfo)) {
			return;
		}
		var page = Math.max(0, !pageNum ? tableInfo[tblId].currentPage - 1 : pageNum - 1), d1 = tableInfo[tblId].rowsPerPage * page, d2 = Math.min(tableInfo[tblId].totalRows, d1 + tableInfo[tblId].rowsPerPage), cnt = 0, rc = 0, reg = /(^|\s)invisibleRow(\s|$)/, row = [];
		buildPagination(tblId);
	};
	var buttonClick = function (e) {
		e = e || window.event;
	};
	var onUnLoad = function (e) {
		var tbl, lis, pagination, uls;
		for (tblId in tableInfo) {
			uls = [tblId + "-tablePaginater", tblId + "-tablePaginaterClone"];
			for (var z = 0; z < 2; z++) {
				pagination = document.getElementById(uls[z]);
				if (!pagination) {
					continue;
				}
				lis = pagination.getElementsByTagName("li");
				for (var i = 0, li; li = lis[i]; i++) {
					li.onclick = null;
					if (li.getElementsByTagName("a").length) {
						li.getElementsByTagName("a")[0].onclick = null;
					}
				}
			}
		}
	};
	return {init:function (dataGridId, rowsPerPage, currentPage, totalRows, numPages) {
		init(dataGridId, rowsPerPage, currentPage, totalRows, numPages);
	}};
})();

