/*jshint browser: true */
/*global angular */

(function () {
    'use strict';
    angular.module('sky.check', [])
        .directive('bbCheck', ['$templateCache', '$compile', function ($templateCache, $compile) {
            function createEl(name, scope) {
                var templateHtml;
                
                templateHtml = $templateCache.get('sky/templates/check/' + name + '.html');
                if (scope) {
                    templateHtml = $compile(templateHtml)(scope);
                }
                return angular.element(templateHtml);
            }

            return {
                require: ['ngModel', '?ngValue'],
                link: function (scope, el, attr, ctrls) {
                    var labelEl = el.parent('label'),
                        styledEl,
                        typeClass,
                        ngModel = ctrls[0],
                        ngValue = ctrls[1];

                    if (labelEl.length < 1) {
                        el.wrap(createEl('wrapper'));
                    } else {
                        labelEl.addClass('bb-check-wrapper');

                        /*  wrap non-whitespace label text to set
                            vertical-align middle properly */ 
                        labelEl.contents()
                        .filter(function () {
                            return this.nodeType === 3 && /\S/.test(this.textContent);
                        })
                        .wrap(createEl('labeltext'));

                    }
                    
                    scope.checkModel = ngModel;
                    
                    if (attr.type === 'radio') {

                        scope.$watch(function () {
                            return scope.$eval(attr.ngValue);
                        }, function (newValue) {
                           scope.checkValue = newValue; 
                        });

                        typeClass = 'bb-check-radio';
                    } else {
                        typeClass = 'bb-check-checkbox';
                    }
                    
                    styledEl = createEl(('styled' + attr.type), scope);
                    
                    styledEl.addClass(typeClass);

                    el.after(styledEl);
                }
            };
        }]);
}());

@@ -624,17 +624,16 @@
 equire: 
-%5B
 'ngModel
@@ -638,21 +638,8 @@
 el',
- '?ngValue'%5D,
 %0A   
@@ -684,21 +684,23 @@
 , attr, 
-ctrls
+ngModel
 ) %7B%0A    
@@ -783,16 +783,16 @@
 yledEl,%0A
+
         
@@ -820,96 +820,8 @@
 lass
-,%0A                        ngModel = ctrls%5B0%5D,%0A                        ngValue = ctrls%5B1%5D
 ;%0A%0A 

remove unused ngValue