<?php
class functions{
	var $mysocket;
	var $myerror;
	/**
	 * displays navigation bar
	 *
	 * @param $q
	 *   search query
	 * @param $start
	 *   start number of current page
	 * @param $span
	 *   number of items on current page
	 * @param $total
	 *   total number of records found by current search
	 */
	function functions()
	{
   		$this->mysocket = FALSE;
   		$this->myerror = "";
	} 
	function displayNavigationBlock($m,$q,$url_opts,$start,$span,$total) {

		$start = $start=='' ? 0 : $start ;
		$span = $span=='' ? 15 : $span ;

		$total_pages = ceil($total/$span);
		$start_page = floor($start/$span);

		// if more than ten pages start at this page minus ten otherwise start at zero
		$begin = ($start_page>10)?($start_page-10):0;
		// if more than ten pages then stop at this page plus ten otherwise stop at last page
		$end = ($start_page>8)?($start_page+10):10;

		// url
		$unicode_q = urlencode($q);  // encode search string

		foreach ($url_opts as $key => $value) {
			$option_text .= "&" . $key . "=" . $value;
		}

		$url = "index.php?m=" . $m . "&q=" . $unicode_q . $option_text;

		// build
		if ($start_page!=0) {
			$start_page_text = "<a href='" . $url . "&start=0'><small>".PAGINATION_FIRST."</a>&nbsp;</small>
				<a href=" . $url . "&start=" . ($start-$span) . "><small><</a>&nbsp;</small>";
		}

		for($next_page=$begin;($next_page<$total_pages)&&($next_page<$end);$next_page++) {
			if ($next_page == $start_page) {
				$middle_page_text .= "<small>" . ($next_page+1) . "&nbsp;</small>";
			} else {
				$middle_page_text .= "<a href='" . $url . "&start=" . ($next_page*$span) . "'><small>" . ($next_page+1) . "</a>&nbsp;</small>";
			}
		}
		if ( ($start_page != $total_pages-1)  && ($total != 0) ) {
			$end_page_text = "<a href='" . $url . "&start=" . ($start+$span) . "'><small>></a>&nbsp;</small>
				<a href='" . $url . "&start=" . ($total_pages-1)*$span  . "'><small>".PAGINATION_LAST."</a>&nbsp;</small>";
		}

		$ret .= "<br/><div class='bar_center' id='navi'><small>".PAGINATION_PAGES.":</small>
			" . $start_page_text . "
			" . $middle_page_text . "
			" . $end_page_text . "
			</div>";

		return $ret;
	}
	
}
