/**
 *瀑布流模块内数据生成
 */

$(window).load(function () {
    //去除字符串内的html标签方法
    String.prototype.stripHTML = function () {
        var reTag = /<(?:.|\s)*?>/g;
        return this.replace(reTag, "");
    };
    var ajaxObj = new XAjaxRequester();
    var page = 0;
    var limit = 6;
    $("#more_news").unbind("click");
    $("#more_news").click(function () {
        page++;
        var url = "/article/page/#type#/#page#/#limit#";
        article_type = article_type ? article_type : 5; //如果article_type值不存在则表明实在全部新闻页面 //blade页面内定义的全局变量
        url = url.replace("#type#", article_type); //article_type 在blade页面中
        url = url.replace("#page#", page);
        url = url.replace("#limit#", limit);
        // alert( url);
        ajaxObj.get(url, null, function (res) {
            var data = JSON.parse(res).data;
            console.log("return news" + res);
            if (data.length != 0) {

                //首先将事件参数去掉时分秒
                $.each(data, function (index, item) {
                    for (var prop in item) {
                        if (prop == "created_at") {
                            var dateTime = item[prop];
                            item[prop] = dateTime.substr(0, 10);
                        }
                        if (prop == "content_en" || prop == "content_ch") {
                            var dateTime = item[prop];
                            item[prop] = item[prop].stripHTML();
                        }
                    }
                });

                if (location.href.indexOf("english") != -1) {
                    //生成英文列表
                    producePage(
                        "news-lists-cont",
                        "self-define",
                        data,
                        {
                            article_type: "#article_type#",
                            id: "#id#",
                            title_en: "#title#",
                            title_pic: "#image#",
                            created_at: "#dateTime#",
                            content_en: "#description#"
                        },
                        0, 8, false
                    );
                } else {   //生成中文列表
                    producePage(
                        "news-lists-cont",
                        "self-define",
                        data,
                        {
                            article_type: "#article_type#",
                            id: "#id#",
                            title_ch: "#title#",
                            title_pic: "#image#",
                            created_at: "#dateTime#",
                            content_ch: "#description#"
                        },
                        0, 8, false
                    );
                }

                //绑定新闻列表链接
                setAnchor();
            }
            else {
                alert("已没有更多资讯！");
            }

        }, function () {
            alert("请求错误请稍后重试！");
        });
    });

});