/**
 * 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 init = null,
        bindings = null;

    init = function () {

        // bind all events
        bindings();
    };

    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
            jQuery('#fldFilterBreastpumps').parents('li').find('ul li input[type="checkbox"]').attr('disabled', '');

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

        /**
         * @event filterbreastpumpsdisabled
         */
        MEDELA.locationfinder.handleEvent('filterbreastpumpsdisabled', function () {
            // handle checkbox
            jQuery('#fldFilterBreastpumps').parents('li').find('ul li input[type="checkbox"]').attr({
                'disabled'  : 'disabled',
                'checked' : null
            });

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

        /**
         * @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();
});


