﻿$(document).ready(function() {

    var $listActions = $(".actionBarLink");
    var $enableActions = $(".enableAction");
    var $actionBar = $('.actionBar');

    $listActions.each(function() {
        $(this).data('href', $(this).attr('href'));
    });

    $('#selectAll').click(function() {
        var checked_status = this.checked;
        $enableActions.each(function() {
            this.checked = checked_status;
        });
    });

    enableAction();

    function enableAction() {
        if ($enableActions.is(':checked')) {
            //If any of the enableAction checkboxes are checked:
            //1. Remove any "inactive" from the icon src.
            $actionBar.find('a img:not(.actionBarStaticImage)').each(function() {
                var imgUrl = $(this).attr('src');
                $(this).attr('src', imgUrl.replace('Inactive.png', '.png'));
            });
            //2. Put any stored "href" or "onclick" attributes back in the href.
            $listActions.each(function() {
                $(this).removeClass('inactive');
                $(this).attr('href', $(this).data('href'));
                $(this).click(function() {
                    $('#actionForm').submit();
                });
            });
        } else {
            //If none of the enableAction checkboxes are checked:
            //1. Add "inactive" to the image src.
            $actionBar.find('a img:not(.actionBarStaticImage)').each(function() {
                var imgUrl = $(this).attr('src');
                $(this).attr('src', imgUrl.replace('.png', 'Inactive.png'));
            });
            //2. Remove the href and onclick attributes from the a tag,
            //but store it for use in the "enable" flow.
            $listActions.each(function() {
                $(this).addClass('inactive');
                $(this).removeAttr('href');
                $(this).unbind('click');
            });
        }
    }

    $enableActions.click(function() {
        enableAction();
    });

});