"use strict";
/**
 * Handle all routes.
 **/
var $ = require("jquery"),
    _ = require("underscore"),
    Backbone = require("backbone"),
    Book = require("./models/Book.js"),
    couchUtils = require("./couchutils.js");

// Define the application router.
var Router = Backbone.Router.extend({
    routes: {
        "": "index",
        "login": "login",
        "main" : "main",
        "books": "books",
        "books/:id": "book",
        "addbook": "addbook"
    },

    initialize: function(options) {
        this._lastPageId = null;
        this._currentPageId = null;
        this._views = options.views;
        this._catalog = options.catalog;
    },

    /**
     * Index route (default)
     */
    index: function() {
        this._log("/ route.");
        // this._changeScreen(this._views.frontPageView);
    },

    login: function() {
        this._log("/login");
        this._changeScreen(this._views.loginPageView);
    },

    main: function() {
        this._log("/main");
        $.when(
            this._catalog.updateLibraryMetadata()
        ).always(_.bind(function() {
            this._changeScreen(this._views.mainPageView);
        }, this));
    },

    books: function() {
        this._log("/books");
        
        // Only load the spine collection *once*.
        // Since the view renders itself when the collection syncs, no need to call it here.
        $.when(
            this._catalog.loadBooksByLetter(),
            this._catalog.loadSpines()
        ).always(_.bind(function() {
            this._changeScreen(this._views.booksPageView);
        }, this));
    },

    book: function(bookId) {
        this._log("/book/" + bookId);
        this._views.bookPageView.model.clear({"silent": true});
        this._views.bookPageView.model.set("_id", bookId);
        $.when(
            this._views.bookPageView.model.fetch()
        ).always(_.bind(function() {
            this._views.bookPageView.render();
            this._changeScreen(this._views.bookPageView);
        }, this));
    },

    /**
     * Show the edit book view with no data.
     **/
    addbook: function() {
        this._log("/addbook");
        this._views.editBookPageView.model = new Book();
        this._views.editBookPageView.render();
        this._changeScreen(this._views.editBookPageView);
    },

    /**
     * Change to another view.
     */
    _changeScreen: function(view, options) {
        // console.info("Changing from", this._currentPageId, "to", view.$el.attr("id"),
        //     "last==" + this._lastPageId);
        // Check if moving to the previous page.
        var changeOptions = {};
        if (this._lastPageId === view.$el.attr("id")) {
            changeOptions.reverse = true;
            this._lastPageId = null;
        } else {
            this._lastPageId = this._currentPageId;
        }
        this._currentPageId = view.$el.attr("id");

        // $("body").pagecontainer("change", view.$el, changeOptions);
        $("div#baseContent").append(view.render().$el);
    },

    _log: function() {
        console.info("[router]", _.toArray(arguments).join(" "));
    }

});

module.exports = Router;

@@ -510,35 +510,38 @@
 %7B%0A        this._
-las
+curren
 tPageId = null;%0A
@@ -553,38 +553,36 @@
    this._current
-PageId
+View
  = null;%0A       
@@ -2430,348 +2430,236 @@
     
-// console.info(%22Changing from%22, this._currentPageId, %22to%22, view.$el.attr(%22id%22),%0A        //     %22last==%22 + this._lastPageId);%0A        // Check if moving to the previous page.%0A        var changeOptions = %7B%7D;%0A        if (this._lastPageId === view.$el.attr(%22id%22)) %7B%0A            changeOptions.reverse = true;%0A            this._lastPageId = null
+if (!_.isNull(this._currentPageId)) %7B%0A            // Replacing view - kill existing.%0A            this._log(%22Changing from %22 + this._currentPageId);%0A            this._currentView.remove();%0A            $(%22div#baseContent%22).empty()
 ;%0A  
@@ -2669,15 +2669,8 @@
    %7D
- else %7B
 %0A   
@@ -2678,61 +2678,33 @@
     
-    this._lastPageId = this._currentPageId;%0A        %7D
+this._currentView = view;
 %0A   
@@ -2751,25 +2751,24 @@
 (%22id%22);%0A
-%0A
         
 // $(%22bo
@@ -2763,85 +2763,11 @@
     
-// $(%22body%22).pagecontainer(%22change%22, view.$el, changeOptions);%0A        $(%22div
+$(%22
 #bas

Remove views before adding to #baseContent