'jQuery' in window && 'MJL' in window && /^2\.1\.\d+$/.test(MJL.version) && (function($) {
// オプション設定例
/*
<script type="text/javascript" src="/shared/js/auto-tab.js" id="auto-tab"></script>
<script type="text/javascript">
'autoTab' in window && window.autoTab({
    // 自動切替に要する時間 (単位: ms)
    // 有効: 数値リテラル, 無効: false
    auto : 5000,
    // フェードイン効果時間 (単位: ms)
    // 有効: 数値リテラル, 無効: false
    fade : 1000,
    // コンテンツ種類
    // 先頭にある条件が先に判定される
    type : [
    //  {
    //      cond : function() {
    //          // コンテンツ include ファイル ロード条件判定
    //          // ロード: true, 次の条件に進む: false
    //          return true; // ないし false
    //      },
    //      file : '実際にロードするファイルのパス'
    //  }, ...
        { // 例: 土日用コンテンツ
            cond : function() {
                var day = (new Date()).getDay();
                return (0 === day || 6 === day);
            },
            file : '/shared/include/contents-top/auto-tab-weekend.html'
        },
        { // 例: 通常コンテンツ
            cond : function() { return true },
            file : '/shared/include/contents-top/auto-tab-default.html'
        }
    ]
});
</script>
*/


    // ------------------------------------------------------------------------
    // enable API 設定
    // ------------------------------------------------------------------------
    window.autoTab = function(options) {
        // ロード条件判定
        var type  = options.type,
            ntype = type.length,
            t, file;
        for (t = 0; t < ntype; t++) {
            if (type[t].cond()) {
                file = type[t].file;
                break;
            }
        }
        // ファイルロード
        file && $('#auto-tab').parent().load(file, function() {
            $('.auto-tabs', this).each(function() {
                (new AutoTab(this, options)).create();
            });
        });
    };


    // ------------------------------------------------------------------------
    // AutoTab: 自動切替機能つきタブ (depend: jQuery 1.3+)
    // ------------------------------------------------------------------------
    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, after;
            // 元処理実行
            origin.apply(this, arguments);
            // Trident: 閲覧者による操作時、前状態の blur → mouseout →
            //          mouseleave が発生するため __setAuto が実行され、
            //          結果的に自動切替が再開してしまう
            if (MJL.ua.trident && this._isValidId(id)) {
                // 明示的に __removeAuto を実行させる
                this.__removeAuto();
            }
            // フェードイン効果付与
            if (this.options.fade) {
                before = this.items[aid].__panel;
                // 初回起動時
                if (!this._isValidId(id)) {
                    // 表示タブパネルをフェイドイン
                    before.css('opacity', 0).animate({
                        'opacity' : 1
                    }, this.options.fade);
                // 通常切替
                } else if (id === this._getActiveId() && id !== aid) {
                    // 切替が早いと前のタブパネルが animate 中の可能性あり
                    //   -> stop させてスタイルを通常に戻しておく
                    before.stop().css('opacity', 1);
                    // 表示タブパネルをフェイドイン
                    this.items[id].__panel.css('opacity', 0).animate({
                        'opacity' : 1
                    }, this.options.fade);
                }
                // 同一タブパネルの場合は何もしない
            }
        };
    })();

    // 自動生成 id 属性値接頭語
    AutoTab.prototype._ID_PREFIX   = "MJL_AUTOTAB_ITEM_";

    // Cookie 項目名
    AutoTab.prototype._COOKIE_NAME = "MJL.AutoTab";

    // 各種イベント
    AutoTab.prototype._EVENTS = (function() {
        var origins = MJL.Tab.prototype._EVENTS,
            events  = {},
            o;
        // _EVENTS の参照を代入して元オブジェクトに影響を及ぼさないように
        // 各プロパティを明示的にコピー
        for (o in origins) {
            events[o] = origins[o];
        }
        // click 時の挙動は通常のアンカーと同等にさせるため、削除
        delete events.click;
        // click の代替として mouseover 時の切替を有効にする
        events.mouseover = function(event, id) {
            // 未アクティブ時のみ実行
            // アクティブ時に実行すると無限ループ化するため
            if (!MJL.hasClassName(this.items[id].tab, this.classes.active)) {
                this.active(id);
            }
            // mouseenter/mouseleave を阻害するため伝搬は阻止しない
        };
        // 新しいオブジェクトとして追加
        return events;
    })();

    // タブリスト生成
    AutoTab.prototype._createList = (function() {
        var origin = AutoTab.prototype._createList;
        return function(/* ... */) {
            // 元処理実行
            origin.apply(this, arguments);
            var items = this.items,
                id, item, href;
            for (id in items) {
                item = items[id];
                // 最初の a 要素のみ参照
                href = (
                    (item.panel.getElementsByTagName("a") || [])[0] || {}
                ).href;
                // タブ click 時にタブパネル内 a 要素と同一の URI に遷移する
                // ように変更
                if (href) {
                    item.anchor.href = href;
                }
                // img 要素をあらかじめ収集
                item.__imgs = MJL.convArray(
                    item.anchor.getElementsByTagName("img") || []
                );
            }
        };
    })();

    // タブパネルから id - アイテム集合を取得
    AutoTab.prototype._getContents = (function() {
        var origin = AutoTab.prototype._getContents;
        return function() {
            origin.apply(this, arguments);
            // jQuery オブジェクトをあらかじめ生成しておく
            var items  = this._items,
                nitems = items.length,
                i, item;
            for (i = 0; i < nitems; i++) {
                item = items[i];
                item.__panel = $(item.panel);
            }
        };
    })();

    // 次のアクティブタブに変更
    AutoTab.prototype.__activeNext = function() {
        var created = this._created,
            before, after, num, max,
            imgs, nimgs, i;
        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 アクティベートだけ実行
            if (created) {
                MJL.event.dispatch(before.anchor, "blur");
                imgs  = after.__imgs;
                nimgs = imgs.length;
                for (i = 0; i < nimgs; i++) {
                    MJL.event.dispatch(imgs[i], "mouseover");
                }
            }
            // 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;
        }
    };
})(jQuery);

