al_animation_time = 400;
function al_add_new_action(news_list,html,user_id,action_id){
    // usuwamy stara akcje poniewaz bedziemy wstawiac nowa na poczatku listy
    al_remove_action(news_list,action_id);
    // sprawdzamy czy pierwszym elementem listy jest juz akcja uzytkownika
    // jesli tak to dodajemy akcje na pierwsze miejsca a stara przesowamy do rozwiniecia akcji uzytkownika (anty spam)
    user_set = news_list.find('.action_set').first();
    console.log('1');
    if(user_set.length != 0){
        // if any action exists
        console.log('2');
        if(user_set.hasClass('action_set_'+user_id)){
            console.log('3');
            // if user set exists and is first element
            action_move = user_set.find('.item').first().clone(false);
            al_remove_element(user_set.find('.item').first());
            action_set_more = user_set.find('.action_set_more .action_inner');
            if(action_set_more.length == 0){
                console.log('4');
                //create action_set_more
                user_set.append('<div class="action_set_more"><a class="action_more roll_down" title="more"> </a><div class="action_inner hide" style="display:none"></div></div>');
                action_set_more = user_set.find('.action_set_more .action_inner');
            }
            // prepend action_set_more
            action_set_more.prepend(action_move);
        } else {
            console.log('5');
            news_list.prepend('<div class="action_set_'+user_id+' action_set"></div>');
            user_set = news_list.find('.action_set').first();
        }
    } else {
        console.log('6');
        // create first action set
        news_list.append('<div class="action_set_'+user_id+' action_set"></div>');
        user_set = news_list.find('.action_set').first();
    }
    console.log('7');
    user_set.prepend(html);
    al_show_element(user_set.find('#action_'+action_id).first());
}

function al_remove_action(news_list,action_id){
    action_item = $(news_list).find('#action_'+action_id).first();
    console.log('a1');
    if(action_item.parent().hasClass('action_set')){
        console.log('a2');
        //if is first action in action_set
        action_set = action_item.parent();
        action_set_more = action_set.find('.action_set_more').first();
        if(action_set_more.length == 0){
            console.log('a3');
            //if more actions doesnt exist
            // remove whole set
//            action_item.parent().remove();
            al_remove_element(action_item.parent());
        } else {
            console.log('a4');
            // move first element from action_set_more
//            action_item.remove();
            al_remove_element(action_item);
            action_move = action_set_more.find('.item').first().remove();
            action_set.prepend(action_move);
            if(action_set_more.find('.item').length == 0){
                console.log('a5');
//                action_set_more.remove();
                al_remove_element(action_set_more);
            }
        }
    } else if(action_item.parent().parent().hasClass('action_set_more')){
        console.log('a6');
        // if action_set_more is empty than remove the block
        action_set_more = action_item.parent().parent();
//        action_item.remove();
        al_remove_element(action_item);
        if(action_set_more.find('.item').length == 0){
            console.log('a7');
//            action_set_more.remove();
            al_remove_element(action_set_more);
        }
    }
    console.log('a8');
}

function al_show_element(element){   
    element.slideDown(al_animation_time);
    element.fadeIn(al_animation_time);
}


function al_remove_element(element){
    element.removeAttr('id');
    element.removeAttr('class');
//    element.animate({"height": "toggle", "opacity": "toggle"}, "slow");
    element.slideUp(al_animation_time);
    element.fadeOut(al_animation_time,function(){
        element.remove();
    })
}


function get_news_top(){
    $.ajax({
        type: 'POST',
        async: true,
        url: "/h/ajax/news_top",
        processData: false,
        data: "area_id="+_config.area_id+"&news_first_action_id="+news_first_action_id,
        dataType: 'json',
        beforeSend: function(){
        },
        success: function(msg){
            news_first_action_id = msg.news_first_action_id;
            if(msg.html.length > 0){
                for(var i in msg.html){
                    user_id = msg.users[i];
                    action_id = msg.actions[i];
                    al_add_new_action($('.jq_more_list'),msg.html[i],user_id,action_id);
                }
            }
        }
    });
}


$(document).ready(function(){
    // get news each 30 seconds
    news_top_timer = setInterval(get_news_top, 1*60*1000);

    $('#link_top_news').live('click',function(e){
        e.preventDefault();
        get_news_top();
    });
    
    $('.action_set_more a.action_more').live('click',function(e){
        e.preventDefault();
        al_inner = $(this).parent().find('.action_inner');
        if(al_inner.hasClass('hide')){
            $(this).addClass('roll_up');
            $(this).removeClass('roll_down');
            al_inner.addClass('show');
            al_inner.removeClass('hide');
            al_inner.slideDown(al_animation_time);
        }else {
            $(this).addClass('roll_down');
            $(this).removeClass('roll_up');
            al_inner.addClass('hide');
            al_inner.removeClass('show');
            al_inner.slideUp(al_animation_time);
        }
    });
});
