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 osi = $(".siwh ul li").eq(0).attr('data-id');

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

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

/*----------------------------------------------------------------------------------*/

// 封装 ajax 方法
function AjaxFn(){
    page = 0;
    page++;
    $.ajax({
        type: 'GET',
        url: '/news/homeform?&page='+page+'&limit='+size + '&cid='+thisID,
        dataType: 'json',
        success: function(data){
            if(data.code == 200){
                    $('.news_list .ns').append(ModuleWay(data.data))    //调用选择模块
            }
        },
        error: function(xhr, type){
        }
    });
}



function ModuleWay(conditions){
        var _html="";
        $.each(conditions.data,function (key,val) {
          
                _html +='<div class="plan_one">';
                _html +='<a href="/news/details/'+val.id+'/">';
                _html +='<div class="plan_one_title">';
                _html +='<p class="text">'+val.subject+'</p>';
                _html +=' <p> <span class="guis">'+val.source+' </span><i>·</i><span class="data">'+val.open_time+'</span></p>';
                _html +='</div>';
                _html +='<div class="plan_one_img"><img src="'+conditions.url+val.thumb[0]+'" alt=""></div>';
                _html +='</a>';
                _html +='</div>';

        })

        return _html ;
}


$(function(){

    thisID=$('#demo06 ul li').eq(0).attr('data-id');

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

    var ali= 0;

    // 点击切换导航栏 ajax 切换
    $(".siwh ul").on('click','li',function(){
        $('.news_list .dropload-down').remove();
        var $this = $(this);
        thisID = $this.attr('data-id');
        $(".siwh ul li").removeClass('cur');
        $('.siwh ul li[data-id="'+thisID+'"]').addClass('cur');

        osi = thisID;

        $('.news_list .ns').html("");     //切换内容之前先清空盒子
        AjaxFn();                 //调用Ajax方法
        fun();
    })

    function fun(){
         // 上滑加载
        $('.news_list').dropload({
            // 调用加载
            scrollArea : window,
            threshold:4/10,
            loadDownFn : function(me){
                page++;
                $.ajax({
                    type: 'GET',
                    url:  '/news/homeform?&page='+page+'&limit='+size + '&cid='+thisID,
                    dataType: 'json',
                    success: function(data){
                        // console.log(data);
                        if(data.code == 200){
                                $('.news_list .ns').append(ModuleWay(data.data))    //调用选择模块

                                //限制字符个数
                                $(".news_list .plan_one").each(function(){
                                        var maxwidth=35;
                                        var _lenght = $(this).find('.plan_one_title .text').text().length
                                        var that = $(this).find('.plan_one_title .text')
                                        if(_lenght > maxwidth){
                                          that.text(that.text().substring(0,maxwidth));
                                          that.html(that.html()+'…');
                                        }
                                });

                        }else{
                                // 锁定
                                me.lock();
                                // 无数据
                                me.noData();
                        }
                        // alert(data);
                        // 每次数据加载完,必须重置
                        setTimeout(function(){
                            // 插入数据到页面,放到最后面
                            // $('.news_list').append(result);
                            // 每次数据插入,必须重置
                            me.resetload();
                        },1000);
                        // me.resetload();
                        // $('.dropload-down').hide();
                    },
                    error: function(xhr, type){
                        // alert('Ajax error!');
                        // 即使加载出错,也得重置
                        me.resetload();
                    }
                });
            }
        });
    }


    fun();

    
    
})


var mySwiper = new Swiper ('.swiper-container2', {
    autoplay: true,//可选选项,自动滑动
    speed:300,
    loop : true,
    autoplay:5000,//等同于以下设置
    pagination: '.swiper-pagination'

})

//demo示例六 通过id调取
$('#demo06').navbarscroll({
    defaultSelect:0,
    scrollerWidth:6,
    fingerClick:1,
    endClickScroll:function(obj){
        // $(".siwh ul li").removeClass('cur');
        // $('.siwh ul li[data-id="'+osi+'"]').addClass('cur');
    }
});


$(window).scroll(function (){
    var st = $(this).scrollTop();
    // console.log(st)
    if(st >300){
        $('.find_nav').show();
        //demo示例六 通过id调取
        $('#demo05').navbarscroll({
            defaultSelect:0,
            scrollerWidth:6,
            fingerClick:1,
            endClickScroll:function(obj){
                $(".siwh ul li").removeClass('cur');
                $('.siwh ul li[data-id="'+osi+'"]').addClass('cur');
            }
        });
    }else{
        $('.find_nav').hide();
    }
});