if (typeof $ != 'function') {
    function $(el) { return (typeof el == 'string') ? document.getElementById(el) : el };
}

function MicropostNav() {
    var self = this;

    this.activateBack = function () {
        if (this.informer_back.className == 'micropost_nav_active') return;
        this.informer_back.className = 'micropost_nav_active';
        addHandler(this.informer_back, 'click', this.informerBack);
    };
    this.disableBack = function () {
        if (this.informer_back.className == 'micropost_nav_disabled') return;
        this.informer_back.className = 'micropost_nav_disabled';
        removeHandler(this.informer_back, 'click', this.informerBack);
    };
    this.activateForward = function () {
        if (this.informer_forward.className == 'micropost_nav_active') return;
        this.informer_forward.className = 'micropost_nav_active';
        addHandler(this.informer_forward, 'click', this.informerForward);
    };
    this.disableForward = function () {
        if (this.informer_forward.className == 'micropost_nav_disabled') return;
        this.informer_forward.className = 'micropost_nav_disabled';
        removeHandler(this.informer_forward, 'click', this.informerForward);
    };

    this.informerBack = function (e) {
        e = e || window.event;
        var len = self.posts.length;
        var prefetch = 0;
        if (self.current_post < len) { // switch to prev post if already fetched
            self.posts[self.current_post - 1].style.display = 'none';
            self.posts[self.current_post].style.display = '';
            self.current_post++;
            prefetch = 1;
        }
        // activate `next` link if needed
        if (self.current_post > 1) {
            self.activateForward();
        }
        if (self.current_post == self.last_post) {
            self.disableBack();
            cancelEvent(e);
            return;
        }
        if (self.current_post == len) { // switch to prev post if not fetched or prefetch for future fast switching
            if (!self.id_re.exec(self.posts[self.current_post - 1].id)) return;
            if (!prefetch) {
                self.disableBack();
                self.waiting_new_post = 1;
            }
            perl_prev_informer(RegExp.$1, prefetch, PerlVar_back_value, self.prev_informer_callback);
        }
        cancelEvent(e);
    };
    this.informerForward = function (e) { // no prefetch here. Switch forward only to informers[0] post
        e = e || window.event;
        self.waiting_new_post = 0;
        var len = self.posts.length;
        if (self.current_post > 1) {
            self.posts[--self.current_post].style.display = 'none';
            self.posts[self.current_post - 1].style.display = '';
        }
        if (self.current_post == 1) {
            self.disableForward();
        }
        if (self.current_post < len) {
            self.activateBack();
        }
        cancelEvent(e);
    };

    this.prev_informer_callback = function (PostID, html, prefetch) {
        if (PostID == 0) {
            self.last_post = self.posts.length;
            self.disableBack();
            return;
        }

        var div = document.createElement('div');
        div.id = 'post_' + PostID;
        div.innerHTML = html;
        self.posts.push(div);

        if (!prefetch && self.waiting_new_post) {
            // should switch and user still waiting for new post
            self.informerBack();
        } else {
            div.style.display = 'none';
        }
        self.container.appendChild(div);
        self.waiting_new_post = 0;
    };


    // подключаемся работаем
    this.enable = function () {
        self.container = $('microposts_container');
        self.informer_back = $('micropost_informer_back');
        self.informer_forward = $('micropost_informer_forward');
        if (!self.container || !self.informer_back || !self.informer_forward) return;

        self.informer_back.onclick = function() { return false };
        self.informer_forward.onclick = function() { return false };

        self.posts = [];
        self.id_re = /^post_([a-fA-F0-9]+)$/;
        var childs = self.container.getElementsByTagName('div');
        var len = childs.length;
        for (var i = 0; i < len; i++) {
            var elem = childs[i];
            if (!self.id_re.test(elem.id)) continue;
            self.posts.push(elem);
        }
        self.current_post = 1;
        if (self.posts.length > 1) {
            self.activateBack();
        }
    };
}
var mpnav = new MicropostNav()

addHandler(document, 'dom:loaded', mpnav.enable);
