﻿var causeValidation = true;
$(document).ready(function() {

    $("#approval-stats").click(function() {
        $("#approval-list").slideToggle();
    });


    $("#save-topic").click(function() {
        if (WBS.Data.Topic.Save(topicId)) {
            var a = $(this);
            a.blur();
            a.attr("title", "Saved");
            a.html("Saved");
            a.unbind("click");
        };
        return false;
    });

    $("#print-topic").click(function() {
        window.print();
        return false;
    });

    $("#flag-topic").click(function() {
        if (confirm("Are you sure you want to flag this topic?")) {
            if (WBS.Data.Topic.Flag(topicId)) {

                var error = $("<div class=\"error\">This Topic has been flagged</div>");
                error.hide();
                error.insertBefore(".actionLinks").slideDown();

                $(".actionLinks, #comment-form").hide();
            };
        };
        return false;
    });




    $(".commentFlag").click(function() {
        if (confirm("Are you sure you want to flag this comment?")) {
            if (WBS.Data.Comment.Flag($(this).attr("href"))) {
                var commentDiv = $($(this).parent().parent().parent());
                var h = commentDiv.height();
                commentDiv.removeAttr("class");
                commentDiv.addClass("error");
                commentDiv.css({ "height": h });
                commentDiv.text("This Comment has been flagged.");
                commentDiv.animate({ "height": "1em" }, 500).animate({ "opacity": 1 }, 1000).fadeOut();
            };
        };
        return false;
    });




    //===================================================
    //  COMMENT CODE
    //===================================================
    $("#aspnetForm").submit(function() {
        // ignore poll submits
        if (!causeValidation) return true;

        disableCommentSubmit();

        var e = [];
        var currentUserId = $("#current-user-id").val();
        var updateElement = "#comment-list";
        var CommentEmail = $.trim($("#CommentEmail").val());
        var CommentPassword = $.trim($("#CommentPassword").val());
        var CommentNewEmail = $.trim($("#CommentNewEmail").val());
        var CommentNewPassword = $.trim($("#CommentNewPassword").val());
        var CommentMemberName = $.trim($("#CommentMemberName").val());
        var CommentTitle = $.trim($("#CommentTitle").val());
        var CommentContents = $.trim($("#CommentContents").val());


        if (CommentTitle == "")
            e.push("You must enter a title for your comment");

        if (CommentContents == "")
            e.push("You must enter a comment");

        if (!currentUserId) {
            if (CommentEmail == "" && CommentNewEmail == "") {
                e.push("You need to enter your email address");
            }
            else if (CommentEmail != "") {
                if (CommentPassword == "")
                    e.push("You must enter your password");
            }
            else if (CommentNewEmail != "") {
                if (CommentNewPassword == "")
                    e.push("You must enter your password");

                if (CommentMemberName == "")
                    e.push("You must choose a Member Name");
            }
        }

        if (e.length > 0) {
            alert("Oops. Please correct the following:\n - " + e.join("\n - "));
            enableCommentSubmit();
            return false;
        }

        var data = {};

        if (CommentNewEmail != "") {
            data = {
                isAnonymous: true,
                topicId: topicId,
                email: CommentNewEmail,
                password: CommentNewPassword,
                username: CommentMemberName,
                title: CommentTitle,
                contents: CommentContents
            };
        }
        else {
            data = {
                isAnonymous: false,
                topicId: topicId,
                userId: $("#current-user-id").val(),
                title: CommentTitle,
                contents: CommentContents
            };

            if (!data.userId) {
                WBS.Data.User.Authenticate(CommentEmail, CommentPassword, {
                    async: false,
                    success: function(result) {
                        switch (result.response) {
                            case "OK":
                                data.userId = result.UserId;
                                break;

                            case "FAIL":
                                e.push("Authentication Failed");
                                break;
                        }
                    }
                });
            };
        }

        if (e.length > 0) {
            alert("Error:\n - " + e.join("\n - "));
            enableCommentSubmit();
            return false;
        }


        WBS.Data.Topic.Comment(data, {

            success: function(result) {
                if (result.Response == "OK") {
                    // Clear sign-in form
                    $("#CommentEmail").val("");
                    $("#CommentPassword").val("");
                    $("#CommentNewEmail").val("");
                    $("#CommentNewPassword").val("");
                    $("#CommentMemberName").val("");
                    $("#CommentTitle").val("");
                    $("#CommentContents").val("");

                    var url = "/resource.aspx/GetComment?CommentId=" + result.CommentId;
                    $.get(url, function(data) {
                        var contents = $(data);
                        WBS.UI.BindActionLinks(contents);
                        contents.hide().appendTo($("#comment-list")).slideDown("fast");
                    });

                    $("#signup").slideUp("fast");

                    if ($("#signed-in-as").length == 0) {
                        var div = $("<div>").load("/resource.aspx/GetSignedInAs").hide();
                        $("#CommentSubmit").before(div);
                        div.slideDown();
                    }
                }
            }
        });


        enableCommentSubmit();
        return false;
    });



    $("#CommentEmail,#CommentPassword").bind("keypress", function(e) {
        if ($.trim($(this).val()).length > 0) {
            $("#CommentNewEmail").val("");
            $("#CommentNewPassword").val("");
            $("#CommentMemberName").val("");
        }
        if (e.which == 13)
            $("#CommentSubmit").trigger("click");
    });

    $("#CommentNewEmail,#CommentNewPassword,#CommentMemberName").bind("keypress", function(e) {
        if ($.trim($(this).val()).length > 0) {
            $("#CommentEmail").val("");
            $("#CommentPassword").val("");
        }
        if (e.which == 13)
            $("#CommentSubmit").trigger("click");
    });

    $("#CommentTitle").bind("keypress", function(e) {
        if (e.which == 13) $("#CommentSubmit").trigger("click");
    });

    $("#save-topic").tooltip({
        bodyHandler: function() {
            return $("#saveHelpContent").html();
        },
        showURL: false
    });
});


function enableCommentSubmit() {
    $("#CommentSubmit").removeAttr("disabled");
}

function disableCommentSubmit() {
    $("#CommentSubmit").attr("disabled", "disabled");
}

function disableVoting(el, vote) {
    el.empty();
    el.append("<em class=\"vote\">You said: "+ vote +"</em>");
}

