'use strict';

/**
 * @ngdoc service
 * @name $anchorScroll
 * @kind function
 * @requires $window
 * @requires $location
 * @requires $rootScope
 *
 * @description
 * When called, it checks current value of `$location.hash()` and scrolls to the related element,
 * according to rules specified in
 * [Html5 spec](http://dev.w3.org/html5/spec/Overview.html#the-indicated-part-of-the-document).
 *
 * It also watches the `$location.hash()` and scrolls whenever it changes to match any anchor.
 * This can be disabled by calling `$anchorScrollProvider.disableAutoScrolling()`.
 *
 * @example
   <example module="anchorScrollExample">
     <file name="index.html">
       <div id="scrollArea" ng-controller="ScrollController">
         <a ng-click="gotoBottom()">Go to bottom</a>
         <a id="bottom"></a> You're at the bottom!
       </div>
     </file>
     <file name="script.js">
       angular.module('anchorScrollExample', [])
         .controller('ScrollController', ['$scope', '$location', '$anchorScroll',
           function ($scope, $location, $anchorScroll) {
             $scope.gotoBottom = function() {
               // set the location.hash to the id of
               // the element you wish to scroll to.
               $location.hash('bottom');

               // call $anchorScroll()
               $anchorScroll();
             };
           }]);
     </file>
     <file name="style.css">
       #scrollArea {
         height: 350px;
         overflow: auto;
       }

       #bottom {
         display: block;
         margin-top: 2000px;
       }
     </file>
   </example>
 */
function $AnchorScrollProvider() {

  var autoScrollingEnabled = true;

  this.disableAutoScrolling = function() {
    autoScrollingEnabled = false;
  };

  this.$get = ['$window', '$location', '$rootScope', function($window, $location, $rootScope) {
    var document = $window.document;

    // helper function to get first anchor from a NodeList
    // can't use filter.filter, as it accepts only instances of Array
    // and IE can't convert NodeList to an array using [].slice
    // TODO(vojta): use filter if we change it to accept lists as well
    function getFirstAnchor(list) {
      var result = null;
      forEach(list, function(element) {
        if (!result && nodeName_(element) === 'a') result = element;
      });
      return result;
    }

    function scroll() {
      var hash = $location.hash(), elm;

      // empty hash, scroll to the top of the page
      if (!hash) $window.scrollTo(0, 0);

      // element with given id
      else if ((elm = document.getElementById(hash))) elm.scrollIntoView();

      // first anchor with given name :-D
      else if ((elm = getFirstAnchor(document.getElementsByName(hash)))) elm.scrollIntoView();

      // no element and hash == 'top', scroll to the top of the page
      else if (hash === 'top') $window.scrollTo(0, 0);
    }

    // does not scroll when user clicks on anchor link that is currently on
    // (no url change, no $location.hash() change), browser native does scroll
    if (autoScrollingEnabled) {
      $rootScope.$watch(function autoScrollWatch() {return $location.hash();},
        function autoScrollWatchAction(newVal, oldVal) {
          // skip the initial scroll if $location.hash is empty
          if (newVal === oldVal && newVal === '') return;

          $rootScope.$evalAsync(scroll);
        });
    }

    return scroll;
  }];
}


@@ -1891,17 +1891,17 @@
 %0A    // 
-h
+H
 elper fu
@@ -1954,205 +1954,135 @@
  // 
-can't use filter.filter, as it accepts only instances of Array%0A    // and IE can't convert NodeList to an array using %5B%5D.slice%0A    // TODO(vojta): use filter if we cha
+(using %60Array#some()%60 instead of %60angular#forEach()%60 since it's more performant%0A    //  and worki
 ng
-e
  i
-t to accept lists as well
+n all supported browsers.)
 %0A   
@@ -2145,23 +2145,41 @@
 ;%0A      
-forEach
+Array.prototype.some.call
 (list, f
@@ -2213,19 +2213,8 @@
 if (
-!result && 
 node
@@ -2236,16 +2236,28 @@
 === 'a')
+ %7B%0A         
  result 
@@ -2267,16 +2267,49 @@
 lement;%0A
+          return true;%0A        %7D%0A
       %7D)

use Array.some for better performance