$(document).ready(function(){
  $('input.starred_checkbox').toggleStarred();
  $('a.alert').toggleAlerting();
});

// From shortcut.js
shortcut.add("s", function() {
  $('#query').focus();
},{
  'disable_in_input':true,
  'propagate':false
});

$.fn.setAltText = function() {
  var alt_text = "{0} alert is {1}.  Click to turn the alert {2}.";

  this.attr('title', alt_text.printf(this.hasClass("dvd") ? "DVD" : "Theatrical",
                                        this.hasClass("alerting") ? "on" : "off",
                                        this.hasClass("alerting") ? "off" : "on"));
};

$.fn.toggleStarred = function() {
  this.each(function() {
    $(this).click(function(e) {
      var id = this.id.match(/^movie_starred_(\d+)$/)[1];
      $.ajax({
        type: "PUT",
        url: '/movies/' + id,
        data: "movie[starred]=" + this.checked
      });
    });
  });
};

$.fn.toggleAlerting = function() {
  this.each(function() {
    $(this).click(function(e) {
      e.preventDefault();

      var id = this.id.match(/^movie_(\d+).*/)[1];
      var release_type = $(this).hasClass("theatrical") ? "theatrical" : "dvd";

      $(this).toggleClass("alerting");
      $(this).setAltText();

      $.post($(this).attr('href'), "alert[movie_id]=" + id + "&alert[release_type]=" + release_type)
    });
  });
};
