// ***** Items-per-page  for doc-lists *****
//
// Needs to re-load page when different items-per-page selected

jQuery(document).ready(function() {
	jQuery('#itemsperpage').change(newItemsPerPage);
	jQuery('.JS').css('display', 'block');
	
	// Added by DG for the new login/search pop-out box
	// Should be updated when the chunk "login-search-jquery" is updated
	jQuery("#login-search").addClass("jquery-login-search").hide();
	jQuery("#login-search-tab").show();
	jQuery("#login-button-tr").addClass("jquery-login-button-tr");
	jQuery("#sub-nav").css("margin-top", "27px");

	jQuery("#login-search-tab-button").click(function() {
  		jQuery("#login-search-tab").hide();
  		jQuery("#login-search").show();
  		return false;
	});
});

function stripGetParameter(url, param) // Strip GET parameter from url; for newItemsPerPage()
	{
	var locn = url.indexOf(param);
	var locnEnd;

	if (locn != -1)
		{
		locnEnd = url.indexOf('&', locn);
		if (locnEnd == -1) 
			return url.substr(0, locn - 1) // no parameters after this one
		else
			return url.substr(0, locn).concat(url.substr(locnEnd+1));
		}
	else
		return url;
	}
function newItemsPerPage()
	{
	var url = window.location.href;
	var itemsperpage = this.value;
	var startparam = 'doclist_start', startitem;

	if (url.indexOf(startparam) != -1)
		{
		startitem = parseInt(url.substr(url.indexOf(startparam) + startparam.length + 1));
		startitem = Math.floor(startitem/itemsperpage) * itemsperpage; // startitem must be set to a multiple of the new 'itemsperpage'
		}
	else startitem = 0;

	url = stripGetParameter(url, 'itemsperpage'); // Strip old parameters
	url = stripGetParameter(url, startparam);

	if (url.indexOf('?') != -1)
		{
		// GET parameters already in URL
		url = url.concat('&');
		}
	else
		url = url.concat('?');

	// append new parameters to URL
	window.location.href = url.concat('itemsperpage=').concat(itemsperpage).concat('&').concat(startparam).concat('=').concat(startitem);
	}

