function dialogController($scope, dialog, data)
{
  $scope.data = data;

  $scope.close = function(){
    dialog.close($scope.data);
  };
}

function authController($scope, Restangular, $dialog, $timeout)
{
  $scope.username = '';
  $scope.password = '';
  $scope.authenticated = false;

  var loginTemplate = '\
      <div class="modal-header">\
        <h2>Login</h2>\
      </div>\
      <div class="modal-body">\
        <form class="form-horizontal">\
        <div class="control-group">\
          <label class="control-label" for="username">User Name</label>\
          <div class="controls">\
            <input type="text" id="username" placeholder="User Name" data-ng-model="data.username">\
          </div>\
        </div>\
        <div class="control-group">\
          <label class="control-label" for="password">Password</label>\
          <div class="controls">\
            <input type="password" id="password" placeholder="Password" data-ng-model="data.password">\
          </div>\
        </div>\
        </form>\
      </div>\
      <div class="modal-footer">\
        <div class="control-group">\
          <div class="controls">\
            <button type="submit" class="btn" data-ng-click="close(data)" data-ng-disabled="data.username == \'\' || data.password == \'\'">Sign in</button>\
          </div>\
        </div>\
      </div>\
  ';

  $scope.login = function(){
    $dialog.dialog({
      template: loginTemplate,
      controller: 'dialogController',
      resolve: {
        data: function(){
          return {
            username: '',
            password: ''
          };
        }
      },
      keyboard: false,
      backdropClick: false,
      backdrop: true
    })
    .open()
    .then(function(data){
      Restangular
        .all('acreadmins')
        .customPOST('login', {}, {'Content-Type':'application/json'}, {username: data.username, password: data.password})
        .then(function(){
          // login passed
          $scope.on_login();
        }, function(error){
          // login failed, re-present login dialog
          $scope.login();
        });
    });
  };

  $scope.logout = function(){
    Restangular
      .all('acreadmins')
      .customPOST('logout', {}, {'Content-Type':'application/json'}, {})
      .then(function(){
        $scope.authenticated = false;
        $scope.login();
      });
  };

  $scope.on_login = function(){
    $scope.authenticated = true;
  };

  $scope.$on('auth-session-expired', function(event){
   $scope.login();
  });

  $scope.$on('$routeChangeSuccess', function(event, current, previous){
    function check()
    {
      Restangular
        .all('acreadmins')
        .customGET('test', {})
        .then(function(){
          // already logged in
          $scope.on_login();
        }, function(error){
          //no login, so prompt
          $scope.login();
        });
    }

    $timeout(check, 300);
  });
}

function alertController($scope)
{
  $scope.alerts = [];

  $scope.close = function(index) {
    $scope.alerts.splice(index, 1);
  };

  $scope.$on('post-alert', function(event, type, msg){
	 $scope.alerts.push({type: type, msg: msg});
  });
}

function navigationController($scope)
{
  $scope.show = false; //whether or not to show nav bar

  $scope.tabs = [
    <%
    for (var i in models)
    {
      var model = models[i];
    %>
      {title: inflect.pluralize('<%=model.name%>'), active: false, model: '<%=model.collection%>', href: '#/<%=model.collection%>'}<%if (i < models.length - 1){%>,<%}%>
    <%
    }
    %>
  ];

  $scope.crumbs = [
  ];

  $scope.$on('$routeChangeSuccess', function (event, current, previous) {
    var i;

    for (i in $scope.tabs)
    {
      $scope.tabs[i].active = false;
    }
    $scope.crumbs.splice(0, $scope.crumbs.length);

    switch (current.loadedTemplateUrl)
    {
      <%
      for(i in models)
      {
        var model = models[i];
      %>
        case '<%=model.views.create%>':
        case '<%=model.views.retrieve%>':
        case '<%=model.views.update%>':
          $scope.show = true;

          for (i in $scope.tabs)
          {
            if ($scope.tabs[i].model === '<%=model.collection%>')
            {
              $scope.tabs[i].active = true;
            }
          }

          if (current.loadedTemplateUrl === '<%=model.views.update%>')
          {
            $scope.crumbs.push({active: false, href: '#/<%=model.collection%>', title: 'All ' + inflect.capitalize('<%=model.collection%>')});
            $scope.crumbs.push({active: true, href: '#', title: current.params.id});
          }
          else if (current.loadedTemplateUrl === '<%=model.views.create%>')
          {
            $scope.crumbs.push({active: false, href: '#', title: 'All ' + inflect.capitalize('<%=model.collection%>')});
          }
          else
          {
            $scope.crumbs.push({active: true, href: '#', title: 'All ' + inflect.capitalize('<%=model.collection%>')});
          }
          break;
      <%
      }
      %>

        default:
          $scope.show = false;
          break;
    }
  });
}