<?php
/*
Started : 24-8-2007 6:53 AM
*/
/*
 * Edited : 18-5/2010 3:57 AM
 * By : Muaid Al Mohammadi
 */

class PowerBBPager
{
	var $total;			// The total number of rows
	var $perpage;			// How many rows per page
	var $count;			// Variable which help us to do pagers
	var $location;			// The web page location
        var $current_page;
	var $pages_number;		// The number of pages
	var $var_name;			// The count's variable name
	var $print_style = array();	// The style of print
	var $limit = 7;                 // How many of pages will print?
	var $i = 0;			// Will use it in loop
	var $x;				// Will use it in loop
	var $p;

	function SetInformation($style)
	{
		$this->print_style = $style;
	}

	/**
	 * This function setup important variables and count the pages number
	 */
	function start($total,$perpage,$count,$location,$var)
	{
            global $PowerBB;
            $this->total 		= 	($total < 0) ? 0 : $total;
            $this->perpage 		= 	($perpage < 0) ? 10 : $perpage;
            $this->count 		= 	($count < 0) ? 0 : $count;
            $this->location		=	$location;
            $this->var_name		=	$var;

            if (($this->count <> 0) && ($this->count > 0) && ($this->count < $this->total) && (($this->count % $this->perpage) === 0)){
                $this->current_page =  $this->count / $this->perpage;
            }else{
                $this->current_page = 0;
            }
            $this->current_page++;
            $this->pages_number = 	ceil($this->total/$this->perpage);
            $this->limit = $PowerBB->_CONF['info_row']['page_max'] - 2;
	}

	/**
	 * Return output which contain pages with links
	 */
	function show()
	{
                /* check if there is any pages to display pager */
                if ($this->pages_number !== 0){
                    $output = $this->_proc($this->print_style[0]);
                    $vactor = floor(($this->limit) / 2);
                    if (($this->limit + 2) <= $this->pages_number){
                        if (($this->current_page - $vactor) <= 1){
                            for($this->x = 1;$this->x <= ($this->limit + 1);$this->x++)
                                $output .= $this->output_style($this->x);
                            $this->x = $this->pages_number;
                            $output .= $this->output_style($this->x);
                        }elseif (($this->current_page + $vactor) >= $this->pages_number){
                            $this->x = 1;
                            $output .= $this->output_style($this->x);
                            for($this->x = ($this->pages_number - $this->limit);$this->x <= $this->pages_number; $this->x++)
                                $output .= $this->output_style($this->x);
                        }else{
                            $this->x = 1;
                            $output .= $this->output_style($this->x);
                            $oddeven = ($this->limit % 2) ? 0 : 1;
                            for($this->x = ($this->current_page - $vactor + $oddeven);$this->x <= ($this->current_page + $vactor); $this->x++)
                                $output .= $this->output_style($this->x);
                            $this->x = $this->pages_number;
                            $output .= $this->output_style($this->x);
                        }
                    }else{
                        for($this->x = 1;$this->x <= $this->pages_number;$this->x++){
                            $output .= $this->output_style($this->x);
                        }
                    }

                }
                          $output .= $this->print_style[3];

		return $output;
	}

        function output_style($_x){
            $this->i = ($_x - 1) * $this->perpage;
            if ($_x == $this->current_page)
                return $this->_proc($this->print_style[1]);
            else
                return $this->_proc($this->print_style[2]);
        }

	function _proc($string)
	{
	global $PowerBB;

		/**
		 * [l] = location
		 * [v] = var_name
		 * [c] = i
		 * [p] = x
		 */

		$string = str_replace('[l]',$this->location,$string);
		$string = str_replace('[v]',$this->var_name,$string);
		$string = str_replace('[c]',$this->i,$string);
        $string = str_replace('[p]',$this->x,$string);
        $string = str_replace('[Pages]',$PowerBB->_CONF['template']['lang']['Pages'],$string);

		return $string;
	}
}

?>
