﻿var likeClassName = 'adorar';

function loadLikeIconList() {
    loadLikeIconListInContext('');
}

function loadLikeIconListInContext(pSelectorContext) {
    var likeRequestArray = new Array();

    if (pSelectorContext && pSelectorContext != '') {
        pSelectorContext = pSelectorContext + ' ';
    }

    var likeIconList = $(pSelectorContext + '.' + likeClassName);

    likeIconList.each(function (e) {
        var contentId = $(this).attr('contentId');
        var contentTypeId = $(this).attr('contentTypeId');

        likeRequestArray.push({ "ContentId": contentId, "ContentTypeId": contentTypeId });
    });

    if (likeIconList.length > 0) {
        callServerMethodByPost('/Service/Like/LikeService.svc/GetLikeList/' + LoggedUserId, likeRequestArray, loadLikeIconListResponse, likeIconList);
    }
}

function loadLikeIconListResponse(pResponse, pLikeIconList) {
    for (var x = 0; x < pResponse.length; x++) {
        var likeIcon = pLikeIconList.filter('[contentId="' + pResponse[x].ContentId + '"][contentTypeId="' + pResponse[x].ContentTypeId + '"]');

        likeIcon.attr('class', likeClassName);
        likeIcon.html('(' + pResponse[x].Total + ')');

        if (pResponse[x].UserAlreadyLiked) {
            disableLink(likeIcon, 'Você já adorou este item!');
        }
    }
}

function likeClickHandler() {
    if (!$(this).hasClass('disabled')) {
        callServerMethod('/Service/Like/LikeService.svc/AddLike/' + $(this).attr('contentId') + '/' + $(this).attr('contentTypeId') + '/' + LoggedUserId, likeClickResponse, $(this));
        clearRequestCache();
    }

    return false;
}

function likeClickResponse(pResponse, pCallerReference) {
    pCallerReference.html('(' + pResponse + ')');
    disableLink(pCallerReference, 'Você já adorou este item!');
    ModalBox.Show({ Title: 'Item adorado', Description: '', Width: 270, Height: 'auto' });
    //showMessage('Item adorado', true);
}

$(document).ready(function() {
    $('.' + likeClassName).live('click', likeClickHandler);
});
