﻿YAHOO.namespace("CW.Comment");

CW.Comment = function() {
    //  private variables and methods
    var $E = YAHOO.util.Event;
    var $D = YAHOO.util.Dom;
    var $DLG = YAHOO.widget.Dialog;
    var $ = $D.get;

    var dialog;
    var parentCommentId;

    var lastCommentId = 0;

    //  public variables and methods
    return {

        init: function() {

            $E.addListener('btnAddComment', 'click', CW.Comment.addCommentHandler, true);

        },


        // Handlers
        //------------------------------------
        addCommentHandler: function() {

            var body = encodeURIComponent(tinyMCE.activeEditor.getContent());
            //alert(itemType); return;
            if (body.length > 0) {

                var comment = {
                    itemId: itemId,
                    itemType: itemType,
                    comment: body,
                    parentCommentId: 0
                };

                CW.Comment.serverAddComment(comment);
            }
        },

        editCommentHandler: function(commentId) {

            //$('commentBodyEdit_'+commentId).setStyle('opacity', 0);

            $D.addClass('commentBody_' + commentId, 'hide');
            $D.removeClass('commentBodyEdit_' + commentId, 'hide');

            $D.addClass('buttonBarPanel_' + commentId, 'hide');
            $D.removeClass('editCommentPanel_' + commentId, 'hide');


            //var displayComment = new YAHOO.util.Anim('commentBodyEdit_'+commentId, { opacity: { to: 1 } }, 1, YAHOO.util.Easing.easeIn);
            //displayComment.animate();

        },

        editCommentCancelHandler: function(commentId) {
            CW.Comment.commentNormalView(commentId);
        },

        editCommentSaveHandler: function(commentId) {

            var body = $('commentBodyEditTextArea_' + commentId).value;

            if (body.length > 0) {

                var comment = {
                    commentId: commentId,
                    comment: encodeURIComponent(body)
                };

                CW.Comment.serverUpdateComment(comment);
            }
        },

        removeCommentHandler: function(commentId) {
            var comment = {
                itemId: commentId
            };
            CW.Comment.serverRemoveComment(comment);
        },

        acceptAnswerHandler: function(commentId) {



            alert("Accept as best answer " + commentId);
        },

        // Change views
        //------------------------------------
        commentNormalView: function(commentId) {

            $D.removeClass('commentBody_' + commentId, 'hide');
            $D.addClass('commentBodyEdit_' + commentId, 'hide');

            $D.removeClass('buttonBarPanel_' + commentId, 'hide');
            $D.addClass('editCommentPanel_' + commentId, 'hide');

        },

        addCommentToView: function(newCommentId) {

            lastCommentId += 1;
            //var newCommentId = "comment_" + lastCommentId;

            var commentNode = document.createElement("div");
            commentNode.innerHTML = $('commentTemplate').innerHTML;

            commentNode.id = "comment_" + newCommentId;
            $D.setStyle(commentNode, 'opacity', 0);
            $D.setStyle(commentNode, 'background-color', '#ffffcc');

            // Parse values into template
            //var newCommentBody = tinyMCE.get('addCommentBody').getContent();
            var newCommentBody = tinyMCE.activeEditor.getContent();

            while (commentNode.innerHTML.indexOf("__ctBody__") > -1) {
                commentNode.innerHTML = commentNode.innerHTML.replace("__ctBody__", newCommentBody);
            }

            while (commentNode.innerHTML.indexOf("__ctCommentId__") > -1) {
                commentNode.innerHTML = commentNode.innerHTML.replace("__ctCommentId__", newCommentId);
            }

            // Reset form
            //$('addCommentBody').value = "";
            //tinyMCE.get('addCommentBody').setContent("");
            tinyMCE.activeEditor.setContent("");

            var commentsContainer = document.getElementById('newComments');
            commentsContainer.appendChild(commentNode);


            // Animate
            //---

            // Fade in 
            var attributes = { opacity: { to: 1} };
            var fade = new YAHOO.util.Anim(commentNode, attributes, 1, YAHOO.util.Easing.easeIn);

            // Change color to white
            var changeColor = new YAHOO.util.ColorAnim(commentNode, { backgroundColor: { to: '#ffffff'} }, 2);

            // Chain events
            fade.onComplete.subscribe(function() {
                changeColor.animate();
            });

            fade.animate();

            // Update comment count
            CW.Comment.updateCommentCount(1)

        },

        removeCommentFromView: function(commentId) {

            var commentNodeId = 'comment_' + commentId;

            var commentNode = document.getElementById(commentNodeId);

            // Animate
            //---

            // Color to yellow
            var changeColor = new YAHOO.util.ColorAnim(commentNode, { backgroundColor: { to: '#FFFFCC'} }, 0.2);

            // Fade out
            var attributes = { opacity: { to: 0} };
            var fade = new YAHOO.util.Anim(commentNode, attributes, 1, YAHOO.util.Easing.easeIn);

            // Remove 
            var removeElement = function() {
                var el = commentNode;
                el.parentNode.removeChild(el);
            }

            // Chain events
            changeColor.onComplete.subscribe(function() {
                fade.onComplete.subscribe(removeElement);
                fade.animate();
            });

            changeColor.animate();

            // Update comment count
            CW.Comment.updateCommentCount(-1)
        },

        updateCommentCount: function(change) {

            itemCount = itemCount + change;
            var newText = itemCount + " ";

            // Articels
            if (itemType == "Post") {
                newText += "Comment";
            }
            else if (itemType == "Question") {
                newText += "Answer";
            }

            if (itemCount != 1)
                newText += "s";
            else {
                newText = newText.replace("Answers", "Answer");
                newText = newText.replace("Comments", "Comment");
            }
            $('itemCountBottom').innerHTML = newText;

            if ($('itemCountTop') != undefined)
                $('itemCountTop').innerHTML = newText;



        },

        // Server methods
        //------------------------------------

        // Add comment
        //------------------------------------
        serverAddComment: function(serverComment) {

            var url = "/API/Comment.asmx?op=Submit";
            var methodName = "Submit";

            var callback = {
                success: CW.Comment.serverAddCommentSuccess,
                failure: CW.Comment.serverAddCommentFailure,
                argument: [methodName, serverComment],
                cache: false,
                scope: CW.Comment
            };

            CW.showPanel('panelSaving');
            
            CW.Ajax.fnGenerateAjaxWSRequest(url, callback, methodName, serverComment);
        },

        serverAddCommentSuccess: function(o) {

            var returnCode = CW.Ajax.fnHandleSoapResponse(o, o.argument[0]);

            var newCommentId = returnCode;

            setTimeout("CW.hidePanel(); CW.Comment.addCommentToView(" + newCommentId + ")", 1500);
        },

        serverAddCommentFailure: function(o) {
            alert(CW.Ajax.fnHandleSoapResponse(o, o.argument[0]));
        },


        // Update comment
        //------------------------------------
        serverUpdateComment: function(serverComment) {

            var url = "/API/Comment.asmx?op=Update";
            var methodName = "Update";

            var callback = {
                success: CW.Comment.serverUpdateCommentSuccess,
                failure: CW.Comment.serverUpdateCommentFailure,
                argument: [methodName, serverComment],
                cache: false,
                scope: CW.Comment
            };

            CW.showPanel('panelSaving');

            CW.Ajax.fnGenerateAjaxWSRequest(url, callback, methodName, serverComment);
        },

        serverUpdateCommentSuccess: function(o) {

            var returnCode = CW.Ajax.fnHandleSoapResponse(o, o.argument[0]);
            CW.hidePanel();

            var commentId = o.argument[1].commentId;
            var body = decodeURIComponent(o.argument[1].comment);

            CW.Comment.commentNormalView(commentId);

            $('commentBody_' + commentId).innerHTML = "<p>" + body + "</p>";

        },

        serverUpdateCommentFailure: function(o) {
            alert(CW.Ajax.fnHandleSoapResponse(o, o.argument[0]));
        },


        // Remove comment
        //------------------------------------
        serverRemoveComment: function(serverComment) {
            
            var url = "/API/Comment.asmx?op=Remove";
            var methodName = "Remove";

            var callback = {
                success: CW.Comment.serverRemoveCommentSuccess,
                failure: CW.Comment.serverRemoveCommentFailure,
                argument: [methodName, serverComment],
                cache: false,
                scope: CW.Comment
            };

            CW.showPanel('panelSaving');

            CW.Ajax.fnGenerateAjaxWSRequest(url, callback, methodName, serverComment);
        },

        serverRemoveCommentSuccess: function(o) {

            var returnCode = CW.Ajax.fnHandleSoapResponse(o, o.argument[0]);
            CW.hidePanel();

            var commentId = o.argument[1].itemId;

            CW.Comment.removeCommentFromView(commentId);

        },

        serverRemoveCommentFailure: function(o) {
            alert(CW.Ajax.fnHandleSoapResponse(o, o.argument[0]));
        }



    }
} (); 
	   
YAHOO.util.Event.on(window, 'load', CW.Comment.init);
