$(function () {

    var win_width = 0;

    var win_height = 0;

    var bannerInd = 0;

    var bannerInv = null;

    init();

    main();
    function init() {

        win_width = $(window).width();

        win_height = $(window).height();

        setImgMax($(".bgImgs img"), 1800, 840);
        $(".main").css({ height: win_height - 120 });
    }



    TonyShare.ShareWebSite('tsina', $('.weibo'));

    TonyShare.ShareWebSite('qzone', $('.qq'));



    function main() {

        navCurAnimate();

        createBtns();

        bannerChange();
        $(".nav li").mouseover(function () {

            $(this).find(".p1").stop(false, false).animate({ top: -50, opacity: "hide" });

            $(this).find(".p2").stop(false, false).animate({ top: 0, opacity: "show" }, 300);

            $(this).find(".t2").stop(false, false).animate({ left: 0, opacity: "show" }, 300);
            navLineAnimate($(this).index());
            if ($(this).find(".t2").size() > 0) {
                $(".subNavBg").stop(false, false).slideDown();
            }

        }).mouseleave(function () {

            $(this).find(".p1").stop(false, false).animate({ top: 0, opacity: "show" }, 300);

            $(this).find(".p2").stop(false, false).animate({ top: 50, opacity: "hide" });

            $(this).find(".t2").stop(false, false).animate({ left: 50, opacity: "hide" }, 300);

            $(".subNavBg").stop(false, false).slideUp();

            navCurAnimate();

        });
        $(".search .input").blur(function () {

            if ($(this).val() == '') {

                $(this).addClass("inputBg")
            }

        }).focus(function () {

            $(this).removeClass("inputBg");

        });
        $(".main .right li").mouseover(function () {

            $(this).find(".text").stop(false, false).animate({ top: 0, opacity: "show" });

        }).mouseleave(function () {

            $(this).find(".text").stop(false, false).animate({ top: 113, opacity: "hide" });
        });

        $(".main .btns span").mouseover(function () {

            if ($(this).hasClass("cur")) {

                return;

            }

            bannerInd = $(this).index();

            bannerChange();

        });
        $(".weixin").mouseover(function () {

            $(this).find(".erweima").stop(false, false).animate({ bottom: 20, opacity: "show" });

        }).mouseleave(function () {

            $(this).find(".erweima").stop(false, false).animate({ bottom: 10, opacity: "hide" });

        });



    }

    function bannerAuto() {

        clearTimeout(bannerInv);
        bannerInv = setTimeout(function () {
            if (bannerInd >= $(".bgImgs img").size() - 1) {
                bannerInd = 0;
            } else {
                bannerInd++;
            }

            bannerChange();

        }, 5000);

    }

    function bannerChange() {

        $(".bgImgs img").stop(false, false).fadeOut(1000).eq(bannerInd).stop(false, false).fadeIn(1000);

        $(".main .btns span").removeClass("cur").eq(bannerInd).addClass("cur");
        $(".main .bannerText .text1").stop(false, false).css({ marginTop: 120 }).hide().eq(bannerInd).animate({ marginTop: 0, opacity: "show" }, 700);
        bannerAuto();
    }
    function createBtns() {
        var str = '';
        for (var i = 0; i < $(".bgImgs img").size() ; i++) {
            str += '<span></span>';
        }

        $(".main .btns").html(str);
    }

    function navCurAnimate() {

        var c = $(".nav .cur");

        if (c.size() > 0) {

            navLineAnimate(c.index());

        } else {

            $(".head .line").stop(false, false).animate({ left: 120, opacity: "hide" });

        }

    }

    function navLineAnimate(ind) {

        $(".head .line").stop(false, false).animate({ left: 436 + 75 * ind, opacity: "show" });
    }

    function setImgMax(img, imgW, imgH, tW, tH) {

        //设置图片宽高比例
        var tWidth = tW || win_width;
        var tHeight = tH || win_height;

        var coe = imgH / imgW;

        var coe2 = tHeight / tWidth;
        if (coe < coe2) {
            var imgWidth = tHeight / coe;
            img.css({ height: tHeight, width: imgWidth, left: -(imgWidth - tWidth) / 2, top: 0 });
        } else {
            var imgHeight = tWidth * coe;
            img.css({ height: imgHeight, width: tWidth, left: 0, top: -(imgHeight - tHeight) / 2 });
        }
    }
    $(window).resize(function () {

        init();

    });



    //搜索

    $(".searchinput").keyup(function (event) {

        var keyvalue = event.which;

        var vakes = $(this).val();

        if (keyvalue == 13) {

            if (vakes == '')

                return;

            window.location = 'search.aspx?keyword=' + encodeURIComponent(vakes);

        }

    });

    $('#btnsumits').click(function () {

        var vakes = $('.searchinput').val();

        if (vakes == '')

            return;

        window.location = 'search.aspx?keyword=' + encodeURIComponent(vakes);

    })



});

