﻿var _postWidgets = new Array();
var _posting = false;

function Declare(clientID, preDiv, actualDiv, postedDiv, txtArea, txtAuthor, txtEmail, pnl, postID, valGrp) {
    
    this.ClientID = clientID;
    this.preDiv = preDiv;
    this.actualDiv = actualDiv;
    this.postedDiv = postedDiv;
    this.TextArea = txtArea;
    this.TextAuthor = txtAuthor;
    this.TextEmail = txtEmail;
    this.Panel = pnl;
    this.postID = postID;
    this.Shown = false;
    this.ValidationGroup = valGrp;


    this.Swap = function() {
        jqBlog('#' + this.preDiv).toggle();
        jqBlog('#' + this.actualDiv).toggle();

        if (!this.Shown) {
            this.Shown = true;
            jqBlog('#' + this.TextArea).autoResize({
                onResize: function() {
                },
                animateCallback: function() {
                },
                animateDuration: 120,
                extraSpace: 15
            });
        }
    }

    this.Post = function() {
        if (_posting) {
            alert("Please wait until I finish posting your previous comment :)");
            return;
        }

        if (!Page_ClientValidate(this.ValidationGroup)) return;

        _posting = true;

        var vars = "Comment=" + encodeURIComponent(this.TheComment()) +
            "&Author=" + encodeURIComponent(this.TheAuthor()) +
            "&Email=" + encodeURIComponent(this.TheEmail()) + 
            "&pid=" + this.postID;

        var me = this;

        jqBlog.post("/Blog/PostComment.aspx", vars, function() { me.IBack(); });

        jqBlog('#' + this.Panel + ' *').attr('disabled', 'disabled');
    }

    this.IBack = function() {
    
        _posting = false;
        
        jqBlog('#' + this.preDiv).hide();
        jqBlog('#' + this.actualDiv).hide();
        jqBlog('#' + this.postedDiv).show();

        var a = this.TheComment().replace(new RegExp("\\n", "g"), "<br/>");

        jqBlog('#' + this.postedDiv).prepend(a);
    }

    this.TheComment = function() {
        return jqBlog('#' + this.TextArea).attr('value');
    }

    this.TheAuthor = function() {
        return jqBlog('#' + this.TextAuthor).attr('value');
    }

    this.TheEmail = function() {
        return jqBlog('#' + this.TextEmail).attr('value');
    }

    _postWidgets[_postWidgets.length] = this;

}

function _BlogCmntsAction(name, action) {

    for (n = 0; n < _postWidgets.length; n++) {
        if (_postWidgets[n].ClientID == name) {
            if (action == 0) {
                _postWidgets[n].Swap();
            } else if (action == 1) {
                _postWidgets[n].Post();
            }
        }
    }
}
