$(document).ready(function() {
                           
    /* Wish List Notification */
    $('#email a').colorbox({
        'width': 598, 
        'inline': true, 
        'href': '#mmpr-email-wishlist'
    });
    
    $('#mmpr-email-wishlist #wishlist-form').bind('submit', function (event) {
        var form = $(this);
        $.ajax({
            'url': form.attr('action'),
            'type': 'POST',
            'cache': false,
            'data': form.serialize(),
            'success': function (data, status) {
                $('#mmpr-email-wishlist #mmprlb-content').empty().append($(data));
            },
            'error': function (xhr, status, error) {
                alert('Sorry, there was an error while preparing to email your wishlist.');
            }
        });
        return false;
    });
    
    /* Wish List */
    
    $("#mmpr-wish-list").sortable({
        update: function(event, ui) {
            var data = $(this).sortable('serialize');
            $.ajax({
                'url': '/mmpr/wishlist/sort/',
                'type': 'POST',
                'cache': false,
                'data': data,
                'success': function (data, status) {
                    $('#mmpr-wish-list .number').each(function (i) {
                        $(this).text(i + 1);
                    });
                },
                'error': function (xhr, status, error) {
                    
                }
            });
        }
    });
    
    $('.remove a').bind('click', function (event) {
        var listItem = $(this).closest('#mmpr-wish-list > li').fadeOut(),
            url = $(this).attr('href');
        
        $.ajax({
            'url': url,
            'cache': false,
            'success': function (data, status) {
                listItem.fadeOut(function () {
                    listItem.remove();
                    $('#mmpr-wish-list .number').each(function (i) {
                        $(this).text(i + 1);
                    });
                });
            },
            'error': function (xhr, status, error) {
                alert('There was an error removing this item from your wishlist.');
            }
        });
        
        return false;
    });
    
});
