﻿if (!$.isPopulated(window.videoMe)) window.videoMe = {};

window.videoMe.videoComments = function(commentOptions)
{
    var dialogOptions = {
        userName: commentOptions.userName,
        userId: commentOptions.userId,
        videoOwnerUserId: commentOptions.videoOwnerUserId,
        userHrefBasePath: 'user.aspx?uid=',
        vid: commentOptions.videoId,
        dataRetriever: function(startIndex, pageSize, callback)
        {
            JsonServiceProxy.invoke('WebServices/VideoMeService.svc/', 'ListCommentView',
			    {
			        videoId: commentOptions.videoId,
			        startRowIndex: startIndex + 1, // convert to 1-based indexing
			        pageSize: pageSize
			    },
			    function(rawData)
			    {
			        var data = {
			            comments: [],
			            commentCount: rawData.TotalRows
			        };

			        for (var i = 0; i < rawData.CommentList.length; i++)
			        {
			            data.comments[i] = rawData.CommentList[i];
			            data.comments[i].DateCreated = Date.parseWCFDateStringToUTC(rawData.CommentList[i].DateCreated);
			        }

			        callback(data);
			    },
                function(error) { alert("error: " + error.Message); }
		    );

        },
        dataPersister: function(options, callback)
        {
            if (options.action == "save")
            {
                JsonServiceProxy.invoke(
                    'WebServices/VideoMeSecureService.svc/',
                    'AddComment',
			        {
			            channelId: commentOptions.channelId,
			            showId: commentOptions.showId,
			            videoId: commentOptions.videoId,
			            commentText: options.text
			        },
			        function(rawData)
			        {
			            var data =
			            {
			                success: true,
			                comment: rawData
			            };

			            data.comment.DateCreated = Date.parseWCFDateStringToUTC(rawData.DateCreated);

			            callback(data);
			        },
                    function(error)
                    {
                        callback({ success: false });
                    }
		        );
            }
            else if (options.action == "delete")
            {
                JsonServiceProxy.invoke(
                    'WebServices/VideoMeSecureService.svc/',
                    'DeleteComment',
			        {
			            channelId: commentOptions.channelId,
			            showId: commentOptions.showId,
			            videoId: commentOptions.videoId,
			            commentId: options.commentId
			        },
			        function(rawData)
			        {
			            callback({ success: true, commentId: options.commentId });
			        },
                    function(error)
                    {
                        callback({ success: false });
                    }
		        );
            }
            else
            {
                if ($.isFunction(callback)) callback(null);
            }
        }
    }

    if ($.isPopulated(commentOptions.mode)) dialogOptions.mode = commentOptions.mode;

	commentOptions.commentDiv.show().comments(dialogOptions);


}    
