﻿
//Video detail page
videoDetailPage = {
    init: function(videoId, videoGalleryId, videoIdInt) {

        // Set videoitem events
        $('#main-container .flag-as-inappropriate-video').click(function() {
            $.pf.videoDetailPage.report.flagAsInappropriate(videoId);
        });

        $('#resource-title-section').find('.likes-vote .like').click(function() {
            $.pf.videoDetailPage.likes.registerUpdate(videoId, 'like');
        });

        $('#resource-title-section').find('.likes-vote .dislike').click(function() {
            $.pf.videoDetailPage.likes.registerUpdate(videoId, 'dislike');
        });

        $.pf.videoDetailPage.likes.displayLikesStatus();

        $(".share-menu li.friend").click(function() {
            $("div.share-menu-container div.tip-a-friend-menu span.close").click(function(index, span) {
                $("div.share-menu-container div.tip-a-friend-menu").hide();
            });
            $("div.share-menu-container div.share-menu").hide();
            $("div.share-menu-container div.tip-a-friend-menu").show();
        });

        $(".tip-a-friend-menu .button").click(function(index, send_to_friend_link) {
            $.pf.videoDetailPage.sendToAFriend.send($('.share-menu-container'), videoId);
        });

        $.pf.videoDetailPage.comments.init(videoIdInt);


        // Render list
        $.pf.jsonRepeater.initMostPopularTopicVideoItems(videoGalleryId, "4", videoId, "video");

    },
    comments: {
        currentItem: null,
        init: function(videoId) {
            $('#comments-section .button-section .button').click(function() { $.pf.videoDetailPage.comments.createNewComment(videoId, $('#comments-section textarea').val()); return false; })
            $('#comments-section .see-all-section').click(function() {
                $('#comments-section .comment:gt(5)').fadeIn('slow');
                $(this).toggleClass('hidden');
                $('#comments-section .hide-all-section').toggleClass('hidden');
            });
            $('#comments-section .hide-all-section').click(function() {
                $('#comments-section .comment:gt(5)').fadeOut('fast');
                $(this).toggleClass('hidden');
                $('#comments-section .see-all-section').toggleClass('hidden');
                $('#comments-section .comment:gt(5)').fadeOut('fast');
            });
            $.pf.videoDetailPage.comments.getComments(videoId);
        },
        getComments: function(videoId) {
            var payload = { VideoCommentItems: { videoId: videoId} };
            $.pf.data.doJsonPost('videoDetailPage.comments.renderTemplate', payload);
        },
        renderTemplate: function(response) {
            if (response[0].DataSet[0]) {
                if ($.isArray(response[0].DataSet)) {
                    $('#comment-template').setTemplateURL('/Templates/Main/Scripts/CommentTemplate.htm');
                    $('#comment-template').processTemplate(response[0]);
                }
                else {
                    $('#comment-template').html(response[0].DataSet);
                }
            }
            $.pf.videoDetailPage.comments.setCommentItemEvents();
        },
        deleteComment: function(current, commentId) {
            currentItem = current;
            var payload = { DeleteVideoComment: { commentId: commentId} };
            $.pf.data.doJsonPost('videoDetailPage.comments.commentDeleted', payload);
        },
        commentDeleted: function(value) {
            //Delete from list
            if (value[0].DataSet) {
                $('#comments-section .comment').each(function() {
                    if (parseInt($(this).data('id')) == value[0].DataSet)
                        $(this).remove();
                });
            }
            $('#comments-section .comment:lt(6)').show();
            $('#comments-section .expand-controller .see-all-section span').html($('#comments-section .comment').length);
            if ($('#comments-section .comment').length < 7) {
                $('#comments-section .expand-controller').hide();
            }

            $('#comments-section h2 .comments .number').html($('#comments-section .comment').length);

        },
        createNewComment: function(videoId, body) {
            if (body != "") {
                var payload = { AddNewVideoComment: { videoId: videoId, body: body} };
                $.pf.data.doJsonPost('videoDetailPage.comments.newCommentCreated', payload);
                $('#comments-section textarea').val('');
            }
        },
        newCommentCreated: function(value) {
            if (value[0].DataSet) {
                $.pf.videoDetailPage.comments.getComments(value[0].DataSet.VideoId);
            }
        },
        registerCommentReport: function(current, id) {
            currentItem = current;
            var payload = { FlagCommentAsInappropriate: { id: id} };
            $.pf.data.doJsonPost('videoDetailPage.comments.flagAsInappropriate', payload);
        },
        updateCommentReportStatus: function(value) {
            if (currentItem)
                $(currentItem).find('.flag-as-inappropriate').html(value[0].DataSet);
        },
        setCommentItemEvents: function() {
            // Set id as data instead of hidden value when rendered
            // Set commentevents
            $('#comments-section .comment').each(function() {
                var hidden = $(this).find('input');
                var id = hidden.attr('value');
                hidden.remove();

                $(this).data('id', id);

                var current = $(this);
                $(this).find('.flag-as-inappropriate').click(function() {
                    var commentId = current.data('id');
                    $.pf.videoDetailPage.comments.registerCommentReport(current, parseInt(commentId));
                });

                $(this).find('.delete').click(function() {
                    var commentId = current.data('id');
                    $.pf.videoDetailPage.comments.deleteComment(current, parseInt(commentId));
                });
            });

            // set first/last classes
            $('#comments-section .comment:first').toggleClass('first');
            $('#comments-section .comment:last').toggleClass('last');

            if ($('#comments-section .comment').length > 6) {
                $('#comments-section .expand-controller').show();
                $('#comments-section .comment:gt(5)').fadeOut('fast');
                $('#comments-section .expand-controller .see-all-section span').html($('#comments-section .comment').length);                
            }
            else $('#comments-section .expand-controller').hide();
            
            $('#comments-section h2 .comments .number').html($('#comments-section .comment').length);
        }
    },
    likes: {
        registerUpdate: function(id, type) {
            var payload = { UpdateCurrentLikes: { id: id, type: type} };
            $.pf.data.doJsonPost('videoDetailPage.likes.updateLikes', payload);
        },
        updateLikes: function(value) {
            if (value[0].DataSet && value[0].DataSet.LikesTotal)
                $('#resource-title-section').find('.likes .number').html(value[0].DataSet.LikesTotal);

            $.pf.videoDetailPage.likes.displayLikesStatus();
        },
        displayLikesStatus: function() {
            var voteHtml = '';

            if ($.cookie("_perfunkt") != null && $.cookie("_perfunkt").indexOf('videoId' + $('#_hidden-videoid').val() + '=like') >= 0) {
                voteHtml = '<span class="text">' + $("#_hidden_youlikethis").val() + '</span>';
            }
            else if ($.cookie("_perfunkt") != null && $.cookie("_perfunkt").indexOf('videoId' + $('#_hidden-videoid').val() + '=dislike') >= 0) {
                voteHtml = '<span class="text">' + $("#_hidden_youdislikethis").val() + '</span>';
            }

            if (voteHtml != '') {
                var element = $('#resource-title-section').find('.vote');
                element.attr('class', 'likes');
                element.html(voteHtml);
            }
        }
    },
    report:
    {
        flagAsInappropriate: function(id) {
            var payload = { FlagAsInappropriate: { id: id} };
            $.pf.data.doJsonPost('videoDetailPage.report.flagAsInappropriate', payload);
        },
        updateReportStatus: function(value) {
            if (value[0])
                $('#main-container .flag-as-inappropriate-video').html(value[0].DataSet);
        }
    },
    sendToAFriend:
    {
        currentItem: null,
        send: function(current, id) {
            currentItem = current;
            var desc = $(current).find('.tip-a-friend-menu .description').val();
            var name = $(current).find('.tip-a-friend-menu .name').val();
            var email = $(current).find('.tip-a-friend-menu .email').val();
            var payload = { SendRecommendToFriendEmail: { eMail: email, userName: name, description: desc, videoId: id} };
            $.pf.data.doJsonPost('videoDetailPage.sendToAFriend.sent', payload);
        },
        sent: function(value) {
            if (value[0].DataSet) {
                $(currentItem).find('.tip-a-friend-menu fieldset input').val('');
                $(currentItem).find('.tip-a-friend-menu fieldset textarea').val('');
                $(currentItem).find('.tip-a-friend-menu .confirm-box').html(value[0].DataSet);
            }
        }
    }
};

$(window).bind('videoDetailPage.sendToAFriend.sent', function(event, response) {
    $.pf.videoDetailPage.sendToAFriend.sent(response);
});

$(window).bind('videoDetailPage.comments.commentDeleted', function(event, response) {
    $.pf.videoDetailPage.comments.commentDeleted(response);
});

$(window).bind('videoDetailPage.comments.newCommentCreated', function(event, response) {
    $.pf.videoDetailPage.comments.newCommentCreated(response);
});

$(window).bind('videoDetailPage.comments.renderTemplate', function(event, response) {
    $.pf.videoDetailPage.comments.renderTemplate(response);
});

$(window).bind('videoDetailPage.comments.flagAsInappropriate', function(event, response) {
    $.pf.videoDetailPage.comments.updateCommentReportStatus(response);
});

$(window).bind('videoDetailPage.report.flagAsInappropriate', function(event, response) {
    $.pf.videoDetailPage.report.updateReportStatus(response);
});

$(window).bind('videoDetailPage.likes.updateLikes', function(event, response) {
    $.pf.videoDetailPage.likes.updateLikes(response);
});


$.extend(true, $.pf.videoDetailPage, videoDetailPage);



