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

var RecipeImport = function() {

    return {
        hideToggle : function(div, label)
        {
            $(div).hide();
            $(label).click(function() {
                RecipeImport.showToggle(div, label);
            });
        },
        showToggle : function(div, label)
        {
            $(div).show();
            $(label).click(function() {
                RecipeImport.hideToggle(div, label);
            });
        },
        choose : function(selected, ignored)
        {
            $(selected).removeAttr('disabled');
            $(ignored).attr('disabled', true);
        },
        tagPhoto: function(recipe_id, photo_id)
        {
            var photo_tag = '#photo_' + recipe_id + "_" + photo_id; 
            $(photo_tag).attr('checked', !$(photo_tag).is(':checked'));
        }
    };
}();

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);
            }
        },

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

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

            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();
            }
    };
}();

var Recipe = function() {

    return {
        reload : 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();
            return false;
        }
    };
}();

var AutoComplete = function() {

    return {
        match: function(response, term, list) {
                   var term = term.replace(/[-+'\/\s_]/g,"");

                   var matcher = new RegExp( $.ui.autocomplete.escapeRegex(term), "i");
                   response( $.grep(list, function(value) {
                       return value != null ? matcher.test(value.value) : null;
                   }) );
               }
    };
}();

// Make sure that every Ajax request sends the CSRF token
function CSRFProtection(xhr) {
    var token = $('meta[name="csrf-token"]').attr('content');
    if (token) xhr.setRequestHeader('X-CSRF-Token', token);
}
if ('ajaxPrefilter' in $) $.ajaxPrefilter(function(options, originalOptions, xhr) { CSRFProtection(xhr); });
else $(document).ajaxSend(function(e, xhr) { CSRFProtection(xhr); });

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;
    });

    // 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();
    });

    // Recipe HTML
    $("#html_link_click").click(function() {
        $("#html_link").dialog("open");
        return false;
    });
    $("#html_link").dialog({autoOpen: false, height: 200, width: 420, title: 'Link to this recipe' });

    // Tagging photos
    $('#tag_submit').click(function() {
        $('#flickr-tagging-status').show();
    });

    // Make text in 'tagged' textarea selected when clicked
    $('#tagged-textarea').click(function() {
        $('#tagged-textarea').select();
    });

    // Add select/deselect handlers to links in above page
    $('#tag-select-all').click(function() {
        Tagger.selectAll('photo[]');
        return false;
    });
    $('#tag-unselect-all').click(function() {
        Tagger.deselectAll('photo[]');
        return false;
    });
});


