<?php 

class bdg {
	public $bd_dir;
	private $output, $page, $pages, $total;
	
	public function __construct ($bd_dir) {
		$this->bd_dir = str_replace('//', '/', $bd_dir.'/');
		$this->ls ($this->bd_dir);
		$this->paginate();
		$this->parsePage();
	}
	
	private function ls ($dir) {
		$dh  = opendir($dir);
		while ($filename = readdir($dh)):
			if (is_file($dir.$filename) && !in_array($filename, array('.','..')) && substr($filename,-4) == '.jpg') $files[] = $filename;
		endwhile;
		usort($files, 'strnatcmp');
		$this->pages = $files;
	}

	private function paginate () {
		$this->page  = intval($_GET['page']);
		$this->total = sizeof($this->pages);
		$this->nextp = ($this->page != $this->total-1)	? $this->page + 1 : FALSE;
		$this->prevp = ($this->page != 0)					? $this->page - 1 : FALSE;
	}
	
	private function parsePage () {
		$this->output['image']  = '<p id="bdpage"><img src="'.$this->bd_dir.$this->pages[$this->page].'" alt="" /></p>';
		$this->output['lien_p'] .= ($this->prevp !== FALSE) ? '<a href="?page='.$this->prevp.'">page précédente</a>' : '';
		$this->output['lien_n'] .= ($this->nextp !== FALSE) ? '<a href="?page='.$this->nextp.'">page suivante </a>' : '';
	}
	
	public function output ($e) {
		if ( isset($this->output[$e]) ):
			return $this->output[$e];
		else:
			return FALSE;
		endif;
	}
}


$bdg = new bdg('.');

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
	
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
	<title></title>
</head>

<body>

<?php echo $bdg->output('image'); ?>

<p><?php echo $bdg->output('lien_p'); ?> | <?php echo $bdg->output('lien_n'); ?></p>
</body>
</html>