function initBooklist(series)
{
    var tablerow = '#book_table tr ';
    var selector = tablerow + 'td:nth-child(1) a, '
                 + tablerow + 'td:nth-child(4) a, '
                 + tablerow + 'td:nth-child(5) a, '
                 + '#recommendation a[href^="/books/"]';
    $(selector).click(function(){
        var matches = this.href.match(/books\/(\d+)/);
        view_book(matches[1]);
        return false;
    });

    $('#book_table tbody tr').each(function(){
        var tds = $(this).find('td');
        var sort_title = sortable_string($(tds[0]).text());
        for (i = 0; i < tds.size(); i++) {
            var td = tds[i];
            var data = null;
            switch (i) {
                case 0: // title
                    data = sort_title;
                    break;
                case 1: // author(s)
                    data = strip_spaces($(td).text());
                    break;
                case 2: // series/volume
                    var text = strip_spaces($(td).text());
                    if (text) {
                        var matches = text.match(/(.*)\s+(\d+)/);
                        var title = matches[1];
                        var volume = matches[2];
                        data = sortable_string(title) + pad_number(volume);
                    }
                    else {
                        data = 'ZZZ';
                    }
                    break;
                case 3: // rating
                    var img = $(td).find('img');
                    data = (img.size()
                            ? ''+(10 - parseInt(img[0].alt))
                            : 'ZZZ');
                    break;
                case 4: // notes
                    data = ($(td).find('a').size() ? 'a' : 'z');
                    break;
                default:
                    data = strip_spaces($(td).text());
                    if (!data)
                        data = 'ZZZ';
                    break;
            }
            td._book_sort_data = data;
        }
    });

    $('#book_table').tablesorter({
        sortList: (series ? [[2,0]] : [[0,0]]),
        textExtraction: function(td) {
            return td._book_sort_data;
        }
    });
}


function pad_number(num, length)
{
    if (length == undefined)
        length = 2;
    num = num.toString();
    while (num.length < length)
        num = '0' + num;
    return num;
}

function strip_spaces(str)
{
    // http://blog.stevenlevithan.com/archives/faster-trim-javascript
   	var	str = str.replace(/^\s\s*/, ''),
	    ws = /\s/,
	    i = str.length;
	while (ws.test(str.charAt(--i)));
	return str.slice(0, i + 1);
}

/**
 * Trim leading/trailing whitespace and remove the word "the", if it's the
 * first word in a title
 */
function sortable_string(s)
{
    return strip_spaces(s).replace(/^The\s+/, '');
}



function view_book(id)
{
    $.get('/books/'+id+'?popup=1', null, show_dialogue);
    return false;
}

function show_dialogue(data)
{
    var div = $('<div/>').html(data);
    var title = div.find('h1:first').html();
    div.find('h1:first').remove();
    $('body').append(div);
    var width = data.length > 2500 ? 600 : null;
    div.dialog({title: title, width: width});
}

