var ajaxComment = {
    resultContainer:null,
    ajaxResponse:null,
    commentForm:null,
    isPrimary:null,
    requestUrl:null,
    loadingDiv:null,    
    callBack:null,
    animConfig:{
        comment:{
            minOpacity: 0,
            maxOpacity: 1,
            fadeInRate: .5,
            fadeOutRate: .5
        },
        loading:{
            minOpacity: 0,
            maxOpacity: 1,
            fadeInRate: .5,
            fadeOutRate: .5
        }
    },

    init:function(){
        this.loadingDiv = yuiDom.get("loadingDiv");
        this.requestUrl = '/service/ajax/add_comment.html';
        this.callBack = {
            success:this.handleSuccess,
            failure:this.handleFailure,
            scope:this
        }
    },
    update:function(parentCommentId){
        if(parentCommentId){
            this.isPrimary = false;
            this.resultContainer = yuiDom.get(parentCommentId);
            this.commentForm = yuiDom.get("conversationComments_" + this.resultContainer.id);
        }
        else{
            this.isPrimary = true;
            this.resultContainer = yuiDom.get("quoteBoxContainer");
            this.commentForm = yuiDom.get("conversationComments");
        }
    },

    makeRequest:function(isSubComment){
        this.showLoading(isSubComment);
        yuiConnect.setForm(this.commentForm);
        this.request = yuiConnect.asyncRequest('POST', this.requestUrl, this.callBack);
    },

    handleSuccess:function(response){
        this.ajaxResponse = YAHOO.lang.JSON.parse(response.responseText);
        if(this.ajaxResponse.success){
            this.createNewComment();
        }
        else{            
            mm.Error.add(this.ajaxResponse.error);
            mm.Error.generateBridge();
        }
        this.doPostAjax();
    },

    handleFailure:function(){
        alert("the requested action failed!!");
        this.doPostAjax();
    },

    doPostAjax:function(){
        this.resetForm();
        this.hideLoading();
    },

    createNewComment:function(){
        var tempTextString;
        var tempTextNode;
        var quoteNode = document.createElement("div");
            yuiDom.addClass(quoteNode, "quoteBox newlyAdded");
            quoteNode.setAttribute("id", this.ajaxResponse.id);
            // create the comment div
            var commentNode = document.createElement("div");
                yuiDom.addClass(commentNode, "commentBox");
                var commentFlags = document.createElement("p");
                    commentFlags.setAttribute("class", "respondText");
                    if(this.isPrimary){
                        var respondLink = document.createElement("a");
                        respondLink.setAttribute("href", "javascript:toggleSubComment('" + this.ajaxResponse.id + "');");
                        tempTextString = "Respond";
                        tempTextNode = document.createTextNode(tempTextString);
                        respondLink.appendChild(tempTextNode);
                        commentFlags.appendChild(respondLink);
                        /*tempTextString = " | ";
                        tempTextNode = document.createTextNode(tempTextString);
                        commentFlags.appendChild(tempTextNode);*/
                    }
                    /*var flagLink = document.createElement("a");
                    tempTextString = "Flag";
                    tempTextNode = document.createTextNode(tempTextString);
                    flagLink.appendChild(tempTextNode);
                    commentFlags.appendChild(flagLink);*/
                commentNode.appendChild(commentFlags);

                // use innerHTML as data from service already contains the markup
                var commentText = document.createElement("span");
                    commentText.innerHTML = this.ajaxResponse.text;
                commentNode.appendChild(commentText);


                if(this.isPrimary){
                    var subCommentNode = document.createElement("div");
                        subCommentNode.setAttribute("id", "subCommentContainer_" + this.ajaxResponse.id);
                        yuiDom.addClass(subCommentNode, "subCommentContainer");
                        var subCommentForm = document.createElement("form");
                            subCommentForm.setAttribute("id", "conversationComments_" + this.ajaxResponse.id);
                            subCommentForm.setAttribute("name", "conversationComments_" + this.ajaxResponse.id);
                            var subCommentParentField = document.createElement("input");
                                subCommentParentField.setAttribute("type", "hidden");
                                subCommentParentField.setAttribute("name", "parentCommentId");
                                subCommentParentField.setAttribute("value", this.ajaxResponse.id);
                            subCommentForm.appendChild(subCommentParentField);
                            var subCommentTextField = document.createElement("textarea");
                                subCommentTextField.setAttribute("name", "text");
                            subCommentForm.appendChild(subCommentTextField);                            
                                var subCommentSubmitButton = document.createElement("a");
                                    subCommentSubmitButton.setAttribute("href", "javascript:void(0);");
                                    subCommentSubmitButton.setAttribute("class", "btnSubmit");
                                    subCommentSubmitButton.setAttribute("onclick", "ajaxComment.makeRequestBridge('" + this.ajaxResponse.id + "', true)");
                                    var subCommentSubmitText = document.createElement("span");
                                        tempTextNode = document.createTextNode("Submit");
                                        subCommentSubmitText.appendChild(tempTextNode);
                                    subCommentSubmitButton.appendChild(subCommentSubmitText);
                            subCommentForm.appendChild(subCommentSubmitButton);
                        subCommentNode.appendChild(subCommentForm);
                        var subCommentClearDiv = document.createElement("div");
                            subCommentClearDiv.setAttribute("class", "clearDiv");
                        subCommentNode.appendChild(subCommentClearDiv);
                    commentNode.appendChild(subCommentNode);
                }
            quoteNode.appendChild(commentNode);
        // create the date div
            var dateNode = document.createElement("div");
                yuiDom.addClass(dateNode, "dateBox");
                    tempTextString = this.ajaxResponse.date;
                    tempTextNode = document.createTextNode(tempTextString);
                dateNode.appendChild(tempTextNode);
            quoteNode.appendChild(dateNode);
            // create the signature div
            var signatureNode = document.createElement("div");
                yuiDom.addClass(signatureNode, "signitureBox");
                var signatureLink = document.createElement("a");
                    signatureLink.setAttribute("href", "/" + this.ajaxResponse.username);                    
                    tempTextString = this.ajaxResponse.username;
                    tempTextNode = document.createTextNode(tempTextString);
                signatureLink.appendChild(tempTextNode);
                signatureNode.appendChild(signatureLink);
        quoteNode.appendChild(signatureNode);
        yuiDom.setStyle(quoteNode, "opacity", "0");

        if(!this.isPrimary){
            var subComments = yuiDom.get('subCommentContainer_' + this.resultContainer.id);;
            yuiDom.insertAfter(quoteNode, subComments);
            yuiDom.addClass(this.resultContainer, "altColor");
            yuiDom.addClass(this.resultContainer, "subQuote");
        }
        else{
            var currentFirstChild = yuiDom.getFirstChild(this.resultContainer);
            if(currentFirstChild){
                yuiDom.insertBefore(quoteNode, currentFirstChild);
            }
            else{
                this.resultContainer.appendChild(quoteNode);
            }
        }

        var commentFadeIn = new yuiAnim(quoteNode, { opacity: {to: this.animConfig.comment.maxOpacity} }, this.animConfig.comment.fadeInRate, yuiEasing.easeIn);
        commentFadeIn.animate();
    },

    resetForm:function(){
        this.commentForm.reset();
        if(!this.isPrimary){
            toggleSubComment(this.resultContainer.id);
        }
    },
    
    showLoading:function(isSubComment){
        if(isSubComment){
            yuiDom.addClass(this.loadingDiv, "subLayer");
        }
        else{
            yuiDom.removeClass(this.loadingDiv, "subLayer");            
        }
        yuiDom.setStyle(this.loadingDiv, "opacity", "0");
        yuiDom.setStyle(this.loadingDiv, "display", "block");
        yuiDom.setXY(this.loadingDiv, yuiDom.getXY(this.commentForm));

        var loadingFadeIn = new yuiAnim(this.loadingDiv, { opacity: {to: this.animConfig.loading.maxOpacity} }, this.animConfig.loading.fadeInRate, yuiEasing.easeIn);
        loadingFadeIn.animate();
    },

    hideLoading:function(){
        var self = ajaxComment;
        var loadingFadeOut = new yuiAnim(this.loadingDiv, { opacity: {to: this.animConfig.loading.minOpacity} }, this.animConfig.loading.fadeOutRate, yuiEasing.easeIn);
        loadingFadeOut.onComplete.subscribe(function(){
            yuiDom.setStyle(self.loadingDiv, "display", "none");
        });
        loadingFadeOut.animate();
    },
    
    makeRequestBridge:function(parentCommentId, isSubComment){
        var self = ajaxComment;
        self.update(parentCommentId);
        self.makeRequest(isSubComment);
    },
    initBridge:function(){
        var self = ajaxComment;
        self.init();
    }
};

yuiEvent.onDOMReady(ajaxComment.initBridge);


function toggleSubComment(parentCommentId){
    var subCommentBlock = yuiDom.get('subCommentContainer_' + parentCommentId);
    var subCommentForm =  yuiDom.get('conversationComments_' + parentCommentId);
    if(yuiDom.hasClass(subCommentBlock, "subCommentContainer")){
        yuiDom.removeClass(subCommentBlock, "subCommentContainer");
        subCommentForm.text.focus();
    }
    else{
        yuiDom.addClass(subCommentBlock, "subCommentContainer");
    }
}