angular.element(document).ready(function () {
    //angular.element(document).tooltip({ position: { my: 'left center', at: "right center" } });
    //angular.element('input').input();
    angular.bootstrap(document, ['qusion.cms']);
});

/**
 */
var app = angular.module('qusion.cms', [
    'app',
    'qusion',
    'system',
    'global',
    'localization',
    'navigation',
    'dashboard',
    'pages',
    'users',
    'roles',
    'articles',
    'images',
    'libraries',
    'documents',
    'videos',
    'classifications',
    'blogs',
    'newsletters',
    'events',
    'settings',
    'contents',
    'karta',
    'jezik',
    'facebook',
    'polls',
    'googlechart',
    'ep'
])
    .config([
        '$routeProvider',
        '$httpProvider',
        function ($routeProvider, $httpProvider) {
            $routeProvider
                .when('/', { redirectTo: '/dashboard' })
                .when('/dashboard/', { templateUrl: template('dashboard/main.html') })
                .when('/pages', { templateUrl: template('pages/main.html') })
                .when('/content', { templateUrl: template('content/main.html') })
                .when('/design', { templateUrl: template('content/design.html') })
                .when('/settings', { templateUrl: template('settings/main.html') })
                .when('/404', { templateUrl: template('error/404.html') })
                //pages
                .when('/pages', { templateUrl: template('pages/main.html') })
                .when('/pages/create/', { templateUrl: template('pages/create.html') })
                .when('/pages/create/:id', { templateUrl: template('pages/create.html') })
                .when('/pages/:id', { templateUrl: template('pages/page.html') })
                .when('/pages/:id/edit', { templateUrl: template('pages/edit.html') })
                // users
                .when('/users', { templateUrl: template('users/main.html') })
                .when('/users/create', { templateUrl: template('users/create.html') })
                .when('/users/settings', { templateUrl: template('users/settings.html') })
                .when('/users/:id', { templateUrl: template('users/main.html') })
                .when('/users/:id/edit', { templateUrl: template('users/edit.html') })
                // articles
                .when('/articles', { templateUrl: template('articles/main.html'), reloadOnSearch: false })
                .when('/articles/settings', { templateUrl: template('articles/settings.html') })
                .when('/articles/create', { templateUrl: template('articles/create.html') })
                .when('/articles/:id', { templateUrl: template('articles/article.html') })
                .when('/articles/:id/edit', { templateUrl: template('articles/edit.html') })
                //images
                .when('/images', { templateUrl: template('images/main.html') })
                .when('/eimages', { templateUrl: template('images/emain.html') })
                .when('/gimages', { templateUrl: template('images/gmain.html') })
                .when('/eimages/:libraryId', { templateUrl: template('images/emain.html') })
                .when('/images/upload/', { templateUrl: template('images/upload.html') })
                .when('/images/settings', { templateUrl: template('images/settings.html') })
                .when('/images/:libraryId', { templateUrl: template('images/main.html') })
                .when('/image/create/:id', { templateUrl: template('images/add.html') })
                .when('/imagesjson', { templateUrl: template('images/json.html') })
                //Videos
                .when('/videos', { templateUrl: template('videos/main.html') })
                .when('/videos/upload', { templateUrl: template('videos/upload.html') })
                .when('/videos/settings', { templateUrl: template('videos/settings.html') })
                .when('/videos/:libraryId', { templateUrl: template('videos/main.html') })
                .when('/video/:id', { templateUrl: template('videos/video.html') })
                //documents
                .when('/documents', { templateUrl: template('documents/main.html') })
                .when('/documents/upload/', { templateUrl: template('documents/upload.html') })
                .when('/documents/upload/:id', { templateUrl: template('documents/upload.html') })
                .when('/documents/settings', { templateUrl: template('documents/settings.html') })
                .when('/documents/:libraryId', { templateUrl: template('documents/main.html') })
                .when('/documents/download/:Id', { templateUrl: template('documents/document.html') })
                //glibrarystype
                .when('/glibrarystype', { templateUrl: template('libraries/maintype.html') })
                .when('/glibrarystype/:GlibraryTypeID', { templateUrl: template('libraries/glibrarystype.html') })
                //classifications
                .when('/classifications', { templateUrl: template('classifications/main.html'), reloadOnSearch: false })
                .when('/classifications/:alias', { templateUrl: template('classifications/classification.html'), reloadOnSearch: false })
                .when('/classifications/:id/edit/', { templateUrl: template('classifications/edit.html') })
                .when('/label/:id', { templateUrl: template('classifications/label.html'), reloadOnSearch: false })
                //blogs
                .when('/blogs', { templateUrl: template('blogs/main.html') })
                .when('/blogs/:BlogID', { templateUrl: template('blogs/blog.html') })
                .when('/post/:id/create', { templateUrl: template('blogs/postadd.html') })
                .when('/post/:id/edit', { templateUrl: template('blogs/postedit.html') })
                  //poll
                .when('/polls', { templateUrl: template('polls/main.html') })
                .when('/polls/:PollID', { templateUrl: template('polls/poll.html') })
                .when('/question/:id/create', { templateUrl: template('polls/quеstionadd.html') })
                .when('/question/:id/edit', { templateUrl: template('polls/quеstionedit.html') })
                .when('/vote/:id', { templateUrl: template('polls/vote.html') })
                .when('/fat/:QuestionID', { templateUrl: template('polls/fat.html') })          
                 //newsletter
                .when('/newsletters', { templateUrl: template('newsletters/newsletters.html') })
                .when('/newsletters/create', { templateUrl: template('newsletters/newsletteradd.html') })
                .when('/newsletters/:id/edit', { templateUrl: template('newsletters/newsletteredit.html') })
                //events
                .when('/events', { templateUrl: template('events/main.html') })
                .when('/events/create', { templateUrl: template('events/create.html') })
                .when('/events/:id', { templateUrl: template('events/event.html') })
                .when('/events/:id/edit', { templateUrl: template('events/edit.html') })
                //contents
                .when('/contents', { templateUrl: template('contents/main.html') })
                .when('/contents/create', { templateUrl: template('contents/create.html') })
                 .when('/contents/:pageID/create', { templateUrl: template('contents/create.html') })
                .when('/contents/:id/edit', { templateUrl: template('contents/edit.html') })
                //karta
                .when('/karta', { templateUrl: template('karta/karta.html') })
                .when('/karta/create', { templateUrl: template('karta/addkarta.html') })
                .when('/karta/ruta', { templateUrl: template('karta/ruta.html') })
                  //facebook
                 .when('/facebook', { templateUrl: template('facebook/index.html') })
                 .when('/facebook/:id', { templateUrl: template('facebook/index.html') })
                  .when('/oauthcallback', { templateUrl: template('facebook/oauthcallback.html') })
                //settings
                .when('/settings/:id', { templateUrl: template('settings/main.html') })
                 .when('/settings/general', { templateUrl: template('settings/general.html') })

                .otherwise({
                    redirectTo: '/404'
                });

            function template(url) {
                return '/areas/qusion/scripts/templates/' + url;
            }

            // 
            // Common system http error handler
            //
            var interceptor = [
                '$rootScope',
                '$q',
                'Config',
                'System',
                function ($rootScope, $q, Config, System) {

                    function success(response) {
                        System.Broadcast.broadcast(Config.HTTP_STATUS_KEY, Config.HTTP_REQUEST_STATUS.NONE);
                        return response;
                    }

                    function error(response) {
                        System.Broadcast.broadcast(Config.HTTP_ERROR_KEY, response.status);
                        var reject = $q.reject(response);
                        System.Broadcast.broadcast(Config.HTTP_STATUS_KEY, Config.HTTP_REQUEST_STATUS.NONE);
                        return reject;
                    }

                    return function (promise) {
                        System.Broadcast.broadcast(Config.HTTP_STATUS_KEY, Config.HTTP_REQUEST_STATUS.LOADING);
                        return promise.then(success, error);
                    };
                }];

            $httpProvider.responseInterceptors.push(interceptor);
        }]);