var format = require('util').format,
  highlight = require('em-highlight');

var oldController = function($scope, $timeout, Post, $log, growl, $q, $http, $location, $analytics, query, community) {

  $scope.community = community;

  $scope.postType = "all";
  $scope.postSort = "top";

  $scope.start = 0;
  $scope.limit = 12;

  $scope.posts = [];
  $scope.searchQuery = query;

  // Initially Disabled Infinite Scroll
  $scope.disableInfiniteScroll = true;

  $scope.$watch("postType", function(newFilter) {
    $scope.start = 0;
    $scope.posts = [];
    queryFn();
  });

  $scope.$watch("postSort", function(newFilter) {
    $scope.start = 0;
    $scope.posts = [];
    queryFn();
  });

  var cancelerStack = [];

  $scope.queryTimeout = _.throttle(function() {
    $scope.start = 0;
    $scope.posts = [];
    queryFn();
  }, 750, {leading: false});

  $scope.queryNow = function() {
    queryFn();
  }

  $scope.removePost = function(postToRemove) {
    growl.addSuccessMessage("Seed has been removed: " + postToRemove.name, {ttl: 5000});
    $scope.posts.splice($scope.posts.indexOf(postToRemove), 1);
  }

  var queryFn = function() {
    // Cancel any outstanding queries
    _.each(cancelerStack, function(canceler) { canceler.resolve() });
    cancelerStack = [];

    // Push a new cancelPromise onto stack.
    var newCanceler = $q.defer();
    cancelerStack.push(newCanceler);

    // Disable infinite scroll until the query completes
    $scope.disableInfiniteScroll = true;
    $scope.searching = true;

    $http.get('/noo/community/' + community.id + "/seeds", {
      params: {
        q: $scope.searchQuery,
        postType: $scope.postType,
        sort: $scope.postSort,
        start: $scope.start,
        limit: $scope.limit
      },
      timeout: newCanceler.promise,
      responseType: 'json'
    }).success(function(posts) {
      if ($scope.searchQuery) {
        $analytics.eventTrack('Search: Filter', {query: $scope.searchQuery, community_id: community.slug})
      }

      var firstLoad = $scope.posts.length < $scope.limit;

      if (!firstLoad) {
        $analytics.eventTrack('Posts: Load more in Search', {community_id: community.slug});
      }
      else {
        $analytics.eventTrack('Search: Load Search Page', {community_id: community.slug});
      }

      // Push posts to stack
      angular.forEach(posts, function(post, key) {
        if (!_.findWhere($scope.posts, {id: post.id})) {
          $scope.posts.push(post);
          $scope.start++;
        }
      });

      $scope.searching = false;
      $scope.noResults = $scope.posts.length == 0;

      if (posts.length == 0) { // There were no more posts... disable infinite scroll now
        $scope.disableInfiniteScroll = true;
      } else {
        $scope.disableInfiniteScroll = false;
      }
    });
  };

  $scope.queryNow();

};

var controller = function($scope, $history, searchCommunity, query, Search, growl) {
  $scope.query = query || '';
  $scope.searching = true;
  $scope.seeds = [];
  $scope.people = [];
  var community = $scope.community = searchCommunity;
  var communityId = (community ? community.id : null);

  var fetch = _.debounce(function(query) {
    if (query.length <= 2) return;

    var newUrl = format('%s?q=%s&c=',
      location.pathname, query, (communityId ? communityId : ''));
    window.history.replaceState({}, 'Hylo', newUrl);

    $scope.searching = true;
    Search.get({
      q: query,
      communityId: communityId,
      include: ['seeds', 'people'],
      limit: 5
    }, function(results) {
      $scope.seeds = results.seeds;
      $scope.people = results.people;
      $scope.searching = false;
    })
  }, 350);

  $scope.$watch('query', fetch);

  $scope.close = function() {
    if ($history.isEmpty()) {
      $scope.$state.go('home');
    } else {
      $history.go(-1);
    }
  };

  $scope.removeSeed = function(seed) {
    growl.addSuccessMessage("That seed has been removed.", {ttl: 5000});
    $scope.seeds.splice($scope.seeds.indexOf(seed), 1);
  };

  $scope.name = function(person) {
    return highlight.find(person.name, $scope.query);
  };

  $scope.blurb = function(person) {
    var query = $scope.query, pattern = new RegExp(query, 'i');

    if (person.bio && person.bio.match(pattern)) {
      return highlight.find(person.bio, query);

    } else if (_.any(person.skills, function(skill) { return !!skill.match(pattern); })) {
      return 'Skills: ' + _.map(person.skills, function(skill) {
        return highlight.find(skill, query);
      }).join(', ');

    } else if (_.any(person.organizations, function(org) { return !!org.match(pattern); })) {
      return 'Affiliations: ' + _.map(person.organizations, function(org) {
        return highlight.find(org, query);
      }).join(', ');
    }
    return person.bio || format('Skills: %s<br/>Affiliations: %s',
      person.skills.join(', '), person.organizations.join(', '));
  };
};

module.exports = function(angularModule) {
  angularModule.controller('SearchCtrl', controller);
};
@@ -74,2782 +74,8 @@
 );%0A%0A
-var oldController = function($scope, $timeout, Post, $log, growl, $q, $http, $location, $analytics, query, community) %7B%0A%0A  $scope.community = community;%0A%0A  $scope.postType = %22all%22;%0A  $scope.postSort = %22top%22;%0A%0A  $scope.start = 0;%0A  $scope.limit = 12;%0A%0A  $scope.posts = %5B%5D;%0A  $scope.searchQuery = query;%0A%0A  // Initially Disabled Infinite Scroll%0A  $scope.disableInfiniteScroll = true;%0A%0A  $scope.$watch(%22postType%22, function(newFilter) %7B%0A    $scope.start = 0;%0A    $scope.posts = %5B%5D;%0A    queryFn();%0A  %7D);%0A%0A  $scope.$watch(%22postSort%22, function(newFilter) %7B%0A    $scope.start = 0;%0A    $scope.posts = %5B%5D;%0A    queryFn();%0A  %7D);%0A%0A  var cancelerStack = %5B%5D;%0A%0A  $scope.queryTimeout = _.throttle(function() %7B%0A    $scope.start = 0;%0A    $scope.posts = %5B%5D;%0A    queryFn();%0A  %7D, 750, %7Bleading: false%7D);%0A%0A  $scope.queryNow = function() %7B%0A    queryFn();%0A  %7D%0A%0A  $scope.removePost = function(postToRemove) %7B%0A    growl.addSuccessMessage(%22Seed has been removed: %22 + postToRemove.name, %7Bttl: 5000%7D);%0A    $scope.posts.splice($scope.posts.indexOf(postToRemove), 1);%0A  %7D%0A%0A  var queryFn = function() %7B%0A    // Cancel any outstanding queries%0A    _.each(cancelerStack, function(canceler) %7B canceler.resolve() %7D);%0A    cancelerStack = %5B%5D;%0A%0A    // Push a new cancelPromise onto stack.%0A    var newCanceler = $q.defer();%0A    cancelerStack.push(newCanceler);%0A%0A    // Disable infinite scroll until the query completes%0A    $scope.disableInfiniteScroll = true;%0A    $scope.searching = true;%0A%0A    $http.get('/noo/community/' + community.id + %22/seeds%22, %7B%0A      params: %7B%0A        q: $scope.searchQuery,%0A        postType: $scope.postType,%0A        sort: $scope.postSort,%0A        start: $scope.start,%0A        limit: $scope.limit%0A      %7D,%0A      timeout: newCanceler.promise,%0A      responseType: 'json'%0A    %7D).success(function(posts) %7B%0A      if ($scope.searchQuery) %7B%0A        $analytics.eventTrack('Search: Filter', %7Bquery: $scope.searchQuery, community_id: community.slug%7D)%0A      %7D%0A%0A      var firstLoad = $scope.posts.length %3C $scope.limit;%0A%0A      if (!firstLoad) %7B%0A        $analytics.eventTrack('Posts: Load more in Search', %7Bcommunity_id: community.slug%7D);%0A      %7D%0A      else %7B%0A        $analytics.eventTrack('Search: Load Search Page', %7Bcommunity_id: community.slug%7D);%0A      %7D%0A%0A      // Push posts to stack%0A      angular.forEach(posts, function(post, key) %7B%0A        if (!_.findWhere($scope.posts, %7Bid: post.id%7D)) %7B%0A          $scope.posts.push(post);%0A          $scope.start++;%0A        %7D%0A      %7D);%0A%0A      $scope.searching = false;%0A      $scope.noResults = $scope.posts.length == 0;%0A%0A      if (posts.length == 0) %7B // There were no more posts... disable infinite scroll now%0A        $scope.disableInfiniteScroll = true;%0A      %7D else %7B%0A        $scope.disableInfiniteScroll = false;%0A      %7D%0A    %7D);%0A  %7D;%0A%0A  $scope.queryNow();%0A%0A%7D;%0A%0A
 var 
@@ -155,16 +155,16 @@
 rowl) %7B%0A
+
   $scope
@@ -897,10 +897,10 @@
  %7D, 
-3
 5
+0
 0);%0A

remove old controller code