var PageSize = 10;
var CurrentPage = 1;
var status = 1;

$(document).ready(function() {
	BindReview('Page=1');
	
	
});

//绑定数据
function BindReview(urldata){
    $('#reviewtemplate').hide();//将第一行模板隐藏
    $.ajax({
        type:"get",
        dataType:"json",
        url:'MyReview.aspx?NoCopyRight=1&type=ajax&Action=GetList&PageSize=' + PageSize,
        data:urldata,
        cache:false,
        success:function(msg){
             $('div.item:gt(0)','#ReviewContent').remove();
            var data = msg.ProductReview;
            
            $.each(data,function(i,n){
               var row = $('#reviewtemplate').clone().show();
               row.find('.icon').find('img').attr('src',baseUrl +'upload/MemberGroupLogo/' + n.Logo);
               row.find('.username').text(n.MemberName);
               row.find('.groupname').text(n.GroupName);
               
//               row.find('.proname').html(row.find('.proname').html() + '<a href="products/product-' + n.ProductID + '.html" target="_blank">' + n.ProductName + '</a>');
               row.find('.proname').html(row.find('.proname').html() + '<a href="' + GetUrl(n.ReviewType,n.ProductID) + '" target="_blank">' + n.ProductName + '</a>');
               row.find('.revtitle').text(n.Title);
               row.find('.revscore').css('width',12*Number(n.Score) + 'px');
               row.find('.revtime').text(n.AddTime);
               row.find('.revbody').html(n.Body);
               
               var objyesNum = row.find('.yesnum').text(n.YesNum);
               var objnotNum = row.find('.notnum').text(n.NotNum);
               row.find('.vote_y').click(function(){
                    if(Vote(n.ProductReviewID,1) == '1'){
                        objyesNum.text(Number(objyesNum.text())+1);
                    };
               });
               row.find('.vote_n').click(function(){
                    if(Vote(n.ProductReviewID,0) == '1'){
                        objnotNum.text(Number(objnotNum.text())+1);
                    };
               });
               
               //绑定回复
               var replytemplate = row.find('#reptemplate');
               $.each(n.Reply,function(j,m){
                    var replyRow = replytemplate.clone().show();
                    replyRow.find('.replySerial').text((j+1));
                    replyRow.find('.repname').text(m.MemberName + '：');
                    replyRow.find('.repbody').html(m.Body);
                    replyRow.find('.reptime').text(m.ReplyTime);
                    replyRow.attr('id','replyitem_'+(j+1));
                    replytemplate.after(replyRow);
               });
               row.find('.btnreply').click(function(){
                    var submitReplyContainer = $('#ReplySumbit').remove();
                    submitReplyContainer.show().appendTo(row.find('.revinfo'));
                    $('#hidden_reviewID').val(n.ProductReviewID);
                    $('#txtReply').focus();
                    
                    $('#btnReply').click(function(){
	                    $.ajax({
	                        url:baseUrl + 'ProductReview.aspx?NoCopyRight=1&type=Reply',
	                        type:'post',
	                        data:$("#frmReply").serialize(),
	                        success:function(data){
	                            if(data == "NOLOGIN" && confirm('登录后才能回复,现在要登录吗?')){
                                   window.location.href = 'Login.aspx?ReturnUrl=' + returnUrl;
	                            }else if(data > 0){
	                                alert('回复成功');
	                                $('#btnCancel').click();
	                                BindReview('Page=' + CurrentPage);
	                            };
	                        },
	                        error:function(a,b){alert(b);}
	                    });
	                });
	                
	                $('#btnCancel').click(function(){
	                    submitReplyContainer.hide();
	                    $('#txtReply').val('');
	                });
               });//点击回复结束
               
               row.find('.btnviewrep').toggle(function(){
                    $(this).text('关闭回复');
                    row.find('.reply').show();
               },function(){
                    $(this).text('查看回复');
                    row.find('.reply').hide();
               });
               
               row.attr('id','item_'+i).appendTo($('#ReviewContent'));
            });                    
           
            //分页
            $('.sub_container_page').show();
            var pagerStyle = "flickr";//jPagerBar样式
            ShowPageBar("pageDataList1_Pager",//[containerId]
               "ProductReview.aspx",//[url]
               {style:pagerStyle,mark:"pageDataList1Mark",
               totalCount:msg.totalCount,showPageNumber:3,pageCount:PageSize,currentPageIndex:CurrentPage,noRecordTip:" 没有记录",preWord:"上一页",nextWord:"下一页",
               onclick:"CurrentPage={pageindex};BindReview('Page={pageindex}');return false;"}//[attr]
            );
        },//end success
        error:function(XMLHttpRequest,textStatus){alert(textStatus);}
    });//end ajax
};//end funciton

function Vote(paid,val){
    var result = '0';
    $.ajax({
        type:"get",
        url:'ProductReview.aspx?NoCopyRight=1&type=Vote&PRID='+paid+'&val='+val + '&r='+Math.random(),
        async:false,
        success:function(data){
            if(data == 'NOLOGIN' && confirm('登录后才能投票,现在要登录吗?')){
                window.location.href = shopFolder+'Login.aspx?ReturnUrl=' + returnUrl;
                return false;
            };
            result = data;
        },error:function(){alert('error');}
    });
    return result;
};

function GetUrl(reviewType,id){
    var showurl = '';
    switch(reviewType){
        case '1':
            showurl = 'products/product-' + id + '.html';
            break;
        case '2':
            showurl = 'auction/product-' + id + '.html';
            break;
        case '3':
            showurl = 'tgproduct-' + id + '.html';
            break;
        case '4':
            showurl = 'GiftDetails-' + id + '.html'
            break;
    };
    return showurl;
}