Powered.Comment = Class.create({
    divId: null,
    objectId : null,
    objectTypeId : null,
    objectUserId : null,
    abuseTarget : null,
    paging: false,
    callingUrl: null,
    page: 1,

    initialize : function(divId, objectId, objectTypeId, objectUserId, abuseTarget, page) {
        this.divId = divId;
        this.objectId = objectId;
        this.objectTypeId = objectTypeId;
        this.objectUserId = objectUserId;
        this.abuseTarget = abuseTarget;
        if (page != undefined && page > 0) {
            this.page = page;
        } else {
            var queryParams = new String(window.location).toQueryParams();
            if (queryParams.page != undefined && queryParams.page > 0) {
                this.page = queryParams.page;
            }
        }
    },
    
    setPaging : function(paging) {
        this.paging = paging;
    },

    setCallingUrl : function(callingUrl) {
        this.callingUrl = callingUrl;
    },

    getParams : function() {
        return  "objectId=" + this.objectId + "&objectTypeId=" + this.objectTypeId + "&objectUserId=" + this.objectUserId + "&abuseTarget=" + this.abuseTarget + "&paging=" + this.paging + "&page=" + this.getPage() + "&callingUrl=" + encodeURIComponent(this.callingUrl);
    },

    getPage : function() {
        return this.page;
    },

    setPage : function(page) {
        this.page = page;
    },

    loadComments : function() {
        var params = this.getParams();
        params += "&command=view";
        new Ajax.Updater(this.divId, Powered.contextPath + '/ugc/fragments/comments.jsp',
        {
            method: 'post',
            asynchronous: true,
            parameters: params,
            evalScripts: true,
            onSuccess : function() {
            }
        });
    },

    renderEditComment: function (id) {
        new Effect.Fade('CommentBox');
        new Ajax.Updater("CommentBox_" + id, Powered.contextPath + '/ugc/fragments/editComment.jsp',
        {
            method: 'post',
            asynchronous: true,
            parameters: "commentId=" + id,
            evalScripts: true
        });
    },

    renderReply: function (id, errorState, commentText) {
        new Effect.Fade('CommentBox');
        if (!commentText) {
            commentText = "";
        }
        new Ajax.Updater(id + '_reply', Powered.contextPath + '/ugc/fragments/commentReply.jsp',
        {
            method: 'post',
            asynchronous: true,
            parameters: "commentId=" + id + "&errorState=" + errorState + "&commentText=" + encodeURIComponent(commentText),
            evalScripts: true
        });
    },

    saveComment : function(elem, formObject) {
        elem.onclick = "return false;";
        var params = this.getParams();
        params += "&" + Form.serialize(formObject) + "&command=save";
        new Ajax.Updater(this.divId, Powered.contextPath + '/ugc/fragments/comments.jsp',
        {
            method: 'post',
            asynchronous: true,
            parameters: params,
            evalScripts: true,
            onSuccess : function() {
            }
        });
    },

    addComment : function(elem, formObject) {
        elem.onclick = "return false;";
        var params = this.getParams();
        params += "&" + Form.serialize(formObject) + "&command=add";
        new Ajax.Updater(this.divId, Powered.contextPath + '/ugc/fragments/comments.jsp',
        {
            method: 'post',
            asynchronous: true,
            parameters: params,
            evalScripts: true,
            onSuccess : function() {
            }
        });
    },

    addCommentReply : function(elem, id, formObject) {
        elem.onclick = "return false;";
        var params = this.getParams();
        params += "&" + Form.serialize(formObject) + "&command=add&parentCommentId=" + id;
        new Ajax.Updater(this.divId, Powered.contextPath + '/ugc/fragments/comments.jsp',
        {
            method: 'post',
            asynchronous: true,
            parameters: params,
            evalScripts: true,
            onSuccess : function() {
            }
        });
    },

    deleteComment : function(commentId) {
        var params = this.getParams();
        params += "&commentId=" + commentId + "&command=delete";
        new Ajax.Updater(this.divId, Powered.contextPath + '/ugc/fragments/comments.jsp',
        {
            method: 'post',
            asynchronous: true,
            parameters: params,
            evalScripts: true,
            onSuccess : function() {
            }
        });
    },

    cancelReply : function(commentId) {
        $(commentId + '_reply').innerHTML = '';
        $('Error_' + commentId).innerHTML = '';
        new Effect.Appear('CommentBox');
        $('commentText').value = '';
    },

    cancelEdit : function() {
        var params = this.getParams();
        new Ajax.Updater(this.divId, Powered.contextPath + '/ugc/fragments/comments.jsp',
        {
            method: 'post',
            parameters: params,
            asynchronous: true,
            evalScripts: true
        });
    },

    updateCommentCounts : function(msg) {
        var elements = $$('.CommentCount');
        if (elements != null) {
            for (var i = 0 ; i < elements.length ; i++) {
                var e = elements[i];
                e.update(msg);
            }
        }
    }
});