'use strict';


pptwitter.config(['$routeProvider', function($routeProvider) {
    console.log('Setting up routes...');
    $routeProvider.
        when('/', {redirectTo: '/home'}).
        when('/home', {
            templateUrl: 'static/partials/home.html',
            controller: 'HomeCtrl'
        }).
        when('/poetic/:tweet', {
            templateUrl: 'static/partials/poetic.html',
            controller: 'PoeticCtrl',
            resolve: {
                tweets: pptwitter.apiResolver('tweet/', {ordering: '-created_at'}),
                tweet: pptwitter.apiResolver(function(params) {
                    return 'tweet/' + params.tweet;
                })
            }
        }).
        otherwise({
            redirectTo: '/'
        });
}]);


pptwitter.controller('AppCtrl', function($rootScope, $scope, $timeout, $location, Data) {
    console.log('Loading AppCtrl...');
    $scope.Data = Data;
    $(window).resize(function() {
        $scope.navCollapsed = $(window).width() < 768;
        $scope.$apply();
    });

    $rootScope.$on('$routeChangeStart', function(event, next, current) {
        Data.message = null;
        Data.loading = true;
        $scope['class'] = {};
        if (next.$$route) {
            $scope['class'][next.$$route.controller] = 'active';
        }
    });

    $rootScope.$on('$routeChangeSuccess', function(event, current, previous) {
        $scope.navCollapsed = true;
        Data.loading = false;
        window.scrollTo(0, 0);
    });

    $rootScope.$on('$routeChangeError', function(event, current, previous, rejection) {
        console.log(rejection);
        Data.loading = false;
        Data.flash(rejection);
    });
});


pptwitter.controller('HomeCtrl', function($scope, Data) {
    console.log('Loading HomeCtrl...');
    $scope.Data = Data;
    window.location = 'http://www.powerpoetry.org';
});


pptwitter.controller('PoeticCtrl', function($http, $route, $scope, $timeout, tweets, tweet, Data) {
    console.log('Loading PoeticCtrl...');
    $scope.Data = Data;
    $scope.tweet = tweet;
    $scope.tweets = tweets.objects;

    var score = tweet.score;
    $scope.averageScore = (score.poetic + score.sentiment + score.language) / 3.0;

    $timeout(function() {
        console.log('Fetching more tweets');
        var url = '/api/tweet?ordering=-created_at';
        if ($scope.tweets.length) {
            url += '&id__gt=' + $scope.tweets[0].id;
        }

        $http({
            url: url,
            method: 'GET'
        }).success(function(data, status, headers, config) {
            console.log('Received ' + data.objects.length + ' new tweets.');
            data.objects.reverse();
            angular.forEach(data.objects, function(t) {
                $scope.tweets.unshift(t);
            });
        }).error(function(data, status, headers, config) {
            console.log('Error fetching tweets.');
        });
    }, 10000);
});

@@ -170,138 +170,17 @@
 : '/
-home'%7D).%0A        when('/home', %7B%0A            templateUrl: 'static/partials/home.html',%0A            controller: 'HomeCtrl'%0A        
+poetic/1'
 %7D).%0A
@@ -1574,188 +1574,8 @@
 ;%0A%0A%0A
-pptwitter.controller('HomeCtrl', function($scope, Data) %7B%0A    console.log('Loading HomeCtrl...');%0A    $scope.Data = Data;%0A    window.location = 'http://www.powerpoetry.org';%0A%7D);%0A%0A%0A
 pptw

Change behavior on index page.