(function () {
    'use strict';

    angular
        .module('fehlerberichtApp', [
            'ngAnimate',
            'ngCookies',
            'ngResource',
            'ngRoute',
            'ngSanitize',
            'ngTouch'
        ])
        .directive('tabs', function () {
            return {
                restrict: 'A',
                transclude: true,
                scope: { style: "@tabs" },
                controller: [ "$scope", function ($scope) {
                    $scope.panels = [];

                    $scope.select = function (i) {
                        angular.forEach($scope.panels, function (p) {
                            p.selected = false;
                        });
                        $scope.panels[i].selected = true;
                    };

                    this.addPanel = function (panel) {
                        if ($scope.panels.length === 0) {
                            panel.selected = true;
                        }
                        $scope.panels.push(panel);
                    };
                }],
                template: '<div class="tabbable">' +
                    '<ul class="nav nav-{{style||\'tabs\'}}">' +
                    '<li ng-repeat="panel in panels" ng-class="{active:panel.selected}">' +
                    '<a href="" ng-click="select($index)">{{panel.title}}</a>' +
                    '</li>' +
                    '</ul>' +
                    '<div class="tab-content" ng-transclude></div>' +
                    '</div>',
                replace: true
            };
        })
        .directive('panel', function () {
            return {
                require: '^tabs',
                restrict: 'A',
                transclude: true,
                scope: { title: "@panel" },
                link: function (scope, element, attrs, tabsCtrl) {
                    tabsCtrl.addPanel(scope);
                },
                template: '<div class="tab-panel" ng-show="selected" ng-transclude>' +
                    '</div>',
                replace: true
            };
        })


        .directive('popover', function() {
            return {
                restrict: 'EA',
                link: function(scope, element, attrs){
                    $(element).popover({
                        html : true,
                        content: function() {
                            return scope.getCommentsInfo($(element).attr('data-popover'),$(element).attr('data-kunde'),$(element).attr('data-datum'));
                        }
                    });
                    $(element).hover(function(){
                        $(element).popover('show');
                    }, function(){
                        $(element).popover('hide');
                    });
                }
            };
        })
        .config(function ($routeProvider) {
            $routeProvider
                .when('/overview/:customer/:date/:status', {
                    templateUrl: 'views/overview.html',
                    controller: 'OverviewCtrl'
                })
                .when('/report/:customer/:date/:start', {
                    templateUrl: 'views/report.html',
                    controller: 'OverviewCtrl'
                })                
                .when('/tickets', {
                    templateUrl: 'views/tickets.html',
                    controller: 'TicketsCtrl'
                })
                .when('/calendar', {
                    templateUrl: 'views/calendar.html',
                    controller: 'CalendarCtrl'
                })
                .when('/customers', {
                    templateUrl: 'views/customers.html',
                    controller: 'CustomersCtrl'
                })
                .when('/stats/:customer', {
                    templateUrl: 'views/stats.html',
                    controller: 'StatsCtrl'
                })
                .when('/config/:customer/:date', {
                    templateUrl: 'views/config.html',
                    controller: 'ConfigCtrl'
                })
                .when('/config', {
                    templateUrl: 'views/config.html',
                    controller: 'ConfigCtrl'
                })
                .when('/changelog', {
                    templateUrl: 'views/changelog.html',
                    controller: 'ChangelogCtrl'
                })
                .otherwise({
                    redirectTo: '/overview/all/today/errors'
                });
        });

    moment.locale('de', {
        calendar: {
            lastDay: "[Gestern]",
            lastWeek: "[letzten] dddd",
            nextDay: "[Morgen]",
            nextWeek: "dddd",
            sameDay: "[Heute]",
            sameElse: "L"
        }
    });
})();