(function(window){
Like = {
stat : function( obj ){
jQuery(obj).each( function() {
jQuery(this).html( jQuery(this).attr('data-likes') | '' )
});
return this
},
add : function( obj ){
jQuery(obj).each( function() {
jQuery(this).html( jQuery(this).attr('data-likes') + ' Нравится' )
});
jQuery(obj).click(function(){
var self = this;
var likes = jQuery( self ).attr('data-likes')
jQuery.post( "/api/v2/like", function( data ) {
jQuery( self ).attr('data-likes', ++likes);
jQuery( self ).html( likes + ' Спасибо!')
jQuery( self ).addClass( "liked" )
}).error(function(){
var likes = jQuery( self ).attr('data-likes')
jQuery( self ).html( likes + ' Уже лайкали')
});
})
}
}
Views = {
init : function( obj ){
jQuery(obj).each( function() {
jQuery(this).html( jQuery(this).attr('data-views') )
});
}
}
Comments = {
init : function( obj ){
jQuery(obj).each( function() {
jQuery(this).html( jQuery(this).attr('data-comments') )
});
jQuery(obj).click(function(){
jQuery('html,body').animate( { scrollTop: jQuery("#commentForm").offset().top - 100 }, 'slow' );
})
}
}
Favotire = {
stat : function( obj ){
jQuery(obj).each( function() {
jQuery(this).html( jQuery(this).attr('data-favorites') | '' )
});
return this
},
add : function( obj ){
jQuery(obj).each( function() {
jQuery(this).html( 'В избранное ' )
});
jQuery(obj).click(function(){
var self = this;
var favorites = jQuery( self ).attr('data-favorite') | 0
jQuery.post( "/api/v2/favorite", function( data ) {
jQuery( self ).html( 'Добавлено!')
}).error(function( res ){
if( res.status == 401 ){
jQuery( self ).html( 'Вам необходимо авторизироваться')
}
if( res.status == 402 ){
jQuery( self ).html( 'Уже в избранном')
}
});
})
}
}
window.Like = Like
window.Views = Views
})(window);
jQuery(document).ready(function(){
Views.init(".views")
Comments.init(".comment-button")
Like.stat('.like-button').add('.like-action')
Favotire.add('.favorite-action')
})