<?PHP

class pageGenerator extends engine{

	function page_generator($pageList, $show_name){

		if(!empty($pageList) && count($pageList)>1){

			$count = count($pageList);

			$prev_page_number = $_GET['page_number'] == 1 || !$_GET['page_number'] ? 1 : $_GET['page_number']-1;
			$next_page_number = $_GET['page_number'] == $pageList[$count]['page_number'] ? $pageList[$count]['page_number'] : $_GET['page_number']+1;
			$next_page_number = !$_GET['page_number'] && $pageList[2]['page_number'] ? $pageList[2]['page_number'] : $next_page_number;
			$next_page_number = !$_GET['page_number'] && !$pageList[2]['page_number'] ? $pageList[1]['page_number'] : $next_page_number;
			$_FULL_URL = _RootUrl.str_replace('/uus', '', $_SERVER['REQUEST_URI']);
			$_FULL_URL = preg_replace('/&page_number=([0-9_]+)/s', '', $_FULL_URL);
			$_FULL_URL = preg_replace('/\?page_number=([0-9_]+)/s', '', $_FULL_URL);
			$sep = preg_match('/\?/i', $_FULL_URL) ? '&' : '?';

			$a_pageList = '<a href="'.$this->Link($_FULL_URL.$sep.'page_number=1', $show_name).'" class="page_start">'.(htmlspecialchars('<<')).'</a>';
			$a_pageList .= ' <a href="'.$this->Link($_FULL_URL.$sep.'page_number='.$prev_page_number, $show_name).'" style="margin-right:5px;" class="page_left">'.(htmlspecialchars('<')).'</a>';

			$is_visible = false;
			$was_in_visible = false;
			$control = true;
			$shown = 0;

			foreach($pageList as $v){

				if($v['is_visible'] == true){

					if($v['page_number'] > $v['show_page_numbers'] && $control == true && $was_in_visible == true) $is_visible = true;
				}
				if($v['is_visible'] == false && $control == false){

					$is_visible = true;
				}
				if($v['is_visible'] == false){

					$was_in_visible = true;
				}

				if($is_visible == true && $shown < 2){

					$a_pageList .= '<span class="page_nr_text"> ... </span>';
					$is_visible = false;
					$control = false;
					$shown ++;
				}
				if($v['is_visible'] == true){

					$a_pageList .= ' <a href="'.$this->Link($_FULL_URL.$sep.'page_number='.$v['page_number'], $show_name).'" '.
						(($_GET['page_number'] == $v['page_number']) || (!$_GET['page_number'] && $v['page_number'] == 1) ? ' class="page_number_selected page_nr" ' : ' class="page_nr" ').
					'>'.$v['page_number'].'</a>';
				}
			}

			$a_pageList .= ' <a href="'.$this->Link($_FULL_URL.$sep.'page_number='.$next_page_number, $show_name).'" style="margin-left:5px;" class="page_right">'.(htmlspecialchars('>')).'</a>';
			$a_pageList .= ' <a href="'.$this->Link($_FULL_URL.$sep.'page_number='.$pageList[$count]['page_number'], $show_name).'" class="page_end">'.(htmlspecialchars('>>')).'</a>';


			echo $a_pageList;
		}
	}
}

?>