var thumbs = {
   my_votes: [],
   all_votes: [],

   load: function(defid, up, down, my_vote) {
      thumbs.all_votes[defid] = [up, down];
      thumbs.set_label(defid);

      if (my_vote != undefined) {
         thumbs.my_votes[defid] = my_vote;
         thumbs.activate(defid, !my_vote);
      }
   },

   send: function(defid, is_up) {
      var img = new Image();
      img.src = 'http://thumbs.urbandictionary.com/save.php?defid=' + defid + '&is_up=' + (is_up ? 1 : 0) + '&r=' + Math.random();
   },

   click: function(defid, is_up) {
      if (thumbs.all_votes[defid] == undefined) {
         thumbs.all_votes[defid] = [0, 0];
      }

      if (thumbs.my_votes[defid] != is_up) {
         thumbs.all_votes[defid][is_up ? 0 : 1]++;

         if (thumbs.my_votes[defid] != undefined) {
            thumbs.all_votes[defid][is_up ? 1 : 0]--;
         }

         thumbs.my_votes[defid] = is_up;
         thumbs.send(defid, is_up);

         thumbs.set_label(defid);
         thumbs.flash(defid, true);
         thumbs.activate(defid, !is_up);
      }
   },

   element: function(defid) {
      return document.getElementById('thumbs_' + defid);
   },

   flash: function(defid, is_on) {
      if (is_on) {
         thumbs.element(defid).style.backgroundColor = '#E7EDCE';
         setTimeout('thumbs.flash(' + defid + ', false)', 300);
      } else {
         thumbs.element(defid).style.backgroundColor = '';
      }
   },

   set_label: function(defid) {
      var up = thumbs.all_votes[defid][0] - 0;
      var down = thumbs.all_votes[defid][1] - 0;
      var label = '';

      if (up && down) {
         label = '<b>' + up + '</b> up, <b>' + down + '</b> down';
      } else if (up) {
         label = '<b>' + up + '</b> ' + (up == 1 ? 'thumb' : 'thumbs') + ' up';
      } else if (down) {
         label = '<b>' + down + '</b> ' + (down == 1 ? 'thumb' : 'thumbs') + ' down';
      }

      thumbs.element(defid).innerHTML = label;
   },

   activate: function(defid, is_up) {
      document.getElementById('thumbs_' + defid + '_1_gif').className = is_up ? 'on' : 'off';
      document.getElementById('thumbs_' + defid + '_0_gif').className = is_up ? 'off' : 'on';
   }
};
