/// <reference path="jquery-1.10.2.min.js" />
var pageindex = 1;
var pagesize = 0;
var pagecount = 0;
var pid = 1;

$(function () {
    draw();
    $("#Cv_Code").click(function () {
        context.clearRect(0, 0, 120, 40);
        draw();

    })
    editor = CKEDITOR.replace('Gamebody',
{
    filebrowserImageUploadUrl: "/ajax/Admin_AjaxUpImage.ashx?cmd=uploadImage"
});

    $("#submit").click(function (e) {
        e.preventDefault();
        var gamesid = $.trim($("#gamesid").val());
        var input_val = $.trim($(".input_val").val());
        var text = editor.getData();
        if (text == "") {
            alert("评论内容不能为空");
            return;

        } if (input_val == "") {
            alert("验证码不能为空");
            return;
        } else if (input_val.toUpperCase() != $.cookie('logincode')) {
            alert("验证码不正确");
            return;
        }
        $.post("/ajax/GameBase.ashx", { "type": "PinLun", "text": text, "gamesid": gamesid }, function (data) {
            if (data == "errer") {
                alert("评论内容不能以空格提交");
                return;
            }
            if (data == "No") {
                alert("不能提交敏感词");
                return;
            }
            var data = eval('(' + data + ')');
            if (data.Success == true) {
                alert("评论成功");
                location.reload();

            } else {
                alert("评论失败")

            }






        })


    })





})




    $(function () {
        var gamesid = $.trim($("#gamesid").val());
        $.post("/ajax/GameBase.ashx", { "pageindex": pageindex, "pagesize": pagesize, "type": "PinlunList", "gamesid": gamesid, "pid": pid }, function (data) {
            var data = eval('(' + data + ')');
            pagecount = data.pagecount;
            $("#comment_list").html("").append(data.pagelist);
            layui.use(['laypage', 'layer'], function () {
                var nums = pagesize; //每页出现的数据量
                var laypage = layui.laypage
                , layer = layui.layer;
                laypage.render({
                    elem: 'pager'
                   , count: pagecount //总页数
                   , groups: 1
                    , jump: function (obj) {
                        pageindex = obj.curr;//获取到当前页
                        $.post("/ajax/GameBase.ashx", { "pageindex": pageindex, "pagesize": pagesize, "type": "PinlunList", "gamesid": gamesid, "pid": pid }, function (data) {
                            var data = eval('(' + data + ')');
                            pagecount = data.pagecount;
                            $("#comment_list").html("").append(data.pagelist);

                        });

                    }
                });
            });
        });
    });




    function draw() {
        $.cookie('logincode', null, { path: '/' });
        canvas = document.getElementById("Cv_Code");
        context = canvas.getContext("2d");
        canvas.width = 120;
        canvas.height = 40;
        context.strokeStyle = "#009688";
        context.strokeRect(0, 0, 120, 40);
        var scode = "";
        var aCode = "A,B,C,E,F,G,H,J,K,L,M,N,P,Q,R,S,T,W,X,Y,1,2,3,4,5,6,7,8,9";
        var aLength = aCode.split(",").length;
        for (var i = 0; i <= 3; i++) {
            var x = 20 + i * 20;
            var y = 20 + Math.random() * 10;
            var j = Math.floor(Math.random() * aLength);
            var deg = Math.random() * 90 * Math.PI / 180;//随机弧度
            txt = aCode.split(",")[j];
            scode += txt;
            context.fillStyle = randomRgbColor();
            context.font = "bold 20px 微软雅黑";
            //修改坐标原点和旋转角度
            context.translate(x, y);
            context.rotate(deg);
            context.fillText(txt, 0, 0);
            //恢复坐标原点和旋转角度
            context.rotate(-deg);
            context.translate(-x, -y);
        }
        $.cookie('logincode', scode, { path: '/' });
        //干扰线
        for (var i = 0; i < 8; i++) {
            context.strokeStyle = randomRgbColor();
            context.beginPath();
            context.moveTo(Math.random() * 120, Math.random() * 40);
            context.lineTo(Math.random() * 120, Math.random() * 40);
            context.stroke();
        }
        /**绘制干扰点**/
        for (var i = 0; i < 10; i++) {
            context.fillStyle = randomRgbColor();
            context.beginPath();
            context.arc(Math.random() * 120, Math.random() * 40, 1, 0, 2 * Math.PI);
            context.fill();
        }
    }
    function randomRgbColor() { //随机生成RGB颜色
        var r = Math.floor(Math.random() * 256); //随机生成256以内r值
        var g = Math.floor(Math.random() * 256); //随机生成256以内g值
        var b = Math.floor(Math.random() * 256); //随机生成256以内b值
        return "rgb(" + r + "," + g + "," + b + ")"; //返回rgb(r,g,b)格式颜色
    }



