﻿var gPageSize = 5;
function pageLoad(sender, args) 
{
    if(typeof($("commentPage")) != "undefined")
    {
        RendPageNavigator(comPage,"commentPage","GetPage");
    }
     
}
//得到页面数据
function GetPage(pageIndex)
{
    showProgressBar(true);    
    GetAllComment(pageIndex);
}
//通过页面得到小黑板上面的留言
function GetAllComment(pageIndex)
{
 var body="MethodName=GetCommentOfPage&docid="+gDocID+"&p="+pageIndex+"&s="+gPageSize;
     PostWebRequest($Ajax_GetData$,body,onsucccm,null);
    //WebServices.WWW.Files.GetCommentOfPage(gDocID,pageIndex,gPageSize,onsucccm);
}
//初始化小黑板数据
function onsucccm(result)
{
    if(typeof(result.d) != "undefined")result=result.d;
   RendList(result[0],"divComment");
   RendPageNavigator(result[1],"commentPage","GetPage");
   showProgressBar(false);
}
//下载
function Download()
{
   
    window.open("/learn/document_download.htm?DocID="+gDocID);
}
//删除文件
function DeleteFiles()
{
    showProgressBar(false,true);
    $("$divIput$").style.display = "none";
    var body="<p><strong>"+isDelSure+"</strong></p><br/><div class='popup_button'><input type='button' value='"+C0069+"' id='addOkButton' class='button' onclick=\"opWebService()\" />&nbsp;&nbsp;<input type='button' value='"+gCancel+"' id='addCancelButton' class='buttong' onclick='webFailure()'/></div>";
    var obj = {}
    $ShowMessageBox$(delTitle,body,obj,webFailure,400);    
}
//执行
function opWebService()
{
    $("delID").value = gDocID;
    $("Form1").action = "/my/mysubmissions.htm?ty=0";
    $("Form1").submit();
}
//调用webService方法失败
function webFailure()
{
    $EndMessageBox$();
}

//添加评论
function AddComment()
{
    if(!$ValidButton$())return;
    if(__P_M==""){top.location = LoginHost+"/user/login.htm?IsShow=1&ReturnUrl="+top.location.href;return;}
    $("btnSave").disabled = true;
    showProgressBar(true);
     var body="MethodName=FAddComment&docid="+gDocID+"&desc="+encodeURIComponent($("texta_desc").value)+"&p=1&s="+gPageSize;
     PostWebRequest($Ajax_GetData$,body,onsucc,null);
    //WebServices.WWW.Files.AddComment(gDocID,$("texta_desc").value,gPageSize,1,onsucc,onerror);
    //AsyAddComment(desc);
}
//添加评论执行结果
function onsucc(result)
{
    if(typeof(result.d) != "undefined")result=result.d;
    $("texta_desc").value="";
    $("btnSave").disabled = false;
    showProgressBar(false);
    if(result[2].State)
    {
        RendList(result[0],"divComment");
        RendPageNavigator(result[1],"commentPage","GetPage");
    }
    else
    {
        if(result[2].Msg =="l") window.location = LoginHost+"/user/login.htm?IsShow=1&ReturnUrl="+window.location.href;
    }
}
//替换数据
function RendList(list,divID)
{
    var comDiv = $("CommentList");   
    if(comDiv!=null)
    {
        if(Sys.UI.DomElement.containsCssClass($("CommentList"), "DisplayNone"))
        {
            Sys.UI.DomElement.removeCssClass($("CommentList"), "DisplayNone");
        }
        comDiv.style.display="";
        var domDiv =$(divID);
        if(domDiv!=null)
        {
            if($("CommentItem")==null) return;        
            var item= $("CommentItem").innerHTML;
            var listBuilder = new Sys.StringBuilder();
            var listLength = list.length;
            for (var index = 0; index < listLength; index++) 
            {
                var obj = list[index];                     
                var tmp = item.replace(/\$Avatar\$/gi,"<img src=\""+(obj.Avatar==""?"/images/no_pic.gif":(obj.ImgSite == "0" ? "/UpLoadImages/Avatar/" : $Mosso_ImgAvatar$)+obj.MemberNo+"_Avatar_3.jpg")+"\"/>").replace(/\$MemberNo\$/gi,obj.MemberNo).replace(/\$NickName\$/gi,obj.Nickname);            
                tmp = tmp.replace(/\$sayTime\$/gi,GetFormatDate(gDateFormat,gStrDate,obj.DiffLastUpdate)).replace(/\$Desc\$/gi,EncodeMessage(obj.Desc.replace(/</gi,"&lt;")));            
                listBuilder.append(tmp);
            }
            var sTemp = listBuilder.toString();
             domDiv.innerHTML = sTemp;           
        }
    }
}
function onerror(result)
{
//    $("btnSave").disabled = false;
//    $("MessagesBox").style.display='block';
//    $("MessageImg").src = MessageImageF;
//    $("MessageText").innerHTML = MessageTextF;
}
///
/////////////////////

function RateOver(value)
{
    RateValue = value;
    UpdateRate();
}
function RateOut()
{
    RateValue = sRateValue;
    UpdateRate();
}
function RateClick(value)
{
    if(__P_M=="")
    {
        top.location = LoginHost+"/user/login.htm?IsShow=1&ReturnUrl="+top.location.href;return;
    }
    tmp = sRateValue;
    sRateValue = value;
    RateValue = value;
     var body="MethodName=FRate&pageName="+gDocID+"&v="+value;
     PostWebRequest($Ajax_GetData$,body,RateSucc,null);
    //WebServices.WWW.Files.Rate(gDocID,value,RateSucc,RateError);
     
}

function UpdateRate()
{
    for (var i = 1; i < 6; i++) 
    {
        var starElement=$("ThaiRating_Star_"+i);
        if(starElement==null) return;
        if (RateValue > (i-1)) 
        {
            try
            {
                starElement.setAttribute("class","ratingStar filledRatingStar");
            }
            catch(e)
            {
			    starElement.setAttribute("className","ratingStar filledRatingStar"); 
			} 
        }
        else 
        {
            try
            {
                starElement.setAttribute("class","ratingStar emptyRatingStar");
            }
            catch(e)
            {
			    starElement.setAttribute("className","ratingStar emptyRatingStar");
			}
        }
    }
}
function RateSucc(result)
{
    if(typeof(result.d) != "undefined")result=result.d;
    if(result[0].State)
    {
        var obj1 = $("ThaiRating");
        var obj2 = $("RFen");
        var obj3 = $("RRen");
        obj2.innerHTML = result[1];
        obj3.innerHTML = result[2];
        RateValue = result[3];
        var listBuilder = new Sys.StringBuilder();
        for (var index = 0; index < RateValue; ++index)
        {
            listBuilder.append('<span class="ratingStar savedRatingStar" style="float: left;">&nbsp;</span>');
        }
        for(var index=RateValue;index<5; ++index)
        {
            listBuilder.append('<span class="ratingStar emptyRatingStar" style="float: left;">&nbsp;</span>');
        }
        obj1.innerHTML = listBuilder.toString();
    }
    else
    {
        sRateValue = tmp;
        RateValue = sRateValue;
        UpdateRate();
    }
}
function RateError(result)
{
    sRateValue = tmp;
    RateValue = sRateValue;
    UpdateRate();
}