(function($) {
    var userAgent = navigator.userAgent.toLowerCase();

    $.browser = {
        version: (userAgent.match( /.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/ ) || [0,'0'])[1],
        safari: /webkit/.test( userAgent ),
        opera: /opera/.test( userAgent ),
        msie: /msie/.test( userAgent ) && !/opera/.test( userAgent ),
        mozilla: /mozilla/.test( userAgent ) && !/(compatible|webkit)/.test( userAgent )
    };

})(jQuery);


cms3Forum = function() {
    var self = this;
    var editting = -1;
    var editor;
    var content;

    $(".updated").each(function() {
        if ($.browser.msie && $.browser.version == 6)
          return;
        var self = $(this);
        self.text(new Date(self.attr("title")).timeAgoInWords());
    });
    $(".Post .edit").click(function() {
        cms3Forum.EditPost($(this).parent().parent().attr("rel"));
    });
    $(".Post .save").click(function() {
        cms3Forum.SavePost($(this).parent().parent().attr("rel"));
    });
    $(".Post .cancel").click(function() {
        cms3Forum.CancelEdit($(this).parent().parent().attr("rel"));
    });
    $(".Post .delete").click(function() {
        cms3Forum.DeletePost($(this).parent().parent().attr("rel"));
    });
    $(".forum #reply").click(function() {
        cms3Forum.Reply();
    });
    $(".forum .forum-logout").click(function() {
        cms3Forum.Logout($(this).attr("rel"));
    });


    var toggle = function(id) {
        var tr = $("#post-" + id);
        tr.find(".controls p").toggle();
    };
    return {
        Message: function(message) {
            $.jGrowl(message);
        },
        EditPost: function(id) {
            if (editting != -1)
                return;
            var tr = $("#post-" + id);
            var contentElement = tr.find(".entry-content");
            content = contentElement.html();

            editor = new nicEditor({ buttonList: ['bold', 'italic', 'underline', 'strikeThrough', 'link'], iconsPath: BaseUrl + "cms/images/nicEditorIcons.gif" });
            editor.panelInstance("Post-body-" + id, { hasPanel: true });
            toggle(id);
            editting = id;
        },
        SavePost: function(id) {
            if (editting == -1)
                return;

            editor.removeInstance("Post-body-" + id);
            editor = null;
            toggle(id);
            editting = -1;

            $.ajax({ type: "POST",
                url: ForumUrls.EditPost.replace(-5, id),
                data: { id: id, text: $("#Post-body-" + id).html() },
                dataType: "json",
                success: function(data) {
                    cms3Forum.Message(data.Message);
                }
            });
        },
        CancelEdit: function(id) {
            $("#post-" + id + " .entry-content").html(content);
            editting = -1;
            toggle(id);
        },
        DeletePost: function(id) {
            if (confirm("Are you sure?")) {
                cms3.Ajax(ForumUrls.DeletePost.replace(-5, id), {}, function(data) {
                    cms3Forum.Message(data.Message);
                    $("#post-" + id).fadeOut();
                });
            }
        },
        Reply: function() {
            
            
            $(".forum #post-reply div").toggle();
            editor = new nicEditor({ buttonList: ['bold', 'italic', 'underline', 'strikeThrough', 'link'], iconsPath: BaseUrl + "cms/images/nicEditorIcons.gif" });
            editor.panelInstance("body", { hasPanel: true });
            $("#replyform > div").css({ width: "590px" });
        },
        Logout: function(id) {
            $.ajax({ type: "POST",
                url: ForumUrls.Logout.replace(-5, id),
                data: {},
                dataType: "json",
                success: function(data) {
                    cms3Forum.Message(data.Message);
                }
            });
            return false;
        }
    };
} ();