Event.observe(window, 'load', load);

var pager;
var background;
var loadingAnimation;

function load() {
    background = new BackgroundOverlay(document.body);
    loadingAnimation = new LoadingAnimationOverlay(document.body);
    pager = new BasicPager();
    pager.load();
//    pager = new AjaxPager();
//    pager.search($('defaultPageNum').value);
}

var Pager = Class.create({
    options : [],

    url: '/search/works',

    initialize: function () {
        this.pager = $('pager');
        this.form  = document.searchForm;
        this.title = $('seachCondition');
    },

    getCurrentPageNumber: function () {
        return Number(this.getCurrentPageNumberInput().value);
    },

    getCurrentPageNumberInput: function () {
        return $('curPageNum');
    },

    setMoreReadEvent: function (className) {
        $A(document.getElementsByClassName(className)).each(function (anchor) {
            anchor.observe('click', function () {
                this.hide();
                this.previousElementSibling.hide();
                this.nextElementSibling.show();
            }.bind(anchor));
        });
    }

});

var BasicPager = Class.create(Pager, {

    load : function () {
        initPagerFavorite();
    },

    backgroundHide: function () {
        setTimeout(loadingAnimation.hide.bind(loadingAnimation), 200);
        setTimeout(background.hide.bind(background), 200);
    }

});

var AjaxPager = Class.create(Pager, {

    search: function (page) {
        if (page === undefined || page <= 0) {
            page = 1;
        }
        background.show();
        loadingAnimation.show();

        new Ajax.Request(
            '/search/getWords',
            {
                method: 'get',
                parameters: Form.serialize('search_form') + '&page=' + page,
                onSuccess: function (transport) {
                    if (transport.responseText) {
                        this.title.innerHTML = '<h2>' + (page + 'ページ目：' + transport.responseText + 'の派遣求人').truncate(40) + '</h2>';
                    } else {
                        this.title.innerHTML = '<h2>' + hpage + 'ページ目：' + '派遣求人' + '</h2>';
                    }
                }.bind(this)
            }
        );

        new Ajax.Request(
            this.url,
            {
                method: 'get',
                parameters: Form.serialize('search_form') + '&page=' + page,
                onSuccess: function (request) {
                    this.pager.innerHTML = request.responseText;
                    this.initMarquee();
                    this.setMoreReadEvent('moreAppealPoint');
                    this.setMoreReadEvent('moreJobContents');
                    initPagerFavorite();
                    setTimeout(loadingAnimation.hide.bind(loadingAnimation), 750);
                    setTimeout(background.hide.bind(background), 750);
                }.bind(this)
            }
        );
    },

    prev: function () {
        location.href = '#contents';
        this.search(this.getCurrentPageNumber() - 1);
    },

    next: function () {
        location.href = '#contents';
        this.search(this.getCurrentPageNumber() + 1);
    },

    initMarquee: function () {
        $A(document.getElementsByTagName('marquee')).each(function (marquee) {
            if (Prototype.Browser.Gecko) {
                marquee.init();
            }
        });
    }
});

