Event.observe(window, 'load', load);

var prefModalDialog;
var lineModalDialog;
var cityModalDialog;
var jobModalDialog;
var styleModalDialog;
var industryModalDialog;
var environmentModalDialog;
var qualificationModalDialog;
var experienceModalDialog;

var optMgr;
var prefMgr;
var cityMgr;
var lineMgr;
var stationMgr;
var jobMgr;
var industryMgr;
var environmentMgr;
var qualificationMgr;
var experienceMgr;
var styleMgr;


var prefLinkMgr;
var cityLinkMgr;
var lineLinkMgr;
var stationLinkMgr;
var jobLinkMgr;
var industryLinkMgr;
var environmentLinkMgr;
var qualificationLinkMgr;
var experienceLinkMgr;
var styleLinkManager;

var keywordSearchBtn;
var salarySelect;

function load() {
    prefModalDialog          = new PrefModalDialog(document.body);
    lineModalDialog          = new LineModalDialog(document.body);
    stationModalDialog       = new StationModalDialog(document.body);
    cityModalDialog          = new CityModalDialog(document.body);
    jobModalDialog           = new JobModalDialog(document.body);
    styleModalDialog         = new StyleModalDialog(document.body);
    industryModalDialog      = new IndustryModalDialog(document.body);
    environmentModalDialog   = new EnvironmentModalDialog(document.body);
    qualificationModalDialog = new QualificationModalDialog(document.body);
    experienceModalDialog    = new ExperienceModalDialog(document.body);

    var map = {
        pref:          prefModalDialog,
        line:          lineModalDialog,
        station:       stationModalDialog,
        city:          cityModalDialog,
        job:           jobModalDialog,
        style:         styleModalDialog,
        industry:      industryModalDialog,
        environment:   environmentModalDialog,
        qualification: qualificationModalDialog,
        experience:    experienceModalDialog
    };

    $A(document.getElementsByClassName('thickbox')).each(function (anchor) {
        var modal;
        for (key in map) {
            if (anchor.id.match(new RegExp(key, 'i'))) {
                modal = map[key];
                break;
            }
        }
        anchor.observe('click', function () {
            background.show();
            loadingAnimation.show();
            modal.open(this.href);
            jQuery(jQuery.browser.safari ? 'body' : 'html').animate({
                scrollTop: jQuery('main').offset().top
            }, 1000, "swing");
        }.bind(anchor));
    });

    optMgr               = new ThickboxOptionManager(new ThickboxOptionFactory());
    prefMgr              = new PrefManager(new PrefFactory(), prefModalDialog);
    cityMgr              = new CityManager(new CityFactory(), cityModalDialog);
    lineMgr              = new LineManager(new LineFactory(), lineModalDialog);
    stationMgr           = new StationManager(new StationFactory(), stationModalDialog);
    jobMgr               = new JobManager(new JobFactory(), jobModalDialog);
    industryMgr          = new IndustryManager(new IndustryFactory(), industryModalDialog);
    environmentMgr       = new EnvironmentManager(new EnvironmentFactory(), environmentModalDialog);
    qualificationMgr     = new QualificationManager(new QualificationFactory(), qualificationModalDialog);
    experienceMgr        = new ExperienceManager(new ExperienceFactory(), experienceModalDialog);
    styleMgr             = new StyleManager(new StyleFactory(), styleModalDialog);

    prefLinkMgr          = new PrefLinkManager([optMgr]);
    cityLinkMgr          = new CityLinkManager([prefMgr, optMgr]);
    lineLinkMgr          = new LineLinkManager([prefMgr, optMgr]);
    stationLinkMgr       = new StationLinkManager([prefMgr, lineMgr, optMgr]);
    jobLinkMgr           = new JobLinkManager([optMgr]);
    industryLinkMgr      = new IndustryLinkManager([optMgr]);
    environmentLinkMgr   = new EnvironmentLinkManager([optMgr]);
    qualificationLinkMgr = new QualificationLinkManager([optMgr]);
    experienceLinkMgr    = new ExperienceLinkManager([optMgr]);
    styleLinkMgr         = new StyleLinkManager([optMgr]);

    keywordSearchBtn     = new KeywordSearchButton();
    salarySelect         = new SalarySelect();

    new Form.Observer(document.searchForm, 0.33, function (element, value) {
        updateParameter();
        //setTimeout(pager.search.bind(pager), 500);
//        pager.search(1);
        pager.backgroundHide();
    });

    $A(document.getElementsByClassName('recruitmentBtn')).each(function (anchor) {
        new RecruitmentBox(anchor, $('input_' + anchor.id));
    });

    updateParameter();
}

function updateParameter() {
    prefLinkMgr.update();
    cityLinkMgr.update();
    lineLinkMgr.update();
    stationLinkMgr.update();
    jobLinkMgr.update();
    industryLinkMgr.update();
    environmentLinkMgr.update();
    qualificationLinkMgr.update();
    experienceLinkMgr.update();
    styleLinkMgr.update();
}
