'use strict';

angular
  .module('noCAPTCHA', [])
  .service('googleGrecaptcha', [
    '$q',
    '$window',
    '$document',
    '$rootScope',
    function googleGrecaptchaService($q, $window, $document, $rootScope){
      var deferred = $q.defer();

      $window.recaptchaOnloadCallback = function (){
        $rootScope.$apply(function (){
          deferred.resolve();
        });
      };

      var s = $document[0].createElement('script');
      s.src = 'https://www.google.com/recaptcha/api.js?onload=recaptchaOnloadCallback&render=explicit';
      $document[0].body.appendChild(s);

      return deferred.promise;
    }])
  .provider('noCAPTCHA', function noCaptchaProvider(){
    var siteKey,
      theme;

    this.setSiteKey = function (_siteKey){
      siteKey = _siteKey;
    };

    this.setTheme = function (_theme){
      theme = _theme;
    };

    this.$get = [function noCaptchaFactory(){
      return {
        theme: theme,
        siteKey: siteKey
      }
    }];
  })
  .directive('noCaptcha', [
    'noCAPTCHA',
    'googleGrecaptcha',
    '$document',
    function (noCaptcha, googleGrecaptcha, $document){
      /**
       * Removes all .pls-container elements
       *
       * This is a dirty workaround for fixing reCAPTCHAs bug
       * which leaves obsolete elements that causes errors
       * when closing dialogues or changing pages
       *
       * This will be removed as soon as Google fixes the bug
       *
       * Discussion: https://groups.google.com/forum/#!topic/recaptcha/miaW9qdVIgA
       */
      var removePLSContainers = function(){
        // remove pls-containers
        var plsContainers = $document[0].getElementsByClassName('pls-container');
        for(var i = 0; i < plsContainers.length; i++){
          var parent = plsContainers[i].parentNode;
          while (parent.firstChild) {
            parent.removeChild(parent.firstChild);
          }
        }
      };

      return {
        restrict: 'EA',
        scope: {
          gRecaptchaResponse: '=',
          siteKey: '@',
          theme: '@',
          control: '=?',
          expiredCallback: '=?'
        },
        replace: true,
        link: function (scope, element){
          scope.control = scope.control || {};

          var widgetId;
          var grecaptchaCreateParameters;
          var control = scope.control;

          grecaptchaCreateParameters = {
            sitekey: scope.siteKey || noCaptcha.siteKey,
            theme: scope.theme || noCaptcha.theme,
            callback: function (r){
              scope.$apply(function (){
                scope.gRecaptchaResponse = r;
              });
            },
            'expired-callback': function (){
              if(scope.expiredCallback && typeof (scope.expiredCallback) === 'function') {
                scope.$apply(function (){
                  scope.expiredCallback();
                });
              }
            }
          };

          if(!grecaptchaCreateParameters.sitekey) {
            throw new Error('Site Key is required');
          }

          googleGrecaptcha.then(function (){
            widgetId = grecaptcha.render(
              element[0],
              grecaptchaCreateParameters
            );
            control.reset = function (){
              grecaptcha.reset(widgetId);
              scope.gRecaptchaResponse = null;
            };
          });

          scope.$on('$destroy', function (){
            if(grecaptcha) {
              grecaptcha.reset(widgetId);
            }
            removePLSContainers();
          });
        }
      };
    }]);

@@ -1067,24 +1067,39 @@
 $document',%0A
+    '$window',%0A
     function
@@ -1134,24 +1134,33 @@
 a, $document
+, $window
 )%7B%0A      /**
@@ -3143,16 +3143,24 @@
 getId = 
+$window.
 grecaptc
@@ -3170,16 +3170,16 @@
 render(%0A
-
         
@@ -3299,32 +3299,40 @@
 %7B%0A              
+$window.
 grecaptcha.reset
@@ -3480,16 +3480,24 @@
      if(
+$window.
 grecaptc
@@ -3508,32 +3508,40 @@
 %7B%0A              
+$window.
 grecaptcha.reset

Use grecaptcha from window