jQuery(document).ready(function($){

function formatViews(n){

n=parseInt(n)||0;

if(n>=1000000){
return (n/1000000).toFixed(1)+'M';
}

if(n>=1000){
return (n/1000).toFixed(1)+'k';
}

return n.toString();

}

function incrementView(postId){

if(!postId)return;

$.ajax({

url:wprg_views.ajax_url,

type:'POST',

data:{
action:'wprg_increment_view',
post_id:postId,
nonce:wprg_views.nonce
},

success:function(res){

if(res.success){

var views=res.data.views;
var formatted=res.data.formatted_views||formatViews(views);

$('.wprg-thumbnail-views[data-post-id="'+postId+'"] .wprg-views-count').text(formatted);

}

}

});

}

$(document).on('click','.wprg-carousel-item',function(){

var postId=$(this).data('post-id');

incrementView(postId);

});

});
