$(function () {
    $.preLoadImages = function () {
        var args_len = arguments.length;
        for (var i = args_len; i--; ) {
            var cacheImage = document.createElement('img');
            cacheImage.src = arguments[i];
            cache.push(cacheImage);
        }
    }

    function toggleFlagByParentId(parentId, offDirection, onDirection, speed) {
        if (parent) {
            $("#" + parentId + " .flag_off").stop(true, true).toggle("slide", { direction: offDirection }, speed);
            $("#" + parentId + " .flag_on").stop(true, true).toggle("slide", { direction: onDirection }, speed);
        }
    }

    function toggleMenuByParentElement(parentObj, offDirection, onDirection, speed) {
        if (parentObj) {
            $(parentObj).children().children(".menu_off").stop(true, true).toggle("slide", { direction: offDirection }, speed);
            $(parentObj).children().children(".menu_on").stop(true, true).toggle("slide", { direction: onDirection }, speed);

            $(parentObj).next(".sub_area").slideDown();
            $(parentObj).next(".sub_area").find(".sub_menu li:eq(" + ($("#subNum").val() - 1) + ") img").toggle();
        }
    }

    var cache = [];
    $(window).load(function () {
        $.preLoadImages(
            "/images/common/menu_01_On_Jpn.gif", "/images/common/menu_02_On_Jpn.gif", "/images/common/menu_03_On_Jpn.gif", "/images/common/menu_04_On_Jpn.gif"
            , "/images/common/menu_05_On_Jpn.gif", "/images/common/menu_06_On_Jpn.gif", "/images/common/menu_07_On_Jpn.gif"
            , "/images/common/menu_01_On_Chn.gif", "/images/common/menu_02_On_Chn.gif", "/images/common/menu_03_On_Chn.gif", "/images/common/menu_04_On_Chn.gif"
            , "/images/common/menu_05_On_Chn.gif", "/images/common/menu_06_On_Chn.gif", "/images/common/menu_07_On_Chn.gif"
            , "/images/main/bg_sem_eng.gif", "/images/main/bg_seo_eng.gif", "/images/main/bg_sns_eng.gif", "/images/main/bg_medical_eng.gif"
            , "/images/main/bg_mobile_eng.gif", "/images/main/bg_multisite_eng.gif", "/images/main/bg_seostats_eng.gif"
            , "/images/main/bg_sem_jpn.gif", "/images/main/bg_seo_jpn.gif", "/images/main/bg_sns_jpn.gif", "/images/main/bg_medical_jpn.gif"
            , "/images/main/bg_mobile_jpn.gif", "/images/main/bg_multisite_jpn.gif", "/images/main/bg_seostats_jpn.gif"
            , "/images/main/bg_sem_chn.gif", "/images/main/bg_seo_chn.gif", "/images/main/bg_sns_chn.gif", "/images/main/bg_medical_chn.gif"
            , "/images/main/bg_mobile_chn.gif", "/images/main/bg_multisite_chn.gif", "/images/main/bg_seostats_chn.gif"
        );

        if ($("#pageNum").val() > 0)
            toggleMenuByParentElement($(".menu_area .menu:eq(" + ($("#pageNum").val() - 1) + ")"), "down", "up", 200);

        if ($("#pageLangNum").val() < 1)
            $("#pageLangNum").attr("value", "1");

        switch ($("#pageLangNum").val()) {
            case "2":
                changeLangCssForFlash("Jpn");
                break;
            case "3":
                changeLangCssForFlash("Chn");
                break;
        }

        $(".flag dd:eq(" + ($("#pageLangNum").val() - 1) + ")").trigger("click");


    });

    $().ready(function () {
        if ($(window).height() > $("#container").height())
            $("#header").height($(window).height());
        else
            $("#header").height($("#container").height());
    });

    $(window).scroll(function () {
        var topPosition = ($(window).height() - $('#quickmenu').height()) / 2 + $(document).scrollTop();

        setTimeout(function () {
            $('#quickmenu').stop().animate({ top: topPosition }, 500, 'easeOutBack');
        }, 100);
    });

    $(window).resize(function () {
        $(window).trigger("scroll");
    });

    $(".flag dd").click(function () {
        if ($("#" + this.id + " .flag_off:visible").length) {
            var currentMenu = $(".menu_area .menu:eq(" + ($("#pageNum").val() - 1) + ") .menu_on");
            if (currentMenu.length) {
                var currentMenuSrc = currentMenu.attr("src");
                currentMenu.attr("src", currentMenuSrc.replace(/(On_)(.*)\.gif/, "On_" + this.id.replace("flag", "") + ".gif"));
            }

            toggleFlagByParentId(this.id, "up", "down", 200);
            toggleFlagByParentId($(this).parent().children("dd:not(#" + this.id + ")").children(".flag_off:hidden").parent().attr("id"), "up", "down", 200);

            $("#imgColorLine").attr("src", "/images/common/" + this.id + "_line.gif");
            $("#imgColorLine").animate({ left: $(this).offset().left + 'px' }, 200);

            $("#mainflash img:animated").stop(true, true).hide();
            $("#mainflash img:visible").animate({ opacity: 'toggle' }, 500);
            $("#mainflash img:eq(" + ($(this).index() - 1) + ")").animate({ opacity: 'toggle' }, 1000);

            $("#css_locale").attr("href", "/css/" + this.id.replace("flag", "lang_") + ".css");

            changeLangLink(this.id);
        }
    });

    $(".sub_area .sub_menu li").hover(
        function () {
            if ($(this).find(".menu_on:hidden").length)
                $(this).children().children().toggle();
        },
        function () {
            if ($(this).find(".menu_on:visible").length && $(this).index() != $(".menu_area .menu:eq(" + ($("#pageNum").val() - 1) + ")").next(".sub_area").find(".sub_menu li:eq(" + ($("#subNum").val() - 1) + ")").index())
                $(this).children().children().toggle();
        }
    );

    $("#togglebtn").click(function () {
        var maxLeft = 477;
        var toggleSize = 310;
        var toggleUnit = "+", toggleImageSrc = "btn_toggle_l.gif";
        var toggleSizeString;

        if ($("#togglebtn").offset().left == maxLeft) {
            toggleUnit = "-";
            toggleImageSrc = "btn_toggle_r.gif";
        }

        toggleSizeString = toggleUnit + '=' + toggleSize + "px";

        $("#header").stop(true, true).animate({ width: toggleSizeString }, 1000);
        $("#container").stop(true, true).animate({ left: toggleSizeString }, 1000);
        $("#togglebtn").stop(true, true).animate({ left: toggleSizeString }, 1000);
        $("#globalmenu").stop(true, true).animate({ left: toggleSizeString }, 1000);
        $(".path").stop(true, true).animate({ left: toggleSizeString }, 1000);

        setTimeout(function () {
            $("#togglebtn").css("background-image", "url(/images/common/" + toggleImageSrc + ")")
        }, 1000);
    });
});

function changeLangLink(seletedFlagId) {
    var localeStr = seletedFlagId.replace("flag", "");
    for (var i = 0; i < $(".link_lang").length; i++) {
        var link_lang = $(".link_lang:eq(" + i + ")");
        link_lang.attr("href", link_lang.attr("href").replace(/(_)(...)\.php/, "_" + localeStr + ".php"));
    }

    var g_CurrFileName = document.URL.substring(document.URL.lastIndexOf("/") + 1, document.URL.lastIndexOf("/") + 20);
    if (g_CurrFileName != "" && g_CurrFileName != "index.php") {
        var linkRex = /(_)(...)\.php/g;
        var currentLangStr = linkRex.exec(g_CurrFileName);

        if (currentLangStr) {
            if (currentLangStr[0].replace("_", "").replace(".php", "") != localeStr)
                location.href = "./" + g_CurrFileName.replace(linkRex, "_" + localeStr + ".php");
        }
    }
}

function changeLangCssForFlash(locale) {
    $("#css_locale").attr("href", "/css/lang_" + locale + ".css");
    changeLangLink("flag" + locale);
}
