jQuery.ajaxSetup({
  'beforeSend': function(xhr) {xhr.setRequestHeader("Accept", "text/javascript")}
})
$(document).ajaxSend(function(event, request, settings) {
  if (typeof(AUTH_TOKEN) == "undefined") return;
  settings.data = settings.data || "";
  settings.data += (settings.data ? "&" : "") + "authenticity_token=" + encodeURIComponent(AUTH_TOKEN);
});


function galChanged()
  {
    if (!isNaN(parseInt($(this).val()))) {
      $("#photo_id").load('/javascripts/dynamic_photos.js', {q: $(this).val() } );
      $("#photo_id").show();
    } else {
      $("#photo_id")[0].options.length=1;
      $("#photo_id").hide();
    }
    $("#thumbnail").hide();
  }

function picChanged()
  {
    $("#thumbnail").hide();
    if(!isNaN(parseInt($(this).val()))) {
      $.ajax({
        type: "POST",
        url: "/admin/photos/0/thumbnail",
        data: { photo_id: $(this).val() },
        success: function(msg){
          $("#thumbnail").html(msg);
          $("#thumbnail a").fancybox({'overlayShow':true, 'overlayOpacity':0.7});
          $("#thumbnail").show();
        }
      });
    }
  }

function toggleBlind()
  {
    var show = "show"; hide = "hide";
    $(this).children('span.flag').html( ($(this).children('span').html()==hide ? show : hide ) );
    $(this).siblings('div').slideToggle("slow");
  }


$(document).ready(function() {
  $('.tabset > ul').tabs();
  $('.gallery a').fancybox({'overlayShow':true, 'overlayOpacity':0.7});
  $('#photoselector #gallery_id').change(galChanged);
  $('#photoselector #photo_id').change(picChanged);
  $('fieldset.blinder legend').click(toggleBlind);


  // choose text for the show/hide link - can contain HTML (e.g. an image)
  var showText="Show";
  var hideText="Hide";

  // append show/hide links to the element directly preceding the element with a class of "toggle"
  $(".toggle").prev().append(' (<a href="#" class="toggleLink">'+showText+'</a>)');

  // hide all of the elements with a class of 'toggle'
  $('.toggle').hide();

  // capture clicks on the toggle links
  $('a.toggleLink').click(function() {

    // change the link depending on whether the element is shown or hidden
    if ($(this).html()==showText) {
      $(this).html(hideText);
    }
    else {
      $(this).html(showText);
    }

    // toggle the display - uncomment the next line for a basic "accordion" style
    //$('.toggle').hide();
    $(this).parent().next('.toggle').toggle('slow');

    // return false so any link destination is not followed
    return false;
  });

});







