/* 
* @Author: anchen
* @Date:   2017-02-09 15:09:11
* @Last Modified by:   yubing
* @Last Modified time: 2017-02-10 12:00:00
*/
var contextPath="";
var type="";
var infoId="";
var userId="";

$(document).ready(function(){
	contextPath=$("#contextPath").val();
	type=$("#type").val();
	infoId=$("#infoId").val();
	userId=$("#userId").val();
	
    /*
     * 点击点赞
     */
    $(".js_thumbs").click(function(){
    	var flag="";
        var _this = $(this);
        var praiseNum = parseInt(_this.find(".js_thumbs_text").attr("num"));
        //点赞
        if(!_this.find(".js_thumbs_icon").hasClass("js_praise")) {
            _this.find(".js_thumbs_icon").addClass("js_praise");
            praiseNum = praiseNum + 1;
            flag="1";
        } 
        //取消点赞
        else {
            _this.find(".js_thumbs_icon").removeClass("js_praise");
            praiseNum = praiseNum - 1;
            flag="0";
        }
        _this.find(".js_thumbs_text").attr("num", praiseNum);
        abbreviatedNumber();
        
        $.ajax({
    		url :contextPath+"/wechat/info/infoOperate",
    		type : "POST",
    		data : {actType:"1",id:infoId,type:type,userId:userId,flag:flag},
    		success : function(data) {
    			/*if("success"==data){
    				alert("点赞处理成功");
    			}
    			else{
    				alert("点赞处理失败");
    			}*/
    		},
    		error : function(data) {
    			//alert("处理失败，请检查网络或稍后再试.");
    		}
    	});
    });
    
    /*
     * 点击收藏
     */
    $(".a_collection").click(function(){
    	var flag="";
        var _this = $(this);
        //点赞
        if(!_this.find(".collection-icon").hasClass("js_collection")) {
            _this.find(".collection-icon").addClass("js_collection");
            flag="1";
        } 
        //取消点赞
        else {
            _this.find(".collection-icon").removeClass("js_collection");
            flag="0";
        }
        $.ajax({
    		url :contextPath+"/wechat/info/infoOperate",
    		type : "POST",
    		data : {actType:"2",id:infoId,type:type,userId:userId,flag:flag},
    		success : function(data) {
    			/*if("success"==data){
    				alert("收藏处理成功");
    			}
    			else{
    				alert("收藏处理失败");
    			}*/
    		},
    		error : function(data) {
    			//alert("处理失败，请检查网络或稍后再试.");
    		}
    	});
    });
    
    // 当数字大于万时以“万+”显示
    function abbreviatedNumber(){
        for(var i = 0; i < $(".js_number").length; i++){
            var  figure = parseInt($(".js_number").eq(i).attr("num"));
            var targetVal = 10000;
            if(figure >= targetVal) {
                var amount = parseInt(figure / targetVal);
                $(".js_number").eq(i).html(amount + "万+");
            } else {
                $(".js_number").eq(i).html(figure);
            }
        }
    } abbreviatedNumber();
});