
function enable_buttons()
{
  $('#next-btn').attr('disabled', false);
  if ($('#stack-pos').val() > 0) {
    $('#prev-btn').attr('disabled', false);
  }
  $('#versions-menu-btn').attr('disabled', false);
  $('#print-btn').attr('disabled', false);
}

function setup_examples() {
  $('#more-examples-note input[type=button]').click(function () {
    $('#more-examples-note').hide();
    $('#more-examples-buttons').show();
  });
  $('#more-examples-buttons input[type=button]').click(function() {
    $('#raw-words').val($('#'+this.name+'-text').val());
  });
  $('#more-examples-note').show();
}

function setup_printer_version() {
  // When the user asks to show printer version,
  // insruct the form to open in a new window.
  //
  // (We can't open the printer version in the currect window,
  // because the user might eventually press 'Back' ...and most
  // browsers can't restore form contents.)
  $('#print-btn').click(function() {
    this.form.open_in_new_window = true;
  });
  $('#crossword-form').submit(function() {
    if (this.open_in_new_window) {
      this.target = '_blank';
      this.open_in_new_window = false;
    } else {
      this.target = '';
    }
  });
}

function setup_form() {
  $('#fill-in')   .click(function() { $('#allow-tzmudim').attr('checked', false) } );
  $('#find-words').click(function() { $('#allow-tzmudim').attr('checked', 'checked') } );

  $('#versions-menu-btn').click(function() {
    $(this).parent('span').hide();
    $('#versions-menu').show('slow');
  });
}

$(function() {

  // 'js enabled' cookie.
  document.cookie = 'has_js=1; path=/';

  enable_buttons();
  setup_examples();
  setup_printer_version();
  setup_form();

});
