'use strict';

angular.module('myApp.thoughts', ['ngRoute'])

.config(['$routeProvider', function($routeProvider) {
  $routeProvider.when('/thoughts', {
    templateUrl: 'thoughts/thoughts.html',
    controller: 'ThoughtsCtrl'
  });
}])

.controller('ThoughtsCtrl', ['$http', '$scope', '$sce', 
	function($http, $scope, $sce) {
		var getLatestPost = function() {
			$scope.post = {};
			
			$http.jsonp('http://svinegar.tumblr.com/api/read/json?callback=JSON_CALLBACK')
			.success(function(response) {
				console.log('retrieved post');
				$scope.post.title = response.posts[0]['regular-title'];
				$scope.post.body  = $sce.trustAsHtml(response.posts[0]['regular-body']);
			})
			.error(function(response) {
			});
	}

	getLatestPost();
}]);