﻿$(document).ready(function() {

    //===================================================
    //  COMMENT CODE
    //===================================================
    $("#CommentSubmit").click(function() {
        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,
                pollId: pollId,
                email: CommentNewEmail,
                password: CommentNewPassword,
                username: CommentMemberName,
                title: CommentTitle,
                contents: CommentContents
            };
        }
        else {
            data = {
                isAnonymous: false,
                pollId: pollId,
                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;
                        }
                    }
                });
            };
        }
        
        
        var result = WBS.Data.Poll.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() {
        if ($.trim($(this).val()).length > 0)
        {
            $("#CommentNewEmail").val("");
            $("#CommentNewPassword").val("");
            $("#CommentMemberName").val("");
        }
    });

    $("#CommentNewEmail,#CommentNewPassword,#CommentMemberName").bind("keypress", function() {
        if ($.trim($(this).val()).length > 0)
        {
            $("#CommentEmail").val("");
            $("#CommentPassword").val("");
        }
    });


    //================================================
    //   VOTING CODE
    //================================================ 

    $("a.commentVoteYes,a.commentVoteNo,a.pollVoteYes,a.pollVoteNo").bind("click", function () {
        var link = $(this);
        var url = link.attr("href");
        
        if (WBS.Data.Comment.Vote(url))
        {
            var vote = (url.toLowerCase().indexOf("vote=yes") > -1) ? "Yes" : "No";
            disableVoting(link.parent(), vote);
        }
        return false;
    });



    //================================================
    //   PEER SPHERE
    //================================================ 

    $("a.peersphere").bind("click", function () {
        var link = $(this);
        var url = link.attr("href");
        
        $.getJSON(url, function(data) {
            $(link).before("<span class=\"inpeersphere\">In your sphere</span>");
            link.remove();
        });

        return 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>");
}

