(function($) {
    /**
    * Checbox list class
    */
    $.checkboxList = function($el, selectText, clearText) {
        this.selectText = selectText;
        this.clearText = clearText;
        this.$checkboxes = $(':checkbox', $el);

        var self = this;

        this.$link = $('<a href="#"></a>').addClass('select-all').click(function(evt) {
            evt.preventDefault();
            evt.stopPropagation();

            if ($(this).text() == self.selectText) {
                self.$checkboxes.attr('checked', 'checked');
            } else {
                self.$checkboxes.removeAttr('checked');
            }
            self.updateLink();
            return false;
        });	

        this.$checkboxes.click(function() {
            self.updateLink();
        });

        self.updateLink();
        $el.before(this.$link);
    };
    $.checkboxList.prototype.updateLink = function() {
        var all_checked = (this.$checkboxes.size() == this.$checkboxes.filter('[checked]').size());
        if (all_checked) {
            this.$link.text(this.clearText);
        } else {
            this.$link.text(this.selectText);
        }
    };
    $.fn.checkboxList = function(selectText, clearText) {
        var selectText = selectText || 'Select all';
        var clearText = clearText || 'Clear all';

        return this.each(function() {
            new $.checkboxList($(this), selectText, clearText);
        });
    };

    /**
    * Checks the correponding radiobox when a field gets focus.
    */
    $.fn.quickSearchSelector = function() {
        $('#search-title').focus(function() {
            $(':radio', this).removeAttr('checked');
            $('#search-in-title').attr('checked', 'checked');
        });
        $('#search-in-title').click(function() {
            $('#search-title').focus();
        });
        $('#search-fulltext').focus(function() {
            $(':radio', this).removeAttr('checked');
            $('#search-in-fulltext').attr('checked', 'checked');
        });
        $('#search-in-fulltext').click(function() {
            $('#search-fulltext').focus();
        });
        $('#search-in-title').attr('checked', 'checked');
    };

    /**
    * Checks all boxes in result list
    */
    $.fn.checkAll = function() {
        var $checkboxes = $(':checkbox', $(this).parents('table:eq(0)').children('tbody'));
        var self = $(this);

        $(this).click(function(evt) {
            if (self.is(':checked')) {
                $checkboxes.attr('checked', 'checked');
                self.attr('checked', 'checked');
            } else {
                $checkboxes.removeAttr('checked');
                self.removeAttr('checked');
            }
            return true;
        });
    };

    /**
    * Met en gras les onglets dont au moins une case
    * à cocher est cochée.
    */
    emphasisTab = function(panelID) {
        var $tab_link = $('#tabs a').filter('[@href='+panelID+']');
        var $tab_checkboxes = $(':checkbox', $(panelID));
        var $tab_select_all_links = $('a.select-all', $(panelID));
        var $tab_categories_links = $('.ui-tabs-nav a', $(panelID));
        var $tab_input_texts = $(':text', $(panelID));
        var $tab_time_slot = $('select#time-slot');
        var $tab_developed_in = $('select#developed-in-in');
        var $tab_start_time_from = $('select#start-time-from');
        var $tab_start_time_to = $('select#start-time-to');

        function _areCheckboxModified() {
            var response = false;

            $tab_checkboxes.each(function() {
                if ($(this).is(':checked')) {
                    response = true;
                    return;
                }
            });

            return response;
        }

        function _areInputTextModified() {
            var response = false;

            $tab_input_texts.each(function() {
                if ($(this).val() != '') {
                    response = true;
                    return;
                }
            });

            return response;
        }

        function _isTimeSlotModified() {
            var response = ($tab_time_slot.val() != 'all');
            return response;
        }

        function _isDevelopedInModified() {
            var response = ($tab_developed_in.val() != null && $tab_developed_in.val() != '-1');
            return response;
        }

        function _areProgramTimeModified() {
            var response = false;
            response = response || ($tab_start_time_from.val() != '00:00') || ($tab_start_time_to.val() != '23:59');
            return response;
        }

        function _isTabModified() {
            var response = false;

            // #tab1 doesn't have checkboxes
            if(panelID != '#tab1') {
                response = response || _areCheckboxModified();
            }

            switch(panelID) {
                case '#tab1':
                response = response || _areInputTextModified();
                break;

                case '#tab5':
                response = response || _isDevelopedInModified();
                break;

                case '#tab2':
                response = response || _isTimeSlotModified() || _areProgramTimeModified();
                break;
            }

            return response;
        }

        function _emphasisTab() {
            var font_weight = _isTabModified() ? 'bold' : 'normal';

            $tab_link.css({fontWeight: font_weight});
        };

        function _emphasisTabCategory(category_id) {
            var category_link = $tab_categories_links.filter('[@href='+category_id+']');
            var nb_checkbox_checked = $('div'+category_id+'.ui-tabs-panel', $(panelID)).find(':checkbox[checked]').size();
            var font_weight = (nb_checkbox_checked > 0) ? 'bold' : 'normal';

            category_link.css({fontWeight: font_weight});
        };

        function _updateCurrentCategory(checkbox_clicked) {
            var current_category_id = '#' + checkbox_clicked.parents('div.ui-tabs-panel').attr('id');

            _emphasisTab();
            _emphasisTabCategory(current_category_id);
        }

        $tab_input_texts.change(function() {
            _updateCurrentCategory($(this));
        });
        $tab_time_slot.change(function() {
            _updateCurrentCategory($(this));
        });
        $tab_start_time_from.change(function() {
            _updateCurrentCategory($(this));
        });
        $tab_start_time_to.change(function() {
            _updateCurrentCategory($(this));
        });
        $tab_checkboxes.click(function() {
            _updateCurrentCategory($(this));
        });
        $tab_select_all_links.click(function() {
            _updateCurrentCategory($(this));
        });
        $tab_developed_in.change(function() {
            _updateCurrentCategory($(this));
        });

        // Set initial tab state on load
        _emphasisTab();

        jQuery.each($tab_categories_links, function(index, link) {
            var category_id = $(link).attr('href');

            _emphasisTabCategory(category_id);
        });
    };

    $(document).ready(function() {
        $('.checkbox-list').checkboxList();
        $('#quick-search').quickSearchSelector();

        if (typeof($.fn.tabs) == 'function') {
            $('#tabs').tabs();
            $('#tab4 > ul.item-list').tabs();
            $('#tab5 > ul.item-list').tabs();

            jQuery.each($('#tabs a'), function(index, tab) {
                emphasisTab($(tab).attr('href'));
            });

            $('legend').css('display', 'none');
        }

        $('#check-all').checkAll();

        // Set focus on the first text input element
        $('#content :text:first').focus();
    });

})(jQuery);

// vim: ts=4:sw=4
