/*globals window,alert */
var $                     = require('jquery'),
    _                     = require('underscore'),
    Backbone              = require('backbone'),
    BaseController        = require('lib/base_controller'),
    AdminListView         = require('views/admin_list_view'),
    DetailView            = require('views/detail_view'),
    EditView              = require('views/edit_view'),
    FilterView            = require('views/filter_view'),
    IndexView             = require('views/index_view'),
    ListView              = require('views/list_view'),
    AboutView             = require('views/about_view'),
    Query                 = require('lib/query'),
    applicationController = new BaseController({ el: '#linksf' }),
    facilities            = require('collections/facilities').instance,
    FacilityCollection    = require('collections/facilities').FacilityCollection,
    fetchLocation         = require('cloud/lib/fetch_location');

var Router = Backbone.Router.extend({
  routes: {
    '':                   'index',
    'query?:queryString': 'query',
    'query':              'query',
    'detail/:id':         'detail',
    'edit/:id':           'edit',
    'about' :             'about',
    'filter':             'filter'
  },

  listView: null,

  listViewClass: ListView,
  initialize: function() {
    this.routesHit = 0;
    //keep count of number of routes handled by your application
    Backbone.history.on('route', function() { this.routesHit++; }, this);
  },
  back: function() {
    if(this.routesHit > 1) {
      //more than one route hit -> user did not land to current page directly
      window.history.back();
    } else {
      //otherwise go to the home page. Use replaceState if available so
      //the navigation doesn't create an extra history entry
      this.navigate('', {trigger:true, replace:true});
    }
  },
  index: function() {
    var indexView = new IndexView();
    return applicationController.render(indexView);
  },

  query: function(queryString) {
    var listViewClass = this.listViewClass,
        self          = this,
        queryParams;

    this.listView     = this.listView || new listViewClass({ collection: facilities, isSingleton: true });
    applicationController.render(this.listView);
    this.listView.showSpinner();
    window.scrollTo(0, 0);

    queryParams       = this.listView.generateQueryParams(queryString);
    queryParams.limit = 20;

    this.listView.submitQuery(queryParams).done(function(results) {
      fetchLocation().done(function(loc) {
        self.listView.options.categories      = queryParams.filter.categories || [];
        self.listView.options.currentLocation = loc;
        self.listView.hideSpinner();
        window.scrollTo(0, 0); // Scroll to top
      });
    }).fail(function() {
      console.log('submitQuery error', arguments);
      self.listView.hideSpinner();
    });
  },

  filter: function() {
    this.filterView = this.filterView || new FilterView({isSingleton: true });
    applicationController.render(this.filterView);
  },

  renderFacility: function(facility) {
    var detailView = new DetailView({ model: facility.presentJSON() });
    window.scrollTo(0, 0); // Scroll to top
    return applicationController.render(detailView);
  },

  renderEdit: function(facility) {
    var editView = new EditView({ model: facility });
    return applicationController.render(editView);
  },

  detail: function(id) {
    this._getFacility(id, function(facility) {
      this.renderFacility(facility);
    }.bind(this));
  },

  edit: function(id) {
    this._getFacility(id, function(fac) {
      this.renderEdit(fac);
    }.bind(this));
  },
  about: function() {
    this.aboutView = this.aboutView || new AboutView();
    applicationController.render(this.aboutView);
  },

  _getFacility: function(id, done) {
    var facility = facilities.get(id);

    if ( !facility ) {
      //Fetch Facility from backend if not in collection
      Query.getByID(id).then(function(facility) {
        done(facility);
      });
    } else {
      done(facility);
    }
  }
});

var instance = new Router();
module.exports = { instance: instance };

@@ -2142,20 +2142,16 @@
 istView 
-    
 = this.l
@@ -2231,118 +2231,54 @@
 %7D);%0A
+%0A
     
-applicationController.render(this.listView);%0A    this.listView.showSpinner();%0A    window.scrollTo(0, 0);%0A%0A
+fetchLocation().always(function(loc) %7B%0A  
     
@@ -2297,20 +2297,20 @@
       = 
-this
+self
 .listVie
@@ -2345,24 +2345,26 @@
 tring);%0A    
+  
 queryParams.
@@ -2376,28 +2376,30 @@
  = 20;%0A%0A    
-this
+  self
 .listView.su
@@ -2400,238 +2400,344 @@
 iew.
-submitQuery(queryParams).done(function(results) %7B%0A      fetchLocation().done(function(loc) %7B%0A        self.listView.options.categories      = queryParams.filter.categories %7C%7C %5B%5D;%0A        self.listView.options.currentLocation = loc;
+options.categories = queryParams.filter.categories %7C%7C %5B%5D;%0A      if (loc.lon && loc.lat) %7B%0A        self.listView.options.currentLocation = loc;%0A      %7D%0A      applicationController.render(self.listView);%0A      self.listView.showSpinner();%0A      window.scrollTo(0, 0);%0A%0A      self.listView.submitQuery(queryParams).done(function(results) %7B
 %0A   
@@ -2820,24 +2820,16 @@
 o top%0A  
-    %7D);%0A
     %7D).f
@@ -2841,24 +2841,26 @@
 unction() %7B%0A
+  
       consol
@@ -2896,32 +2896,34 @@
 guments);%0A      
+  
 self.listView.hi
@@ -2939,20 +2939,31 @@
 ();%0A    
+  
 %7D);%0A
+    %7D);%0A%0A
   %7D,%0A%0A  

Fix order list view is rendered; Fix categories showing for list view; Fix case where user denies location access.