﻿// JavaScript Document

	// Global variables and constants
	var _NUM_ITEMS_PER_PAGE = 10;
	var _LOWER_LIMIT = 0;
	var _UPPER_LIMIT = _LOWER_LIMIT + _NUM_ITEMS_PER_PAGE - 1;
	var _CURRENT_LINK = 1;
	var _ACTIVE_PAGE = 1; // Variable to hold the number of the ACTIVE page
	
	$(document).ready(function(){
		
		// Initialise the SELECTED COURSE ID
		var SelectedCourseID = "";
		
		// Check whether any URL parameters have been passed
		URL = location.href.split("?");
		if (URL.length > 1)
		{
			// Split all the parameters into an array
			URLParameters = URL[1].split("&");
			
			// Now traverse through each of the parameters, checking if a COURSEID has been specified
			for (Count = 0; Count < URLParameters.length; Count++)
			{
				// Check for COURSE ID
				if (URLParameters[Count].indexOf("CourseID") >= 0)
				{
					// Obtain the value of the COURSE ID
					FullParameter = URLParameters[Count].split("=");
					
					// Store the value of the SELECTED COURSE ID for future use
					SelectedCourseID = FullParameter[1];
				}
			}
		}
		
		// Create a reference to the PAGINATED LIST items and the LINKS
		// NOTE: This may need to be changed later, as it currently allows only one paginated list on the page.
		// In the future we might need the ability to have more than one paginated list on a page.
		var MyList = $("dl#PaginatedList > dt");
		var MyListLinks = $("dl#PaginatedList > dt a");
		
		// Hide all the list items
		$("dl#PaginatedList > dt").hide();
		
		// How many list items are there?
		var NumListItems = MyList.length;
		
		// If a course has been selected, then cycle through the PAGINATED LIST to determine the appropriate value for the LOWER and UPPER LIMITS
		if (SelectedCourseID != "")
		{
			for (Count=0; ((Count<NumListItems) && (MyListLinks[Count].id != SelectedCourseID)); Count++)
			{
				// If we've reached the UPPER_LIMIT value, then the current page hasn't been found
				if (Count >= _UPPER_LIMIT)
				{
					// Update the LOWER AND UPPER LIMITS
					_LOWER_LIMIT = _UPPER_LIMIT + 1;
					_UPPER_LIMIT = _LOWER_LIMIT + _NUM_ITEMS_PER_PAGE - 1;
					
					// Increment the value of the ACTIVE page number
					_ACTIVE_PAGE++;
				}
			}
		}
			
		// Cycle through the PAGINATED LIST and assign an ID to each row
		for (Count=0; Count<NumListItems; Count++)
		{
			// Create a copy of COUNT with leading zeroes
			CountWithZeroes = Count;
			if (Count < 10) CountWithZeroes = "0" + CountWithZeroes;
			
			MyList[Count].id = "PL_" + CountWithZeroes;
			
			// If the count falls within the LOWER and UPPER LIMITS,
			// then show the respective link, otherwise let it stay hidden
			if ((Count >= _LOWER_LIMIT) && (Count <= _UPPER_LIMIT))
				$("#PL_" + CountWithZeroes).show();
		}
		
		// How many pages will we need?
		var NumPages =  parseInt(NumListItems / _NUM_ITEMS_PER_PAGE);
		
		// Will we need a last page with the leftover links?
		if ((NumListItems % _NUM_ITEMS_PER_PAGE) > 0)
			NumPages++;
		
		// Create links to the pages
		var Paginator = "<div class='Paginator'><a href='#' id='PrevLink'>prev</a> | ";
		
		// Now add the PAGE LINKS
		for (Count = 1; Count <= NumPages; Count++ )
		{
			Paginator += "<a href='#' id='Link_" + Count + "'>" + Count + "</a> | ";
		}
		
		// Add the NEXT PAGE LINK and close the PAGINATOR code
		Paginator += "<a href='#' id='NextLink'>next</a>";
		Paginator += "</div><br /><br />";
		
		// Add the PAGINATOR to the start of the PAGINATED LIST
		$("dl#PaginatedList").prepend(Paginator);
		
		// Set the ACTIVE page link
		$("a#Link_" + _ACTIVE_PAGE).addClass("Active");
		
		// Based on the value of the ACTIVE PAGE variable, enable/disable the PREV/NEXT links
		if (_ACTIVE_PAGE == 1)
			$("a#PrevLink").addClass("Inactive");
		
		if (_ACTIVE_PAGE == NumPages)
			$("a#NextLink").addClass("Inactive");
		
		// Now cycle through the links and add a CLICK FUNCTION to each one
		for (Count = 1; Count <= NumPages; Count++ )
		{
			// Add a CLICK FUNCTION to each link
			$("a#Link_" + Count).click(function() {
												
				// Cycle through all the LINKS and remove the ACTIVE class, if applicable
				for (LinkCount = 1; LinkCount <= NumPages; LinkCount++)
					$("a#Link_" + LinkCount).removeClass("Active");
				
				// Set the current CLASS to ACTIVE
				$(this).addClass("Active");
				
				// Extract the number of the current LINK ID
				// The ID is in the format LINK_xx and we want just the 'xx' bit,
				// so we use the substr function to get rid of the first 5 characters, i.e. 'LINK_'
				var LinkID = parseInt(this.id.substr(5));
				
				// Update the CURRENTLINK global variable
				_CURRENT_LINK = LinkID;
					
				// If we're back at the FIRST LINK, then make the PREVIOUS link inactive
				if (_CURRENT_LINK == 1)
					$("a#PrevLink").addClass("Inactive");
				else
					$("a#PrevLink").removeClass("Inactive");

				// If we're at the LAST LINK, then make the NEXT link inactive
				if (_CURRENT_LINK == NumPages)
					$("a#NextLink").addClass("Inactive");
				else
					$("a#NextLink").removeClass("Inactive");
				
				// Update the LOWER and UPPER LIMIT variables accordingly
				_LOWER_LIMIT = (LinkID - 1) * _NUM_ITEMS_PER_PAGE;
				_UPPER_LIMIT = _LOWER_LIMIT + _NUM_ITEMS_PER_PAGE - 1;
				
				// Now cycle through the LIST ITEMS once again, 
				// hiding the ones outside the specified LOWER and UPPER limits,
				// and showing the ones within
				for (ListCount=0; ListCount<NumListItems; ListCount++)
				{
					// Create a copy of COUNT with leading zeroes
					CountWithZeroes = ListCount;
					if (ListCount < 10) CountWithZeroes = "0" + CountWithZeroes;
					
					// If the count falls within the LOWER and UPPER LIMITS,
					// then show the respective link
					if ((ListCount >= _LOWER_LIMIT) && (ListCount <= _UPPER_LIMIT))
						$("#PL_" + CountWithZeroes).show();
					else
						$("#PL_" + CountWithZeroes).hide(); // Otherwise, hide it!
				}
			});
		}
		
		// Add the CLICK function to the PREVIOUS link 
		$("a#PrevLink").click( function() {
			
			// Proceed only if you actually CAN have a previous link
			if (_CURRENT_LINK > 1)
			{
				// Make the new page link active
				$("a#Link_" + _CURRENT_LINK).removeClass("Active");
					
				// Update the CURRENTLINK global variable
				_CURRENT_LINK--;
				
				// Make the new page link active
				$("a#Link_" + _CURRENT_LINK).addClass("Active");
				
				// If we're back at the FIRST LINK, then make the PREVIOUS link inactive
				if (_CURRENT_LINK == 1)
					$(this).addClass("Inactive");
				else
					$(this).removeClass("Inactive");
					
				// If we're at anything other than the last link,
				// make the NEXT link active as well
				if (_CURRENT_LINK < NumPages)
					$("a#NextLink").removeClass("Inactive");
					
				// Update the LOWER and UPPER LIMIT variables accordingly
				_LOWER_LIMIT = (_CURRENT_LINK - 1) * _NUM_ITEMS_PER_PAGE;
				_UPPER_LIMIT = _LOWER_LIMIT + _NUM_ITEMS_PER_PAGE - 1;
				
				// Now cycle through the LIST ITEMS once again, 
				// hiding the ones outside the specified LOWER and UPPER limits,
				// and showing the ones within
				for (ListCount=0; ListCount<NumListItems; ListCount++)
				{
					// Create a copy of COUNT with leading zeroes
					CountWithZeroes = ListCount;
					if (ListCount < 10) CountWithZeroes = "0" + CountWithZeroes;
					
					// If the count falls within the LOWER and UPPER LIMITS,
					// then show the respective link
					if ((ListCount >= _LOWER_LIMIT) && (ListCount <= _UPPER_LIMIT))
						$("#PL_" + CountWithZeroes).show();
					else
						$("#PL_" + CountWithZeroes).hide(); // Otherwise, hide it!
				}
			}
		});
		
		// Add the CLICK function to the NEXT link 
		$("a#NextLink").click( function() {
			
			// Proceed only if you actually CAN have a next link
			if (_CURRENT_LINK < NumPages)
			{
				// Make the new page link active
				$("a#Link_" + _CURRENT_LINK).removeClass("Active");
					
				// Update the CURRENTLINK global variable
				_CURRENT_LINK++;
				
				// Make the new page link active
				$("a#Link_" + _CURRENT_LINK).addClass("Active");
					
				// If we're at the LAST LINK, then make the NEXT link inactive
				if (_CURRENT_LINK == NumPages)
					$(this).addClass("Inactive");
				else
					$(this).removeClass("Inactive");
				
				// If we're at anything other than the first link,
				// make the PREVIOUS link active as well
				if (_CURRENT_LINK > 1)
					$("a#PrevLink").removeClass("Inactive");
					
				// Update the LOWER and UPPER LIMIT variables accordingly
				_LOWER_LIMIT = (_CURRENT_LINK - 1) * _NUM_ITEMS_PER_PAGE;
				_UPPER_LIMIT = _LOWER_LIMIT + _NUM_ITEMS_PER_PAGE - 1;
				
				// Now cycle through the LIST ITEMS once again, 
				// hiding the ones outside the specified LOWER and UPPER limits,
				// and showing the ones within
				for (ListCount=0; ListCount<NumListItems; ListCount++)
				{
					// Create a copy of COUNT with leading zeroes
					CountWithZeroes = ListCount;
					if (ListCount < 10) CountWithZeroes = "0" + CountWithZeroes;
					
					// If the count falls within the LOWER and UPPER LIMITS,
					// then show the respective link
					if ((ListCount >= _LOWER_LIMIT) && (ListCount <= _UPPER_LIMIT))
						$("#PL_" + CountWithZeroes).show();
					else
						$("#PL_" + CountWithZeroes).hide(); // Otherwise, hide it!
				}
			}
			
		});
	});
	
	// The UPDATELIST function 
	function UpdateList() 
	{
		
	}
