text: ﻿'use strict';
  backendApp
  
  .controller('appController', [
    '$scope',
    '$mdSidenav',
    '$timeout',
    '$mdDialog',
    'menuService',
    '$location',
    '$rootScope',
    '$window',
    '$log',
    '$mdBottomSheet',
    'authService',
  function($scope, $mdSidenav, $timeout, $mdDialog, menuService, $location, $rootScope, $window, $log, $mdBottomSheet, authService) {
  
  $scope.logOut = function () {
          authService.logOut();
          //$location.path('/');
          $window.location="../accounts/";
      }
  
      $scope.authentication = authService.authentication;
  
  
    var self = this;
    $scope.menu = menuService;
    $scope.path = path;
    $scope.goHome = goHome;
    $scope.openMenu = openMenu;
    $scope.closeMenu = closeMenu;
    $scope.isSectionSelected = isSectionSelected;
  
  
  
    // Methods used by menuLink and menuToggle directives
    this.isOpen = isOpen;
    this.isSelected = isSelected;
    this.toggleOpen = toggleOpen;
    //this.autoFocusContent = false;
  
  
    //$rootScope.$on('$locationChangeSuccess', openPage);
  
    $rootScope.$on("$locationChangeSuccess", function(event, next, current){
     $mdSidenav("left").then(function(instance){ instance.close();});  });
  
    //$scope.focusMainContent = focusMainContent;
  /*
    //-- Define a fake model for the related page selector
    Object.defineProperty($rootScope, "relatedPage", {
      get: function () { return null; },
      set: angular.noop,
      enumerable: true,
      configurable: true
    });
    $rootScope.redirectToUrl = function (url) {
      $window.location.hash = url;
      $timeout(function () { $rootScope.relatedPage = null; }, 100);
    };
  
    
  */
  
    //var mainContentArea = document.querySelector("[role='main']");
  
    // *********************
    // Internal methods
    // *********************
  
    function closeMenu() {
      $timeout(function() { $mdSidenav('left').close(); });
    }
  
    function openMenu() {
      $timeout(function() { $mdSidenav('left').open(); });
    }
  
    function path() {
      return $location.path();
    }
  
    function goHome($event) {
      menuService.selectPage(null, null);
      $location.path( '/' );
    }
  
    function openPage() {
      $scope.closeMenu();
  /*
      if (self.autoFocusContent) {
        focusMainContent();
        self.autoFocusContent = false;
      }*/
    }
  /*
    function focusMainContent($event) {
      // prevent skip link from redirecting
      if ($event) { $event.preventDefault(); }
  
      $timeout(function(){
        mainContentArea.focus();
      },90);
  
    }*/
  
    function isSelected(page) {
      return menuService.isPageSelected(page);
    }
  
    function isSectionSelected(section) {
      var selected = false;
      var openedSection = menuService.openedSection;
      if(openedSection === section){
        selected = true;
      }
      else if(section.children) {
        section.children.forEach(function(childSection) {
          if(childSection === openedSection){
            selected = true;
          }
        });
      }
      return selected;
    }
  
    function isOpen(section) {
      return menuService.isSectionSelected(section);
    }
  
    function toggleOpen(section) {
      menuService.toggleSelectSection(section);
    }
  
  
    $scope.alert = '';
    $scope.showListBottomSheet = function($event) {
      $scope.alert = '';
      $mdBottomSheet.show({
        template: '<md-bottom-sheet class="md-list md-has-header"> <md-subheader>Settings</md-subheader> <md-list> <md-item ng-repeat="item in items"><md-item-content md-ink-ripple flex class="inset"> <a flex aria-label="{{item.name}}" ng-click="listItemClick($index)"> <span class="md-inline-list-icon-label">{{ item.name }}</span> </a></md-item-content> </md-item> </md-list></md-bottom-sheet>',
        controller: 'ListBottomSheetCtrl',
        targetEvent: $event
      }).then(function(clickedItem) {
        $scope.alert = clickedItem.name + ' clicked!';
      });
    };
  
  
  
    $scope.showAdd = function(ev) {
      $mdDialog.show({
        controller: DialogController,
        template: '<md-dialog aria-label="Mango (Fruit)"> <md-content class="md-padding"> <form name="userForm"> <div layout layout-sm="column"> <md-input-container flex> <label>First Name</label> <input ng-model="user.firstName" placeholder="Placeholder text"> </md-input-container> <md-input-container flex> <label>Last Name</label> <input ng-model="theMax"> </md-input-container> </div> <md-input-container flex> <label>Address</label> <input ng-model="user.address"> </md-input-container> <div layout layout-sm="column"> <md-input-container flex> <label>City</label> <input ng-model="user.city"> </md-input-container> <md-input-container flex> <label>State</label> <input ng-model="user.state"> </md-input-container> <md-input-container flex> <label>Postal Code</label> <input ng-model="user.postalCode"> </md-input-container> </div> <md-input-container flex> <label>Biography</label> <textarea ng-model="user.biography" columns="1" md-maxlength="150"></textarea> </md-input-container> </form> </md-content> <div class="md-actions" layout="row"> <span flex></span> <md-button ng-click="answer(\'not useful\')"> Cancel </md-button> <md-button ng-click="answer(\'useful\')" class="md-primary"> Save </md-button> </div></md-dialog>',
        targetEvent: ev,
      })
      .then(function(answer) {
        $scope.alert = 'You said the information was "' + answer + '".';
      }, function() {
        $scope.alert = 'You cancelled the dialog.';
      });
    };
  
  
  }]);
  
  
  
  backendApp
  
  .controller('ListBottomSheetCtrl', function($scope, $mdBottomSheet) {
    $scope.items = [
      { name: 'Share', icon: 'share' },
      { name: 'Upload', icon: 'upload' },
      { name: 'Copy', icon: 'copy' },
      { name: 'Print this page', icon: 'print' },
    ];
    
    $scope.listItemClick = function($index) {
      var clickedItem = $scope.items[$index];
      $mdBottomSheet.hide(clickedItem);
    };
  });
  
  
  function DialogController($scope, $mdDialog) {
    $scope.hide = function() {
      $mdDialog.hide();
    };
    $scope.cancel = function() {
      $mdDialog.cancel();
    };
    $scope.answer = function(answer) {
      $mdDialog.hide(answer);
    };
  };
  