Event.observe(window, 'load', initRawkNRoll);

function initRawkNRoll(){
    observe_login();
}

function observe_post_comment(){
    var EL = $('postform');
    var BT = $('post_comment');
    var WAIT = $('shoutWait');
    if (EL == null || BT == null) 
        return false;
    BT.observe('click', function(){
        if ($('replymessage').value.blank()) {
            alert('е па напиши нещо де!');
            return;
        }
        if (WAIT) 
            WAIT.toggle();
        EL.toggle();
        
        EL.request({
            onSuccess: function(t){
                $('comments_data').insert(t.responseText);
                $('replymessage').value = '';
                update_comments_count();
            },
            onFailure: function(t){
                alert(t.statusText);
            },
            onComplete: function(t){
                if (WAIT) 
                    WAIT.toggle();
                EL.toggle();
            }
        });
    });
    return false;
}

function set_classname(el, cn){
    clean_classnames(el);
    el.addClassName(cn);
}

function clean_classnames(el){
    el.classNames().each(function(cn){
        el.removeClassName(cn);
    });
}

function get_id(idname){
    return parseInt(idname.split('_')[1]);
}

function observe_lock_comments(){
    var EL = $('lock_comments');
    if (EL == null) 
        return;
    EL.observe('click', function(){
        var ctype = parseInt($('ctype').innerHTML);
        var aid = parseInt($('aid').innerHTML);
        if (ctype < 1 || aid < 1) 
            return;
        set_classname(EL, 'loading-icon');
        var oldclass = EL.classNames()[0];
        var url = '/comments/lock/' + aid + '/' + ctype;
        new Ajax.Request(url, {
            method: 'get',
            onSuccess: function(t){
                set_classname(EL, t.responseText);
                $('showComment').toggle();
            },
            onFailure: function(t){
                set_classname(EL, oldclass);
            }
            
        });
    });
}

function observe_more(){
    $$('span[class="more-icon"]').each(function(el){
        el.observe('click', function(){
            el.setStyle({
                backgroundImage: 'url("/images/loading.gif")',
                backgroundRepeat: 'no-repeat',
                backgroundPosition: 'left'
            });
            var aid = get_id(el.id);
            new Ajax.Updater('article_content_' + aid, '/news/' + aid + '_m');
        });
    });
}

function observe_older(){
    if ($('older') == null) 
        return;
    $('older').observe('click', function(){
        var articles = $('articles').select('div[class="news-article-border"]');
        var page = parseInt(articles.length / 24) + 1;
        $('older').toggle();
        $('older-loading').toggle();
        new Ajax.Request('/news/p/' + page + '_o', {
            method: 'get',
            onSuccess: function(t){
                $('articles').innerHTML += t.responseText;
                $('older').toggle();
                $('older-loading').toggle();
                observe_more();
            }
        });
    });
}

function deleteComment(cid, aid){
    if (!confirm('трия батка, ар ю шуар?')) 
        return;
    var url = '/comments/delete/' + cid + '/' + aid;
    new Ajax.Request(url, {
        method: 'get',
        onSuccess: function(t){
            var comment = $('comment_' + cid);
            if (comment != null) 
                comment.remove();
            // update_comments_count();
        },
        onFailure: function(t){
            alert(t.responseText);
        }
    });
}

function update_comments_count(){
    var total_comments = $('total_comments');
    if (total_comments == null) 
        return;
    var comments = $('comments_data').select('li');
    total_comments.innerHTML = (comments.length > 0) ? '(' + comments.length + ')' : '';
}

function update_comments(){
    var comments = $('comments_data');
    if (comments == null) 
        return;
    var ctype = parseInt($('ctype').innerHTML);
    var aid = parseInt($('aid').innerHTML);
    var url = '/updater/comments/' + aid + '/' + ctype;
    new Ajax.PeriodicalUpdater('comments_data', url, {
        method: 'get',
        frequency: 10,
        decay: 1,
        insertion: Insertion.Bottom
    });
}

function simple_updater(el, ob, freq){
    var content = $(el);
    if (content == null) 
        return;
    new Ajax.PeriodicalUpdater(content, '/updater/' + ob, {
        method: 'get',
        frequency: freq,
        decay: 1
    });
}

function updater_frontpage(freq){
    var articles = $('articles');
    var label = $('news_label');
    if (articles == null || label == null) 
        return;
    
    var updater = new PeriodicalExecuter(_newsUpdater, freq);
    
    function _newsUpdater(){
        var last_id = articles.childElements().collect(function(el){
            return parseInt(el.id.split('_')[1]);
        }).max();
        new Ajax.Updater('articles', '/updater/news/', {
            parameters: {
                last_update: last_id
            },
            insertion: 'top',
            onComplete: function(t){
                var news = $('articles').select('div[class="news-article-border hidden"]');
                var n = news.size();
                if (n > 0) {
                    label.update(n + ' нови');
                    label.show();
                }
            }
        });
    }
    
    
    
}

function show_news(){
    var news_label = $('news_label');
    if (news_label == null) 
        return;
    news_label.observe('click', function(){
        var articles = $('articles');
        articles.select('div[class="news-article-border hidden"]').each(function(el, idx){
            articles.select('div[class="news-article-border"]').last().remove();
            el.removeClassName('hidden');
            el.toggle();
        });
        news_label.hide();
    });
}

var newwindow;

function observe_login(){
    var el = $('commentsLogin');
    if (el == null) 
        return;
    el.observe('click', function(){
        login('/login', 140, 330);
    });
}

function login(url, x, y){
    parameters = "height=" + x + ",width=" + y +
    ",status=no,toolbar=no,menubar=no,localtion=no,scrollbars=no";
    newwindow = window.open(url, 'name', parameters);
    if (window.focus) {
        newwindow.focus();
    }
}
