﻿//Item repeater
jsonRepeater = {
    initMostPopular: function(maxCount, type) {
        jsonRepeater.items.getMostPopularVideoItems(maxCount, type);
    },
    initMostPopularTopicVideoItems: function(galleryId, maxCount, excludeVideoId, type) {
        jsonRepeater.items.getMostPopularTopicVideoItems(galleryId, maxCount, excludeVideoId, type);
    },
    initTopic: function(galleryId, sortOrder, excludeUserId, maxCount, type) {
        jsonRepeater.items.getTopicVideoItems(galleryId, sortOrder, excludeUserId, maxCount, type);
    },
    initUser: function(userId, sortOrder, excludeGalleryId, type) {
        jsonRepeater.items.getUserVideoItems(userId, sortOrder, excludeGalleryId, type);
    },
    initUserTopic: function(userId, galleryId, sortOrder, type) {
        jsonRepeater.items.getUserTopicVideoItems(userId, galleryId, sortOrder, type);
    },
    initOthersTopic: function(galleryId, sortOrder, excludeUserId, type) {
        jsonRepeater.items.getOthersTopicVideoItems(galleryId, sortOrder, excludeUserId, type);
    },
    items: {
        getMostPopularVideoItems: function(maxCount, type) {
            var items = { MostPopularVideoItems: { maxCount: maxCount, listType: type} };
            $.pf.data.doJsonPost('jsonRepeater.template.renderTemplate', items);
        },
        getMostPopularTopicVideoItems: function(galleryId, maxCount, excludeVideoId, type) {
            var items = { MostPopularTopicVideoItems: { galleryId: galleryId, maxCount: maxCount, excludeVideoId: excludeVideoId, listType: type} };
            $.pf.data.doJsonPost('jsonRepeater.template.renderTemplate', items);
        },
        getTopicVideoItems: function(galleryId, sortOrder, excludeUserId, maxCount, type) {
            $.pf.jsonRepeater.sorting.enableSorting(galleryId, excludeUserId, type);
            if (document.location.toString().indexOf('#new') > 0) {
                var items = { TopicVideoItems: { galleryId: galleryId, sortOrder: "new", excludeUserId: excludeUserId, listType: type} };
            }
            else {
                var items = { TopicVideoItems: { galleryId: galleryId, sortOrder: sortOrder, excludeUserId: excludeUserId, listType: type} };
            }
            $.pf.data.doJsonPost('jsonRepeater.template.renderTemplate', items);
        },
        getUserVideoItems: function(userId, sortOrder, excludeGalleryId, type) {
            var items = { UserVideoItems: { userId: userId, sortOrder: sortOrder, excludeGalleryId: excludeGalleryId, listType: type} };
            $.pf.data.doJsonPost('jsonRepeater.template.renderTemplate', items);
        },
        getUserTopicVideoItems: function(userId, galleryId, sortOrder, type) {
            var items = { UserTopicVideoItems: { userId: userId, galleryId: galleryId, sortOrder: sortOrder, listType: type} };
            $.pf.data.doJsonPost('jsonRepeater.template.renderTemplate', items);
        },
        getOthersTopicVideoItems: function(galleryId, sortOrder, excludeUserId, type) {
            var items = { OthersTopicVideoItems: { galleryId: galleryId, sortOrder: sortOrder, excludeUserId: excludeUserId, listType: type} };
            $.pf.data.doJsonPost('jsonRepeater.template.renderTemplate', items);
        }
    },
    template: {
        renderTemplate: function(response) {
            var type = "start";
            if (response[0].InternalType) {
                type = response[0].InternalType;
            }
            if (response[0].DataSet) {
                if ($.isArray(response[0].DataSet)) {

                    $('#' + type + '-template').setTemplateURL('/Templates/Main/Scripts/ListTemplate.htm');
                    $('#' + type + '-template').setParam('type', type);
                    $('#' + type + '-template').processTemplate(response[0]);

                    // Set id as data instead of hidden value when rendered
                    $('#' + type + '-template .box, #' + type + '-template .wide-box').each(function() {
                        var hidden = $(this).find('input.id');
                        var id = hidden.attr('value');
                        hidden.remove();

                        $(this).data('id', id);
                    });

                    //HTML encode specific elements when rendered.
                    $('#' + type + '-template .main h3 a, #' + type + '-template .main h4 a').each(function() {
                        var me = jQuery(this);
                        var html = me.html();
                        me.html(html.replace(/&amp;/g, '&'));
                    });

                    //Callback functions when template is rendered.
                    $.pf.jsonRepeater.template.setItemStyle(type);
                    $.pf.jsonRepeater.events.resetVideoItemEvents(type);
                }
                else {
                    // $('#' + type + '-template').html(response[0].DataSet)
                    $('#' + type + '-template-heading').hide();
                    $('#' + type + '-template').hide();
                }
            }
            $('#' + type + '-template .ajax-load').fadeOut();
        },
        setItemStyle: function(type) {
            $('#' + type + '-template .box-grid').each(function() {
                $(this).find('.box:first').addClass("first");
                $(this).find('.box:last').addClass("last");
            });
            if (type == "topic") {
                $('#' + type + '-template .box-grid:first').each(function() {
                    $(this).find('.box:first').removeClass("first");
                });
            }
        }

    },
    sorting: {
        enableSorting: function(galleryId, excludeUserId, type) {

            if (document.location.toString().indexOf('#new') > 0) {
                $('#topic-content .sort-nav .badge').each(function() { $(this).removeClass('black').removeClass('orange'); });
                $('#topic-content .sort-nav .badge em.new').parents('.badge').addClass('orange');
                $('#topic-content .sort-nav .badge em.new').parents('.badge').siblings('.badge').addClass('black');
            }

            $('#topic-content .sort-nav .badge').click(function() {

                var sortOrder = $(this).find('em').attr('class');
                $('#topic-content .sort-nav .badge').each(function() { $(this).removeClass('black').removeClass('orange'); });
                $(this).addClass('orange');
                $(this).siblings('.badge').addClass('black');

                var items = { TopicVideoItems: { galleryId: galleryId, sortOrder: sortOrder, excludeUserId: excludeUserId, listType: type} };
                $.pf.data.doJsonPost('jsonRepeater.template.renderTemplate', items);
            });
        }
    },
    events: {
        resetVideoItemEvents: function(type) {



            // enable share-menu links in submission boxes
            $("div.box .show-share-menu").each(function(index, share_link) {
                $(share_link).click(function() {
                    var box = $(share_link).parents("div.box");
                    box.find("div.share-menu span.close").click(function(index, span) {
                        box.find("div.share-menu").hide();
                    });
                    box.find("div.share-menu").show();
                });
            });

            $("div.box .share-menu li.friend").each(function(index, send_to_friend_link) {
                $(send_to_friend_link).click(function() {
                    var box = $(send_to_friend_link).parents("div.box");
                    box.find("div.tip-a-friend-menu span.close").click(function(index, span) {
                        box.find("div.tip-a-friend-menu").hide();
                    });
                    box.find("div.share-menu").hide();
                    box.find("div.tip-a-friend-menu").show();
                });
            });

            // Set events for send to a friend


            // enable like/dislike and reporting events
            $('#' + type + '-template div.box').each(function() {
                var id = $(this).data('id');
                var current = $(this);

                $(this).find('.like-share .vote .like').click(function() {
                    $.pf.videoItem.likes.registerUpdate(current, id, 'like');
                });
                $(this).find('.like-share .vote .dislike').click(function() {
                    $.pf.videoItem.likes.registerUpdate(current, id, 'dislike');
                });
                $(this).find('.comments-inappropriate .flag-as-inappropriate').click(function() {
                    $.pf.videoItem.report.flagAsInappropriate(current, id);
                });
                $(this).find('.your-technique .button').click(function() {
                });
                $(this).find('.tip-a-friend-menu .button').click(function() {
                    $.pf.videoItem.sendToAFriend.send(current, id);
                });


            });



            if (type == "topic") {
                // enable share-menu links in submission box
                $("div.wide-box .show-share-menu").each(function(index, share_link) {
                    $(share_link).click(function() {
                        var box = $(share_link).parents("div.wide-box");
                        box.find("div.share-menu span.close").click(function(index, span) {
                            box.find("div.share-menu").hide();
                        });
                        box.find("div.share-menu").show();
                    });
                });

                $("div.wide-box .share-menu li.friend").each(function(index, send_to_friend_link) {
                    $(send_to_friend_link).click(function() {
                        var box = $(send_to_friend_link).parents("div.wide-box");
                        box.find("div.tip-a-friend-menu span.close").click(function(index, span) {
                            box.find("div.tip-a-friend-menu").hide();
                        });
                        box.find("div.share-menu").hide();
                        box.find("div.tip-a-friend-menu").show();
                    });
                });

                // enable like/dislike and reporting events
                $('#' + type + '-template div.wide-box').each(function() {
                    var id = $(this).data('id');
                    var current = $(this);

                    $(this).find('.vote-section .like').click(function() {
                        $.pf.videoItem.likes.registerUpdate(current, id, 'like');
                    });
                    $(this).find('.vote-section .dislike').click(function() {
                        $.pf.videoItem.likes.registerUpdate(current, id, 'dislike');
                    });
                    $(this).find('.flag-as-inappropriate').click(function() {
                        $.pf.videoItem.report.flagAsInappropriate(current, id);
                    });
                    $(this).find('.tip-a-friend-menu .button').click(function() {
                        $.pf.videoItem.sendToAFriend.send(current, id);
                    });
                });
            }
        }
    }
};

$(window).bind('jsonRepeater.template.renderTemplate', function(event, response) {
    $.pf.jsonRepeater.template.renderTemplate(response);
});

$.extend(true, $.pf.jsonRepeater, jsonRepeater);


// Video item
videoItem = {
    likes: {
        currentItem: null,
        registerUpdate: function(current, id, type) {
            currentItem = current;
            var payload = { UpdateCurrentLikes: { id: id, type: type} };
            $.pf.data.doJsonPost('videoItem.likes.updateLikes', payload);
        },
        updateLikes: function(value) {
            if (currentItem) {
                //TODO: Add update for likestatus
                if (value[0].DataSet) {
                    if (value[0].DataSet.LikesStatus != "") {
                        $(currentItem).find('.vote').html("<span class='like-status'>" + value[0].DataSet.LikesStatus + "</span>");                       
                    }

                    $(currentItem).find('.likes .number').html(value[0].DataSet.LikesTotal);
                }
            }
        }
    },
    report:
    {
        currentItem: null,
        flagAsInappropriate: function(current, id) {
            currentItem = current;
            var payload = { FlagAsInappropriate: { id: id} };
            $.pf.data.doJsonPost('videoItem.report.flagAsInappropriate', payload);
        },
        updateReportStatus: function(value) {
            $(currentItem).find('.flag-as-inappropriate').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('videoItem.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('videoItem.sendToAFriend.sent', function(event, response) {
    $.pf.videoItem.sendToAFriend.sent(response);
});

$(window).bind('videoItem.report.flagAsInappropriate', function(event, response) {
    $.pf.videoItem.report.updateReportStatus(response);
});

$(window).bind('videoItem.likes.updateLikes', function(event, response) {
    $.pf.videoItem.likes.updateLikes(response);
});

$.extend(true, $.pf.videoItem, videoItem);



