$(function(){
    $('a.dismiss_message').click(function(e){
        e.preventDefault();
        $(this).parents('.message').hide();
        if($('#messages > .container').children(':visible').size() == 0){
            $('#messages').hide();
        }
    });
    // :empty doesn't work bc the templates insert some white space...
    if($('#messages .container > *').size() == 0){
      $('#messages').hide();
    }
    $('a.PDF').click(function(e){
        var article_id = $(this).attr('article_id');
        $.ajax({
            type: "POST",
            url: "/articles/pdf/",
            data: "article_id=" + article_id,
            error: function(){
            },
            success: function(){
            }
        });
    });
    $('a.bundle').click(function(e){
        e.preventDefault();
        var article_id = $(this).attr('article_id');
        alert("bundle is being called");
        $.ajax({
            type: "POST",
            url: "/preferences/add_to_bundle_AJAX/",
            data: "article_id=" + article_id,
            error: function(){
                alert("problem creating bundle");
            },
            success: function(){
            }
        });
        return false;
    });
    $('a.save').click(function(e){
        e.preventDefault();
        var article_id = $(this).attr('article_id');
        var saveunsavelink = $(this);
        saveunsavelink.text("----");
        $.ajax({
            type: "POST",
            url: "/articles/save/",
            data: "save_article=" + article_id,
            error: function(){
                alert("error saving");
            },
            success: function(){
                saveunsavelink.text("Save");
                saveunsavelink.toggle();
                saveunsavelink.siblings().toggle();
            }
        });
        return false;
    });
    $('a.unsave').click(function(e){
        e.preventDefault();
        var article_id = $(this).attr('article_id');
        var saveunsavelink = $(this);
        saveunsavelink.text("----");
        $.ajax({
            type: "POST",
            url: "/articles/unsave/",
            data: "unsave_article=" + article_id,
            error: function(){
                alert("error unsaving");
            },
            success: function(){
                saveunsavelink.text("Unsave");
                saveunsavelink.toggle();
                saveunsavelink.siblings().toggle();
            }
        });
        return false;
    });
    $('a.logout').click(function(e){
        e.preventDefault();
        $('[name=logoutform]').submit();
    });

    $('#notes textarea').css('height', $('#note-text').height()+'px');
    $('#note-text').click(edit_notes);
    $('#edit_notes').click(edit_notes);
    $('#edit_notes_cancel').click(function(e){
        e.preventDefault();
        $('#note-text').show();
        $('#edit_notes').show();
        $('#note-form').hide();
    });
});

function edit_notes(e){
    $('#edit_notes').hide();
    $('#note-text').hide();
    $('#note-form').show();
}

