$(document).ready(function() {
	//BindReview('Page=1');
	$('#btnSubmit').click(function(){
	    if($.trim($('#txtTitle').val()) == ''){
	        alert('请填写标题');return false;
	    };
	    if($.trim($('#txtContent').val()) == ''){
	        alert('请填写评论内容');return false;
	    };
	    if($('[@name=rblScore][@checked]').length == 0){
	        alert('请选择评分等级');return false;
	    };
	});
});


//绑定数据
function BindReview(urldata){
    $('#reviewtemplate').hide();//将第一行模板隐藏
	$.ajax({
        type:"get",
        dataType:"json",
        url:baseUrl + 'ProductReview.aspx?NoCopyRight=1&type=GetList&PID=' + productID + '&ReviewType=' + reviewType + '&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 != '' ? n.Logo : 'nopic.gif'));
               row.find('.username').text(n.MemberName);
               row.find('.groupname').text(n.GroupName);
               
               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(){
                    var result = Vote(n.ProductReviewID,1);
                    if(result == '1'){
                        objyesNum.text(Number(objyesNum.text())+1);
                    }else if(result == '2'){
                        alert('不能重复投票!');
                    };
               });
               row.find('.vote_n').click(function(){
                    var result = Vote(n.ProductReviewID,0);
                    if(result == '1'){
                        objnotNum.text(Number(objnotNum.text())+1);
                    }else if(result == '2'){
                        alert('不能重复投票!');
                    };
               });
               
               //绑定回复
               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 = shopFolder + 'Login.aspx?ReturnUrl=' + returnUrl;
	                            }else if(data > 0){
	                                alert('回复成功');
	                                if(!row.find('.reply').is(':visible')){
	                                    row.find('.btnviewrep').click();
	                                }
//	                                row.find('.reply').show();
	                                $.ajax({
	                                    url:baseUrl + 'ProductReview.aspx?NoCopyRight=1&type=GetReply',
	                                    type:'post',
	                                    dataType:'json',
	                                    data:'hidden_reviewID='+$('#hidden_reviewID').val(),
	                                    cache:false,
	                                    success:function(replyData){
//	                                        replytemplate.siblings().remove();
	                                        while(replytemplate.siblings().length != 0){
	                                            replytemplate.siblings().remove();
	                                        }
//	                                        row.find('.reply').children('.reptemplate:gt(0)').remove();
	                                        $.each(replyData.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);
                                           });
	                                    },
	                                    error:function(){alert('error when get reply');}
	                                });
	                                $('#btnCancel').click();
	                            };
	                        },
	                        error:function(a,b){alert(b);}
	                    });
	                });
	                
	                $('#btnCancel').click(function(){
	                    submitReplyContainer.hide();
	                    $('#txtReply').val('');
	                });
               });//点击回复结束
               
//               row.find('.btnviewrep').toggle(function(){
//                    $(this).text('查看回复');
//                    row.find('.reply').hide();
//               },function(){
//                    $(this).text('关闭回复');
//                    row.find('.reply').show();
//               });
               
               row.find('.btnviewrep').click(function(){
                    var replycontent = row.find('.reply');
                    if(replycontent.is(':visible')){
                        replycontent.hide();
                        $(this).text('查看回复');
                    }else{
                        replycontent.show();
                        $(this).text('关闭回复');
                    }
               });
               
               row.attr('id','item_'+i).appendTo($('#ReviewContent'));
            });                    
           
           if(showpage){
                //分页
                $('.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:shopFolder + '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;
};