$(document).ready(function() {
    //tabview.init();
    if ($("#newsSubtitle").html() != "") {
        $("#newsSubtitle").show();
    }

    getNewsViews(); //显示点击数,并给相应文章增加1个点击
    getNewsList("NewsListLastest", "count=10&titlelen=28&categoryid=" + CATEGORY_ID); //最新文章
    getNewsList("NewsListRecommended", "count=10&titlelen=28&recommend=1&categoryid=" + CATEGORY_ID); //推荐文章
    //NEWS_ID="1";//debug
    $("#title").val("Re:" + $("#newsTitle").html());
    getCommentList(0);

});

function getNewsViews(){
    $.ajax({
        url: '/Ajax.ashx',
        data: 'action=getnewsclicks&newsid=' + NEWS_ID,
        type: 'get',
        dataType: 'text',
        cache: false,
        timeout: 10000,
        beforeSend: function(){
            $("#clicks").html("...");
        },                
        error: function(){
            $("#clicks").html("--");
        },
        success: function(responseText){
            $("#clicks").html(responseText);
        }
    });
}

function getNewsList(showdiv, querystring){
    var jqdiv = $("#" + showdiv);
    $.ajax({
        url: '/Ajax.ashx',
        data: 'action=getnewslist&' + querystring,
        type: 'get',
        dataType: 'json',
        cache: false,
        timeout: 10000,
        beforeSend: function(){
            jqdiv.html("正在装载新闻列表...");
        },                
        error: function(){
            jqdiv.html("尝试装载新闻列表出错!");
        },
        success: function(responseJSON){
            if(!responseJSON.NewsList || responseJSON.NewsList.length<=0){
                jqdiv.html("对不起,暂时没有相应文章!");
                return;
            }
            var builder=[];
            builder.push("<ul class=\"f12list\">");
            for(var i=0; i<responseJSON.NewsList.length; i++){
                var newsinfo = responseJSON.NewsList[i];
                var t="<li><a href=\""+ newsinfo.FilePath +"\" target=\"_blank\" title=\""+ newsinfo.FullTitle +"\">"+ newsinfo.Title +"</a></li>";
                builder.push(t);
            }
            builder.push("</ul>");
            jqdiv.html(builder.join(""));
        }
    });
}

function getCommentList(page){
    var jqdiv = $("#CommentList");
    $.ajax({
        url: '/Ajax.ashx',
        data: 'action=getcommentlist&newsid=' + NEWS_ID + "&page=" + page.toString(),
        type: 'get',
        dataType: 'json',
        cache: false,
        timeout: 20000,
        beforeSend: function(){
            jqdiv.html("正在装载新闻评论列表...");
        },                
        error: function(){
            jqdiv.html("尝试装载新闻评论列表出错!");
        },
        success: function(responseJSON){
            if(!responseJSON.DataList || responseJSON.DataList.length<=0){
                jqdiv.html("沙发为你留着呢，还不抢坐?!");
                return;
            }
            //alert(responseJSON.Totals);
            var num = 0;
            var builder=[];
            builder.push("<h1 style=\"font-size:14px; font-weight:bold; padding-bottom:8px; color:#000\">FeedBack: </h1>");
            for(var i=0; i<responseJSON.DataList.length; i++){
                var info = responseJSON.DataList[i];
                
                var tn = 0;
                tn = ++num;
                var content=info.Content;
                var infos="";
                infos += "&nbsp;&nbsp;<a href=\"javascript:;\" onclick=\"reply_view("+tn+");\">回复</a>";
                infos += "&nbsp;&nbsp;<a href=\"javascript:;\" onclick=\"quote_view("+info.Id+","+tn+");\">引用</a>";
                if(info.UserId>0)
                    infos += "&nbsp;&nbsp;<a href=\"/CommentsByAuthor.aspx?author="+info.UserId+"\">查看</a>"
                if(info.Email && info.Email != '')
                    infos += "&nbsp;&nbsp;<a href=\"mailto:"+info.Email+"\">电子邮箱</a>";
                infos += '<input type="hidden" id="author_'+tn+'" value="';
                if( info.NickName == "匿名" || info.NickName ==""){
                    infos += '访客'+info.Ip+'">';
                    info.NickName = "访客";
                }
                else 
                    infos += info.NickName+'">';
                
                builder.push("<div class=\"commentItem\">");
                builder.push("<div class=\"commentHeader clearfix\">");
                builder.push("    <div class=\"authorFace\"><img src=\""+info.Face+"\" alt=\"\" /></div>");
                builder.push("    <div class=\"postDetails\">");
                builder.push("        <ul>");
                builder.push("            <li>#"+tn+"楼"+infos+"</li>");
                builder.push("            <li>Posted:"+info.CreateDate+" by "+info.NickName+"&nbsp;&nbsp;Form:"+info.Address+"&nbsp;&nbsp;IP:"+info.Ip+" </li>");
                builder.push("        </ul>");
                builder.push("    </div>");
                builder.push("</div>");
                builder.push("<div class=\"commentBody\">"+content+"</div>");
                builder.push("</div>");
            }
            jqdiv.html(builder.join(""));
        }
    });
}

function ContentSize(size){
    var obj=document.getElementById("newscontent");
    obj.style.fontSize=size+"px";
}


function quote_view(id,n){
    $.get("/Ajax.ashx", {action:"getcommentinfo",commentid:id.toString()},function(data, textStatus){
        if(textStatus != "success") {
            return;
        }
        data = $('#author_'+n).val()+":"+data;
        document.getElementById('tbContent').value+= ("--引用--------------------------------------------------\n"+data+"\n--------------------------------------------------------\n");
        document.getElementById('tbContent').focus(); 
    });
}

function reply_view(n){
    var userstr = $('#author_'+n).val();
    document.getElementById('tbContent').value+="@"+userstr+"\n";
    document.getElementById('tbContent').focus();
    return false;
}

function checkcontent(){
    var content = $("#tbContent").val();
    content = content.replace(/(^\s*)|(\s*$)/g, "");
    if( content.length == 0 ){
        alert('评论不能为空,请写点什么吧！');
        $("#tbContent")[0].focus();
        return false;
    }
    return true;
}

function ctrlent(evt){
    var validationPassed = true;
    if(evt.ctrlKey && evt.keyCode==13){
        var content = $("#tbContent").val();
        content = content.replace(/(^\s*)|(\s*$)/g, "");
        if( content.length == 0 ){
            validationPassed=false;
            //return;
        }
        if(validationPassed)
            document.commentform.submit();
        else
            alert('评论信息填写不完整！');
    }
}

function ubb2html(s){
    if(s.match(/\[(\w+)([^\[\]\s]*)\].*\[\/\1\]/)){ 
        s = s.replace(/\[img\]((?:http|ftp|https):\/\/.+?\.(?:gif|jpg|jpeg|png))\[\/img\]/gi, "<img src='$1'>"); 
        s = s.replace(/\[img\](.+?\.(?:gif|jpg|jpeg|png))\[\/img\]/gi, "<img src='$1'>"); 
        s = s.replace(/\[flash\](.+?\.swf)\[\/flash\]/gi, "<embed src=''$1'' quality=high wmode=transparent type=''application/x-shockwave-flash'' width=400 height=300></embed><br> FLASH: <a href=''$1'' target=_blank>$1</a><br>"); 
        s = s.replace(/\[wma\](.+?\.(?:wma|mp3))\[\/wma\]/gi, "<embed src=''$1'' height=40 AutoStart=0></embed><br> WMA: <a href=''$1'' target=_blank>$1</a><br>"); 
        s = s.replace(/\[color=([#0-9a-zA-Z]{1,10})\](.+?)\[\/color\]/gi, "<font color=''$1''>$2</font>"); 
        s = s.replace(/\[b\](.+?)\[\/b\]/gi, "<b>$1</b>"); 
        s = s.replace(/\[i\](.+?)\[\/i\]/gi, "<i>$1</i>"); 
        s = s.replace(/\[url\](.+?)\[\/url\]/gi,"<a href=$1 target=_blank>$1</a>"); 
        s = s.replace(/\[url=(.[^\[]*)\](.[^\[]*)\[\/url\]/gi,"<a  href=''$1''  target=_blank>$2</a>");   
    }
    return s; 	
}