angular.module('n52.core.table', ['n52.core.timeseries', 'ngTable'])
        .controller('tableController', ['$scope', '$filter', 'ngTableParams', 'timeseriesService',
            function ($scope, $filter, ngTableParams, timeseriesService) {
                // http://ngmodules.org/modules/ng-table
                createValueArray = function () {
                    var array = [];
                    var map = {};
                    var count = 0;
                    angular.forEach(timeseriesService.getAllTimeseries(), function (ts) {
                        var data = timeseriesService.getData(ts.internalId);
                        if (data.values.length > 0) {
//                        var values = removeOverlappingValues(ts.getValues());
                            var values = data.values;
                            angular.forEach(values, function (pair) {
                                var time = pair[0];
                                var value = pair[1];
                                if (!map[time]) {
                                    map[time] = {
                                        time: time
                                    };
                                }
                                map[time][ts.internalId] = value;
                            });
                        }
                        count++;
                    });
                    var i = 0;
                    angular.forEach(map, function (entry, idx) {
                        array[i++] = entry;
                    });
                    return array;
                };

                createColumns = function () {
                    var columns = [];
                    columns.push({
                        phenomenon: 'Zeit', field: 'time', visible: true
                    });
                    angular.forEach(timeseriesService.getAllTimeseries(), function (ts) {
                        columns.push({
                            station: ts.parameters.feature.label,
                            phenomenon: ts.parameters.phenomenon.label + " (" + ts.uom + ")",
                            field: ts.internalId,
                            color: ts.styles.color
                        });
                    });
                    return columns;
                };

                createTable = function () {
                    $scope.tableParams = new ngTableParams({
                        page: 1,
                        count: 30,
                        sorting: {
                            time: 'asc'
                        }
                    }, {
                        total: data.length,
                        counts: [],
                        getData: function ($defer, params) {
                            var orderedData = params.sorting() ?
                                    $filter('orderBy')(data, params.orderBy()) :
                                    data;
                            $defer.resolve(orderedData.slice((params.page() - 1) * params.count(), params.page() * params.count()));
                        }
                    });
                };

                removeOverlappingValues = function (values) {
                    // remove values before start
                    var start = TimeController.getCurrentStartAsMillis();
                    var count = 0;
                    while (values[count][0] < start)
                        count++;
                    values.splice(0, count);
                    // remove values after the end
                    var idx = values.length - 1;
                    var end = TimeController.getCurrentEndAsMillis();
                    count = 0;
                    while (values[idx][0] > end) {
                        count++;
                        idx--;
                    }
                    values.splice(++idx, count);
                    return values;
                };

                $scope.loadMoreData = function () {
                    $scope.tableParams.count($scope.tableParams.count() + 10);
                    $scope.tableParams.reload();
                };

                var timeseriesChangedListener = $scope.$on('timeseriesChanged', function (evt, id) {
                    data = createValueArray();
                    $scope.columns = createColumns();
                });

                var timeseriesDataChangedListener = $scope.$on('timeseriesDataChanged', function (evt, id) {
                    data = createValueArray();
                    $scope.columns = createColumns();
                    createTable();
                });

                $scope.columns = createColumns();

                $scope.$on('$destroy', function () {
                    timeseriesChangedListener();
                    timeseriesDataChangedListener();
                });

                var data = createValueArray();
                createTable();
            }])
        .directive('whenScrollEnds', function () {
            return {
                restrict: "A",
                link: function (scope, element, attrs) {
                    findParentHeightElement = function (elem) {
                        if (elem.height() > 0) {
                            return elem;
                        } else {
                            return findParentHeightElement(elem.parent());
                        }
                    };

                    var parent = findParentHeightElement(element);
                    var visibleHeight = parent.height();
                    var threshold = 100;

                    parent.scroll(function () {
                        var scrollableHeight = parent.prop('scrollHeight');
                        var hiddenContentHeight = scrollableHeight - visibleHeight;

                        if (hiddenContentHeight - parent.scrollTop() <= threshold) {
                            // Scroll is almost at the bottom. Loading more rows
                            scope.$apply(attrs.whenScrollEnds);
                        }
                    });
                }
            };
        });
@@ -160,16 +160,30 @@
 ervice',
+ '$rootScope',
 %0A       
@@ -246,16 +246,28 @@
 sService
+, $rootScope
 ) %7B%0A    
@@ -1617,33 +1617,32 @@
              %7D;%0A
-%0A
                 
@@ -2330,33 +2330,32 @@
              %7D;%0A
-%0A
                 
@@ -3160,33 +3160,32 @@
              %7D;%0A
-%0A
                 
@@ -3953,33 +3953,32 @@
              %7D;%0A
-%0A
                 
@@ -4152,33 +4152,32 @@
              %7D;%0A
-%0A
                 
@@ -4201,33 +4201,37 @@
 ngedListener = $
-s
+rootS
 cope.$on('timese
@@ -4378,33 +4378,32 @@
             %7D);%0A
-%0A
                 
@@ -4439,17 +4439,21 @@
 ener = $
-s
+rootS
 cope.$on
@@ -4616,35 +4616,49 @@
             
-createTable
+$scope.tableParams.reload
 ();%0A        
@@ -4661,33 +4661,32 @@
             %7D);%0A
-%0A
                 
@@ -4719,17 +4719,16 @@
 umns();%0A
-%0A
         
@@ -4894,17 +4894,16 @@
     %7D);%0A
-%0A
         
@@ -5459,17 +5459,16 @@
      %7D;%0A
-%0A
         
@@ -5624,17 +5624,16 @@
  = 100;%0A
-%0A
         
@@ -5748,16 +5748,16 @@
 ight');%0A
+
         
@@ -5832,17 +5832,16 @@
 Height;%0A
-%0A
         

improve table controller