// Scripts specific to the lesefux app, mostly ajax updates
//
function add_to_shelf(item_id) {
    $.ajax({
        url: '/ajax/add_to_shelf/' + item_id + '/',
        success: function (data) {
            if (data != '\n'){
                  $('#my_bookshelf ul.bookshelf').append(data);
                  if ($('#my_bookshelf ul.bookshelf li').length > 10) {
                      $('#my_bookshelf ul.bookshelf li:first').remove();
                  }
                  $('#add_to_shelf_button_' + item_id).removeClass('add_to_shelf')
                  $('#add_to_shelf_button_' + item_id).addClass('on_shelf')
                  $('#add_to_shelf_button_' + item_id).text('On Shelf')
            }
            else{
                  alert('Rabbit Plan has only SoftCover books!!!');
            }
        }
    });
}

function remove_from_shelf(shelfitem_id, item_id) {
    $.ajax({
        url: '/ajax/remove_from_shelf/' + shelfitem_id + '/',
        success: function (data) {
            if (data == "deleted") {
                $('#shelf_item_' + shelfitem_id).remove();
            }
            if (item_id) {
                $('#add_to_shelf_button_' + item_id).removeClass('on_shelf')
                $('#add_to_shelf_button_' + item_id).addClass('add_to_shelf')
                $('#add_to_shelf_button_' + item_id).html(
                    '<a href="javascript:add_to_shelf(' + item_id + ');">Add To Shelf</a>')
            }
        }
    });
}

function rate_item(item_id, rating) {
    $.ajax({
        url: '/ajax/rate_item/' + item_id + '/' + rating + '/',
        dataType: 'json',
        success: function(data) {
            $('#your_rating').html("You rated this book " + rating + " stars");
            for (i = 1; i <= data.length +1; i++) {
                if (data[i-1] == true) {
                    $('#overall_rating_' + i).html("<span class='star_full' />");
                }
                else {
                    $('#overall_rating_' + i).html("<span class='star_empty' />");
                }
            }
        }
    });
}

function save_new_child_form(response, statusText) {
    if (response.indexOf("c_new") > -1) {
        //form was invalid, redisplay
        $('#new_child_container').html(response);
        $("#new_child_form").ajaxForm({
            success: save_new_child_form
        });
    }
    else {
        //form was valid, add to end of list and show link back
        $("#existing_children").append(response);
        var id = $("#existing_children form:last")[0].id;
        id = /child_form_(\d+)/.exec(id)[1];
        $('#child_message_' + id).html("The child was added");
        window.setTimeout(function() {
            $('#child_message_' + id).fadeOut('slow');
            }, 5000);
        $("#new_child_container").hide();
        $("#new_child_link").show();
    }
}

function show_new_child_form() {
    $("#new_child_container").load('/ajax/new_child/', '',
            function() {
                $("#new_child_container").show();
                $("#new_child_link").hide();
                $("#new_child_form").ajaxForm({
                    success: save_new_child_form
                });
            });
}

 //Functions from web designer
function change_view(newClass) {
    //Change between icon and list view on the home page
    $('#new_books_list')[0].className = newClass;
   $('#featured_books_list')[0].className = newClass;
   $('#contents')[0].className = "contents_refresh";
}
function change_shelf_view(newClass) {
    //Change between icon and list view on the home page
    $('#out_books_list')[0].className = newClass;
    $('#returned_books_list')[0].className = newClass;
    $('#ordered_books_list')[0].className = newClass;
    $('#requested_books_list')[0].className = newClass;
    $('#browse_contents')[0].className = "contents_refresh";
}

