/**
 * Description
 * @author tjunghans, Namics AG, www.namics.com
 * @class filter
 * @namespace MEDELA.locationfinder.search
 * @requires jQuery
 */

/* JSLint Stuff */
/*globals jQuery */

var MEDELA = MEDELA || {};
MEDELA.locationfinder = MEDELA.locationfinder || {};
MEDELA.locationfinder.search = MEDELA.locationfinder.search || {};

//setCurrentPackage('MEDELA.locationfinder.search');


(MEDELA.locationfinder.search.filter = function () {
    var deactivate_inputs = null,
        init = null,
        bindings = null;

    init = function () {

        // bind all events
        bindings();
    };

    /**
     * Helper method to disable and deselect radio or checkboxes
     * @method deactivate_inputs
     * @param $input
     */
    deactivate_inputs = function ($input) {
        $input.each(function () {
            this.checked = false;
            this.disabled = true;
        });
        return true;
    };

    bindings = function () {
        var eTarget = null,
            $parentLi = null,
            cssClass = 'filterDeactivated';

        /**
         *  @event handle for #medelaLocationfinderSearchFilter .input
         */
        jQuery('#medelaLocationfinderSearchFilter .input').click(function (e) {
            eTarget = e.target;
            $parentLi = jQuery(eTarget).parents('li');
            
            if (eTarget.id === 'fldFilterBreastpumps') {
                if (eTarget.checked) {
                    $parentLi.removeClass(cssClass);
                    MEDELA.locationfinder.fireEvent('filterbreastpumpsactive');
                } else {
                    $parentLi.addClass(cssClass);
                    MEDELA.locationfinder.fireEvent('filterbreastpumpsdisabled');
                }
            }

        });

        /**
         * @event filterbreastpumpsactive
         */
        MEDELA.locationfinder.handleEvent('filterbreastpumpsactive', function () {
            // handle checkbox
            var $checkbox = jQuery('#fldFilterBreastpumps').parents('li').find('ul li input[type="checkbox"]');

            $checkbox.each(function () {
                this.disabled = false;
            });
            
            $checkbox.parent('.mdla-checkbox').removeClass('mdla-checkbox-disabled');

            // handle radio
            jQuery('#fldFilterBreastpumps').parents('li').find('ul li input[type="radio"]').attr('disabled', '');
        });

        /**
         * @event filterbreastpumpsdisabled
         */
        MEDELA.locationfinder.handleEvent('filterbreastpumpsdisabled', function () {
            var $checkbox = null,
                $radio = null;

            // handle checkbox
            $checkbox = jQuery('#fldFilterBreastpumps').parents('li').find('ul li input[type="checkbox"]');
            deactivate_inputs($checkbox);
            $checkbox.parent('.mdla-checkbox').addClass('mdla-checkbox-disabled').children('a').removeClass('filter-checkbox-active');

            // handle radio
            $radio = jQuery('#fldFilterBreastpumps').parents('li').find('ul li input[type="radio"]');
            deactivate_inputs($radio);

        });

        /**
         * @event #medelaLocationfinderSearchFilter .input' change
         */
        jQuery('#medelaLocationfinderSearchFilter .input').change(function (e) {
			if (MEDELA.locationfinder.search.form.isSearchInProgress()) {
				return false;
			}
            MEDELA.locationfinder.search.form.sendFormData();
			return false;
        });
    };
    
    return {
        init : init
    };
}());

jQuery(document).ready(function () {
    MEDELA.locationfinder.search.filter.init();
});



