function registerVote() {
  $('#vote-dialog button').attr('disabled', 'disabled');
  
  var data = {
    nonce: jQuery('#vote-nonce').val(),
    nomineeID: jQuery('#vote-nomineeID').val(),
    categoryID: jQuery('#vote-categoryID').val(),
    userName: jQuery('#vote-userName').val(),
    userEmail: jQuery('#vote-userEmail').val()
  };
  
  jQuery.post('/wp-content/plugins/sll_blogluxe_voting/registerVote.php', data, function(responseData) {
    $('#vote-dialog button').attr('disabled', '');
    $('#vote-dialog').dialog('close');
    
    if(responseData.success) {
      alert('Your vote has been recorded!');
      //window.location.reload(false);
    }
    
    if(responseData.error) {
      alert(responseData.error);
    }
  },'json');
}

function registerNomination() {
  $('#nominate-dialog button').attr('disabled', 'disabled');
  
  var data = {
    nonce: jQuery('#nominate-nonce').val(),
    categoryID: jQuery('#nominate-categoryID').val(),
    blogName: jQuery('#nominate-blogName').val(),
    blogUrl: jQuery('#nominate-blogUrl').val(),
    userName: jQuery('#nominate-userName').val(),
    userEmail: jQuery('#nominate-userEmail').val()
  };
  
  jQuery.post('/wp-content/plugins/sll_blogluxe_voting/registerNomination.php', data, function(responseData) {
    $('#nominate-dialog button').attr('disabled', '');
    $('#nominate-dialog').dialog('close');
    
    if(responseData.success) {
      alert('Your nomination has been recorded. It will appear on our site as soon as it is approved!');
      window.location.reload(false);
    }
    
    if(responseData.error) {
      alert(responseData.error);
    }
  },'json');
}

function openVoteDialog() {
  //...the id attr holds a "id+id"-formatted string
  //   containing the categoryID and nominee ID clicked on...
  var rawId = this.id;
  var ids = /([0-9]+)\+([0-9]+)/g.exec(rawId);
  
  if(ids != null) {
    $('#vote-categoryID').val(ids[1]);
    $('#vote-nomineeID').val(ids[2]);

    $('#vote-dialog').dialog('open');
  }
  return false;
}

function openNominateDialog() {
  //...the id attr holds a "id"-formatted string
  //   containing the categoryID and nominee ID clicked on...
  var rawId = this.id;
  var ids = /nominate-link-([0-9]+)/g.exec(rawId);
  
  if(ids != null) {
    $('#nominate-categoryID').val(ids[1]);
    
    var catName = $('#category-name-' + ids[1]).val();
    $('#nomiate-dialog').attr('title', 'Nomination for the ' + catName + ' BlogLuxe Award');

    $('#nominate-dialog').dialog('open');
  }
  return false;
}

jQuery(document).ready(function($) {
  $('#category-accordion').accordion(
    {
      header: 'h3',
      autoHeight: false,
      
      //...the following two options ("collapsible" and "active" are
      //   set to make all sections collapsed when the page first is
      //   set up...
      collapsible: true,
      active: false
    }
  );
  
  $('#vote-dialog').dialog(
    {
      autoOpen: false,
      modal: true,
      buttons: {
        "Cancel": function() {
          $('#vote-dialog').dialog('close');
        },
        "Vote!": registerVote
      }
    }
  );
  
  $('#nominate-dialog').dialog(
    {
      autoOpen: false,
      modal: true,
      buttons: {
        "Cancel": function() {
          $('#nominate-dialog').dialog('close');
        },
        "Make Nomination": registerNomination
      }
    }
  );
  
  $('.category button.vote-link').click(openVoteDialog);
  $('.category button.nominate-link').click(openNominateDialog);
});
