var G_biz = 70;

var G_NewsDetail = '/m/m201712/detail.shtml';

var G_Source = 'web_m';



function nloadScript(url, callback) {

    var script = document.createElement("script");

    script.type = "text/javascript";

    if (script.readyState) {

        script.onreadystatechange = function() {

            if (script.readyState == "loaded" || script.readyState == "compvare") {

                script.onreadystatechange = null;

                callback && callback();
               
            }

        };

    } else {

        script.onload = function() {

            callback && callback();

        };

    }

    script.src = url;

    document.getElementsByTagName("head")[0].appendChild(script);

}



function ReloadPubdate(string) {

    var re = /^(\d{2,4})-(\d{1,2})-(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/;



    if( re.test(string) ) {

        var t = string.match(re);

         return t[2] +"/"+t[3]



    }



}

function getUrlParam(url, parm) {

    console.log(url)

    var reg = new RegExp("(^|&)" + parm + "=([^&]*)(&|$)");

    var r = url.substr(url.indexOf("\?") + 1).match(reg);

    if (r != null) return unescape(r[2]);

    return null;

}

//







var page = 0,iType = null,thisiType=null,curpage = 1,totalpage = 0,scrollDisable=false,tindex=0,sindex=0;



function setVideo(page,type,tindex){



    type || (type = 4751);

    iType = type;

    tindex || (tindex= 0);

    page = page || curpage;

 console.log(tindex)

   scrollDisable = true;

    var url = "//apps.game.qq.com/wmp/v3.1/?p0=70&p1=searchKeywordsList&page="+page+"&pagesize=6&order=sIdxTime&r0=script&r1=userobj&type=iTag&type=&id="+type+"&source="+G_Source;

    nloadScript(url,function(){

        var data = userobj;

        var list = data.msg.result;

         totalpage = data.msg.totalpage;

         var RetHTML = "";

          if(list){

              for(var x in list){

                  RetHTML += '<li>';

                  RetHTML += '  <a class="v_pic" href="'+G_NewsDetail+'?vid='+list[x].iVideoId+'" onclick="PTTSendClick(\'link\',\'video_list_icon'+tindex+'\',\'视频筛选-'+x+'\')">';

                  RetHTML += '   <img src="'+list[x].sIMG+'">';

                  RetHTML += '   <i class="v_mask"></i>';

                  RetHTML += '    <i class="btns_video"></i>';

                  RetHTML += '   <p class="v_infor">';

                  RetHTML += '     <span class="v_time">'+ReloadPubdate(list[x].sCreated)+'</span>';

                  RetHTML += '     <span class="v_num">'+list[x].iTotalPlay+'</span>';

                  RetHTML += '   </p>';

                  RetHTML += ' </a>';

                  RetHTML += ' <a class="v_tlt" href="'+G_NewsDetail+'?vid='+list[x].iVideoId+'" onclick="PTTSendClick(\'link\',\'video_list_href'+tindex+'\',\'视频筛选-'+x+'\')">'+list[x].sTitle+'</a>';

                  RetHTML += '</li>';

              }



               //console.log(tindex+"..."+iType+".."+totalpage+".."+page+".."+curpage);

              if(curpage == totalpage){

                 $('.loading-move').text('内容已全部加载完');

              }else{

                 $('.loading-move').text('下滑加载更多');

              }

              scrollDisable = false;



          }else{

            $('.loading-move').text('暂时没有内容');



          }

             

           if(page>1){

              $('#liid'+Number(tindex+1)+' li').eq(0).find("ul.video_list").append(RetHTML);

              }else{

               $('#liid'+Number(tindex+1)+' li').eq(0).find("ul.video_list").html(RetHTML);

            }





    })





}



// 搜索



function setsearchVideo(page,txt,order){

      order || (order = "sIdxTime");
      page = page || curpage;
      scrollDisable = true;
      var url="https://apps.game.qq.com/wmp/v3.1/?p1=searchIso&p0=70&r0=script&r1=userobj&order="+order+"&pagesize=6&page="+page+"&type=iTag&source=web_pc&p2="+txt+"&p3=VIDEO";

      nloadScript(url,function(){

      var data = userobj;

      var list = data.msg.result;

      totalpage = data.msg.totalpage;

      var RetHTML = "";

      $('.loading-move').show();

      if(list){
      for(var x in list){

            RetHTML += '<li>';

            RetHTML += '  <a class="v_pic" href="'+G_NewsDetail+'?vid='+list[x].iVideoId+'" onclick="PTTSendClick(\'link\',\'search_list_icon'+order+'\',\'搜索-'+x+'\')">';

            RetHTML += '   <img src="'+list[x].sIMG+'">';

            RetHTML += '   <i class="v_mask"></i>';

            RetHTML += '    <i class="btns_video"></i>';

            RetHTML += '   <p class="v_infor">';

            RetHTML += '     <span class="v_time">'+ReloadPubdate(list[x].sCreated)+'</span>';

            RetHTML += '     <span class="v_num">'+list[x].iTotalPlay+'</span>';

            RetHTML += '   </p>';

            RetHTML += ' </a>';

            RetHTML += ' <a class="v_tlt" href="'+G_NewsDetail+'?vid='+list[x].iVideoId+'" onclick="PTTSendClick(\'link\',\'search_list_href'+order+'\',\'搜索-'+x+'\')">'+list[x].sTitle+'</a>';

            RetHTML += '</li>';
        }

      }else{

       RetHTML="<li>查不到内容呢</li>";

        $('.loading-move').hide();

      }

       

       if(page>1){

        $('#liid'+Number(tindex+1)+' li').eq(0).find("ul.video_list").append(RetHTML);

        }else{

         $('#liid'+Number(tindex+1)+' li').eq(0).find("ul.video_list").html(RetHTML);

        }



        if(curpage == totalpage){

           $('.loading-move').text('内容已全部加载完');

        }else{

           $('.loading-move').text('下滑加载更多');

        }

        scrollDisable = false;

      })

 }







      iType = getUrlParam(window.location.search,'vid');

      if(iType=='12629'){
         $('.video_c_tab li').eq(0).addClass('on');
          $('.video_c_box .swiper-box').eq(0).addClass('on');
            tindex=0;

      }else if(iType=='12630'){
          $('.video_c_tab li').eq(1).addClass('on');
          $('.video_c_box .swiper-box').eq(1).addClass('on');
           tindex=1;
      }else if(iType=='12632'){
         $('.video_c_tab li').eq(2).addClass('on');
          $('.video_c_box .swiper-box').eq(2).addClass('on');
        tindex=2;
      }else if(iType=='12633'){
         $('.video_c_tab li').eq(3).addClass('on');
          $('.video_c_box .swiper-box').eq(3).addClass('on');

           tindex=3;

      }

    setVideo(1,iType,tindex);





    $(".video_c_tab li").click(function(){

            $(this).addClass("on").siblings().removeClass("on");

              tindex=$(this).index();
                
              iType = $('.video_c_box_tab_'+(tindex+1)).find('.swiper-pagination-bullet-active').data('id');

             if(!iType){//第一个子频道
                     $('.video_c_box_tab_'+(tindex+1)+ ' li').eq(0).addClass('swiper-pagination-bullet-active');
                  iType = $('.video_c_box_tab_'+(tindex+1)+ ' li').eq(0).data('id');
              }

            

       $(".video_c_box .swiper-box").eq(tindex).addClass("on").siblings().removeClass("on");

           PTTSendClick('btn','video_c_tab',''+$(this).html()+'');

           var n=null;

             for(var i=0;i<tindex-1;i++){

                 n+=$('.video_c_tab li').eq(i).width()

             }

             $(".video_c_tab").animate({ scrollLeft: n }, 300);

       curpage=1;

      setVideo(curpage,iType,tindex);



    });







  $(".video_tab li").click(function(){

       var sindex =$(this).index();
       var parentNum= $(this).parent().parent().index();
         $(".video_c_tab li").eq(parentNum).addClass('on').siblings().removeClass('on');
       PTTSendClick('btn','video_tab',''+$(this).html()+'');

        $(this).addClass("swiper-pagination-bullet-active").siblings().removeClass("swiper-pagination-bullet-active");

              iType = $(this).data('id');



              setVideo(1,iType,tindex);

               var n=null,cname;

               if(tindex==0){cname=  'video_c_box_tab_1'}else if(tindex==1){cname=  'video_c_box_tab_2'}else{ cname='video_tab'};

           for(var i=0;i<sindex;i++){

               n+=$('.'+cname+' li').eq(i).width()

           }

           //console.log(n+"..."+sindex+".."+n)



           $('.'+cname).animate({ scrollLeft: n }, 300);





  })









//搜索



function searchvideo(){

 var txt = $('.search_input').val();

    if(txt){

         $(".video_c_tab li").removeClass('on');

         $(".video_tab li").removeClass('swiper-pagination-bullet-active');

         curpage=1;

         setsearchVideo(curpage,txt);

    }



}

 $(function(){

// 滚动加载

   $(window).scroll(function(){

            var documentHeight = document.documentElement.offsetHeight;

            var viewPortHeight =window.screen.height;

            var scrollHeight = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0;

            var order;

        //  console.log(documentHeight +"..."+ viewPortHeight  +"..."+ scrollHeight);

              if(documentHeight - viewPortHeight - scrollHeight<20){

                 if(curpage<totalpage&&totalpage>1&&!scrollDisable){

                    var txt = $('.search_input').val();

                        console.log(curpage);

                        if(txt){

                         setsearchVideo(++curpage,txt);

                        }else{

                         setVideo(++curpage,iType,tindex);

                        }





                 }

              }



          });





})

