$(document).ready(function(){
  $(".favheart").click(function(){
    var quote_id = $(this).attr('id').split("_");
    quote_id = quote_id[quote_id.length - 1];
    if ($(this).hasClass("favheartOff")) {
      favs.addToFavs(quote_id);
    }
    if ($(this).hasClass("favheartOn")) {
      favs.removeFromFavs(quote_id);
    }
  });

});


      var favs = {
        addToFavs : function ( quote_id ) {
          var url = '/ajax/fav.php';
          var action = 'add';
          var params = 'action=' + action
                     + '&quote_id=' + quote_id
                     ;
          $.ajax({
            type: "GET",
            url: url,
            data: params,
            success: function(msg) {
              if (msg == 'ok') {
                $('#fav_btn_' + quote_id).addClass('favheartOn');
                $('#fav_btn_' + quote_id).removeClass('favheartOff');
                $('#fav_btn_' + quote_id).attr({alt:'remove from favs', title:'remove from favs'});
                var new_score = parseInt($('#score_' + quote_id).html()) + 1;
                $('#score_' + quote_id).text(new_score);
              } else {
                alert('Please sign in to add this quote to your favs');
              }
            },
            error: function() {
              alert('uh oh, something went wrong.  Please try adding that quote to your favorites again.');
            }
          });
        },

        removeFromFavs : function ( quote_id ) {
          var url = '/ajax/fav.php';
          var action = 'remove';
          var params = 'action=' + action
                     + '&quote_id=' + quote_id
                     ;
          $.ajax({
            type: "GET",
            url: url,
            data: params,
            success: function(msg) {
              if (msg == 'ok') {
                $('#fav_btn_' + quote_id).addClass('favheartOff');
                $('#fav_btn_' + quote_id).removeClass('favheartOn');
                $('#fav_btn_' + quote_id).attr({alt:'add to favs', title:'add to favs'});
                var new_score = parseInt($('#score_' + quote_id).html()) - 1;
                $('#score_' + quote_id).text(new_score);
              } else {
                alert('uh oh');
              }
            },
            error: function() {
              alert('uh oh, something went wrong.  Please try removing that quote from your favorites again.');
            }
          });
        }


      }

      var quote = {
        reportQuote : function ( quote_id ) {
          var url = '/ajax/quote.php';
          var action = 'report';
          var params = 'action=' + action
                     + '&quote_id=' + quote_id
                     ;
          $.ajax({
            type: "GET",
            url: url,
            data: params,
            success: function(msg) {
              if (msg == 'ok') {
                alert('Thank you for telling us that this quote doesn\'t follow the rules.');
              } else {
                alert('Please sign in to report this quote');
              }
            },
            error: function() {
              alert('uh oh. something went wrong.  Please try reporting that quote again.');
            }
          });
        }
      }


      var follow = {
        startfollowing : function ( user_id ) {
          var url = '/ajax/follow.php';
          var action = 'startfollowing';
          var params = 'action=' + action
                     + '&user_id=' + user_id
                     ;
          $.ajax({
            type: "GET",
            url: url,
            data: params,
            success: function(msg) {
              if (msg == 'ok') {
                alert('ok');
              } else {
                alert('Please sign in to follow this person.');
              }
            },
            error: function() {
              alert('uh oh, something went wrong.  Please try following that person again.');
            }
          });
        },

        stopfollowing : function ( user_id ) {
          var url = '/ajax/follow.php';
          var action = 'stopfollowing';
          var params = 'action=' + action
                     + '&user_id=' + user_id
                     ;
          $.ajax({
            type: "GET",
            url: url,
            data: params,
            success: function(msg) {
              if (msg == 'ok') {
                alert('ok');
              } else {
                alert('not ok');
              }
            },
            error: function() {
              alert('uh oh, something went wrong.  Please try to stop following that person again.');
            }
          });
        }


      }


      var block = {
        startblocking : function ( user_id ) {
          var url = '/ajax/block.php';
          var action = 'startblocking';
          var params = 'action=' + action
                     + '&user_id=' + user_id
                     ;
          $.ajax({
            type: "GET",
            url: url,
            data: params,
            success: function(msg) {
              if (msg == 'ok') {
                alert('Ok.  This person\'s comments will no longer show on your profile and your quotes.');
              } else {
                alert('Please sign in to block this person.');
              }
            },
            error: function() {
              alert('uh oh, something went wrong.  Please try blocking that person again.');
            }
          });
        },

        stopblocking : function ( user_id ) {
          var url = '/ajax/block.php';
          var action = 'stopblocking';
          var params = 'action=' + action
                     + '&user_id=' + user_id
                     ;
          $.ajax({
            type: "GET",
            url: url,
            data: params,
            success: function(msg) {
              if (msg == 'ok') {
                alert('ok.  This person\'s comments will now show on your profile and your quotes.');
              } else {
                alert('not ok');
              }
            },
            error: function() {
              alert('uh oh, something went wrong.  Please try to stop blocking that person again.');
            }
          });
        }


      }



      var votes = {

        //
        // vote up a quote
        //
        voteUp : function ( quote_id ) {
          var url = '/ajax/vote.php';
          var action = 'voteup';
          var params = 'action=' + action
                     + '&quote_id=' + quote_id
                     ;
          var intervalID = setInterval('drawrandomchar('+quote_id+')',100);

          $.ajax({
            type: "GET",
            url: url,
            data: params,
            success: function(msg){
              if (msg == 'ok') {
                $.get("/ajax/vote.php?action=getscore&quote_id=" + quote_id, function(data){
                  clearInterval(intervalID);
                  $("#score_" + quote_id).html(parseInt(data) + 1);
                });
              } else {
                clearInterval(intervalID);
                $("#score_" + quote_id).html("<h3 class='quotevotenum'>signin</h3>").animate({opactiy:1},4000).load("/ajax/vote.php?action=getscore&quote_id=" + quote_id);
              }
            },
            error: function() {
              clearInterval(intervalID);
              alert('Uh oh,  something went wrong.  Please try voting up this quote again.');
            }
          });
        },

        //
        // vote down a quote
        //
        voteDown : function ( quote_id ) {
          var url = '/ajax/vote.php';
          var action = 'votedown';
          var params = 'action=' + action
                     + '&quote_id=' + quote_id
                     ;
          var intervalID = setInterval('drawrandomchar('+quote_id+')',100);
          $.ajax({
            type: "GET",
            url: url,
            data: params,
            success: function(msg){
              if (msg == 'ok') {
                $.get("/ajax/vote.php?action=getscore&quote_id=" + quote_id, function(data){
                  clearInterval(intervalID);
                  $("#score_" + quote_id).html(parseInt(data) - 1);
                });
              } else {
                clearInterval(intervalID);
                $("#score_" + quote_id).html("<h3 class='quotevotenum'>signin</h3>").animate({opactiy:1},4000).load("/ajax/vote.php?action=getscore&quote_id=" + quote_id);
              }
            },
            error: function() {
              clearInterval(intervalID);
              //$("#score_" + quote_id).html("?");
              alert('Uh oh,  something went wrong.  Please try voting down this quote again.');
            }
          });
        },

        //
        // vote up a quote RANDOM
        //
        voteUpRandom : function ( quote_id ) {
          var url = '/ajax/vote.php';
          var action = 'voteup';
          var params = 'action=' + action
                     + '&quote_id=' + quote_id
                     ;
          var intervalID = setInterval('drawrandomchar('+quote_id+')',100);
          $.ajax({
            type: "GET",
            url: url,
            data: params,
            success: function(msg){
              if (msg == 'ok') {
                $.get("/ajax/vote.php?action=getscore&quote_id=" + quote_id, function(data){
                  clearInterval(intervalID);
                  $("#score_" + quote_id).html(data);
                  window.location = '/random-quote';
                });
              } else {
                clearInterval(intervalID);
                $("#score_" + quote_id).html("<h3 class='quotevotenum'>signin</h3>").animate({opactiy:1},4000).load("/ajax/vote.php?action=getscore&quote_id=" + quote_id);
              }
            },
            error: function() {
              clearInterval(intervalID);
              alert('Uh oh,  something went wrong.  Please try voting up this quote again.');
            }
          });
        },

        //
        // vote down a quote RANDOM
        //
        voteDownRandom : function ( quote_id ) {
          var url = '/ajax/vote.php';
          var action = 'votedown';
          var params = 'action=' + action
                     + '&quote_id=' + quote_id
                     ;
          var intervalID = setInterval('drawrandomchar('+quote_id+')',100);
          $.ajax({
            type: "GET",
            url: url,
            data: params,
            success: function(msg){
              if (msg == 'ok') {
                $.get("/ajax/vote.php?action=getscore&quote_id=" + quote_id, function(data){
                  clearInterval(intervalID);
                  $("#score_" + quote_id).html(data);
                  window.location = '/random-quote';
                });
              } else {
                clearInterval(intervalID);
                $("#score_" + quote_id).html("<h3 class='quotevotenum'>signin</h3>").animate({opactiy:1},4000).load("/ajax/vote.php?action=getscore&quote_id=" + quote_id);
              }
            },
            error: function() {
              clearInterval(intervalID);
              //$("#score_" + quote_id).html("?");
              alert('Uh oh,  something went wrong.  Please try voting down this quote again.');
            }
          });
        }

      }

var uglychars = ['.','..','...','..'];
var curuglychar = 0;

function drawrandomchar(quote_id) {
    numchars = uglychars.length;
    curuglychar = ++curuglychar%numchars;
    $("#score_"+quote_id).html(uglychars[curuglychar]);
}

