﻿if (typeof GMI.AccordionSelect === 'undefined') {
    GMI.AccordionSelect = {};
}

GMI.AccordionSelect.RemoveAttr = function(evt) {
    $this = $(this);
    var checkId = $this.parent().attr('id').replace('LABEL', '');
    $('input#' + checkId).attr('checked', false);
    $this.parent().remove();
    return false;
}

GMI.AccordionSelect.ToggleSelection = function() {
    var $this = $(this);
    
    if ($this.attr('checked')) {
        var facetText = $('table label[for=' + $this.attr('id') + ']').first().text();
        var removeLink = $('<a href="#" class="btn-remove">( remove )</a>');
        removeLink.click(GMI.AccordionSelect.RemoveAttr);
        var facetContainer = $('<div class="toolbox-header-output" id="LABEL' + $this.attr('id') + '">' + facetText + '</div>');
        facetContainer.append(removeLink);
        $this.closest('div').parent('div').prev().append(facetContainer);
    } else {
        $('#LABEL' + $this.attr('id')).remove();
    }
}

$(document).ready(function() {
    $('div.toolbox-pane table input[type=checkbox]').click(GMI.AccordionSelect.ToggleSelection);

    //hook up the remove link clicks
    $('.btn-remove').click(GMI.AccordionSelect.RemoveAttr);
});
