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

jQuery.fn.submitWithAjax = function() {
    this.submit(function() {
        $.post(this.action, $(this).serialize(), null, 'script');
        return false;
    })
    return this;
};


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

var Event = function() {

    return {
        load : function(action)
        {
            $('#events').hide();
            $('#wait-indicator').show();
            $('#loading').show();

            $.get(action, function(data) {
              $('#events').html(data);
                $('#wait-indicator').hide();
                $('#loading').hide();
                $('#events').show();
            });
        },
        load_group : function(group_id)
        {
            $('#groups-list').hide();
            $('#group-events').hide();
            $('#wait-indicator').show();
            $('#loading').show();

            $.get('/event/get_group_events?id=' + group_id, function(data) {
              $('#group-events').html(data);
                $('#groups-list').show();
                $('#group-events').show();
                $('#wait-indicator').hide();
                $('#loading').hide();
            });
        },
    };
}();


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

$(document).ready(function() {

    $('#flickr-get-photos').submitWithAjax();

    // Fetch photos for event
    $('#flickr-get-photos').click(function() {
        $('#wait-indicator').show();
    });

    if ($('#events').length) {
        Event.load('/event/get_events');
    }

    // Fetch all events when button clicked
    $('#events-fetch-all').click(function() {
        Event.load('/event/get_events');
    });

    // Fetch attended events when button clicked
    $('#events-fetch-attended').click(function() {
        Event.load('/event/get_events?status=attend_only');
    });

    // Fetch watched events when button clicked
    $('#events-fetch-watched').click(function() {
        Event.load('/event/get_events?status=watch_only');
    });

    // Fetch groups when button clicked
    $('#events-fetch-groups').click(function() {
        Event.load('/event/groups');
    });
});

