
var curPage = 0;
var pageLen = 6;
var totalLen = $("#new-video li").length;

var V = {
    init : function(){
        V.scrollNews();
        V.triggerTab("hotVd-tab");
    },
    scrollNews : function() {   // 新闻翻滚
        var $scrollNews = $('#scrollNews');
        if (!$scrollNews) return;
        var news = $scrollNews.children(),
            len = news.length,
            cHeight = $scrollNews.height(),
            perHeight = cHeight / len,
            interval = 2000,

            index = 0,//初始记录

            timer = setInterval(function () {

                $scrollNews.animate({
                    top: - (perHeight * index) + "px"
                }, interval / 3)

                if (index === len - 1)
                    index = 0;
                else
                    index++;
            }, interval);
    },
    triggerTab : function(id){  //tab切换
        $ul=$("#"+id);
        $ul.on("click","li",function(){
            var index = $(this).index();
            $(this).addClass("active").siblings().removeClass("active");
            $($(this).attr("data-id")).show().siblings("ul").hide();
            $ul.parent().find(".more").eq(index).show().siblings(".more").hide();
        })
    }
}

var C = {
    init : function(){
        C.initFullAd()
        C.initYYLiveAd()

        // 最新视频刷新
        $(".col-right").on("click",".refresh",function(){
            ++curPage;

            if(curPage >= totalLen/pageLen){
                curPage = 0;
            }

            $("#new-video li").addClass("u-hide");
            for(var i=0; i<pageLen; i++){
                var $curLi = $("#new-video li").eq(curPage*pageLen+i);
                var $curImg = $curLi.find("img");
                var original = $curImg.attr("data-original");

                if(original){
                    $curImg.removeAttr("data-original").attr("src",original);
                }
                $curLi.removeClass("u-hide");
            }
        });
    },
    initFullAd : function() {
        /** 广告时间判断 S */
        // var date = new Date();
        // var month = date.getMonth();
        // var day = date.getDate();
        // var hour = date.getHours();
        // var isTime = false;
        // if(month == 7 && day >= 22 && day <= 24) {
        //     if(hour >= 15 || hour <= 10) {
        //         isTime = true;
        //     }
        // } else if(month == 7 && day >= 25 && day <= 26) {
        //     isTime = true;
        // }
        // if(isTime == false) {
        //     return;
        // }
        /** 广告时间判断 E */
        
        var isInitFullDa = $.cookie("isInitFullDa");
        var hasDa = $(".mod-da-full img").length;
        if(isInitFullDa != 1 && hasDa > 0) {
            $(".o-wrap").append("<div class='fullDa-wrap'><div class='fullDa-mask'></div><div class='fullDa-inner'><em></em></div></div>")

            $(".fullDa-inner").append($(".mod-da-full").html())

            $(".o-wrap").on("click", ".fullDa-wrap em", function() {
                $(".fullDa-wrap").remove();
            })

            $.cookie("isInitFullDa", 1, {
                expires : 1,
                path : "/"
            })
        }
    },
    initYYLiveAd : function() {
        $.ajax({
            url: 'http://da.duowan.com/loc/158',
            type: 'GET',
            dataType: 'jsonp',
            success: function(ret) {
                var locid = ret.locid
                var pid = ret.pid
                var item = ret.extraJson.list[0]
                var dom = '<li><a class="da-block-wrapper" href="'+item.url+'" target="_blank" locid="'+locid+'" pid="'+pid+'"><span class="video-img"><img src="'+item.img+'"></span></a><h2><a href="'+item.url+'" target="_blank">'+item.name+' '+item.desc+'</a></h2></li> '
                $("#new-video li").eq(5).replaceWith(dom)
            }
        })
    }
}

C.init();
V.init();

