var headerContainer = $('.header-container')
var nullHeight = 10000 // null 高度
var isScroll = true // 是否发生了滚动
var isBodyScroll = false // html || body

// 导航变色
$(function () {
    headerContainer = $('.header-container')
    var flag = false  // 是否已经变色

    // 变化
    function doChange() {
        // 搜索框状态
        var expend = $('.search-modal').attr('expend')
        // 是否需要变色
        var top = $('html').scrollTop() || $('body').scrollTop()
        if($('body').scrollTop() && !isBodyScroll){
            isBodyScroll = true
        }
        if (top > nullHeight - headerContainer.height() - 20) {
            if (!flag) {
                flag = true
                headerContainer.addClass('attach-red').attr('attach', '1')
            }
        } else {
            if (flag) {
                flag = false
                headerContainer.attr('attach', '')
                if (expend) return;
                headerContainer.removeClass('attach-red')
            }
        }
        // 是否发生滚动
        if (top > 20) {
            if (!isScroll) {
                isScroll = true
                // 滚动
                if (!expend) return;
                $('.search-modal').removeClass('active').attr('expend', '')
                $('.search-modal').addClass('active-s').attr('expend', '1')
                headerContainer.addClass('attach-red')
                $('.search-box').css('top', headerContainer.height())
            }
        } else {
            if (isScroll) {
                isScroll = false
                // 未滚动
                if (!expend) return;
                $('.search-modal').removeClass('active-s').attr('expend', '')
                $('.search-box').css('top', 0)
                if (!headerContainer.attr('attach')) {
                    headerContainer.removeClass('attach-red')
                }
                $('.search-modal').addClass('active').attr('expend', '1')
            }
        }
    }

    // 滚动事件
    $(window).scroll(function () {
        doChange()
    })

    doChange()
})

// 大小变化
$(function () {
   headerContainer = $('.header-container')
    nullHeight = window.innerHeight || document.documentElement.clientHeight
    $('.null').css('height', nullHeight + 'px')
    $(window).resize(function () {
        nullHeight = window.innerHeight || document.documentElement.clientHeight
        $('.null').css('height', nullHeight + 'px')
        $('.search-box').css('top', headerContainer.height())
    })
})

// 搜索框
function searchModalClose() {
   headerContainer = $('.header-container')
    $('.search-modal').removeClass('active').attr('expend', '')
    $('.search-modal').removeClass('active-s').attr('expend', '')
    $('.search-box').css('top', 0)
    if (!headerContainer.attr('attach')) {
        headerContainer.removeClass('attach-red')
    }
    $('.m-search-close').hide()
    $('.m-search').show()
}
function searchModalShow() {
    headerContainer = $('.header-container')
    $('.m-search-close').show()
    $('.m-search').hide()
    if (isScroll) {
        // 滚动
        headerContainer.addClass('attach-red')
        $('.search-modal').addClass('active-s').attr('expend', '1')
        $('.search-box').css('top', headerContainer.height())
    } else {
        // 未滚动
        $('.search-modal').addClass('active').attr('expend', '1')
    }
}

$(function () {
    function option() {
        var expend = $('.search-modal').attr('expend')
        if (expend) {
            searchModalClose()
        } else {
            // 关闭菜单模态框
            $('.menu-modal').removeClass('active')
            $('.m-menu-close').hide()
            $('.m-menu').show()

            searchModalShow()
        }
    }

    $('.m-search').click(option)
    $('.l-search').click(option)
    $('.m-search-close').click(option)
    // 蒙版点击关闭
    $('.search-mb').click(option)
})

// 导航
$(function () {
    $('.m-menu').click(function () {
        // 关闭搜索模态框
        searchModalClose()
        $('.menu-modal').addClass('active')
        $(this).hide()
        $('.m-menu-close').show()
    })
    $('.m-menu-close').click(function () {
        $('.menu-modal').removeClass('active')
        $(this).hide()
        $('.m-menu').show()
    })
})

// 上移内容区域
$(function () {
    headerContainer = $('.header-container')
    $('.body-bottom span').click(function () {
        if(isBodyScroll){
            $('body').animate({
                scrollTop: nullHeight - headerContainer.height()
            }, 800)
            return
        }
        $('html').animate({
            scrollTop: nullHeight - headerContainer.height()
        }, 800,function(){
            if(!$('html').scrollTop()){
                $('body').animate({
                    scrollTop: nullHeight - headerContainer.height()
                }, 800)
            }
        })
    })
})

// 数字切换动画
$(function () {
    var datas = $('.c-data')
    var timer = null
    var len = datas.length
    var index = 0

   console.log(len)

    timer = setInterval(function () {
        $('.d-active').removeClass('d-active')
        index++
        if (index >= len) {
            index = 0
        }
        datas.eq(index).addClass('d-active')
    }, 4000)

    setTimeout(function () {
        datas.eq(index).addClass('d-active')
        clearTimeout(this)
    }, 0)
})

// 视频close按钮动画
$(function(){
    $('.video-modal span').hover(function () {
        $(this).stop().animate({
            backgroundPositionY: '-68px'
        }, 300)
    }, function () {
        $(this).stop().animate({
            backgroundPositionY: '0'
        }, 300)
    })
})

// 消息框关闭
$(function () {
    $('.msg-modal span').click(function () {
        $('.msg-modal').hide()
    })
})

// 视频
$(function () {
    jwplayer("video-control").setup({
        flashplayer: "./js/jwplayer.flash.swf",
        file: "/_upload/tpl/04/e0/1248/template1248/images/video.mp4",
        loop: true,
        events: {
            onPlay: function () {
                $('#video-control').css('width', '100%').css('height', '100%');
            }
        }
    });
    var videoModal = $('.video-modal')
    // 打开
    $('.play span').click(function () {
        // 显示视频modal
        videoModal.show()
        jwplayer().play()
    })
    // 关闭
    $('.video-modal span').click(function () {
        jwplayer().pause()
        videoModal.hide()
    })
})

// 轮播图实现
$(function () {
    var flowContainer = $('.swiper')
    var flows = $('.dots li')
    var index = 0
    var timer = null
    var rTimer = null

    function swip(index) {
        var offset = -100 * index + '%'
        flowContainer.stop(false, true).animate({
            left: offset
        }, 1000)
        $('.dot-active').removeClass('dot-active')
        flows.eq(index).addClass('dot-active')
    }

    function interval() {
        timer = setInterval(function () {
            index++
            if (index > 2) {
                index = 0
            }
            swip(index)
        }, 4000)
    }

    flows.click(function () {
        clearInterval(timer)
        timer = null
        index = flows.index(this)
        swip(index)
        interval()
    })

    interval()
})

$(function(){
// ??
    function mNavOpen(_dom) {
        var _h = _dom.children('li').length * 40
        _dom.prev().html('-')
        _dom.show().stop(false, true).animate({
            height: _h + 'px'
        }).attr('m-nav-expend', '1').addClass('m-nav-active')
    }

    function mNavClose(_dom) {
        _dom.prev().html('+')
        _dom.stop(false, true).animate({
            height: 0
        }, 300, function () {
            _dom.hide()
        }).attr('m-nav-expend', '').removeClass('m-nav-active')
    }

    $('.m-nav-b li').click(function () {
        var _dom = $(this).children('.drop-menu')
        var flag = _dom.attr('m-nav-expend')
        if (flag) {
            mNavClose(_dom)
        } else {
            $('.m-nav-active').each(function (i, d) {
                mNavClose($(d))
            })
            mNavOpen(_dom)
        }
    })
})

$(function(){
    $('.news-content').find('.news-item').last().css('margin-right',0);
    $('.nav-b').children('li').last().css('margin-right',0);
    $('.seek-items').find('.item').eq(1).css('margin-left','56px');
    $('.seek-items').find('.item').eq(3).css('margin-left','56px');
})

$(function(){
    // ???
    var index = 0
    var banners = $(".swiper-o .swiper-i li")
    var dots = $(".swiper-o .swiper-d li")
    var count = banners.length
    var interval = null

    dots.click(function(){
        if($(this).hasClass('active')) return
        var index_ = index
        index = Array.prototype.slice.call(dots).indexOf(this)
        stop()
        swip(index_,index)
        start()
    })

    function init(){
        banners .eq(0).addClass('active')
        dots.eq(0).addClass('active')
    }

    function swip(index_,index){
        // ??
        dots.eq(index_).removeClass('active')
        dots.eq(index).addClass('active')
        banners.eq(index).show()
        banners.eq(index).stop(false,true).css('opacity','1')
        banners.eq(index_).stop(false,true).animate({
            opacity: 0
        },800,function(){
            banners.eq(index_).hide()
            banners.eq(index_).css('opacity',1)
            banners.eq(index_).removeClass('active')
            banners.eq(index).addClass('active')
        })
    }

    function start(){
        interval = setInterval(function(){
            var index_ = index
            index ++
            if(index >= count){
                index = 0
            }
            swip(index_,index)
        },5000)
    }

    function stop(){
        if(!interval) return
        clearInterval(interval)
        interval = null
    }

    init()
    start()
})