<?php
/**
 * Description of Page_index
 *
 * @author apour
 */
class PageIndex {
    private $amountOfElements;
    private $amountOfPages;
    private $indexStart;
    private $indexEnd;
    private $indexLength;
    public  $showEmptyElements;
    public  $elementsOnPage;
    public  $pageNum = 1;
    public  $url;
    public  $error = "";
    public  $cssClass = "PINDEX";
    public  $gotoStartChar = "&laquo;";
    public  $gotoEndChar = "&raquo;";

    public function __construct($amountOfElements, $showEmptyElements = true, $elementsOnPage = 30, $url = "", $indexLength = 10) {
	$this->amountOfElements = $amountOfElements;
	$this->showEmptyElements = $showEmptyElements;
	$this->elementsOnPage = $elementsOnPage;
	$this->indexLength = --$indexLength;
	$this->url = $url;

        $this->amountOfPages = $this->amountOfElements / $this->elementsOnPage;
        $this->indexStart = floor($this->pageNum * $this->elementsOnPage);
    }

    public function setLength($len) {
	if(is_numeric($len) AND $len > 1) {
	    $this->indexLength = --$len;
	}
    }

    public function draw($pageNum) {
	if($pageNum >= 1 AND !empty($this->url) AND $pageNum <= ceil($this->amountOfPages)) {
	    $OUT = "";
	    $this->pageNum = $pageNum;

            // SET INDEX START
            if($this->pageNum > ($this->indexLength/2)) {
		$this->indexStart = $this->pageNum - floor($this->indexLength/2);
	    } else {
		$this->indexStart = 1;
	    }

            // SET INDEX END
            if(($this->indexStart + $this->indexLength) > $this->amountOfPages AND $this->amountOfPages > $this->indexLength) {
		$this->indexEnd = ceil($this->amountOfPages);
	    } else {
		$this->indexEnd = $this->indexStart + $this->indexLength;
	    }

            // END OF LIST?
	    if($this->indexEnd - $this->indexStart < $this->indexLength) {
		$this->indexStart = $this->indexStart - ($this->indexLength - ($this->indexEnd - $this->indexStart));
	    } 

            if($this->indexStart < 1) { 
		$this->indexStart = 1;
	    }


	    $URL = preg_replace("/###PAGE###/i",1,$this->url);

            if($this->indexStart > 1) {
		$OUT.= "\n\t".'<li class="ON">'.preg_replace("/###NAME###/i",$this->gotoStartChar,$URL).'</li>';
	    } else {
		$OUT.= "\n\t".'<li class="OFF">'.preg_replace("/###NAME###/i",$this->gotoStartChar,$URL).'</li>';
	    }

            for($this->indexStart;$this->indexStart < $this->indexEnd + 1;$this->indexStart++) {
		$URL = preg_replace("/###PAGE###/i",$this->indexStart,$this->url);
                if($this->indexStart > ceil($this->amountOfPages) AND $this->showEmptyElements) {
		    $URL = preg_replace("/###PAGE###/i",1,$this->url);
		    $OUT.= "\n\t".'<li class="POFF">'.preg_replace("/###NAME###/i",$this->indexStart,$URL).'</li>';
		} else if ($this->indexStart == $this->pageNum) {
		    $OUT.= "\n\t".'<li class="PACT">'.preg_replace("/###NAME###/i",$this->indexStart,$URL).'</li>';
		} else if($this->indexStart <= ceil($this->amountOfPages)) {
		    $OUT.= "\n\t".'<li>'.preg_replace("/###NAME###/i",$this->indexStart,$URL).'</li>';
		}
            }

	    $URL = preg_replace("/###PAGE###/i",ceil($this->amountOfPages),$this->url);
            if($this->indexEnd < $this->amountOfPages) {
		$OUT.= "\n\t".'<li class="ON">'.preg_replace("/###NAME###/i",$this->gotoEndChar,$URL).'</li>';
	    } else {
		$OUT.= "\n\t".'<li class="OFF">'.preg_replace("/###NAME###/i",$this->gotoEndChar,$URL).'</li>';
	    }
	    

	    return "\n".'<ul class="'.$this->cssClass.'">'.$OUT."\n</ul>\n";
	} else {
	    $this->error = "Page ".htmlentities($pageNum)." does not exist!";
	}
    }
}
?>
