var app = app || {};

  adminRouter = Backbone.Router.extend({
    routes: {
        '' : 'index',
        'gallery/:gid' : 'editGallery',
    },

    initialize: function () {

    },

    index: function() {
      if(app.imagesView !== undefined) {
        app.imagesView.remove();
      }
      $('#page-title').text('Galleries');
      $('#page-title').removeClass('caps');
      $('#image-container-controls').hide();
      app.galleriesView = new app.GalleriesView();
      $('#admin-body').html(app.galleriesView.$el);
    },

    editGallery: function(gid) {
      if(app.galleriesView !== undefined) {
        app.galleriesView.remove();
      }

      $('#image-container-controls').show();
      this.galleries = new app.Galleries();
      this.galleries.fetch({
        success: function (collection, response, options) {
          gm = collection.get(gid);
          $('#page-title').text(gm.get('name'));
          $('#page-title').addClass('caps');
        }
      });
      if(app.galleryView !== undefined) {
        
        app.galleryView.undelegateEvents();
      }
      app.imagesView = new app.ImagesView();
      app.imagesView.setGID(gid);
      $('#admin-body').html(app.imagesView.$el);
    },

    clearView: function() {
      if(app.galleryView !== undefined) {
        app.galleryView.undelegateEvents();
      }
      $('#gallery-container').html('');
      $('#controls').html('');
      $('#image-container .image-entry-wrapper').remove();
    }

  });

var tryLogin = function () {
  var user = $('input#login-username').val();
  var pass = $('input#login-password').val();
  var loginData = {
    user: user,
    pass: pass
  };

  $.ajax({
    url: '/api/login',
    type: 'POST',
    dataType: 'json',
    data: JSON.stringify(loginData),
    success: function() {
      $('#login-modal').modal('hide');
      var r = new adminRouter();
      Backbone.history.start({pushState: false, root: '/admin'});
    },
    error: function() {
      $('.incorrect-login-msg').css('visibility', 'visible').css('opacity', 1);
      $('#login-password').val('');
      setTimeout(function() {
        $('.incorrect-login-msg').fadeTo(400, 0, function() {
         $('.incorrect-login-msg').css('visibility', 'hidden');
        });
      }, 1500);
    }
  });
};

var setupLogin = function () {
  // TODO: why below doesn't work?
  if($('#login-username').val() === '')
    setTimeout(function(){$('#login-username').focus();}, 10);
  else
    setTimeout(function(){$('#login-password').focus();}, 10);
  // end todo
  $('.btn-login').click(tryLogin);
  $('input#login-username').keyup(function (ev) {
    if(ev.keyCode === 13)
      tryLogin();
  });
  $('input#login-password').keyup(function (ev) {
    if(ev.keyCode === 13)
      tryLogin();
  });
};

$(document).ready(function(){

  // check to see if we're logged in already
  $.ajax({
    url: '/api/login',
    type: 'POST',
    dataType: 'json',
    success: function () {
      var r = new adminRouter();
      Backbone.history.start({pushState: false, root: '/admin'});  
    },
    error: function() {
      setupLogin();
      $('#login-modal').modal('show');
    }
  });
});
