<?php
/**
 * Prints pagination from data received from fPaginationData.
 * 
 * The received data are in this format
 * page => pagenumber( eg. 1, 2, 50), prev, next, startdot, enddot
 * link => url or null
 * selected => true or false
 * 
 * @author boobs.lover
 */
class fPagination {
	
	/**
	 * 
<div class="wp-pagenavi">
	<span class="pages">Page 1 of 3</span>
	
	<span class="current">1</span>
	
	<a href="http://demo.freshface.net/file/lcp/wp/page/2/" class="page">
		2
	</a>
	
	<a href="http://demo.freshface.net/file/lcp/wp/page/3/" class="page">
		3
	</a>
	<a href="http://demo.freshface.net/file/lcp/wp/page/2/" class="nextpostslink">
		»
	</a>
</div>
	 */
	
	
	public static function Render() {
	
		$pg = new fPaginationData();
		$data = $pg->compute();
		if( $data == null ) return;
		
		$before_button_selected = '<span class="current">';		
		$after_button_selected = '</span>';		
		
		$before_button = '';
		$after_button = '';		
		
		$trans_next = fOpt::Get('translation', 'pagination-next');
		$trans_prev = fOpt::Get('translation', 'pagination-prev');
		$trans_dots = '...';//fOpt::Get('translation', 'pagination-dots');
		
		echo '<div class="wp-pagenavi">';
		
		$counter = 0;
		
		$selected = 0;
		foreach( $data as $one_element ) {
			
			if( $one_element['selected'] == true ) {
				$counter ++;
				$selected = $counter;
				echo $before_button_selected;
				echo $one_element['page'];
				echo $after_button_selected;	
			} else if ( $one_element['page'] == 'startdot' || $one_element['page'] == 'enddot') {
				
				//echo '<div class="pagination_item">';				
				echo $before_button;
				echo $trans_dots;
				echo $after_button;
				//echo '</div>';
				
			} else {
				
				echo '<a href="'.$one_element['link'].'" class="page">';				
				echo $before_button;
				
				if( $one_element['page'] == 'next')
					echo $trans_next;
				else if( $one_element['page'] == 'prev')
					echo $trans_prev;
				else {
					$counter ++;
					echo $one_element['page'];
				}
				echo $after_button;
				echo '</a>';
			} 
				
			
			
		}
		$page_translation = fOpt::Get('translation', 'pagination-page-of');
		$page_translation = str_replace('%', '%d', $page_translation);
		
		echo '<span class="pages">'. sprintf( $page_translation, (int)$selected, (int)$counter ) . '</span>';
		echo '</div>';
	}
}
