<?php
/* All Right Reserved (C) 2010;
  MyTickets;
  Writed By Fahad Salem Al-ghudaier;
  faha1ad2@hotmail.com;
  www.phpx3.com;
*/

	// احضار رقم الصفحة
	$page = intval($_GET['pagen']);
	if($page == 0 OR $page == '' OR ! is_numeric($page) OR ! preg_match("/^([0-9])+$/i", $page)){ $page = 1; $start=0; }
	// أقصى عدد للمستخرجات في الصفحه الواحده
	$limit = 15;
	$adjacents = 6;
	if($page)
	$start = ($page - 1) * $limit;
	else
	$start = 0;
	// لاتنسى تعديل الإستعلام
	$query = "SELECT COUNT(`id`) as num FROM ".$TABLE." ".$WHERE."";
	$total_pages = mysql_fetch_array(mysql_query($query));
	$total_pages = $total_pages[num];
	$prev = $page - 1;
	$next = $page + 1;
	$lastpage = ceil($total_pages/$limit);
	$lpm1 = $lastpage - 1;
	// رابط الصفحة
	$targetpage	= 'tickets.php?do=control';
	/*$language['next']	= 'التالي';
	$language['prev']	= 'السابق';*/


	$pagination = "";
	if($lastpage > 1)
	{	
	$pagination .= "<div class=\"pagination\">";
	if ($page > 1) 
	$pagination.= "<a href=\"$targetpage&amp;pagen=$prev\">« ".$language['prev']."</a>";
	else
	$pagination.= "<span class=\"disabled\">« ".$language['prev']."</span>";	
	if ($lastpage < 7 + ($adjacents * 2))
	{	
	for ($counter = 1; $counter <= $lastpage; $counter++)
	{
	if ($counter == $page)
	$pagination.= "<span class=\"current\">$counter</span>";
	else
	$pagination.= "<a href=\"$targetpage&amp;pagen=$counter\">$counter</a>";					
	}
	}
	elseif($lastpage > 5 + ($adjacents * 2))
	{
	if($page < 1 + ($adjacents * 2))		
	{
	for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
	{
	if ($counter == $page)
	$pagination.= "<span class=\"current\">$counter</span>";
	else
	$pagination.= "<a href=\"$targetpage&amp;pagen=$counter\">$counter</a>";					
	}
	$pagination.= "...";
	$pagination.= "<a href=\"$targetpage&amp;pagen=$lpm1\">$lpm1</a>";
	$pagination.= "<a href=\"$targetpage&amp;pagen=$lastpage\">$lastpage</a>";		
	}
	elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
	{
	$pagination.= "<a href=\"$targetpage&amp;pagen=1\">1</a>";
	$pagination.= "<a href=\"$targetpage&amp;pagen=2\">2</a>";
	$pagination.= "...";
	for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
	{
	if ($counter == $page)
	$pagination.= "<span class=\"current\">$counter</span>";
	else
	$pagination.= "<a href=\"$targetpage&amp;pagen=$counter\">$counter</a>";					
	}
	$pagination.= "...";
	$pagination.= "<a href=\"$targetpage&amp;pagen=$lpm1\">$lpm1</a>";
	$pagination.= "<a href=\"$targetpage&amp;pagen=$lastpage\">$lastpage</a>";		
	}
	else
	{
	$pagination.= "<a href=\"$targetpage&amp;pagen=1\">1</a>";
	$pagination.= "<a href=\"$targetpage&amp;pagen=2\">2</a>";
	$pagination.= "...";
	for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
	{
	if ($counter == $page)
	$pagination.= "<span class=\"current\">$counter</span>";
	else
	$pagination.= "<a href=\"$targetpage&amp;pagen=$counter\">$counter</a>";					
	}
	}
	}
	if ($page < $counter - 1) 
	$pagination.= "&nbsp;&nbsp;<a href=\"$targetpage&amp;pagen=$next\">".$language['next']." »</a>";
	else
	$pagination.= "&nbsp;&nbsp;<span class=\"disabled\">".$language['next']." »</span>";
	$pagination.= "</div>\n";		
	}
	

	$smarty->assign('pages',$pagination);
?>