﻿var site = {
    tests: null
};

site.tests = function () {
    return {
        initialize: function () {
            this.addEventListeners(this);
            //this.addAll();
        },

        addEventListeners: function () {
            var self = this;

            $('.toggleCheck').click(function (e) {
                self.toggleCheckAll.apply(self, [this]);
            });

            $('#content_wrapper').delegate('.booktest', 'click', function (e) {
                self.toggleCheckSingle.apply(self, [this]);
            });

            $('.mybook').delegate('.removeall', 'click', function () {
                self.removeAll.apply(self, [this]);
            });


        },

        toggleCheckAll: function () {
            var self = this;
            var element = arguments[0];

            if ($(element).attr('checked')) {
                self.updateCount('addAllFromList');
                $('.booktest').each(function (i, e) {
                    $(this).attr('checked', 'checked');
                });
                self.addAllFromArray();
            } else {
                self.updateCount('removeAllFromList');
                $('.booktest').each(function (i, e) {
                    $(this).removeAttr('checked', '');
                });
                self.removeAllFromArray();
            }
        },

        toggleCheckSingle: function () {
            var self = this;
            var element = arguments[0];

            if ($(element).attr('checked')) {
                self.add.apply(self, [element]);
            } else {
                self.remove.apply(self, [element]);
            }
        },

        add: function () {
            var self = this;
            var element = arguments[0];

            self.updateCount('add1');

            var testid = $(element).attr('testid');
            $.post('/mybook.asmx/Add', { testid: testid }, function () { });
        },

        remove: function () {
            var self = this;
            var element = arguments[0];

            self.updateCount('remove1');

            var testid = $(element).attr('testid');
            $.post('/mybook.asmx/Remove', { testid: testid }, function () { });
        },

        addAll: function () {
            $.post('/mybook.asmx/AddAll', function () { });
            this.updateCount();
        },

        removeAll: function () {
            var self = this;
            var element = arguments[0];

            $.post('/mybook.asmx/RemoveAll', function () { });
            self.updateCount('removeAll');

            $('.booktest').each(function (i, e) {
                $(this).removeAttr('checked', '');
            });

            $('.toggleCheck').removeAttr('checked', '');
        },

        addAllFromArray: function () {
            var $testIds = [];
            $("input[name='mybook']:checked").each(function () { $testIds.push($(this).attr('testid')); });

            $.post('/mybook.asmx/AddTestIds', { testIds: $testIds }, function () { });
        },

        removeAllFromArray: function () {
            var $testIds = [];
            $("input[name='mybook']:not(:checked)").each(function () { $testIds.push($(this).attr('testid')); });

            $.post('/mybook.asmx/RemoveTestIds', { testIds: $testIds }, function () { });
        },

        updateCount: function (action) {
            var listItems = $('.mybook').length;
            var checkedItems = $("input[name='mybook']:checked").length;
            var uncheckedItems = $("input[name='mybook']:not(:checked)").length;
            var currentCount = parseInt($('.bookcount').html());
            var updatedCount = 0;

            switch (action) {
                case 'add1':
                    updatedCount = currentCount + 1;
                    break;
                case 'remove1':
                    updatedCount = currentCount - 1;
                    break;
                case 'removeAllFromList':
                    updatedCount = currentCount - checkedItems;
                    break;
                case 'addAllFromList':
                    updatedCount = currentCount + uncheckedItems;
                    break;
                case 'removeAll':
                    updatedCount = 0;
                    break;
                default:
                    updatedCount = currentCount;
            }

            $('.bookcount').html(updatedCount);
            this.showHideRemoveButton(updatedCount);
        },

        showHideRemoveButton: function (count) {
            if (count > 0) {
                $('#remove').show();
            } else {
                $('#remove').hide();
            }
        }
    };
};

$(document).ready(function () {
    site.tests().initialize();
});
