var Overlay = Class.create({
    initialize: function (parentNode) {
        this.parentNode = parentNode;
        this.initTag();
    },

    initTag: function () {
    },

    show: function () {
        this.parentNode.appendChild(this.node);
        jQuery(this.node).fadeIn();
    },

    hide: function () {
        $A(this.parentNode.childNodes).each(function (childNode) {
            if (this.node == childNode) {
                this.parentNode.removeChild(this.node);
            }
        }.bind(this));
    }
});

var BackgroundOverlay = Class.create(Overlay, {
    initTag: function () {
        this.node           = document.createElement('div');
        this.node.id        = 'modalOverlay';
        this.node.className = 'modalOverlayBG';
    }
});

var LoadingAnimationOverlay = Class.create(Overlay, {
    loadingAnimation: '/img/circle_thickbox.gif',

    initTag: function () {
        var img = document.createElement('img');
        img.src = this.loadingAnimation;
        img.style.opacity = '0.5';
        this.node = document.createElement('div');
        this.node.id = 'modalLoad';
        this.node.style.display = 'block';
        this.node.appendChild(img);
    }
});

var ModalDialog = Class.create(Overlay, {
    initTag: function () {
        this.node = document.createElement('div');
        this.node.id = 'modalWindow';
        this.node.style.position = 'absolute';
        this.node.style.left = (document.body.clientWidth - 750) / 2 + 'px';
        this.node.style.display = 'block';
    },

    open: function (url) {
        new Ajax.Request(
            url,
            {
                method: 'get',
                onSuccess: function (transport) {
                    this.preOpen();
                    this.setContent(transport.responseText);
                    this.keyupEvent();
                    this.show();
                    loadingAnimation.hide();
                    this.postOpen();
                }.bind(this)
            }
        );
    },

    preOpen: function () {
    },

    postOpen: function () {
    },

    setContent: function (text) {
        var content = document.createElement('div');
        content.id = 'modalAjaxContent';
        content.innerHTML = text;
        this.node.appendChild(content);
    },

    deleteContent: function () {
        $A(this.node.childNodes).each(function (childNode) {
            this.node.removeChild(childNode);
        }.bind(this));
    },

    keyupEvent: function () {
        document.onkeyup = function (e) {
            var keycode;
            if (e == null) {
                keycode = event.keyCode;
            } else {
                keycode = e.which;
            }
            if (keycode == 27) {
                this.remove();
            }
        }.bind(this);
    },

    hide: function () {
        $A(this.node.childNodes).each(function (childNode) {
            this.node.removeChild(childNode);
        }.bind(this));
        $A(this.parentNode.childNodes).each(function (childNode) {
            if (this.node == childNode) {
                this.parentNode.removeChild(this.node);
            }
        }.bind(this));
    },

    remove: function () {
        this.hide();
        background.hide();
    }
});

var PrefModalDialog = Class.create(ModalDialog, {
    name: 'pref',

    postOpen: function () {
        new CloseBtn(this);

        var dialog = new DialogForm(prefMgr);
        dialog.initAnchorEvent();
        dialog.initFormEvent();
        prefMgr.defaultCheck();
        dialog.initFormCheckbox();
    }
});

var CityModalDialog = Class.create(ModalDialog, {
    name: 'city',

    postOpen: function () {
        new CloseBtn(this);

        var dialog = new DialogForm(cityMgr);
        dialog.initAllChecker();
        dialog.initAnchorEvent();
        dialog.initFormEvent();
        dialog.initSearchButton();

        cityMgr.defaultCheck();
    }
});

var LineModalDialog = Class.create(ModalDialog, {
    name: 'line',

    postOpen: function () {
        new CloseBtn(this);

        var dialog = new DialogForm(lineMgr);
        dialog.initAnchorEvent(true);
        dialog.initFormEvent(true);
        lineMgr.defaultCheck();
        dialog.initFormCheckbox();
    }
});

var StationModalDialog = Class.create(ModalDialog, {
    name: 'station',

    postOpen: function () {
        new CloseBtn(this);

        var dialog = new DialogForm(stationMgr);
        dialog.initAnchorEvent();
        dialog.initFormEvent();
        dialog.initSearchButton();

        stationMgr.defaultCheck();

        new PrevPageLink(stationMgr);

        var lineDialog = new DialogForm(lineMgr);
        lineDialog.initAnchorEventForLine();
    }
});

var JobModalDialog = Class.create(ModalDialog, {
    name: 'job',

    postOpen: function () {
        new CloseBtn(this);

        var dialog = new DialogForm(jobMgr);
        dialog.initAllChecker();
        dialog.initAnchorEvent();
        dialog.initFormEvent();
        dialog.initSearchButton();

        jobMgr.defaultCheck();
    }
});

var StyleModalDialog = Class.create(ModalDialog, {
    name: 'style',

    postOpen: function () {
        new CloseBtn(this);

        var dialog = new DialogForm(styleMgr);
        dialog.initAnchorEvent();
        dialog.initFormEvent();
        dialog.initSearchButton();

        styleMgr.defaultCheck();
    }
});

var IndustryModalDialog = Class.create(ModalDialog, {
    name: 'industry',

    postOpen: function () {
        new CloseBtn(this);

        var dialog = new DialogForm(industryMgr);
        dialog.initAllChecker();
        dialog.initAnchorEvent();
        dialog.initFormEvent();
        dialog.initSearchButton();

        industryMgr.defaultCheck();
    }
});

var EnvironmentModalDialog = Class.create(ModalDialog, {
    name: 'environment',

    postOpen: function () {
        new CloseBtn(this);

        var dialog = new DialogForm(environmentMgr);
        dialog.initAllChecker();
        dialog.initAnchorEvent();
        dialog.initFormEvent();
        dialog.initSearchButton();

        environmentMgr.defaultCheck();
    }
});

var QualificationModalDialog = Class.create(ModalDialog, {
    name: 'qualification',

    postOpen: function () {
        new CloseBtn(this);

        var dialog = new DialogForm(qualificationMgr);
        dialog.initAnchorEvent();
        dialog.initFormEvent();
        dialog.initSearchButton();

        qualificationMgr.defaultCheck();
    }
});

var ExperienceModalDialog = Class.create(ModalDialog, {
    name: 'experience',

    postOpen: function () {
        new CloseBtn(this);

        var dialog = new DialogForm(experienceMgr);
        dialog.initAllChecker();
        dialog.initAnchorEvent();
        dialog.initFormEvent();
        dialog.initSearchButton();

        experienceMgr.defaultCheck();
    }
});
