EA.namespace('Ao2.Twitter');

EA.Ao2.Twitter = {
    init: function() {
        EA.Ao2.Twitter.bindEvents();
        EA.Ao2.Twitter.setupDialogs();
    },

    bindEvents: function() {
        $j('.button-box-search a').click(function() {
            $j('#searchForm').submit();
        });

        $j('.tweet-back a').click(function() {
            $j('#postTweetBox').dialog('open');
            return false;
        });

        $j('.tweeter-avatar-follow a').click(function() {
            var author = $j(this).attr('class');
            $j("#followUserBox #followTitle").text('Follow ' + author);
            $j("#followUserBox .followUsername").val(author);
            $j('#followUserBox').dialog('open');
            return false;
        });

        $j('#tabLiveFeed').click(function() {
            EA.Twitter.getLiveFeed(function(data) {
                $j('#liveFeed').show();
                $j('#topReplies, #topUsers').hide();
                $j('#topReplies .content, #topUsers .content').html(" ");

                $j('#tabLiveFeed').attr('class', 'selected');
                $j('#tabTopReplies, #tabTopUsers').attr('class', '');

                $j('#liveFeed .content').html(data);
            });
            return false;
        });

        $j('#tabTopReplies').click(function() {
            EA.Twitter.getTopReplies(function(data) {
                $j('#topReplies').show();
                $j('#liveFeed, #topUsers').hide();
                $j('#liveFeed .content, #topUsers .content').html(" ");

                $j('#tabTopReplies').attr('class', 'selected');
                $j('#tabLiveFeed, #tabTopUsers').attr('class', '');

                $j('#topReplies .content').html(data);
            });
            return false;
        });
        $j('#tabTopUsers').click(function() {
            EA.Twitter.getTopUsers(function(data) {
                $j('#topUsers').show();
                $j('#liveFeed, #topReplies').hide();
                $j('#liveFeed .content, #topReplies .content').html(" ");

                $j('#tabTopUsers').attr('class', 'selected');
                $j('#tabLiveFeed, #tabTopReplies').attr('class', '');

                $j("#topUsers .content").html(data);
            });
            return false;
        });

        $j('.tr-thumbs-up a').live('click', function() {
            if (!$j(this).attr('disabled')) {
                var parent = $j(this).parents('.twitterPost').get(0),
                        postId = EA.Twitter.parseId($j(parent).attr('id'));

                EA.Twitter.votePost('good', postId, EA.Ao2.Twitter.onVoteCompleted);
            }
            return false;
        });
        $j('.tr-thumbs-down a').live('click', function() {
            if (!$j(this).attr('disabled')) {
                var parent = $j(this).parents('.twitterPost').get(0),
                        postId = EA.Twitter.parseId($j(parent).attr('id'));

                EA.Twitter.votePost('bad', postId, EA.Ao2.Twitter.onVoteCompleted);
            }

            return false;
        });

        $j(".ao2-dialog textarea").keyup(function() {
            var textArea = $j(this);
                charCount = EA.Twitter.postCharCount - textArea.val().length;

            if (charCount <= 0) {
                charCount = 0;
                textArea.css('color', 'red');
            } else {
                textArea.css('color', '#999');
            }

            textArea.parent().parent().find('.textLimit').html(charCount);
        }).keyup();

        $j('#add-question').click(function() { $j('#adminAddQuestion').dialog('open'); return false; });
        $j('#edit-question').click(function() { $j('#adminEditQuestion').dialog('open'); return false; });
    },

    setupDialogs: function() {
        $j('#postTweetBox').dialog({
            autoOpen: false,
            bgiframe: true,
            buttons: {
                'Tweet Reply' : function() {
                    EA.Twitter.postReply(
                            $j('#postTweetBox .textBox').val(),
                            $j('#postTweetBox .username').val(),
                            $j('#postTweetBox .password').val(),
                            function (data, textStatus) {
                                if (data == "SUCCESS") {
                                    $j("#postTweetBox").dialog('close');
                                } else if (data == "TWITTER_ERROR") {
                                    $j("#postTweetBox .statusUpdate").html("Invalid username or password.");
                                } else if (data == "SSFAILURE") {
                                    $j("#postTweetBox .statusUpdate").html("Error communicating with server.");
                                }
                            });
                },
                'Cancel': function() {
                    $j(this).dialog('close');
                }
            },
            modal: true,
            open: EA.Ao2.Twitter.onDialogOpen
        });

        $j('#followUserBox').dialog({
            autoOpen: false,
            bgiframe: true,
            buttons: {
                'Follow' : function() {
                    EA.Twitter.followTwitterUser($j('#followUserBox .username').val(), $j('#followUserBox .password').val(), $j('#followUserBox .followUsername').val());
                },
                'Cancel': function() {
                    $j(this).dialog('close');
                }
            },
            modal: true,
            open: EA.Ao2.Twitter.onDialogOpen
        });

        $j('#adminAddQuestion').dialog({
            autoOpen: false,
            bgiframe: true,
            buttons: {
                'Post Question' : function() {
                    EA.Twitter.postQuestion($j('#adminAddQuestion .textBox').val(), $j('#adminAddQuestion .username').val(), $j('#adminAddQuestion .password').val());
                },
                'Cancel': function() {
                    $j(this).dialog('close');
                }
            },
            modal: true,
            open: EA.Ao2.Twitter.onDialogOpen
        });

        $j('#adminEditQuestion').dialog({
            autoOpen: false,
            bgiframe: true,
            buttons: {
                'Edit Question' : function() {
                    EA.Twitter.editQuestion($j('#adminEditQuestion .textBox').val(), $j('#adminEditQuestion .username').val(), $j('#adminEditQuestion .password').val());
                },
                'Cancel': function() {
                    $j(this).dialog('close');
                }
            },
            modal: true,
            open: EA.Ao2.Twitter.onDialogOpen
        });
    },

    onDialogOpen: function(event, ui) {
        $j('.ao2-dialog .textLimit').html(EA.Twitter.postCharCount);
        $j('.ao2-dialog textarea').val('').css('color', '#999');
        $j('.ao2-dialog .statusUpdate').html(' ');
    },

    onVoteCompleted: function(vote, id) {
        var voteElement = $j("#post_" + id + " .tr-votes"),
            voteElementFeatured = $j("#featuredPost_" + id + " .tr-votes"),
            voteVal = voteElement.html();

        if (vote == "good") {
            voteVal++;
        } else if (vote == "bad") {
            voteVal--;
        }

        if (voteVal == 0) {
            voteVal = "0";
        }

        voteElement.html(voteVal);
        voteElementFeatured.html(voteVal);

        EA.Ao2.Twitter.setPostVote(vote, id);
    },

    setPostVote: function(vote, id) {
        if (vote == "good") {
            $j("#post_" + id + " .thumbUp").fadeTo("fast", 0.9).attr('disabled', 'disabled');
            $j("#post_" + id + " .thumbDown").fadeTo("fast", 0.25).attr('disabled', 'disabled');

            $j("#featuredPost_" + id + " .thumbUp").fadeTo("fast", 0.9).attr('disabled', 'disabled');
            $j("#featuredPost_" + id + " .thumbDown").fadeTo("fast", 0.25).attr('disabled', 'disabled');
        }
        else if (vote == "bad") {
            $j("#post_" + id + " .thumbUp").fadeTo("fast", 0.25).attr('disabled', 'disabled');
            $j("#post_" + id + " .thumbDown").fadeTo("fast", 0.9).attr('disabled', 'disabled');

            $j("#featuredPost_" + id + " .thumbUp").fadeTo("fast", 0.25).attr('disabled', 'disabled');
            $j("#featuredPost_" + id + " .thumbDown").fadeTo("fast", 0.9).attr('disabled', 'disabled');
        }
    }
};

$j(EA.Ao2.Twitter.init);