MJL.event.add(window, "load", function(event) {
    // MJL に関係するコードはこの中に記述
    MJL.enable.flash("flash");
    MJL.enable.rollover("roll", {disable : "unroll"});
    MJL.enable.styleSwitcher("styleSwitcher");
    MJL.enable.heightEqualizer("equalize");
    MJL.enable.heightEqualizer("grid-2", {groupBy : 2});
}, false);

// ----------------------------------------------------------------------------
// not MJL Script
// ----------------------------------------------------------------------------
$(function() {
    function gnControl() {
        var bodyClass = $("body").attr("class");
        $("li", "#global-nav").each(function(){
            var listId = $(this).attr("id");
            if(bodyClass == listId) {
                $(this).addClass("unroll");
                $("> a > img", this).each(function(){
                    this.src = this.src.replace(/(\.[a-z]{3})$/, "_o$1");
                });
            }
        });
    }
    gnControl();
    
    //css support
    $("> li:last-child", "#local-nav").addClass("last-child");

    //layout
    var adjust = function(){
        $(".layout", "#main").each(function(){
            _c = $(this).attr("class");
            var _w1 = $(this).width();
            var _w2 = $('> .image img', this).width();
            $(this).width(_w1);
            $(".image", this).width(_w2);
            if(!_c.match("turn")){
                if(_c.match("right")){
                    $("> .column", this).css("margin-right", _w2 + 15);
                } else {
                    $("> .column", this).css("margin-left", _w2 + 15);
                }
            }
        });
    }
    if($.browser.safari){
        setTimeout(function(){
            adjust();
        }, 100);
    } else {
        adjust();
    }  

    //top category tooltip block
		$(function(){
        var _cate2_cont = $('#top div.category2');
        $('#top div.category div.segment').each(function(){
            $('h2 > a', this).hover(function(){
                var _seg_class = $(this).parents('div.segment').attr('class').replace(/segment /,'');
                $('div.'+_seg_class+'', _cate2_cont).addClass('hover').fadeIn('500');
            }, function(){
                var _seg_class = $(this).parents('div.segment').attr('class').replace(/segment /,'');
                $('div.'+_seg_class+'', _cate2_cont).removeClass('hover').hide();
            });
        });
    });

});

// ----------------------------------------------------------------------------
// MJL Extensions
// ----------------------------------------------------------------------------
(function(window, document) {
    // アクティブタブ class 属性値
    var active = "active";

    // isSameNode: 同一ノードの是非を取得
    MJL.isSameNode = document.isSameNode ? function(n1, n2) {
        return n1.isSameNode(n2); // W3C DOM
    } : function(n1, n2) {
        return (n1 == n2);        // Trident etc.
    };

    // イベント発送 (= fire)
    MJL.event.dispatch = document.createEvent ? (function() {
        // BUG Sf2: "HTMLEvents" でないと "DOM Exception 9" 例外発生
        // BUG Op: "Event" 未サポート
        var eventType = (MJL.ua.webkit && MJL.ua.version < 3) ? "HTMLEvents"
                                                              : "Event";
        return function(node, type, bubbles) {
            var event = document.createEvent(eventType);
            event.initEvent(type, bubbles, true);
            return node.dispatchEvent(event);
        };
    })() : document.fireEvent ? function(node, type, bubbles) {
        var event = document.createEventObject();
        event.cancelBubble = !bubbles;
        return node.fireEvent("on"+type, event);
    } : null,


    // --------------------------------
    // Tab
    // --------------------------------
    // アクティブタブ＆コンテンツ切替
    MJL.Tab.prototype._active = MJL.Tab.prototype.active;
    MJL.Tab.prototype.active = function(id) {
        var aid = this._getActiveId();
        this._active.apply(this, arguments);
        if (!this._isValidId(id)) { id = aid; }
        // Rollover アクティベート対応
        MJL.event.dispatch(this.items[id].event, "focus", false);
        if (id !== aid) {
            MJL.event.dispatch(this.items[aid].event, "blur", false);
        }
    };


    // --------------------------------
    // style.Switcher
    // --------------------------------
    // 生成
    MJL.style.Switcher.prototype._create = MJL.style.Switcher.prototype.create;
    MJL.style.Switcher.prototype.create = function() {
        this._create.apply(this, arguments);
        this.set(); // 同時に set を実行するようにしただけ
    };

    // スタイルを title に設定
    MJL.style.Switcher.prototype._set = MJL.style.Switcher.prototype.set;
    MJL.style.Switcher.prototype.set = function(title) {
        this._set.apply(this, arguments);
        // デフォルトスタイルシートの有無を考慮
        if (!title) {
            title = this._getNowStyleTitle();
        }
        var id  = this._title2id(title);
        var aid = isNaN(this._activeId) ? id : this._activeId;
        // Rollover アクティベート対応
        // this._activeId: アクティブなスイッチの ID
        MJL.addClassName(this.targets[id].node, active);
        MJL.event.dispatch(this.targets[id].node, "focus", false);
        if (id !== aid) {
            MJL.removeClassName(this.targets[aid].node, active);
            MJL.event.dispatch(this.targets[aid].node, "blur", false);
        }
        this._activeId = id;
    };

    // スタイルタイトルから id を逆算
    MJL.style.Switcher.prototype._title2id = function(title) {
        var targets = this.targets;
        var ntargets = targets.length;
        for (var t = 0; t < ntargets; t++) { // 線形探索するしかない
            if (title === targets[t].title) {
                return t;
            }
        }
        return 0;
    };

    // 現在アクティブなスタイルのタイトルを取得
    MJL.style.Switcher.prototype._getNowStyleTitle = function() {
        var sheets = document.styleSheets;
        var nsheets = sheets.length;
        for (var s = 0; s < nsheets; s++) {
            if (sheets[s].title && !sheets[s].disabled) {
                return sheets[s].title;
            }
        }
        return "";
    };


    // --------------------------------
    // Rollover
    // --------------------------------
    // 手動アクティブの是非
    MJL.Rollover.prototype._isActive = function(elem) {
        do {
            if (MJL.hasClassName(elem, active)) {
                return true;
            }
        } while (
            !MJL.isSameNode(elem, document) && (elem = elem.parentNode)
        );
        return false;
    };

    // 対象にする要素の種類-条件対応
    MJL.Rollover.prototype._TYPES = (function() {
        function getEventGetter(type) {
            return function(elem) {
                var off = elem.getAttribute("src");
                var on  = off.replace(
                    this.options.switchers.on.cond,
                    this.options.switchers.on.replace
                );
                var obj = this;
                var sw  = ("on" === type) ? true : false;
                this._addCache(on);
                return function(event) {
                    elem.setAttribute(
                        "src",
                        (obj._isActive(elem) || sw) ? on : off
                    );
                };
            };
        }
        function getDescendantEventGetter(type) {
            return function(elem) {
                var imgs = elem.getElementsByTagName("img");
                var nimgs = imgs.length;
                var getters = getEventGetter(type);
                var events = new Array(nimgs);
                for (var i = 0; i < nimgs; i++) {
                    events[0] = getters.call(this, imgs[i]);
                }
                return function(/* ... */) {
                    for (var i = 0; i < nimgs; i++) {
                        events[i].apply(this, arguments);
                    }
                };
            };
        }

        return {
            img : {
                isTarget : function() { return true; },
                getters : {
                    mouseover : getEventGetter("on"),
                    mouseout  : getEventGetter("off")
                }
            },
            input : {
                isTarget : function(elem) {
                    return ("image" == elem.getAttribute("type"));
                },
                getters : {
                    mouseover : getEventGetter("on"),
                    mouseout  : getEventGetter("off"),
                    focus     : getEventGetter("on"),
                    blur      : getEventGetter("off")
                }
            },
            a : {
                isTarget : function(elem) {
                    var imgs = elem.getElementsByTagName("img");
                    return (0 < imgs.length);
                },
                getters : {
                    focus : getDescendantEventGetter("on"),
                    blur  : getDescendantEventGetter("off")
                }
            }
        };
    })();
})(window, document);
