var Books = {};
Books.bookData = null;

//**********************************************************************
// Initialization function
//**********************************************************************
$(function() {
    //$.getJSON('books.json', Books.initialize);
    Books.statusMessage('Loading books data...');
    $.ajax({
        url: 'books.json',
        dataType: "json",
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            console.log("ERROR fetching JSON book data, msg: "+textStatus);
        },
        complete: function(){Books.statusMessage(null);},
        success: function(bookData) {
            // Make the book data globally available.
            Books.bookData = bookData;
            
            // One-time data fixes
            //Books.fixData();

            // Set up the handlers for the interface.
            Books.statusMessage('Initializing...');
            
            Books.setupPage();
            Books.setupForms();
            Books.setupTables();
            
            Books.statusMessage(null);
        }
    });
});

//**********************************************************************
// fixData: one-time data fixes
//**********************************************************************
Books.fixData = function() {
    /*
    $.each(Books.bookData.books, function(i,a) {
        if(a.readDate) {
            a.status = 'HaveRead';
        }
    });
    */
};

//**********************************************************************
// setupPage: Allow each main page section to be collapsed
//**********************************************************************
Books.setupPage = function()
{
    $('div.section-title').toggle(function() {
        $(this).next().hide('normal');
    }, function() {
        $(this).next().show('normal');
    });
    
    // Make the page sections resizable vertically.
    $('.page-section').resizable({
        //autoHide: true,
        //transparent: true,
        stop: function(e, ui) {
            $('div.section-table', ui.element)
                .css('height', ui.size.height - 35);
        },
        handles: 's'
    });

    // Start some page sections out closed
    //$('#authorListSection').next().hide();
    $('#authorListSection').click();

};



