<?php
/**
* Blog page  pagination
* default pagination (1,2,3...)
*/
function portfoliolite_pagination($pages = '', $range =2) {
$showitems = ($range * 2) + 1;
global $paged;
if (empty($paged)){
$paged = 1;
}
if ($pages == '') {
global $wp_query;
$pages = $wp_query->max_num_pages;
if (!$pages) {
$pages = 1;
}
}
if (1 != $pages) {
    echo "<div class='clearfix'></div>";
echo "<ul class='paging'>";
  if ($paged > 2 && $paged > $range + 1 && $showitems < $pages){
  echo "<li class ='nav-previous'><a href='" . get_pagenum_link(1) . "'>&laquo;</a></li>";
}
  if ($paged > 1 && $showitems < $pages){
  echo "<li><a href='" . get_pagenum_link($paged - 1) . "'><i class = 'fa fa-angle-left'></i></a></li>";
}
  for ($i = 1; $i <= $pages; $i++) {
  if (1 != $pages && (!($i >= $paged + $range + 1 || $i <= $paged - $range - 1) || $pages <= $showitems )) {
  echo ($paged == $i) ? "<li><a href='" . get_pagenum_link($i) . "' class='current' >" . $i . "</a></li>" : "<li><a href='" . get_pagenum_link($i) . "' class='inactive' >" . $i . "</a></li>";
  }
  }
  if ($paged < $pages && $showitems < $pages){
  echo "<li><a href='" . get_pagenum_link($paged + 1) . "'><i class = 'fa fa-angle-right'></i></a></li>";
  }
  if ($paged < $pages - 1 && $paged + $range - 1 < $pages && $showitems < $pages){
  echo "<li class = 'nav-next'><a href='" . get_pagenum_link($pages) . "'>&raquo;</a></li>";
}
echo "</ul>\n";
}
}
/**
* Blog Page Pagination
* Ajax Pagination
* Load More & Infinite scrolling Paginetion
*
*/
add_action('wp_ajax_nopriv_more_post_ajax', 'more_post_ajax'); // load more
add_action('wp_ajax_more_post_ajax', 'more_post_ajax');
add_action('wp_ajax_infinite_scroll', 'infinite_post_ajax');    // scroll
add_action('wp_ajax_nopriv_infinite_scroll', 'infinite_post_ajax');
function more_post_ajax(){
scrollPostCalling($_POST,'load_more');
}
function infinite_post_ajax(){
scrollPostCalling($_POST,'infinite');
}
function scrollPostCalling($post,$scroll){
$paged =  $_POST["paged"];
header("Content-Type: text/html");
$args = array(
      'post_type' => 'post',
      'paged'     => $paged,
      );  

$loop = new WP_Query($args);
  global $grid_layout;
     if($grid_layout == 'standard-layout'):
          // Start the post formate loop.
      while ($loop->have_posts()) : $loop->the_post();
      get_template_part( 'content', get_post_format() );
      endwhile;
      else :
        // Start the post formate grid.
      while ($loop->have_posts()) : $loop->the_post();
      get_template_part( 'content', 'grid');
      endwhile;
      endif;
      // If no content, include the "No posts found" template.
exit;
}
// load more ajax
function th_load_more_ajax(){
  global $wp_query;
 echo "<div class='clearfix'></div>";
?>
<div class="th_more_posts"><a id="inifiniteLoader"><img src="<?php echo PORTFOLIOLITE_THEME_URI; ?>/images/loading.gif" /></a><a id="more_posts" >Load More Post</a></div>
<script type="text/javascript">
var ajaxUrl = "<?php echo admin_url('admin-ajax.php')?>";
var page = 2; // What page we are on.
var ppp = "<?php echo get_option('posts_per_page '); ?>"; // Post per page
var total = "<?php echo $wp_query->max_num_pages; ?>"; // number of page
jQuery("#more_posts").on("click",function(){ // When btn is pressed.

if ( jQuery( this ).hasClass( "no-load-more" ) ) {
  return;
    }
  jQuery('a#inifiniteLoader').show('fast');
//jQuery("#more_posts").attr("disabled",true); // Disable the button, temp.
jQuery.post(ajaxUrl, {
action:"more_post_ajax",
offset: (page * ppp) + 1,
paged: page,
ppp: ppp
}).success(function(posts){
page++;
jQuery(".load_post").append(posts);
//alert(total+'-------'+page)
if(total<page){
// CHANGE THIS!
jQuery("#more_posts").attr("disabled",true);
jQuery("#more_posts").html('No More Post');
jQuery("#more_posts").addClass("no-load-more");
}
jQuery('a#inifiniteLoader').hide('1000');

});
});

</script>
<?php
}


// Inifinite pagination
function th_scrolling_ajax(){
global $wp_query;
?>

<a id="inifiniteLoader"><img src="<?php echo PORTFOLIOLITE_THEME_URI; ?>/images/loading.gif" /></a>
<script type="text/javascript">
var ajaxUrl = "<?php echo admin_url('admin-ajax.php');?>";
var page = 1; // What page we are on.
var count = 2; // What page we are on.
var total = <?php echo $wp_query->max_num_pages; ?>;

jQuery(window).scroll(function(){
if  (jQuery(window).scrollTop() == jQuery(document).height() - jQuery(window).height()){
//if(jQuery(window).scrollTop() >= jQuery( ".load_post" ).height() + jQuery( ".home-section" ).height() + jQuery( ".header-wrapper" ).height() - 200){

if (count >total){
return false;
}else{
loadScrolling(count,ajaxUrl);
count++;
}
}
});


function loadScrolling(page,ajaxUrl) {
jQuery('a#inifiniteLoader').show('fast');
jQuery.post(ajaxUrl, {
action:"infinite_scroll",
paged: page,
}).success(function(posts){
page++;
jQuery(".load_post").append(posts); // CHANGE THIS!
jQuery("#more_posts").attr("disabled",false);
jQuery('a#inifiniteLoader').hide('1000');
});
//themehunk_owl_carousel();
return false;
}
</script>
<?php
}
?>