'use strict';

angular.module('qusion.ui.chosen', [
])
    .directive('qnChosen', function () {
        return {
            restrict: 'A',
            require: 'ngModel',
            link: function (scope, elem, attr) {
                //set up the plugin.
                //elem.chosen();
                var options = scope.$eval(attr.qnChosen);
                var source = qusion.parseSource(attr.ngOptions);
                
                var model = attr.ngModel;
                //watch the options source and refresh the plugin.
                scope.$watch(source, function (value) {
                    
                    elem.chosen(options);
                    elem.trigger("liszt:updated");
                });
                
                scope.$watch(model, function (value) {
                    elem.chosen(options);
                    elem.trigger("liszt:updated");
                });
            }
        };
    });