function make_pagination(target_div, url_base, page_number, total_pages)
{
	var full_link_threshold = 20;
	var extra_links = 2;
	var spread_multiplier = 1.8;
	var css_class = 'pagination';
	var previous_page_label = '<<';
	var next_page_label = '>>';
	var disabled_color = 'silver';
	var ellipses = '...';

	var isIE = false;
	if (window.ActiveXObject) { isIE = true; }

	var url_start = '';
	var url_end = '';
	var id_magic = Math.floor(Math.random() * 10000);
	var min_page_small = (parseInt(page_number) - extra_links);
	var max_page_small = (parseInt(page_number) + extra_links);
	var number_of_zeroes = 1;
	var place_value = 10;
	var show_ellipses = false;
	var is_link = false;
	var was_close = false;
	var was_power = false;
	var was_tail = true;
	var is_gap = false;
	var iteration_count = 0;
	var i = 0;
	var output_dom = document.createElement('div');
	var next_power = 0;
	
	if ((null == url_base) || ("" == url_base))
	{
		url_start = "JavaScript:make_pagination('" + target_div + "','','";
		url_end   = "', '" + total_pages + "');";
	}
	else
	{
		url_start = url_base;
		url_end   = ""; 
	}

	
	if (max_page_small > total_pages)
	{
		min_page_small = min_page_small - (max_page_small - total_pages);
	}
	
	if (min_page_small < 1)
	{
		max_page_small = max_page_small + (1 - min_page_small);
	}

	number_of_zeroes = Math.max(1, Math.floor(Math.log(total_pages) / Math.log(10)));
	place_value = Math.pow(10, number_of_zeroes);


	if ((place_value * spread_multiplier) > total_pages)
	{
		number_of_zeroes -= 1;
		place_value = Math.pow(10, number_of_zeroes);
	}
	

	if (page_number > 1)
	{
		var a = document.createElement('a');
		a.setAttribute('href', url_start + (page_number - 1) + url_end);
		
		if (isIE) { a.style.textDecoration = 'none'; } else { a.setAttribute('style', 'text-decoration: none;'); }
	}
	else
	{
		var a = document.createElement('span');		
		if (isIE) { a.style.textDecoration = 'none'; a.style.color = disabled_color; } else { a.setAttribute('style', 'text-decoration: none; color: ' + disabled_color + ';'); }
	}
	
	a.className = css_class;
	a.appendChild(document.createTextNode(previous_page_label));
	
	output_dom.appendChild(document.createTextNode(" "));
	output_dom.appendChild(a);
	output_dom.appendChild(document.createTextNode(" "));
		
	
	while (true)
	{
		i += 1;
		if (i > total_pages) { break; }

		iteration_count += 1;
		
		if (i == page_number)
		{
			is_link = false;
			was_close = true;
		}
		else if (total_pages <= full_link_threshold)
		{
			is_link = true;
		}
		else if ((i >= min_page_small) && (i <= max_page_small))
		{
			show_ellipses = (!was_close && !was_tail) || is_gap;
		
			is_link = true;
			was_close = true;
			was_power = false;
			was_tail = false;
			is_gap = false;
		}
		else if (   ((i >= 1) && (i <= extra_links))
				||  ((i >= (total_pages - extra_links + 1)) && (i <= total_pages))
				)
		{
			show_ellipses = was_power || is_gap;
		
			is_link = true;
			was_close = false;
			was_power = false;
			was_tail = true;
			is_gap = false;
		}
		else if (i % place_value == 0)
		{
			show_ellipses = !was_power;
			
			is_link = true;
			was_close = false;
			was_power = true;
			was_tail = false;
			is_gap = false;
		}
		else
		{
			next_power = i - (i % place_value) + place_value;
			
			i = Math.min(
				(min_page_small > i) ? (min_page_small - 1) : (total_pages - extra_links),
				(next_power     > i) ? (next_power     - 1) : (total_pages - extra_links)
			);
			
			is_gap = true;
			continue;
		}
		
		if (show_ellipses) { output_dom.appendChild(document.createTextNode(" " + ellipses)); show_ellipses = false; }
		
		
		if (is_link)
		{
			var a = document.createElement('a');
			a.setAttribute('href', url_start + i + url_end);
		}
		else
		{
			var a = document.createElement('span');			
			if (isIE) { a.style.fontWeight = 'bold'; } else { a.setAttribute('style', 'font-weight: bold;'); }
		}
		
		
		a.className = css_class;
		a.appendChild(document.createTextNode(i));
		
		output_dom.appendChild(document.createTextNode(" "));
		output_dom.appendChild(a);
		output_dom.appendChild(document.createTextNode(" "));
	}

	if ((parseInt(page_number) + 1) <= total_pages)
	{
		var a = document.createElement('a');
		a.setAttribute('href', url_start + (parseInt(page_number) + 1) + url_end);
		
		if (isIE) { a.style.textDecoration = 'none'; } else { a.setAttribute('style', 'text-decoration: none;'); }
	}
	else
	{
		var a = document.createElement('span');		
		if (isIE) { a.style.textDecoration = 'none'; a.style.color = disabled_color; } else { a.setAttribute('style', 'text-decoration: none; color: ' + disabled_color + ';'); }
	}
	
	a.className = css_class;
	a.appendChild(document.createTextNode(next_page_label));
	
	output_dom.appendChild(document.createTextNode(" "));
	output_dom.appendChild(a);
	output_dom.appendChild(document.createTextNode(" "));

	output_dom.setAttribute('id', 'inner_pagination_' + id_magic);
	output_dom.className = css_class;

	target_dom = document.getElementById(target_div);
	target_dom.innerHTML = '';
	target_dom.appendChild(output_dom);
}