﻿// ==UserScript==
// @name                资讯详细页
// @description	        设置字体大小,获取资讯阅读评论数支持反对数,更新资讯的支持数和反对数,更新资讯点击数,资讯推荐
// @created date        20090811
// @created author      jianzi
// @version             1.0
// @version owner       Copyright © 2009-2010 宁波千仞网络 All rights reserved.
// ==/UserScript==

/********************************************************************
verified time   : 2009/8/14   10:18
verified author : Martin
verified purpose: 增加评论支持反对（updateCommendSupport、setNewsCommendSupportCookie）
*********************************************************************/
/********************************************************************
verified time   : 2009/9/29   11:23
verified author : Martin
verified purpose: 修改字体大小 16 14 12
*********************************************************************/
/*************************************************
verified time 	:	2009/10/13   8:53
verified author	:	Tiger
verified purpose:	修改了一些适合bylou的id和class，还有一些逻辑上的代码
**************************************************/
/// <summary>
/// 设置字体大小
/// </summary>
$(document).ready(
    function() {
        //初始字体fontsize
        //         
        //         var startSize = $.cookie('fontSize');
        //         startSize = parseFloat(startSize, 12);
        //当前字体fontsize
        var currentFontSize = 14;
        //         $(".indent").css('font-size', currentFontSize);
        //         setNewsContentFontSize(startSize);
        $(".news_main_liinfotuijian a").click(
         function() {
             switch ($(this).text()) {
                 case "大":
                     currentFontSize = $(".indent").css('font-size');
                     if (currentFontSize != 16) {
                         setNewsContentFontSize(16);
                     }
                     break;
                 case "中":
                     currentFontSize = $(".indent").css('font-size');
                     if (currentFontSize != 14) {
                         setNewsContentFontSize(14);
                     }
                     break;
                 case "小":
                     currentFontSize = $(".indent").css('font-size');
                     if (currentFontSize != 12) {
                         setNewsContentFontSize(12);
                     }
                     break;
             }
         });
    }
 );

/// <summary>
/// 设置资讯字体fontsize以及子标签的字体fontsize
/// </summary>
/// <param name="fontsizeint">要设置的字体fontsize</param>
function setNewsContentFontSize(fontsizeint) {
    //子标签集合
    var newsChildrenContents = $(".indent").children();
    $(".indent").css('font-size', fontsizeint);
    for (var i = 0; i < newsChildrenContents.length; i++) {
        setNewsContentFontSizeEach(newsChildrenContents[i].children, fontsizeint + "px");
    }
}

/// <summary>
/// 循环子标签并且设置字体fontsize
/// </summary>
/// <param name="newsChildrenContext">子集合</param>
/// <param name="fontsizepx">要设置的字体fontsize像素</param>
function setNewsContentFontSizeEach(newsChildrenContext, fontsizepx) {
    for (var i = 0; i < newsChildrenContext.length; i++) {
        if (newsChildrenContext[i].children.length > 0) {
            setNewsContentFontSizeEach(newsChildrenContext[i].children, fontsizepx);
        }
        else {
            newsChildrenContext[i].style.fontSize = fontsizepx;
        }
    }
}
//window.onbeforeunload = leaveCookie;

///// <summary>
///// 保存当前前面字体fontsize到Cookie
///// </summary>
//function leaveCookie()
//{ 
//    //当前字体fontsize
//    var FontSize = $(".indent").css('font-size');
//    //字体fontsize
//    var IntFontSize = parseFloat(FontSize, 12);
//    $.cookie('fontSize', IntFontSize);
//}

/// <summary>
/// 获取资讯阅读、评论数和支持反对数
/// </summary>
/// <param name="newsID">资讯ID，有页面的onload时传入</param>
/// <param name="type">不同页面的type输入</param>
function GetNewsReadAndCommentNum(newsID, type) {
    $.ajax({
        url: '/ajax/GetJson.aspx',
        type: 'POST',
        dataType: 'text',
        timeout: 3000,
        data: { type: type, newsID: newsID },
        cache: false,
        error: function() {
            $($(".news_zixun_xx .yellow")[0]).text("0"); //次阅读");
            $($(".news_zixun_xx .yellow")[1]).text("0");
        },
        success: function(str) {
            var readandCommentJson = $.evalJSON(str);
            // 在页面onload的时候添加阅读数和评论数
            $($(".news_zixun_xx .yellow")[0]).text(readandCommentJson.crt); // + "次阅读");
            $($(".news_zixun_xx .yellow")[1]).text(readandCommentJson.comment); // + "个评论");
            // 在页面onload的时候添加支持数和反对数
            $(".dingcai .dingcainub:first").text(readandCommentJson.supportcrt);
            $(".dingcai .dingcainub:last").text(readandCommentJson.nosupportcrt);
            // 再次update阅读数
            //updateNewsCrt(newsID);
        }
    });
}

/// <summary>
/// 更新资讯的支持数和反对数
/// </summary>
/// <param name="newsID">资讯ID</param>
/// <param name="SupportOrNo">支持或则反对，1是支持，0是反对</param>
function updateSupport(newsID, SupportOrNo, type) {
    if (setnewsSupportCookie(newsID, SupportOrNo)) {
        $.ajax(
        {
            url: '/ajax/GetJson.aspx',
            type: 'POST',
            dataType: 'text',
            timeout: 3000,
            data: 'type='+ type +'&newsID=' + newsID + '&SorN=' + SupportOrNo,
            cache: false,
            error: function() {
            },
            success: function(str) {
                if (SupportOrNo == 1) {
                    //jAlert("感谢您的支持", "资讯");
                    $(".dingcai .dingcainub:first").text(str);
                }
                else {
                    //jAlert("感谢您的支持", "资讯");
                    $(".dingcai .dingcainub:last").text(str);
                }
            }
        });
    }
    else {
        if (SupportOrNo == 1) {
            //jAlert("感谢您的支持,一天只能支持一次", "资讯");
        }
//        else {
//            //jAlert("感谢您的支持,一天只能反对一次", "资讯");
//        }
    }
}

/// <summary>
/// 更新资讯点击数
/// </summary>
/// <param name="newsID">资讯ID</param>
function updateNewsCrt(newsID) {
    $.ajax(
      {
          url: '/ajax/GetJson.aspx',
          type: 'POST',
          dataType: 'text',
          timeout: 3000,
          data: 'type=UpdateNewsCRT&newsID=' + newsID,
          cache: false,
          error: function() {
          },
          success: function(str) {
              $($(".news_zixun_xx .yellow")[0]).text(str + "次阅读");
          }
      });
}

/// <summary>
/// 设置资讯支持和反对数的cookie
/// </summary>
/// <param name="newsID">资讯ID</param>
/// <param name="SupportOrNo">支持或则反对</param>
function setnewsSupportCookie(newsID, supportType) {
    if ($.cookie("newsSupportOrNoSupportCookie") == null) {
        $.cookie("newsSupportOrNoSupportCookie", "[{\"_newsID\":\"" + newsID + "\",\"_supportOrNo\":\"" + supportType + "\"}]");
        return true;
    }
    else {
        var newsSupportOrNoSupportJosn = $.evalJSON($.cookie("newsSupportOrNoSupportCookie"));
        var newsSupportOrNoSupport = "";
        for (var i = 0; i < newsSupportOrNoSupportJosn.length; i++) {
            if (newsSupportOrNoSupportJosn[i] != undefined) {
                var _supportOrNo = Number(newsSupportOrNoSupportJosn[i]._supportOrNo);
                if (newsSupportOrNoSupportJosn[i]._newsID == newsID && _supportOrNo == supportType) {
                    return false;
                }
                newsSupportOrNoSupport = newsSupportOrNoSupport + "{\"_newsID\":\"" + newsSupportOrNoSupportJosn[i]._newsID + "\",\"_supportOrNo\":\"" + newsSupportOrNoSupportJosn[i]._supportOrNo + "\"},";
            }
        }
        newsSupportOrNoSupport = newsSupportOrNoSupport + "{\"_newsID\":\"" + newsID + "\",\"_supportOrNo\":\"" + supportType + "\"},";
        newsSupportOrNoSupport = "[" + newsSupportOrNoSupport + "]";
        $.cookie("newsSupportOrNoSupportCookie", newsSupportOrNoSupport);
        return true;
    }
}

/// <summary>
/// 更新资讯列表页的支持数和反对数
/// </summary>
/// <param name="newsID">资讯ID</param>
/// <param name="SupportOrNo">支持或则反对，1是支持，0是反对</param>
function updateSupportList(newsID, SupportOrNo, type) {
    if (setnewsSupportCookie(newsID, SupportOrNo)) {
        $.ajax(
        {
            url: '/ajax/GetJson.aspx',
            type: 'POST',
            dataType: 'text',
            timeout: 3000,
            data: 'type=' + type + '&newsID=' + newsID + '&SorN=' + SupportOrNo,
            cache: false,
            error: function() {
            },
            success: function(str) {
                if (SupportOrNo == 1) {
                    //jAlert("感谢您的支持", "资讯");
                    $("#ding_" + newsID).text(str);
                }
                else {
                    //jAlert("感谢您的支持", "资讯");
                    $("#cai_" + newsID).text(str);
                }
            }
        });
    }
    else {
        if (SupportOrNo == 1) {
            //jAlert("感谢您的支持,一天只能支持一次", "资讯");
        }
        //        else {
        //            //jAlert("感谢您的支持,一天只能反对一次", "资讯");
        //        }
    }
}

/// <summary>
/// 资讯推荐
/// </summary>
/// <param name="newsID">资讯ID</param>
function NewsRecommend(newsID) {
    var usrid = 0;
    if (_UserOrCustomer == 2 && _OnUserIDOrCustomerID > 0)
        usrid = _OnUserIDOrCustomerID;
    if (_UserOrCustomer == 1 && _OnUserIDOrCustomerID > 0)
        GetHeadLoginDiv("此操作需登录个人会员后才能进行!", "资讯推荐", "NewsRecommend('" + newsID + "');", '2', '<div class="dcbox"></div>');
    if (usrid > 0) {
        GetHeadLoginDiv("", "资讯推荐", "", '2', '<div class="dcbox"></div>');
        ShowNewsRecom(usrid, newsID);
    }
    else {
        GetHeadLoginDiv("此操作需登录个人会员后才能进行!", "资讯推荐", "NewsRecommend('" + newsID + "');", '2', '<div class="dcbox"></div>');
    }
}
/// <summary>
/// 现在好友列表
/// </summary>
/// <param name="userid">当前用户ID</param>
/// <param name="newsID">资讯ID</param>
function ShowNewsRecom(userid, newsID) {
    $.ajax({
        url: '/ajax/GetJson.aspx',
        type: 'post',
        dataType: 'text',
        data: 'type=getUserFriendsForNews&userid=' + userid + '&newsID=' + newsID,
        timeout: 3000,
        cache: false,
        error: function() {
            //jAlert("对不起，服务器超时", "资讯推荐");
        },
        success: function(str) {
            if (str != '') {
                $(".dcbox").html(str);
            }
//            else
//                //jAlert("对不起，当前你无好友！", "资讯推荐");
        }
    });
}
/// <summary>
/// 全选反选好友
/// </summary>
/// <param name="o">好友</param>
function cball(o) {
    var ns = document.getElementsByName("cbfriends");
    for (var i = 0; i < ns.length; i++) {
        ns[i].checked = o.checked;
    }
}
/// <summary>
/// 推荐资讯给好友
/// </summary>
/// <param name="userid">当前用户ID</param>
/// <param name="newsID">资讯ID</param>
function DoNewsRecommend(userid, newsID) {
    var usrs = "";
    var ns = document.getElementsByName("cbfriends");
    for (var i = 0; i < ns.length; i++) {
        if (ns[i].checked) usrs += "," + ns[i].value;
    }
    if (usrs.length < 1) {
        //jAlert("请选择您的好友", "未选择好友");
        return;
    }
    usrs = usrs.substring(1);
    $.ajax({
        url: '/ajax/GetJson.aspx',
        type: 'POST',
        dataType: 'text',
        timeout: 3000,
        data: 'type=NewsRecommend&usrid=' + userid + '&newsID=' + newsID + '&usrs=' + usrs,
        cache: false,
        error: function() {
        },
        success: function(str) {
            if (str == "1") {
                //jAlert("推荐成功", "资讯推荐");
                setTimeout(function() {
                    $(".boxy-wrapper").hide().remove();
                    $(".boxy-modal-blackout").remove();
                }, 2000);
            }
//            else if (str == "2")
//                //jAlert("此资讯已推荐，无需重复推荐", "已推荐");
//            else
//                //jAlert(str, "推荐失败");
        }
    });
}

/// <summary>
/// 打印功能
/// </summary>
function NewsPrint() {
    $("div.clear .news_main_liinfotopb .indent").printArea();
}

/// <summary>
/// 设置资讯评论的支持和反对数的cookie
/// </summary>
/// <param name="newsCommendID">资讯评论ID</param>
/// <param name="supportType">支持或则反对</param>
function setNewsCommendSupportCookie(newsCommendID, supportType) {
    if ($.cookie("newsCommendSupportOrNoSupportCookie") == null) {
        $.cookie("newsCommendSupportOrNoSupportCookie", "[{\"_newsCommendID\":\"" + newsCommendID + "\",\"_supportOrNo\":\"" + supportType + "\"}]");
        return true;
    }
    else {
        var newsCommendSupportOrNoSupportJosn = $.evalJSON($.cookie("newsCommendSupportOrNoSupportCookie"));
        var newsCommendSupportOrNoSupport = "";
        for (var i = 0; i < newsCommendSupportOrNoSupportJosn.length; i++) {
            var _supportOrNo = Number(newsCommendSupportOrNoSupportJosn[i]._supportOrNo);
            if (newsCommendSupportOrNoSupportJosn[i]._newsCommendID == newsCommendID && _supportOrNo == supportType) {
                return false;
            }
            newsCommendSupportOrNoSupport = newsCommendSupportOrNoSupport + "{\"_newsCommendID\":\"" + newsCommendSupportOrNoSupportJosn[i]._newsCommendID + "\",\"_supportOrNo\":\"" + newsCommendSupportOrNoSupportJosn[i]._supportOrNo + "\"},";
        }
        newsCommendSupportOrNoSupport = newsCommendSupportOrNoSupport + "{\"_newsCommendID\":\"" + newsCommendID + "\",\"_supportOrNo\":\"" + supportType + "\"},";
        newsCommendSupportOrNoSupport = "[" + newsCommendSupportOrNoSupport + "]";
        $.cookie("newsCommendSupportOrNoSupportCookie", newsCommendSupportOrNoSupport);
        return true;
    }
}
/// <summary>
/// 更新资讯评论的支持数和反对数
/// </summary>
/// <param name="newsCommendID">资讯评论ID</param>
/// <param name="SupportOrNo">支持或则反对</param>
function updateNewsCommendSupport(newsCommendID, SupportOrNo) {
    if (setNewsCommendSupportCookie(newsCommendID, SupportOrNo)) {
        $.ajax({
            url: '/ajax/GetJson.aspx',
            type: 'POST',
            dataType: 'text',
            timeout: 3000,
            data: 'type=UpdateNewsCommendSupportOrNo&newsCommendID=' + newsCommendID + '&SorN=' + SupportOrNo,
            cache: false,
            error: function() {
            },
            success: function(str) {
                if (SupportOrNo == 1) {
                    //jAlert("感谢您的支持", "资讯评论");
                    $("#NewsComment" + newsCommendID + "_Support").html(str);
                }
                else {
                    //jAlert("感谢您的支持", "资讯评论");
                    $("#NewsComment" + newsCommendID + "_NoSupport").html(str);
                }
            }
        });
    }
    else {
        if (SupportOrNo == 1) {
            //jAlert("感谢您的支持,一天只能对同一评论支持一次", "资讯评论");
        }
        else {
            //jAlert("感谢您的支持,一天只能对同一评论反对一次", "资讯评论");
        }
    }
}



/// <summary>
/// 设置晓得回答的支持和反对数的cookie
/// </summary>
/// <param name="newsCommendID">资讯评论ID</param>
/// <param name="supportType">支持或则反对</param>
function setxdCommendSupportCookie(qaid, supportType) {
    //debugger;
    if ($.cookie("xdCommendSupportOrNoSupportCookie") == null) {
        $.cookie("xdCommendSupportOrNoSupportCookie", "[{\"_xdCommendID\":\"" + qaid + "\",\"_supportOrNo\":\"" + supportType + "\"}]");
        return true;
    }
    else {
        var xdCommendSupportOrNoSupportJosn = $.evalJSON($.cookie("xdCommendSupportOrNoSupportCookie"));
        var xdCommendSupportOrNoSupport = "";
        for (var i = 0; i < xdCommendSupportOrNoSupportJosn.length; i++) {
            if (xdCommendSupportOrNoSupportJosn[i]!=undefined){
                var _supportOrNo = Number(xdCommendSupportOrNoSupportJosn[i]._supportOrNo);
                if (xdCommendSupportOrNoSupportJosn[i]._xdCommendID == qaid && _supportOrNo == supportType) {
                    return false;
                }
                xdCommendSupportOrNoSupport = xdCommendSupportOrNoSupport + "{\"_xdCommendID\":\"" + xdCommendSupportOrNoSupportJosn[i]._xdCommendID + "\",\"_supportOrNo\":\"" + xdCommendSupportOrNoSupportJosn[i]._supportOrNo + "\"},";
            }
        }
        xdCommendSupportOrNoSupport = xdCommendSupportOrNoSupport + "{\"_xdCommendID\":\"" + qaid + "\",\"_supportOrNo\":\"" + supportType + "\"},";
        xdCommendSupportOrNoSupport = "[" + xdCommendSupportOrNoSupport + "]";
        $.cookie("xdCommendSupportOrNoSupportCookie", xdCommendSupportOrNoSupport);
        return true;
    }
}
/// <summary>
/// 更新晓得回答的支持数和反对数
/// </summary>
/// <param name="newsCommendID">资讯评论ID</param>
/// <param name="SupportOrNo">支持或则反对</param>
function updatexdCommendSupport(qaid, SupportOrNo) {
    if (setxdCommendSupportCookie(qaid, SupportOrNo)) {
        $.ajax({
            url: '/ajax/GetJson.aspx',
            type: 'POST',
            dataType: 'text',
            timeout: 3000,
            data: 'type=UpdatexdCommendSupportOrNo&id=' + qaid + '&SorN=' + SupportOrNo,
            cache: false,
            error: function() {
            },
            success: function(str) {
                if (SupportOrNo == 1) {
                    //jAlert("感谢您的支持", "资讯评论");
                    $("#xdComment" + qaid + "_Support").html(str);
                }
                else {
                    //jAlert("感谢您的支持", "资讯评论");
                    $("#xdComment" + qaid + "_NoSupport").html(str);
                }
            }
        });
    }
    else {
        if (SupportOrNo == 1) {
            //jAlert("感谢您的支持,一天只能对同一评论支持一次", "资讯评论");
        }
        else {
            //jAlert("感谢您的支持,一天只能对同一评论反对一次", "资讯评论");
        }
    }
}

/// <summary>
/// 更新晓得回答点击数
/// </summary>
/// <param name="newsID">资讯ID</param>
function UpdatexdCRT(qaid) {
    $.ajax(
      {
          url: '/ajax/GetJson.aspx',
          type: 'POST',
          dataType: 'text',
          timeout: 3000,
          data: 'type=UpdatexdCRT&id=' + qaid,
          cache: false,
          error: function() {
          },
          success: function(str) {
          $("#xd_crt").text(str);
          }
      });
}
