deactivate = false;

var pli = new Array();

function ShowNotice(message, callback) {
  $('#notice').html(message).css('visibility','visible').css('display','inline').fadeOut(2000, callback);
} //ShowNotice()

function HideNotice() {
  $('#notice').css('visibility','hidden').css('display','none');
} // HideNotice()

function preloadImages() {
  for (i = 1; i <= 10; i++) {
    pli['ball' + i.toString()] = new Image();
    pli['ball' + i.toString()].src = 'images/ball' + i.toString() + '.gif';
    pli['ball' + i.toString() + '_full'] = new Image();
    pli['ball' + i.toString() + '_full'] = 'images/ball' + i.toString() + '_full.gif';
  }
} // preloadImages()

$(document).ready(function() {
  var debug = false;

  preloadImages();

  if (debug) {
    $('#debug').css('visibility','visible').css('display','block');
  }

  $('.vote-ball').hover(function() {
    var dbg = '';
    var id = parseInt(this.id.substr(this.id.indexOf('-') + 1, 2));
    dbg += this.id + ' : ' + this.id.indexOf('-') + '<p/>\n';
    dbg += 'ID: ' + id + '<p/>\n';
    for (var i = 1; i <= 10; i++) {
      //var yn = (i <= id) ? 'yes' : 'no';
      var yn = (i <= id) ? '' : '_full';
      var which = '#vote-' + i.toString();
      dbg += which + '<br/>\n';
      $(which).attr('src', 'images/ball' + i + yn + '.gif');
    }
    //alert(dbg);
    //$('#debug').html(dbg);
  }, function() {
    var dbg = '';
    
    dbg += 'rank: ' + your_current_rank + '<p/>';
    for (var i = 1; i <= 10; i++) {
      //var yn = (i <= your_current_rank) ? 'yes' : 'no';
      var yn = (i <= your_current_rank) ? '' : '_full';
      var which = '#vote-' + i.toString();
      dbg += which + '<br/>\n';
      $(which).attr('src', 'images/ball' + i + yn + '.gif');
    }
    //$('#debug').html(dbg);
  }); // hover()
  
  $('.vote-ball').click(function() {
    //alert(this.id);
    if (!deactivate) {
      deactivate = true;
      var vote = parseInt(this.id.substr(this.id.indexOf('-') + 1, 2));
      your_current_rank = vote;
      ShowNotice("Casting ballot...", function(){});
      $.getJSON('vote.php?snowman-id=' + snowman_id + '&vote=' + vote, function(data) {
        if (data.status < 1) {
          ShowNotice("Voting Error: " + data.message);
          deactivate = false;
        } else {
          ShowNotice("Vote received, navigating to next snowman...", function() {window.location = 'randomsnowman.php';});
        }
      });
    }
  }); // click()
  
  $('#show-me').hover(function() {
    this.src = 'images/showme_over.gif';
  }, function() {
    this.src = 'images/showme.gif';
  });
  
  $('#roll').hover(function() {
    this.src = 'images/roll_over.gif';
  }, function() {
    this.src = 'images/roll.gif';
  });
  
});