// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

var Tagger = function() {

    return {
        tagPhoto : function(photo_id)
        {
            var photo_tag = '#photo_' + photo_id;

            if ($(photo_tag).is(':checked'))
            {
                $(photo_tag).attr('checked', false);
            }
            else
            {
                $(photo_tag).attr('checked', true);
            }
        },

        setAllCheckboxes : function(fieldName)
        {
            $("input[name=" + fieldName + "]").attr('checked', true);
        },

        setCheckboxes : function(fieldName, checkValue, start, limit)
        {
            $("input[name=" + fieldName + "]:lt(" + limit + ")").attr('checked', checkValue);
        },

        clearAllCheckboxes : function(fieldName)
        {
            $("input[name=" + fieldName + "]").attr('checked', false);
        },

        clearAndSetCheckboxes : function(fieldName, checkValue, start, limit)
        {
            Tagger.clearAllCheckboxes(fieldName);
            Tagger.setCheckboxes(fieldName, checkValue, start, limit);
        },

        reTag : function(recipe_id)
        {
            $('#flickr-photos').hide();
            $('#flickr-photos-status').show();
            $('#tag').attr('value', 'filmdev:recipe=' + recipe_id);
            $('#ignore_tagged').attr('checked', false);
            $('#flickr_photos_for_tagging').submit();
        }
    };
}();


jQuery.fn.submitWithAjax = function() {
  this.submit(function() {
    $.post(this.action, $(this).serialize(), null, "script");
    return false;
  })
  return this;
};
 
$(document).ready(function() {
  // Copyright footer
  $('a#copyright').click(function() {
    $('#footer-extra').fadeIn('slow');
    return false;
  });

  // Recipe HTML
  $('a#other').click(function() {
    $('#other-copy').fadeIn('slow');
    return false;
  });

  // Flickr Photos for tagging
  $('#flickr_photos_for_tagging').submitWithAjax();
  $('#tag_search_submit').click(function() {
    $('#flickr-photos').hide();
    $('#flickr-photos-status').show();
  });

  // Adding a film
  $('#film_film_name_id').change(function(){
    var url = '/admin_film/make_tags?film_name_id=' + $("#film_film_name_id").val() + '&film_speed_id=' + $("#film_film_speed_id").val()
    $.ajax({
      type: 'GET',
      dataType: 'script',
      url: url
    });
  });
  $('#film_film_speed_id').change(function(){
    var url = '/admin_film/make_tags?film_name_id=' + $("#film_film_name_id").val() + '&film_speed_id=' + $("#film_film_speed_id").val()
    $.ajax({
      type: 'GET',
      dataType: 'script',
      url: url
    });
  });

  // Adding a developer
  $('#developer_name').change(function(){
    var url = '/admin_developer/make_tags?developer_name=' + $("#developer_name").val() + '&developer_brand_id=' + $("#developer_developer_brand_id").val()
    $.ajax({
      type: 'GET',
      dataType: 'script',
      url: url
    });
  });
  $('#developer_developer_brand_id').change(function(){
    var url = '/admin_developer/make_tags?developer_name=' + $("#developer_name").val() + '&developer_brand_id=' + $("#developer_developer_brand_id").val()
    $.ajax({
      type: 'GET',
      dataType: 'script',
      url: url
    });
  });
  
  // Animate flash div
  $('#flash').css({backgroundColor: '#ff0'});
  setTimeout(function() {
    $('#flash').animate({backgroundColor: '#eee'}, 500);
  }, 500);

  // Animate flash div
  $('#flash_error').css({backgroundColor: '#f00'});
  setTimeout(function() {
    $('#flash_error').animate({backgroundColor: '#eee'}, 500);
  }, 500);

  // Recipe import
  $('#recipe-import-submit').click(function() {
    $('#recipe-import-status').show();
  });


});
 
