$(function () {


    function getParams(url) {
        var theRequest = new Object();
        if (!url)
            url = location.href;
        if (url.indexOf("?") !== -1) {
            var str = url.substr(url.indexOf("?") + 1) + "&";
            var strs = str.split("&");
            for (var i = 0; i < strs.length - 1; i++) {
                var key = strs[i].substring(0, strs[i].indexOf("="));
                var val = strs[i].substring(strs[i].indexOf("=") + 1);
                theRequest[key] = val;
            }
        }
        return theRequest;
    }

    var objUrl = {}; //获取get参数
    var url = window.location.href;


    var params = getParams(url);
    if (!$.isEmptyObject(params)) {
        $.each(params, function (key, val) {
            objUrl[key] = val;
        })
    }
// --------------------------------------------------------------


    $('.m_bm').prev().css('border-bottom', '0');

    var pmheight = $(window).height();
    $('.m_category_list_box').css('height', pmheight);
    $('.m_follow_box').css('height', pmheight);


// 点击关注弹窗
    $('.m_category_xg').on('click', function () {
        $('.m_follow_box').show();
        $(this).hide();
    })

    $('.m_follow_gb').on('click', function () {
        $('.m_category_xg').show();
        $('.m_follow_box').hide();
    })

    $('.m_follow_zez').on('click', function () {
        $('.m_category_xg').show();
        $('.m_follow_box').hide();
    })


    /*----------------定义页面全局变量-----------------------*/
// 页数
// var page = 0;
// // 每页展示5个
// var size = 10;

    var listId = "";  //导航id

    var zxId = 0;    //资讯id

    var isf = true;

    AjaxFn();
    fun();

// 导航ajax--------------------------------
    $('.find_nav_list ul li').on('click', function () {
        var gbdata = $(this).attr('gbid');
        if (gbdata == 1) {
            $('.m_category_list_box').show();
            $(this).attr('gbid', 2).siblings().attr('gbid', 1);
        } else {
            $(this).attr('gbid', 1);
            $('.m_category_list_box').hide();
        }

        listId = $(this).attr('listid');
        // delete objUrl[name];


        ajax_list();

    })

// 关闭
    $('.m_OuterLayer').on('click', function () {
        $('.m_category_list_box').hide();
        $('.find_nav_list ul li').attr('gbid', 1);
    })

    ajax_list();

    function ajax_list() {
        var html = '';
        $.ajax({
            url: "/knowledgebase/getnav",
            data: {id: listId},
            type: "GET",
            dataType: "json",
            success: function (data) {//请求成功完成后要执行的方法
                // console.log(data)
                if (data.code == 200) {
                    $.each(data.data, function (i, data) {    //左边楼盘列表
                        html += '<li zxid="' + data.id + '">' + data.name + '</li>';
                    });

                    var i = null;
                    if (typeof zxId == 'string' && zxId != null) {
                        $.each(data.data, function (k, v) {
                            if (zxId == v.id) {
                                i = v.pid;
                            }
                        })


                        $('.find_nav_list ul li[listid="' + i + '"]').find('a').parent().attr('class', 'find_nav_cur').siblings().attr('class', '');

                        $('.find_nav_list ul li[listid="' + i + '"]').find('a').addClass('on');
                        $('.find_nav_list ul li[listid="' + i + '"]').siblings().find('a').removeClass('on');
                    }

                    $(".m_category_list").html(html);

                    $('.m_category_list li').on('click', function () {

                        $('.m_category_list_box').hide();
                        $('.find_nav_list ul li').attr('gbid', 1);

                        zxId = $(this).attr('zxid');

                        $('.m_category_zx_box').html("");     //切换内容之前先清空盒子

                        // AjaxFn();                 //调用Ajax方法
                        if (!$.isEmptyObject(objUrl)) {
                            window.localStorage.setItem("id", zxId);
                            window.location.href = '/knowledgebase/list';
                        }
                        AjaxFn();                 //调用Ajax方法

                        fun();
                        // console.log(zxId);


                    })
                }
            }
        });
    }

// 导航ajax--------------------------------
// console.log(!$.isEmptyObject(objUrl))

// console.log(txt)


    function AjaxFn() {
        var txt = '';
        // console.log(zxId)
        if (!$.isEmptyObject(objUrl)) {
            txt = '?name=' + objUrl.name
        } else {
            if (isf) {
                if (window.localStorage.getItem("id") != null) {
                    zxId = window.localStorage.getItem("id");
                    window.localStorage.clear();
                }
                isf = false;
            }
            // console.log(zxId)
            txt = '?&id=' + zxId;
        }
        page = 0;
        size = 10;
        page++;
        $.ajax({
            type: 'GET',
            url: '/knowledgebase/getlist' + txt + '&page=' + page + '&limit=' + size,
            // url: '/knowledgebase/getlist?&page='+page+'&limit='+size + '&id='+zxId,
            dataType: 'json',
            success: function (data) {
                if (data.code == 200) {

                    $('.m_category_zx_box').append(ModuleWay(data.data))    //调用选择模块
                    // 报名
                    PublicAction.AjaxSend(
                        {
                            CORID: 'apply_submit', /*操作ID*/
                        }
                    );
                }
                // console.log(data.data);
                // 每次数据加载完,必须重置
                // me.resetload();
            },
            error: function (xhr, type) {
                // alert('Ajax error!');
                // 即使加载出错,也得重置
                //  me.resetload();
            }
        });
        // 上滑加载
        // $('.news_list').dropload({
        //  // 调用加载
        //     scrollArea : window,
        //     threshold:2/3,
        //     loadDownFn : function(me){
        //
        //     }
        // });
    }

// 选择各个模块
    function ModuleWay(data) {

        var html = "";

        $.each(data, function (i, data) {    //左边楼盘列表
            if (data.thumb != null && data.thumb != "" && data.thumb.length == 1) {
                html += '<div class="m_category_zx m_category_zx_bx">';
                html += '<a href="/knowledgebase/details/' + data.id + '" class="c">';

                // _html +='<li><img src="'+conditions.url+val.thumb[k]+'" alt=""></li>';
                html += '<img src=http://pfgimg.pinfangw.com/pc/newsknowledge/' + data.thumb[0] + ' alt="">';
                // html+='<img src="/overseas/img/knowledgebase/zsk10.png" alt="">';
                html += '<div class="m_category_text">';
                html += '<em>' + data.subject + '</em>';
                html += '<p class="c">';
                html += '<i>时间:' + data.create_at + '</i>';
                html += '<i>浏览:' + data.clicks + '</i>';
                html += '</p>';
                html += '</div>';
                html += '</a>';
                html += '</div>';
            } else if (data.thumb != null && data.thumb != "" && data.thumb.length > 1) {

                html += '<div class="m_category_st m_category_zx_bx">';
                html += '<a href="/knowledgebase/details/' + data.id + '">';
                html += '<em>' + data.subject + '</em>';
                html += '<p class="c">';
                html += '<i>来源:' + data.source + '</i>';
                html += '<i>时间:' + data.create_at + '</i>';
                html += '<i>浏览:' + data.clicks + '</i>';
                html += '</p>';
                html += '<ul class="c">';
                for (k in data.thumb) {
                    // _html +='<li><img src="'+conditions.url+val.thumb[k]+'" alt=""></li>';
                    html += '<li><img src=http://images.pinfangge.top/pc/newsknowledge/' + data.thumb[k] + ' alt=""></li>';
                }
                html += '</ul>';
                html += '</a>';
                html += '</div>';
            }

            var _length = $('.m_bm').length;

            if (i == 1 && _length <= 0) {
                // html+='<div class="m_bm c">';
                // html+='<div class="m_bm_l">政策解读</div>';
                // html+='<form class="submit_area">';
                // html+='<input type="hidden" name="aid" value="0"> ';          // 0 为公共报名,其它为楼盘ID
                // html+='<input type="hidden" name="source" value="18">';        //报名来源 具体查看applyVerify.js文件中source 标识说明
                // html+='<input type="hidden" name="equipment" value="1">';     //来源设备 ( PC端  2,手机端   1 )
                // html+='<input type="text" placeholder="请输入您的手机号" name="mobile">';
                // html+='<input type="button" value="立即获取" class="apply_submit">';
                // html+='</form>';
                // html+='</div>';

                html += '<div class="m_bm1 c">';
                html += '<div class="wadlabel" style="top:10px;">广告</div>';
                html += '<img src="/overseas/img/knowledgebase/m_bm1.png" alt="" />';
                html += '<form class="submit_area submit_area1">';
                html += '<input type="hidden" name="aid" value="0"> ';          // 0 为公共报名,其它为楼盘ID
                html += '<input type="hidden" name="source" value="126">';        //报名来源 具体查看applyVerify.js文件中source 标识说明
                html += '<input type="hidden" name="equipment" value="1">';     //来源设备 ( PC端  2,手机端   1 )
                html += '<input type="text" placeholder="请输入您的手机号" name="mobile">';
                html += '<input type="button" value="抢看攻略" class="apply_submit">';
                html += '</form>';
                html += '</div>';

            }

        })
        return html;
    }


    // 判断 objUrl 是否从别到页面跳转到这里
    // if(typeof objUrl.cid == "string"){
    //     zxId = objUrl.cid;
    // }


    function fun() {
        // 上滑加载
        $('#y_mainid').dropload({
            // 调用加载
            scrollArea: window,
            // threshold:2/3,
            loadDownFn: function (me) {
                page++;
                // $('.dropload-down').show();
                $.ajax({
                    type: 'GET',
                    url: '/knowledgebase/getlist?&page=' + page + '&limit=' + size + '&id=' + zxId,
                    dataType: 'json',
                    success: function (data) {
                        // console.log(data);
                        if (data.code == 200) {

                            $('.m_category_zx_box').append(ModuleWay(data.data))    //调用选择模块
                            // 报名
                            PublicAction.AjaxSend(
                                {
                                    CORID: 'apply_submit', /*操作ID*/
                                }
                            );
                        } else {
                            // 锁定
                            me.lock();
                            // 无数据
                            me.noData();

                            // me.resetload();
                        }
                        // alert(data);
                        // 每次数据加载完,必须重置
                        setTimeout(function () {
                            // 插入数据到页面,放到最后面
                            // $('.m_category_zx_box').append(result);
                            // 每次数据插入,必须重置
                            me.resetload();
                        }, 500);
                        $('.dropload-down').eq(0).show().siblings('.dropload-down').remove();


                    },
                    error: function (xhr, type) {
                        // alert('Ajax error!');
                        // 即使加载出错,也得重置
                        // me.resetload();
                    }
                });
            }
        });
    }

})