/*
 * @author Timo 'lino' Kissing <info@teukros.de>
 */
window.teukros = {
    post_box_class: 'post',
    link_class: 'nav_link',
    auto_id: 1,
    fade_time: 0.5,
    init: function() {
        teukros.cn(teukros.link_class, $('header')).each(teukros.inject_onclick);
        teukros.inject_onclick($('permalink'));
        if (window.this_is_ie7) {
            teukros.cn(teukros.post_box_class).each(function(node) {
                node.setStyle({zoom: 1});
            });
        }
    },
    cn: function(name,parent) {
        ret = parent
            ? document.getElementsByClassName(name, parent)
            : document.getElementsByClassName(name);
        return ret;
    },
    gc: function() {
        cbox = $('content');
        posts = teukros.cn(teukros.post_box_class, cbox);
        return posts.findAll(function(node) {
            if (Element.visible(node)) {
                return true;
            }
            return false;
        });
    },
    fp: function(ele) {
        while (ele && !Element.hasClassName(ele, teukros.post_box_class)) {
            ele = ele.parentNode;
        }
        return ele;
    },
    force_id: function(ele) {
        if (!ele) return false;
        ele_id = ele.id ? ele.id : ele.getAttribute('id');
        ele.id = ele_id ? ele_id : 'teukros_auto_id' + teukros.auto_id++;
        return ele.id;
    },
    set_perma: function(obj) {
        teukros.do_set_perma(teukros.force_id(obj.element));
    },
    do_set_perma: function(post_id) {
         teukros.cn(teukros.link_class, $('header')).each(function(node) {
            Element.removeClassName(node.parentNode, 'current_page_item');
        });
        Element.addClassName($('nav_' + post_id).parentNode,
            'current_page_item');
        perma = $('permalink');

        perma.href = post_id == 'index'
                    ? window.baseuri
                    : window.baseuri + '/' + post_id;
        perma.innerHTML = perma.href + '';
        perma.setAttribute('rel', post_id);
        teukros.busy = false;
    },
    prevent_multiple: function(post_id) {
        try {
            post_id = post_id ? post_id
                    : $('permalink').getAttribute('rel');
            if (post_id) {
                old = teukros.gc().findAll(function(node) {
                    return node.id != post_id;
                });
                old.each(function(node) {
                    Effect.BlindUp(node, {
                        queue: 'end',
                        duration: teukros.fade_time
                    })
                });
            }
            if (!Element.visible(post_id)) {
                teukros.show(post_id);
            }
        } catch(e) {alert(e)}
    },
    show: function(post_id) {
        Effect.Appear(post_id, {
            queue: 'end',
            duration: teukros.fade_time,
            afterFinish: teukros.set_perma
        });
    },
    set_current_page: function(post_id) {
        Effect.Fade('loadingAni', {
            queue: 'end',
            duration: teukros.fade_time
        });
        teukros.gc().each(function(node) {
            Effect.Fade(node, {
                queue: 'end',
                duration: teukros.fade_time
            })
        });
        cbox = $('content');
        next = $(post_id);
        if (next.parentNode != cbox) {
            Element.hide(next);
            cbox.appendChild(next.parentNode.removeChild(next));
        }
        teukros.show(post_id);
    },
    inject_onclick: function(ele) {
        if (!ele) return false;
        teukros.force_id(ele);
        link = ele.nodeName == 'A'
            ? ele
            : ele.getElementsByTagName('a')[0];
        Event.observe(link, 'click', teukros.load_page, false);
    },
    load_page: function(evt) {
        Event.stop(evt);
        if (teukros.busy) return false;
        teukros.busy = true;
        link = Event.element(evt);
        link.blur();
        link_id = link.id + '';
        post_id = link_id.replace(/^nav_/, '');
        post_id = link_id == 'permalink'
                ? link.getAttribute('rel')
                : link_id.replace(/^nav_/, '');
        post = $(post_id);
        if (post) {
            if (Element.visible(post)) {
                teukros.prevent_multiple(post_id);
                new Effect.Highlight(post, {
                    duration: 4 * teukros.fade_time,
                    queue: 'end'});
                teukros.busy = false;
                return false;
            } else {
                teukros.set_current_page(post_id);
            }
        } else {
            teukros.do_load_page(post_id);
        }
    },
    do_load_page: function(post_id) {
        Element.show('loadingAni');
        var myAjax = new Ajax.Updater(
            {   success: 'myEleCache'},
            window.ajaxuri + '/' + post_id,
            {   method: 'get',
                evalScripts: true,
                onFailure: function() {
                    window.location.href = window.baseuri + '/' + post_id;
                }
            }
        );
        return false;
    }
}
Event.observe(window, 'load', teukros.init, false);
