﻿    if (!$(document).attr('_advertSelectorAccordionLoaded'))
    {
        $(document).attr('_advertSelectorAccordionLoaded', "yes");
        
        function registerAccordion()
        {
            $('.advertSection .handle').click(toggleAccordionSection);
            $('.advertSection .panel .option input[type=checkbox]').click(function() { getMapAdverts(); });
            $('.advertSection .panel .optionAll input[type=checkbox]').click(categorySelectionClick);
            $('.advertSection .handle .optionAll input[type=checkbox]').click(allSelectionClick);
            $('.advertSection .handle').each(toggleAccordionSection);
            
            function toggleAccordionSection(e)
            {
                var handle = $(this);
                var spinner = handle.find('.spinner');
                var panel = handle.next();
                var section = handle.parent();

                if (section.attr('_status') == "closed")
                {
                    panel.slideDown();
                    spinner.attr('src', '/images/emagin/icon_arrow_down.gif');
                    spinner.attr('alt', 'Expand');
                    section.attr('_status', 'open');
                }
                else
                {
                    panel.slideUp();
                    spinner.attr('src', '/images/emagin/icon_arrow_right.gif');
                    spinner.attr('alt', 'Collapse');
                    section.attr('_status', 'closed');
                }
            }
            function allSelectionClick(e) {
                enableAdverts = true;
            
                var text = $(e.target).parent().find('label').html();

                if (text == 'Tick all categories') {
                    $(e.target).parent().find('label').html('Untick all categories');
                    $('.advertSelector .panel .optionAll label').html('Untick all');
                    $('.advertSelector input[type=checkbox]').attr('checked', 'checked');
                }
                else {
                    $(e.target).parent().find('label').html('Tick all categories');
                    $('.advertSelector .panel .optionAll label').html('Tick all');
                    $('.advertSelector input[type=checkbox]').attr('checked', '');
                }
                getMapAdverts(searchType);
            }
            function categorySelectionClick(e) {
                var text = $(e.target).parent().find('label').html();

                if (text == 'Tick all') {
                    $(e.target).parent().find('label').html('Untick all');
                    $(e.target).parent().parent().find('input[type=checkbox]').attr('checked', 'checked');
                }
                else {
                    $(e.target).parent().find('label').html('Tick all');
                    $(e.target).parent().parent().find('input[type=checkbox]').attr('checked', '');
                }
                getMapAdverts(searchType);
            }
        }
        $(window).load(registerAccordion);
    
        function getSelectedSections()
        {
            var selected = '';

            checkboxes = $('.advertSection .panel .option input[type=checkbox]');
            checked = $('.advertSection .panel .option input[type=checkbox]:checked');

            if (checkboxes.length != checked.length) {
                for (var i = 0; i < checkboxes.length; i++) {
                    if (checkboxes.eq(i).attr('checked')) {
                        if (selected != '')
                            selected += ',';

                        selected += checkboxes.eq(i).attr('value');
                    }
                }
                return selected;
            }
            else
                return '1';
            
        }
        function resetSelectedAdverts() {
            $('.advertSelector input[type=checkbox]').attr('checked', 'checked');
            $('.advertSection .handle .optionAll label').html('Untick all categories');
            $('.advertSelector .panel .optionAll label').html('Untick all');
        }
    }
