﻿var _editBlocks = new Array();

function DeclareEditRegion(clientID, postID, wraperID, editPanelID, adminPanel, 
    updateBtns, textAreaID, txtAuthorID, lblAuthor, lblComment, hlprTextArea) {

    this.ClientID = clientID;
    this.postID = postID;
    this.Wrapper = wraperID;
    this.EditPanel = editPanelID;
    this.PanelAdmiBtns = adminPanel;
    this.PanelUpdateBtns = updateBtns;
    this.Shown = false;
    this.TextArea = textAreaID;
    this.TextAuthor = txtAuthorID;
    this.LabelAuthor = lblAuthor;
    this.LabelComment = lblComment;
    this.HelperTextArea = hlprTextArea;

    this.Delete = function() {
        if (this.IAmBusy()) return;
        if (!confirm('Are you sure you want to delete this comment?')) return;
        _posting = true;
        var vars = "Delete=1&pid=" + this.postID;
        var me = this;
        jqBlog.post("/Blog/PostComment.aspx", vars, function() { me.IDeleted(); });
    }

    this.IDeleted = function() {
        var me = this;
        jqBlog('#' + this.Wrapper).animate({ opacity: 0.6 }, 1000, function() {
            jqBlog('#' + me.Wrapper + ' *').attr('disabled', 'disabled');
        });
        _posting = false;
    }

    this.Edit = function() {

        if (!this.Shown) {
            this.Shown = true;
            jqBlog('#' + this.TextArea).autoResize({
                onResize: function() {
                },
                animateCallback: function() {
                },
                animateDuration: 120,
                extraSpace: 15
            });

            jqBlog('#' + this.TextAuthor).attr('value', jqBlog('#' + this.LabelAuthor).text());
            jqBlog('#' + this.TextArea).attr('value', jqBlog('#' + this.HelperTextArea).attr('value'));

            var me = this;
            setTimeout(function() {
                jqBlog('#' + me.TextArea).trigger('keydown');
            }, 200);
        }

        jqBlog('#' + this.PanelAdmiBtns + ' *').attr('disabled', 'disabled');
        jqBlog('#' + this.EditPanel).toggle();
        jqBlog('#' + this.Wrapper).toggle();
    }

    this.CancelEdit = function() {
        jqBlog('#' + this.PanelAdmiBtns + ' *').attr('disabled', '');
        jqBlog('#' + this.EditPanel).toggle();
        jqBlog('#' + this.Wrapper).toggle();
    }

    this.Update = function() {
        if (this.IAmBusy()) return;
        _posting = true;

        jqBlog('#' + this.PanelUpdateBtns + ' *').attr('disabled', 'disabled');

        var vars = "Update=1&Comment=" + encodeURIComponent(this.TheComment()) +
            "&Author=" + encodeURIComponent(this.TheAuthor()) + "&pid=" + this.postID;

        var me = this;

        jqBlog.post("/Blog/PostComment.aspx", vars, function() { me.IUpdated(); });

        jqBlog('#' + this.Panel + ' *').attr('disabled', 'disabled');

    }

    this.IUpdated = function() {
        jqBlog('#' + this.PanelUpdateBtns + ' *').attr('disabled', '');
        jqBlog('#' + this.Panel + ' *').attr('disabled', '');
        
        jqBlog('#' + this.LabelAuthor).text(this.TheAuthor());
        jqBlog('#' + this.LabelComment).html(this.TheComment().replace(new RegExp("\\n", "g"), "<br/>"));
        
        this.CancelEdit();
        _posting = false;
    }


    this.TheComment = function() {
        return jqBlog('#' + this.TextArea).attr('value');
    }

    this.TheAuthor = function() {
        return jqBlog('#' + this.TextAuthor).attr('value');
    }

    this.IAmBusy = function() {
        if (_posting) {
            alert("Please wait until I finish executing your last request :)");
            return true;
        }
        return false;
    }

    _editBlocks[_editBlocks.length] = this;

}

function _BlogEditAction(name, action) {

    for (n = 0; n < _editBlocks.length; n++) {
        if (_editBlocks[n].ClientID == name) {
            if (action == 0) {
                _editBlocks[n].Delete();
            } else if (action == 1) {
                _editBlocks[n].Edit();
            } else if (action == 2) {
                _editBlocks[n].CancelEdit();
            } else if (action == 3) {
                _editBlocks[n].Update();
            }
        }
    }
}
