<?php
if (isset($tpl['paginator']))
{		
	if($tpl['paginator']['pages'] > 1)
	{
		$page = 1 ;
		$query_string = $_SERVER['QUERY_STRING'];
		if(empty($query_string)){
			$query_string = "controller=pjListings&amp;action=pjActionYachts";
		}
		if(isset($_GET['pjPage'])){
			$page = $_GET['pjPage'];
			$query_string = str_replace("&pjPage=" . $page, "", $query_string);
		}
		?>
		<div class="ylPaging">
			<?php
			$stages = 3;
			$lastpage = $tpl['paginator']['pages'];
								
			if ($page > 1)
			{
				?><a href="<?php echo $_SERVER['PHP_SELF']; ?>?<?php echo $query_string;?>&amp;pjPage=<?php echo $page - 1; ?>" >&lt;</a><?php
			}
			 
			if ($lastpage < 7 + ($stages * 2))
			{
				for ($counter = 1; $counter <= $lastpage; $counter++)
				{
					if ($counter == $page)
					{
						?><a href="javascript:void(0);" class="ylActive"><span><?php echo $counter; ?></span></a><?php
					}else{
						?><a href="<?php echo $_SERVER['PHP_SELF']; ?>?<?php echo $query_string;?>&amp;pjPage=<?php echo $counter; ?>" ><?php echo $counter; ?></a><?php
					}
				}
			} else if ($lastpage > 5 + ($stages * 2)){
				
				if($page < 1 + ($stages * 2))		
				{
					for ($counter = 1; $counter < 4 + ($stages * 2); $counter++)
					{
						if ($counter == $page){
							?><a href="javascript:void(0);" class="ylActive"><span><?php echo $counter; ?></span></a><?php
						}else{
							?><a href="<?php echo $_SERVER['PHP_SELF']; ?>?<?php echo $query_string;?>&amp;pjPage=<?php echo $counter; ?>" ><?php echo $counter; ?></a><?php
						}	
					}
					?>
					<span>.</span>
					<span>.</span>
					<span>.</span>
					<a href="<?php echo $_SERVER['PHP_SELF']; ?>?<?php echo $query_string;?>&amp;pjPage=<?php echo $lastpage - 1; ?>" ><?php echo $lastpage - 1; ?></a>
					<a href="<?php echo $_SERVER['PHP_SELF']; ?>?<?php echo $query_string;?>&amp;pjPage=<?php echo $lastpage; ?>" ><?php echo $lastpage; ?></a>
					<?php
				}else if($lastpage - ($stages * 2) > $page && $page > ($stages * 2)){
					?>
					<a href="<?php echo $_SERVER['PHP_SELF']; ?>?<?php echo $query_string;?>&amp;pjPage=1" >1</a>
					<a href="<?php echo $_SERVER['PHP_SELF']; ?>?<?php echo $query_string;?>&amp;pjPage=2" >2</a>
					<span>.</span>
					<span>.</span>
					<span>.</span>
					<?php
					for ($counter = $page - $stages; $counter <= $page + $stages; $counter++){
						if ($counter == $page)
						{
							?><a href="javascript:void(0);" class="ylActive"><span><?php echo $counter; ?></span></a><?php
						}else{
							?><a href="<?php echo $_SERVER['PHP_SELF']; ?>?<?php echo $query_string;?>&amp;pjPage=<?php echo $counter; ?>" ><?php echo $counter; ?></a><?php
						}
					}
					?>
					<span>.</span>
					<span>.</span>
					<span>.</span>
					<a href="<?php echo $_SERVER['PHP_SELF']; ?>?<?php echo $query_string;?>&amp;pjPage=<?php echo $lastpage - 1; ?>" ><?php echo $lastpage - 1; ?></a>
					<a href="<?php echo $_SERVER['PHP_SELF']; ?>?<?php echo $query_string;?>&amp;pjPage=<?php echo $lastpage; ?>" ><?php echo $lastpage; ?></a>
					<?php
				}else{
					?>
					<a href="<?php echo $_SERVER['PHP_SELF']; ?>?<?php echo $query_string;?>&amp;pjPage=1" >1</a>
					<a href="<?php echo $_SERVER['PHP_SELF']; ?>?<?php echo $query_string;?>&amp;pjPage=2" >2</a>
					<span>.</span>
					<span>.</span>
					<span>.</span>
					<?php
					for ($counter = $lastpage - (2 + ($stages * 2)); $counter <= $lastpage; $counter++)
					{
						if ($counter == $page)
						{
							?><a href="javascript:void(0);" class="ylActive"><span><?php echo $counter; ?></span></a><?php
						}else{
							?><a href="<?php echo $_SERVER['PHP_SELF']; ?>?<?php echo $query_string;?>&amp;pjPage=<?php echo $counter; ?>" ><?php echo $counter; ?></a><?php
						}
					}
				}	
			}
			if ($page < $counter - 1){
				 ?><a href="<?php echo $_SERVER['PHP_SELF']; ?>?<?php echo $query_string;?>&amp;pjPage=<?php echo $page + 1; ?>" >&gt;</a><?php
			}
			?>
		</div>
		<?php
	}
} 
?>