User:Elominius/common.js

// pagination script

// variable scopes and initialization // declaring variables to avoid reference errors that would stop the script var pagination_tabs = {}; var pages = {}; // empty objects var pagination_container_number = 0; // intial value

// main function function switchToPage(page_number,pagination_container_number) { // select first container if none is specified if ( isNaN(pagination_container_number) ) pagination_container_number=0; // Putting tabs and pages into variables for simplification pagination_tabs = document.getElementsByClassName("pagination_tabs")[pagination_container_number].getElementsByTagName("li"); pages = document.getElementsByClassName("pagination_container")[pagination_container_number].getElementsByTagName("div");

pagination_tabs[page_number].style.fontWeight="bold"; // highlight selected tab pages[page_number].style.display="block"; // show page for (	count=0; // Start counter	count < pagination_tabs.length; // Repeat until counter reaches number of page tabs	count++ // Count up by one each time. Equivalent to count+=1 and count=count+1 .	) 	{ // run this code as many times as there are pages in the selected container if (count!=page_number) { // apply to all pages except the selected one pagination_tabs[count].style.fontWeight="normal"; // unhighlight selected tab pages[count].style.display="none"; // hide page }	} }

if 	( document.getElementsByClassName("pagination_tabs").length ) /* checks if the element exists to avoid errors */ { for (count_pagination_containers=0; // start counter		 // repeat until containers are counted		 count_pagination_containers < document.getElementsByClassName("pagination_container").length;		 count_pagination_containers++ // count up		) { // walk through all pagination containers pagination_tabs = document.getElementsByClassName("pagination_tabs")[count_pagination_containers].getElementsByTagName("li"); // Adding click listeners to the tabs for (count_pages=0; count_pages<pagination_tabs.length; count_pages++) { // walk through all pages pagination_tabs[count_pages].setAttribute("onclick","switchToPage("+count_pages+","+count_pagination_containers+");"); }		switchToPage(0,count_pagination_containers); // initialize first page of each container } } else { console.debug("No pagination containers found."); }