function toggle_plus(id){
    showlink = $('#e' + id + ' .showlink');
    if (showlink.text() == '+'){
        showlink.text('-');
    }
    else{
        showlink.text('+');
    }
}

function toggle_entry(entrylink){
    entrylink.toggle();
    href = entrylink.attr('href');
    id = href.split('/')[1];
    toggle_plus(id);
    entrytext = entrylink.parent();

    entrybody = $('#entrybody' + id);
    if (entrybody.length > 0){
        entrybody.toggle();
    }
    else{
        entrytext.append('<div class="rem">Loading...</div>');
        json_url = href + 'json/';
        $.get(json_url, {}, function(data){
            $('.rem').remove();
            entrytext.append('<div id="entrybody' + id + '">' +
                data[0]['fields']['text_html'] + '</div>');
        }, 'json');
    }
}

$(document).ready(function() {
    $('#entrylist > .lightentry, #entrylist > .darkentry').each(function(){
        entryheader = $(this).children('.entryheader').eq(0);
        entryheader.prepend('<a class="showlink" href="#">+</a>');
    });
    $('.entrytext .entrylink').click(function(){
        toggle_entry($(this));
        return false;
    });
    $('.showlink').click(function(){
        toggle_entry($(this).parent().parent().find('.entrytext .entrylink'));
    });
});
