<?
if(!defined("B_PROLOG_INCLUDED") || B_PROLOG_INCLUDED!==true)die();

$strNavQueryString = ($arResult["NavQueryString"] != "" ? $arResult["NavQueryString"]."&amp;" : "");
$strNavQueryStringFull = ($arResult["NavQueryString"] != "" ? "?".$arResult["NavQueryString"] : "");

function _renderPageNumber($section, $page, $p, $query)
{
	if($page == $p)
	{
		if($p === 1)
		{
			return "<li><a href=\"".$section.$query."\"><strong>". $p ."</strong></a></li>";
		}

		return "<li><a href=\"".$section."page-".$p."/".$query."\"><strong>". $p ."</strong></a></li>";
	}

	if($p === 1)
	{
		return "<li><a href=\"".$section.$query."\">". $p ."</a></li>";
	}

	return "<li><a href=\"".$section."page-".$p."/".$query."\">". $p ."</a></li>";
}

$pagesLinks = [];
$pages = $arResult["NavPageCount"];
$page = $arResult["NavPageNomer"];

$section = "/search/";
$query = $arResult['NavQueryString'] ? "?".$arResult['NavQueryString'] : "";

if($page < 0) { $page = $pages; }
$firstPage = 1;
$lastPage = $pages;

$p = $page;
if($lastPage > 6)
{
	if($p === $lastPage)
	{
		$p = $p - 3;
	}
	else
	{
		if($p <= 3) { $p = $p - ($p-1); };
		if($p > 3) { $p = $p - 1; }
		//if(p > (lastPage - 3)) { p = p - 4; }
		if($p > ($lastPage - 3))
		{
			$p = $p - ($lastPage - ($p+1));
		}
	}
}

if($lastPage > 6)
{
	for($i = 0; $i < 7; $i++)
	{
		if($i == 0)
		{
			array_push($pagesLinks, _renderPageNumber($section, $page, $firstPage, $query));
		}
		else if($i == 1)
		{
			if($p >= 3)
			{
				array_push($pagesLinks, "<li>........</li>");
			}
			else
			{
				if($p > 1)
				{
					array_push($pagesLinks, _renderPageNumber($section, $page, $p, $query));
				}
				$p++;
			}
		}
		else if($i == 5)
		{
			if($p < ($lastPage))
			{
				array_push($pagesLinks, "<li>........</li>");
			}
		}
		else if($i == 6)
		{
			array_push($pagesLinks, _renderPageNumber($section, $page, $lastPage, $query));
		}
		else
		{
			if($p < $lastPage)
			{
				array_push($pagesLinks, _renderPageNumber($section, $page, $p, $query));
				$p++;
			}
		}
	}
}
else
{
	for($i = 0; $i < $lastPage; $i++)
	{
		array_push($pagesLinks, _renderPageNumber($section, $page, $i + 1, $query));
	}
}

?>
<? #list ?>
<? if($arResult["NavPageCount"] > 1): ?>
<div class="pagination">
	<ul>
		<? if($arResult["NavPageNomer"] > 1): ?>
			<? if($arResult["NavPageNomer"]-1 === 1): ?>
				<li><a href="<?= $section.$query; ?>"><</a></li>
			<? else: ?>
				<li><a href="<?= $section; ?>page-<?= ($arResult["NavPageNomer"]-1); ?>/<?= $query; ?>"><</a></li>
			<? endif; ?>
		<? endif; ?>
		<? foreach($pagesLinks AS $link): ?>
			<?= $link; ?>
		<? endforeach; ?>
		<? if($arResult["NavPageNomer"] + 1 < $arResult["NavPageCount"]): ?>
			<li><a href="<?= $section; ?>page-<?=($arResult["NavPageNomer"]+1); ?>/<?= $query; ?>">></a></li>
		<? endif; ?>
	</ul>
</div>
<? endif; ?>