$(document).ready(function() {
    function displayFeaturedBook(itemId) {
        $.ajax({
            type: "POST",
            data: "itemId="+itemId,
            url: "scripts/ajax/displayFeaturedBook.php",
            success: function(msg) {
                $("#existingCategories").html(msg);
                $("#existingCategories div.listItem").removeClass('odd');
                $("#existingCategories div.listItem:odd").addClass('odd');
            },
            error: function (xhr, ajaxOptions, thrownError) {
                alert(xhr.status);
                alert(thrownError);
            }
        });
    }

    $("#homeFeatureBack").bind('click', function() {
        previousFeature();
    });
    $("#homeFeatureNext").bind('click', function() {
        nextFeature();
    });

});

function previousFeature() {
    var newCurrent;
    // Hide all homepage features
    $(".homepageFeature").css('display', 'none');
    // If the current feature is the last one, loop back to the first feature
    if (parseInt($("#currentFeature").val()) == 1) {
        newCurrent = parseInt($("#numberOfFeatures").val());
    }
    else {
        newCurrent = parseInt($("#currentFeature").val()) - 1;
    }

    $("#currentFeature").val(newCurrent);
    $("#homepageFeature"+newCurrent).css('display', 'block');
}

function nextFeature() {
    var newCurrent;
    // Hide all homepage features
    $(".homepageFeature").css('display', 'none');
    // If the current feature is the last one, loop back to the first feature
    if (parseInt($("#currentFeature").val()) >= parseInt($("#numberOfFeatures").val())) {
        newCurrent = 1;
    }
    else {
        newCurrent = parseInt($("#currentFeature").val()) + 1;
    }

    $("#currentFeature").val(newCurrent);
    $("#homepageFeature"+newCurrent).css('display', 'block');
}
