/**
 * Created by PAVEI on 30/09/2014.
 * Updated by Ross Martin on 12/05/2014
 */

angular.module('ionicLazyLoad', []);

angular.module('ionicLazyLoad').directive(
    'lazyScroll', function ($rootScope, $timeout) {
        return {
            restrict: 'A',
            link: function ($scope, $element) {

                var scrollTimeoutId = 0;

                $scope.invoke = function () {
                    $rootScope.$broadcast('scrollEvent');
                };

                $element.bind('scroll', function () {

                    $timeout.cancel(scrollTimeoutId);

                    // wait and then invoke listeners (simulates stop event)
                    scrollTimeoutId = $timeout($scope.invoke, 0);

                });


            }
        };
    });

angular.module('ionicLazyLoad').directive(
    'imageLazySrc', function ($document, $timeout) {
        return {
            restrict: 'A',
            link: function ($scope, $element, $attributes) {

                var deregistration = $scope.$on('scrollEvent', function () {
                        //console.log('scroll');
                        if (isInView()) {
                            $element[0].src = $attributes.imageLazySrc; // set src attribute on element (it will load image)
                            deregistration();
                        }
                    }
                );

                function isInView() {
                    var clientHeight = $document[0].documentElement.clientHeight;
                    var clientWidth = $document[0].documentElement.clientWidth;
                    var imageRect = $element[0].getBoundingClientRect();
                    return  (imageRect.top >= 0 && imageRect.bottom <= clientHeight) && (imageRect.left >= 0 && imageRect.right <= clientWidth);
                }

                // bind listener
                // listenerRemover = scrollAndResizeListener.bindListener(isInView);

                // unbind event listeners if element was destroyed
                // it happens when you change view, etc
                $element.on('$destroy', function () {
                    deregistration();
                });

                // explicitly call scroll listener (because, some images are in viewport already and we haven't scrolled yet)
                $timeout(function() {
                    if (isInView()) {
                        $element[0].src = $attributes.imageLazySrc; // set src attribute on element (it will load image)
                        deregistration();
                    }
                }, 500);
            }
        };
    }
);
@@ -138,32 +138,34 @@
 'ionicLazyLoad')
+%0A%0A
 .directive(%0A    
@@ -159,21 +159,16 @@
 rective(
-%0A    
 'lazyScr
@@ -168,24 +168,56 @@
 lazyScroll',
+ %5B'$rootScope', '$timeout', %0A   
  function ($
@@ -213,17 +213,16 @@
 function
- 
 ($rootSc
@@ -802,48 +802,13 @@
  %7D;%0A
-    %7D);%0A%0Aangular.module('ionicLazyLoad')
+%7D%5D)%0A%0A
 .dir
@@ -814,21 +814,16 @@
 rective(
-%0A    
 'imageLa
@@ -829,16 +829,47 @@
 azySrc',
+ %5B'$document', '$timeout', %0A   
  functio
@@ -2636,16 +2636,12 @@
      %7D;%0A
-    %7D%0A
+%7D%5D
 );

update DI to support minification