MJL.event.add(window, "load", function(event) {
    
    //
    // 画像とテキスト (based on jQuery)
    //
    // 画像とテキストの並列処理を行う関数を返す高階関数
    function getImgParallelStyler(side) {
        // side は既定値しか認めない
        if ("left" != side && "right" != side) {
            throw Error("invalid side type: '"+side+"'");
        }
        // 実行関数
        return function() {
            var obj = $(this);
            var imgBlock = $("div.image:first-child", obj);
            var imgWidth = $("p.image img", imgBlock).width(); // 画像幅
            imgBlock.css("width", imgWidth+14+"px"); // 画像幅を強制
            // 別カラムの margin を調整
            $("div.column", obj).css("margin-"+side,
                                     imgBlock[0].offsetWidth+"px");
        };
    }
    // 実行
    $(".image-parallel.image-L").each(getImgParallelStyler("left"));
    $(".image-parallel.image-R").each(getImgParallelStyler("right"));
    
    MJL.enable.rollover("roll", {disable : "unroll"});
    MJL.enable.styleSwitcher("styleswitch");
    //MJL.enable.flash("flash");
    MJL.enable.heightEqualizer("list-col2", {groupBy : 2});
    MJL.enable.heightEqualizer("list-col3", {groupBy : 3});
	MJL.enable.heightEqualizer("list-element-01", {groupBy : 2});
	MJL.enable.heightEqualizer("list-element-02", {groupBy : 2});
	MJL.enable.heightEqualizer("list-index-01", {groupBy : 2});
	MJL.enable.heightEqualizer("list-index-03", {groupBy : 2});
	MJL.enable.heightEqualizer("column-col2", {groupBy : 2});
    //MJL.enable.window("popup02", {width: 700, height: 700});


}, false);

// BUG IE6: 背景画像がキャッシュされない問題を修正
try { document.execCommand("BackgroundImageCache", false, true); } catch(e) {}


// ----------------------------------------------------------------------------
// not MJL Script
// ----------------------------------------------------------------------------
$(function() {
    // -------------------------------------------------
    // 採用サイト: 先輩を知るインデックスビューワ
    // -------------------------------------------------
    (function() {
        $('.box-staff-intro-01').each(function(){
            var stage = $(this);
            var photo = $('.intro img', stage);
            $('li a', stage).mouseover(function(){
                var obj = $('img', this);
                var cond = obj.attr('src').replace(/.+?staff-([^-]+?)\.jpg$/, '$1');
                photo.each(function(){
                    this.src = this.src.replace(/(photo-staff-)[^-]+?(\.jpg)$/, '$1' + cond + '$2');
                });
            });
        });
    })();
});


// ----------------------------------------------------------------------------
// 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);

