$(document).ready(function () {

    var overlayFavoriteHide = function(el) {
        var overlayFavorite = $(el).find("div.overlay-favorite");
        $(overlayFavorite).slideDown();
        $(overlayFavorite).width($(el).width());
    }

    var overlayFavoriteShow = function(el) {
        var overlayFavorite = $(el).find("div.overlay-favorite");
        $(overlayFavorite).slideUp();
        $(overlayFavorite).width($(el).width());
    }

    $('li.carousel-item').mouseover(function () {
        overlayFavoriteHide($(this));
    });

    $('li.carousel-item').mouseleave(function () {
        overlayFavoriteShow($(this));
    });

    $('.add-to-fav').click(function() {
        $addToFav = $(this);

        var mediaId = $(this).parent().parent().find('input[name$=hndMediaId]').val();

        var webServiceInput = { "mediaId": mediaId };
        var jsonStr = JSON.stringify(webServiceInput);
        $.ajax({
            type: 'POST',
            async: false,
            url: '/Services/FoamService.svc/AddFavoriteImage',
            data: jsonStr,
            contentType: "application/json; charset=utf-8",
            dataType: 'json',
            success: function(res) {
                if (res.d) {
                    $addToFav.hide();
                    $addToFav.parent().find('.see-in-profile').show();
                }
            }
        });
    });

    $('.remove-image').click(function() {
        $liImage = $(this).parent();

        var mediaId = $liImage.find('input[name$=hndMediaId]').val();

        var webServiceInput = { "mediaId": mediaId };
        var jsonStr = JSON.stringify(webServiceInput);
        $.ajax({
            type: 'POST',
            async: false,
            url: '/Services/FoamService.svc/RemoveFavoriteImage',
            data: jsonStr,
            contentType: "application/json; charset=utf-8",
            dataType: 'json',
            success: function(res) {
                if (res.d) {
                    location.reload();

//                    $lblImageCount = $('span[id$=lblImageCount]');

//                    var savedImagesStr = $lblImageCount.text();
//                    if (typeof(savedImagesStr) != 'undefined' && savedImagesStr != '') {
//                        var savedImages = parseInt(savedImagesStr);
//                        if (savedImages > 0) {
//                            $lblImageCount.text(savedImages - 1);
//                        }

//                        $liImage.hide();

//                        if (savedImages - 1 <= 0) {
//                            $('div[id$=pnlProfileFavorites]').hide();
//                        }
//                    }
                }
            }
        });
    });
});
