MJL.event.add(window, "load", function() {
    MJL.style.setFontResizeTarget(document.getElementById('str-page'));

    MJL.enable.window('popup-720', { width : 720 });
    MJL.enable.styleSwitcher('nav-fs');
    MJL.enable.tab('tabs');
    MJL.enable.heightEqualizer('nav-global-01', {
        groupBy : 6,
        collect : function(parent) {
            return MJL.getElementsByClassName(parent, 'btn');
        }
    });
    MJL.enable.heightEqualizer('lyt-2col-01', {
        groupBy : 2,
        collect : function(parent) {
            return MJL.getElementsByClassName(parent, 'equalize');
        }
    });
    MJL.enable.heightEqualizer('lyt-3col-01', {
        groupBy : 3,
        collect : function(parent) {
            return MJL.getElementsByClassName(parent, 'equalize');
        }
    });
    MJL.enable.heightEqualizer('lyt-4col-01', {
        groupBy : 4,
        collect : function(parent) {
            return MJL.getElementsByClassName(parent, 'equalize');
        }
    });
    MJL.enable.heightEqualizer('list-link-2col-01', {groupBy : 2});
    MJL.enable.heightEqualizer('list-link-3col-01', {groupBy : 3});
    MJL.enable.heightEqualizer('list-bullet-2col-01', {groupBy : 2});
    MJL.enable.heightEqualizer('list-bullet-3col-01', {groupBy : 3});
    MJL.enable.rollover('roll', {disable : 'unroll'});
    MJL.enable.rollover('nav-fs', {active : 'active'});
    MJL.enable.rollover("roll-active", { active : "active" });
});


// -----------------------------------------------------------------------------
// 追加処理
// -----------------------------------------------------------------------------
'jQuery' in window && (function($) {
    // GET リクエスト パラメタ取得
    function getParams() {
        var strs  = (location.search || '').substring(1).split('&'), // ? 削除
            nstrs = strs.length,
            ret   = {},
            s, tmp;
        for (s = 0; s < nstrs; s++) {
            tmp = strs[s].split('=');
            ret[decodeURIComponent(tmp[0])] = decodeURIComponent(tmp[1]);
        }
        return ret;
    }

    // URL にパラメタ追加
    function setParam(name, value) {
        var href   = location.href,
            path   = href.substring(0, href.indexOf('?')),
            search = location.search;
        return [path, (search ? search + '&' : '?'), name, '=', value, decodeURIComponent(location.hash)].join('');
    }

    // 印刷プレビューモード是非 (是: true, 非: false)
    var printMode = 'print' === getParams().mode;

    // 印刷プレビューモードでは print メディアスタイルシートのみ適用
    printMode && (function() {
        // script 要素ロード時に StyleSheeet ロード要素を編集 (遅延防止)
        var condPrint  = /(?:^|, *)print(?:,|$)/,  // 
            condScreen = /(?:^|, *)screen(?:,|$)/, // 
            sheets     = document.styleSheets,
            nsheets    = sheets.length,
            s;
        // media 属性値に print はあるが screen がない場合
        // screen を追加するよう変更
        var change = MJL.ua.trident ? function(sheet) { // Trident
            // sheet instanceof HTML
            // see also: http://msdn.microsoft.com/en-us/library/ms531200%28VS.85%29.aspx
            var str = sheet.media,
                ret = false;
            if (condPrint.test(str)) {
                if (!condScreen.test(str)) {
                    sheet.media += ",screen";
                }
                ret = true;
            }
            return ret;
        } : function(sheet) {                           // Others
            // sheet instanceof StyleSheet
            // see also: http://www.w3.org/TR/DOM-Level-2-Style/stylesheets.html#StyleSheets-StyleSheetList
            var str = sheet.media.mediaText,
                ret = false;
            if (condPrint.test(str)) {
                if (!condScreen.test(str)) {
                    sheet.media.appendMedium("screen");
                }
                ret = true;
            }
            return ret;
        }
        // media 属性値変更・print メディアでないスタイルシートの無効化
        for (s = 0; s < nsheets; s++) {
            if (!change(sheets[s])) {
                sheets[s].disabled = true;
            }
        }
    })();


    // --------------------------------
    // DOMContentLoaded
    // --------------------------------
    $(function() {
        var popupMode = 0 < $('#str-page.popup').length,
            popup     = window.opener,
            parent    = popup || window.self;

        // 各種挿入
        //   印刷プレビューモードないしポップアップ時は何もしない
        printMode || popupMode || $('#str-header .nav-header-01').append(
            // 印刷ボタン
            $('<p class="nav-print roll"></p>').append(
                $('<a href="'+setParam('mode', 'print')+'"><img src="/ts/image/template/header_btn-print.gif" alt="印刷" width="61" height="22" /></a>').click(function() {
                    var win = window.open(this.href, 'TENSUITE_PRINT_PREVIEW', 'width=800,scrollbars=yes');
                    if (win) {
                        win.focus();
                    }
                    return false;
                })
            )
        ).append(
            // 文字サイズ変更ボタン
            '<dl class="nav-fs"><dt>文字サイズ ：</dt><dd><a href="#small" title="文字サイズを小さくします。"><img src="/ts/image/template/header_btn-fs-small.gif" alt="－" width="30" height="22" /></a></dd><dd><a href="#normal" title="文字サイズを標準にします。"><img src="/ts/image/template/header_btn-fs-normal.gif" alt="標準" width="37" height="22" /></a></dd><dd><a href="#large" title="文字サイズを大きくします。"><img src="/ts/image/template/header_btn-fs-large.gif" alt="＋" width="30" height="22" /></a></dd></dl>'
        );

        // 新規ウインドウを開く
        $('a.blank').click(function() {
            var win = window.open(this.href, '_blank');
            if (win) {
                win.focus();
            }
            return false;
        });

        // 「印刷する」「閉じる」ボタン追加
        (popupMode || printMode) && (function() {
            var pivot = $('#str-main hr');
            // 「印刷する」ボタン
            printMode && pivot.before(
                $('<p class="nav-print-01 roll"></p>').append(
                    $('<input type="image" src="/ts/image/module/btn-print-01.jpg" alt="印刷する" />').click(function() {
                        window.print();
                        if (popup) {
                            window.close();
                            parent.focus();
                        }
                        return false;
                    })
                )
            );
            // 「閉じる」ボタン
            popup && pivot.before(
                $('<p class="nav-close-01"></p>').append(
                    $('<a href="'+location.href+'">閉じる</a>').click(function() {
                        window.close();
                        parent.focus();
                        return false;
                    })
                )
            );
        })();

        // LightBox
        (function() {
            var targets = $('.modal-window');
            if (0 < targets.length) {
                if (!$.fn.lightBox) {
                    $('head').append(
                        '<script type="text/javascript" src="/ts/file/js/jquery.lightbox.js"></script>'
                    );
                }
                targets.lightBox({
                    overlayBgColorn      : '#000',
                    overlayOpacity       : 0.8,
                    imageLoading         : '/ts/image/module/icon-modal-loading-01.gif',
                    imageBlank           : '/ts/image/module/lyt-modal-blank-01.gif',
                    containerResizeSpeed : 0,
                    fixedNavigation      : true,
                    txtImage             : ''
                });
            }
        })();
    });
})(jQuery);


// -----------------------------------------------------------------------------
// MJL Extension
// -----------------------------------------------------------------------------
(function() {
    // GET リクエスト パラメタ取得
    var getParams = function() {
        var strs  = (location.search || '').substring(1).split('&'), // ? 削除
            nstrs = strs.length,
            ret   = {},
            s, tmp;
        for (s = 0; s < nstrs; s++) {
            tmp = strs[s].split('=');
            ret[decodeURIComponent(tmp[0])] = decodeURIComponent(tmp[1]);
        }
        getParams = function() {
            return ret;
        };
        return ret;
    }

    // --------------------------------
    // Tab
    // --------------------------------
    // 最優先タブ アクティブ ID を URI 内パラメタから取得
    MJL.Tab.prototype._ACTIVE_ID_RULES.unshift(function() {
        return getParams().tab || "";
    });

    // --------------------------------
    // AutoTab: 自動切替機能つきタブ
    // --------------------------------
    function AutoTab() {
        this.__autoId   = null; // 自動切替 interval ID
        this.__contaner = null; // $(this.container)
        MJL.Tab.apply(this, arguments); // コンストラクタ実行
    }

    // MJL.Tab を継承
    AutoTab.prototype = new MJL.Tab();
    AutoTab.prototype.constructor = AutoTab;

    // オプション設定
    AutoTab.prototype.setOptions = (function() {
        var origin = AutoTab.prototype.setOptions;
        return function(content, optional) {
            // 元処理実行
            origin.apply(this, arguments);
            // デフォルト設定
            if (!("auto" in this.options)) {
                // 自動切替是非 (是: 数値リテラル, 非: false)
                this.options.auto = 5000; // 切替インターバル (ms)
            }
            if (!("fade" in this.options)) {
                // フェードイン効果是非 (是: 数値リテラル, 非: false)
                this.options.fade = 1000;  // 効果時間 (ms)
            }
            // オプション設定
            if (MJL.isObject(optional)) {
                if ("auto" in optional) {
                    this.options.auto = optional.auto;
                }
                if ("fade" in optional) {
                    this.options.fade = optional.fade;
                }
            }
        };
    })();

    // 生成
    AutoTab.prototype.create = (function() {
        var origin = AutoTab.prototype.create;
        return function() {
            // 元処理実行
            origin.apply(this, arguments);
            // 自動切替
            if (this.options.auto) {
                // コンテナ要素のマウスポインタ状況に応じて切替中断・再開
                $(this.container).mouseenter(MJL.bind( // 中断
                    this.__removeAuto, this
                )).mouseleave(MJL.bind(                // 再開
                    this.__setAuto, this
                ));
                // ロード時から実行
                // 先にイベントを設定しておかないと active 時の挙動が
                // おかしくなる
                this.__setAuto();
            }
        };
    })();

    // アクティブタブを変更
    AutoTab.prototype.active = (function() {
        var origin = MJL.Tab.prototype.active;
        return function(id) {
            // 元処理実行前に、前状態の ID を取得しておく
            var aid = this._getActiveId(),
                before;
            // 元処理実行
            origin.apply(this, arguments);
            // Trident: 閲覧者による操作時、前状態の blur → mouseout →
            //          mouseleave が発生するため自動切替が開始してしまう
            if (MJL.ua.trident && this._isValidId(id)) {
                // 明示的に mouseover → mouseenter を発生させて __removeAuto
                // を実行させる
                MJL.event.dispatch(this.items[id].anchor, "mouseover");
            }
            // フェードイン効果付与
            if (this.options.fade) {
                before = $(this.items[aid].panel);
                if (!this._isValidId(id)) {
                    before.hide().fadeIn(this.options.fade);
                } else if (id === this._getActiveId() && id !== aid) {
                    before.hide();
                    $(this.items[id].panel).hide().fadeIn(this.options.fade);
                } else {
                    before.show();
                }
            }
        };
    })();

    // 自動生成 id 属性値接頭語
    AutoTab.prototype._ID_PREFIX   = "MJL_AUTOTAB_ITEM_";

    // Cookie 項目名
    AutoTab.prototype._COOKIE_NAME = "MJL.AutoTab";

    // コンテナ生成
    AutoTab.prototype._createContainer = (function() {
        var origin = AutoTab.prototype._createContainer;
        return function() {
            origin.apply(this, arguments);
            // jQuery オブジェクトはコンテナ要素が確定した時点で生成
            this.__container = $(this.container);
        };
    })();

    // 次のアクティブタブに変更
    AutoTab.prototype.__activeNext = function() {
        var created = this._created,
            before, after, num, max;
        if (created) {
            before = this.items[this._getActiveId()]; // 前アイテム
            num    = before.num + 1;
            max    = this._items.length - 1;
            // 順序リスト化
            if (max < num) { num = 0; } // 末尾 → 先頭
            after = this._items[num];                 // 次アイテム
            // フォーカス制御を意図的に排したうえで
            // アクティブタブを変更
            this._created = false;
            this.active(after.id);
            this._created = created;
            // フォーカス制御はせずに rollover アクティベートだけ実行
            MJL.event.dispatch(before.anchor, "blur");
            MJL.event.dispatch(after.anchor, "focus");
            // Trident: 自動切替中、focus → mouseover → mouseenter が発生
            //          するため途中で自動切替が止まってしまう
            if (MJL.ua.trident) {
                // 無駄な操作だが明示的に mouseout → mouseleave を発生
                // させて __setAuto を実行させる
                MJL.event.dispatch(after.anchor, "mouseout");
            }
        }
    };

    // 自動切替 有効
    AutoTab.prototype.__setAuto = function(event) {
        if (null === this.__autoId) {
            this.__autoId = setInterval(MJL.bind(
                this.__activeNext, this
            ), this.options.auto);
        }
    };

    // 自動切替 無効
    AutoTab.prototype.__removeAuto = function(event) {
        if (null !== this.__autoId) {
            clearInterval(this.__autoId);
            this.__autoId = null;
        }
    };

    // enable API 登録
    MJL.enable.autoTab = MJL.enable.getRunner(AutoTab);
})();


