'use strict';

// Declare app level module which depends on views, and components
var myApp = angular.module('myApp', ['ngRoute', 'ngMaterial']);

myApp.config(['$locationProvider', '$routeProvider',
    function($locationProvider, $routeProvider) {
        $locationProvider.hashPrefix('!');

        $routeProvider.
        when('/', {
            templateUrl: 'components/landing/landing.html',
            controller: 'landingController'
        }).
        when('/about-us', {
            templateUrl: 'components/about-us/aboutUs.html',
            controller: 'AboutUsController'
        }).
        when('/module-list', {
            templateUrl: 'components/module-list/moduleList.html',
            controller: 'ModuleListController'
        }).
        when('/auth', {
            templateUrl: 'components/module/module.html',
            controller: 'ModuleController'
        }).
        when('/phishing', {
            templateUrl: 'components/module/module.html',
            controller: 'ModuleController'
        }).
        otherwise({
            redirectTo: '/'
        });
    }
]);

myApp.controller('MainController', ['$scope',
    function($scope) {
        $scope.main = {};
        $scope.main.FetchModel = function(url, callback) {
            var data;
            var xmlhttp = new XMLHttpRequest();
            xmlhttp.onreadystatechange = function() {
                if (xmlhttp.readyState === 4 && xmlhttp.status === 200) {
                    callback(JSON.parse(xmlhttp.responseText));
                }
            };
            xmlhttp.open("GET", url, true);
            xmlhttp.send();
        };

        /* Code from: https://coderwall.com/p/ngisma/safe-apply-in-angular-js
         *
         * Used in displayPageContent() in moduleController.js to patch an
         * issue where apply should be called the first time a module loads,
         * but does not need to be called after the fact. This led to a
         * multitude of '$apply already in progress' errors being thrown,
         * and the images in the module were not changing.
         *
         * This function fixes that issue by wrapping $scope.apply() in a check
         * to ensure that apply is not already being called. Feel free to use
         * elsewhere as well!
         */
        $scope.safeApply = function(fn) {
            var phase = this.$root.$$phase;
            if (phase == '$apply' || phase == '$digest') {
                if (fn && (typeof(fn) === 'function')) {
                    fn();
                }
            } else {
                this.$apply(fn);
            }
        };
    }
]);

@@ -1143,16 +1143,25 @@
 $scope',
+ '$http',
 %0A    fun
@@ -1172,16 +1172,23 @@
 n($scope
+, $http
 ) %7B%0A    
@@ -2624,23 +2624,1042 @@
 %7D%0A        %7D;
+%0A%0A        $scope.main.trackBehavior = function(resource, behavior, callback) %7B%0A            $http.post('api/track/' + resource + '/' + behavior)%0A              .then(function successCallback(response) %7B%0A                  var total = response.data.reduce(function(soFar, entry) %7B%0A                      return soFar + (+entry.count)%0A                  %7D, 0);%0A                  console.log(total);%0A                  var resultString = '';%0A                  for (var i = 0; i %3C response.data.length; i++) %7B%0A                      var percent = Math.round(100 * response.data%5Bi%5D.count / total).toString();%0A                      resultString += percent + '%25 of people selected ' +%0A                        response.data%5Bi%5D.behavior + '. ';%0A                  %7D%0A                  callback(response.data, resultString);%0A              %7D, function errorCallback(response) %7B%0A                  console.error(response.data %7C%7C 'Error loading data');%0A                  alert('There was an error loading data.');%0A              %7D);%0A        %7D;
 %0A    %7D%0A%5D);%0A

Add track behavior scope method