var shareModule = function(type, shareparm) {
    if (type == "qq") {
        window.open(
            "http://connect.qq.com/widget/shareqq/iframe_index.html?url=" + escape(shareparm.url) + "&title=" + shareparm.title + "&pics=" + shareparm.pics + "&summary=" + shareparm.summary,
            '_blank'
        );
    }
    if (type == "qzone") {
        window.open(
            "http://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?url=" + escape(shareparm.url) + "&title=" + shareparm.title + "&pics=" + shareparm.pics + "&summary=" + shareparm.summary,
            '_blank'
        );
    } else if (type == "qq_w") {
        window.open(
            "http://share.v.t.qq.com/index.php?c=share&a=index&appkey=12&url=" + escape(shareparm.url) + "&title=" + shareparm.title + "&pic=" + shareparm.pics + "&site=" + shareparm.site,
            '_blank'
        );
    } else if (type == "weibo") {
        window.open(
            "http://service.weibo.com/share/share.php?appkey=12&url=" + shareparm.url + "&title=" + shareparm.desc + "&pic=" + shareparm.pics,
            '_blank'
        );
    } else if (type == "tieba") {
        window.open(
            "http://tieba.baidu.com/f/commit/share/openShareApi?url=" + shareparm.url + "&title=" + shareparm.title + "&pic=" + shareparm.pics,
            '_blank'
        );
    } else if (type == "qq_f") {
        window.open(
            "http://connect.qq.com/widget/shareqq/index.html?showcount=0&url=" + shareparm.url + "&title=" + shareparm.title + "&pics=" + shareparm.pics + "&desc=" + shareparm.desc + "&summary=" + shareparm.summary + "&site=" + shareparm.site,
            '_blank'
        );
    } else if (type == "rr") {
        window.open(
            "http://widget.renren.com/dialog/share?resourceUrl=" + shareparm.site + "&srcUrl=" + shareparm.url + "&title=" + shareparm.title + "&pic=" + shareparm.pics + "&description=" + shareparm.summary,
            '_blank'
        );
    }
}

$(function() {

    var height = $(window).height();
    var docHeight = $(document.body).height();

    if (height > docHeight) {
        $(".g-footer").addClass("footer-fix");
    }


    $(window).on('scroll', goTop).trigger('scroll');

    // 返回顶部
    function goTop() {
        var $top = $('.go-top');
        var st = document.documentElement.scrollTop || document.body.scrollTop;
        $top.hide();
        if (st > 100) {
            $top.css('display', 'block');
        } else {
            $top.hide();
        }

        var height = $(".content-box").height();
        var leftHeight = $(".left-share").height();
        if (st == 0) {
            $(".left-share").css("margin-top", 0);
        } else {
            if (st > 280 && st < (height - 430)) {
                $(".left-share").css("margin-top", st - 240);
            }
        }

    }

    $(".g-right .go-top").on("click", function() {
        $('body,html').animate({ scrollTop: 0 }, 600);
        return false;
    });

    var navLiList = $('.g-nav .g-nav-header li');
    var currentLi = $('.g-nav .g-nav-header li.current');
    if (currentLi.length > 0) {
        var offSetLeft = currentLi[0].offsetLeft;
        var secondNav = $(".g-nav-second .g-second-header.show");
        var secondWidth = secondNav.width();
        if ((secondWidth / 2 + offSetLeft + 240) > 1200) {
            secondNav.css("float", "right");
        } else {
            secondNav.css("padding-left", offSetLeft - secondWidth / 2 + 50);
        }
    }


    $(".g-nav .g-nav-header li").hover(function() {
        var self = $(this);
        var type = self.attr("data-type");


        if (type !== undefined) {
            navLiList.removeClass('active');
            self.addClass("active");
            var next = self.closest(".g-nav").next();
            if(next.find('.g-second-header[data-type="' + type + '"]').find("li").length>0){
                next.addClass("show");
                next.find('.g-second-header').removeClass("show");
                next.find('.g-second-header[data-type="' + type + '"]').addClass("show");
                var currentWidth = next.find('.g-second-header[data-type="' + type + '"]').width();
                var offSetLeft1 = self[0].offsetLeft;
                if ((currentWidth + offSetLeft1 + 240) > 1200) {
                    next.find('.g-second-header[data-type="' + type + '"]').css("float", "right");
                } else {
                    next.find('.g-second-header[data-type="' + type + '"]').css("padding-left", offSetLeft1 - currentWidth / 2 + 50);
                }
            }else{
                next.removeClass("show");
                next.find('.g-second-header').removeClass("show");
            }
            
        } else {

            var next = self.closest(".g-nav").next();
            // next.removeClass("show");
        }
    }, function() {
        // navLiList.removeClass('active');
        // var type=$(currentLi).attr("data-type");
        // currentLi.addClass("active");
        // var next = currentLi.closest(".g-nav").next();
        // next.addClass("show");
        // next.find('.g-second-header').removeClass("show");
        // next.find('.g-second-header[data-type="' + type + '"]').addClass("show");
    });

    $(".g-nav-second .g-second-header").hover(function() {

    }, function() {
        navLiList.removeClass('active');
        var type = $(currentLi).attr("data-type");
        currentLi.addClass("active");
        var next = currentLi.closest(".g-nav").next();
        next.addClass("show");
        next.find('.g-second-header').removeClass("show");
        next.find('.g-second-header[data-type="' + type + '"]').addClass("show");
    });

    $.ajax({
        type: "get",
        url: "/api/login_status/",
        cache: false,
        success: function(res) {
            if (res.status == 1) {
                if (res.is_need_bind == 1){
                    $("#loginiframe").attr('src',res.bind_url);
                    $(".loginPopup").show();
                }else{
                    $(".loginPopup").hide();
                }
                $("#cubegoal_login").html(res.msg);
            }
        }
    });

});

$(".wx-dialog .wx-del").on("click", function(e) {
    if (!e) var e = window.event
    e.cancelBubble = true;
    if (e.stopPropagation) e.stopPropagation();
    $(".wx-dialog").hide();
})

$("body").on("click", function(e) {
    $(".wx-dialog").hide();
})

//john added for login popup
//登录
$("body").on('click',".logregist .login",function () {
    $('#loginiframe').attr('src',$('#cubegoal_hhb_login_url').val()+'?hhburl='+encodeURIComponent(location.href)+'&fromchannel=hhb');
    $('.loginPopup').show();
});
$("body").on('click',".logregist2 .login",function () {
    $('#loginiframe').attr('src',$('#cubegoal_hhb_login_url').val()+'?hhburl='+encodeURIComponent(location.href)+'&fromchannel=hhb');
    $('.loginPopup').show();
});

//注册
$("body").on('click',".logregist .regist",function () {
    $('#loginiframe').attr('src',$('#cubegoal_hhb_reg_url').val());
    $('.loginPopup').show();
});
$("body").on('click',".logregist2 .regist",function () {
    $('#loginiframe').attr('src',$('#cubegoal_hhb_reg_url').val());
    $('.loginPopup').show();
});

//关闭登录框
$("body").on('click',".loginPopup .wrapper",function () {
    if ($('#data_user_id') && $('#data_user_id').attr('data_user_id') > 0){
        $.ajax({
            type: "get",
            url: "/api/isBindLogout",
            success: function (res) {
                $('.loginPopup').hide();
                if(res.status == 1){
                    $("#cubegoal_login").html(res.msg);
                    if (res.img) {
                        var img = document.createElement("img");
                        img.src = res.img;
                    }
                    location.reload(true)
                }
            }
        });
    }else {
        $('.loginPopup').hide();
    }
});

//登录
$("body").on('click',".logregist .login",function () {
    $('.loginPopup').css("display","inline-block");
});
//查看用户登录信息
(function(){
    var _timer = 0;
  $("body").on('mouseenter',".accouDiv",function () {
        clearTimeout(_timer);
        _timer = setTimeout(function(){
          if($('.accouDiv .accinfo:hidden').length){
            $('.accouDiv .accinfo').slideDown();
            $('.accouDiv .accpic .down').hide();
            $('.accouDiv .accpic .up').show();
          }
        },200);
  }).on('mouseleave', '.accouDiv', function(){
        clearTimeout(_timer);
        _timer = setTimeout(function () {
          if($('.accouDiv .accinfo:visible').length){
            $('.accouDiv .accinfo').slideUp();
            $('.accouDiv .accpic .down').show();
            $('.accouDiv .accpic .up').hide();
          }
        }, 200);
  });
})();



$("body").on('click',"a.data-cube-ad",function () {
    var ad_url = $(this).attr('data-cube-ad-a')+$(this).attr('data-cube-ad-b');
    console.log(ad_url);
    window.open(ad_url);
});

//退出
$("body").on('click',".accouDiv .logout",function () {
    $('.accouDiv .accinfo').slideToggle();
    $.ajax({
        type: "get",
        url: "/api/logout",
        success: function (res) {
            console.log(res);
            document.cookie = "app_users=; expires=Thu, 18 Dec 2043 12:00:00 GMT; domain=huanhuba.com; path=/";
            document.cookie = "token=; expires=Thu, 18 Dec 2043 12:00:00 GMT; domain=huanhuba.com; path=/";
            if(res.status==1){
                $('.loginPopup').hide();
                $('#cubegoal_login').html(res.msg);
                if(res.img){
                    var img = document.createElement('img');
                    img.src = res.img;
                }
                window.location.reload()
            }
        }
    });
});


//john added 1-19
$('.leag-select .selecthead').click(function() {
    // console.log($('.leag-select .selectbody').css('display'));
    if ($('.leag-select .selectbody').css('display') == 'none') {
        $('.leag-select .selectbody').show();
    } else {
        $('.leag-select .selectbody').hide();
    }
});
$('.time-select .league-time-select').click(function() {
    $('.leag-select .selectbody').hide();
});
$('.leag-select .selectbody .content ul li span').click(function() {
    var i = $(this).find('i');
    $(i).toggleClass('checked')
});
$('.leag-select .leag-allchose').click(function() {
    var self = $(this);
    var items = self.closest(".leag-select").find("ul li i");
    // for (var i = 0; i < items.length; i++) {
    //     var item = items[i];
    //     if ($(item).hasClass("checked")) {
    //         return false;
    //     } else {
    //         $(item).addClass("checked");
    //     }
    // }
    $.each($(items), function(k, v) {
        if ($(v).hasClass("checked")) {
            //return false;
        } else {
            $(v).addClass("checked");
        }
    })
})
$('.leag-select .leag-cancel').click(function() {
    var self = $(this);
    var items = self.closest(".leag-select").find("ul li i");
    $.each($(items), function(k, v) {
        if ($(v).hasClass("checked")) {
            $(v).removeClass("checked");
        } else {
            $(v).addClass("checked");
            // if ($(v).hasClass("checked")) {
            //     $(v).removeClass("checked");
            // } else {

            // }

        }
    })
})
$('.leag-select .leag-confirm').click(function() {
    var leags = '';
    $.each($(".leag-select").find("ul li i"), function(k, v) {
        if ($(v).hasClass("checked")) {
            leags += $(v).attr("data-type") + "-";
        }
    })
    console.log("leags", leags)
})


$(".robet-right .close").on("click",function(){
    event.stopPropagation();
    $(".robet-right").hide();
})