angular.module('pmp', ['ngLodash', 'uri-template'])
.factory('CollectionDoc', function ($pmp, UriTemplate, lodash) {

  var rootPromise = null;

  function CollectionDoc(data) {
    this.data       = data;
    this.href       = data.href || null;
    this.version    = data.version || '1.0';
    this.attributes = data.attributes;
    this.links      = data.links;
    this.items      = lodash.map(lodash.compact(this.data.items), function (i) { return new CollectionDoc(i); });
  }

  CollectionDoc.init = function () {
    this.root().then( function (rootDoc) {
      console.log('Collection.init complete', rootDoc);
    } );
  };

  CollectionDoc.root = function () {
    rootPromise = rootPromise || $pmp.root().then( function (data) {
      rootDoc = new CollectionDoc(data.data);
      return rootDoc;
    } );
    return rootPromise;
  };

  CollectionDoc.search = function (query) {
    return this.root().then( function (rootDoc) {
      return rootDoc.search(query);
    } );
  }

  CollectionDoc.prototype = {

    next: function () {
      if (this.hasLink('next')) {
        return this.follow('next');
      }
    },

    follow: function (rel, options) {
      var link = this.findLink(rel);
      // console.log('follow link', link);
      var followUrl = null;

      if (link.href) {
        followUrl = link.href;
      } else if (link['href-template']) {
        var tpl   = link['href-template'];
        var vars  = lodash.pick((options || {}), lodash.keys(link['href-vars']));
        followUrl = UriTemplate.parse(tpl).expand(vars);
      }
      // console.log('follow url', followUrl);

      return this.request(followUrl).then( function (data) { return new CollectionDoc(data.data); } );
    },

    hasLink: function (rel) {
      return angular.isDefined(this.findLink(rel));
    },

    findLink: function (rel) {
      var link = null;
      lodash.find(this.data.links, function (typeLinks, type) {
        return link = lodash.find(typeLinks, function (l) {
          return lodash.contains(l.rels, rel);
        });
      });
      return link;
    },

    request: function (url) {
      return $pmp.follow(url);
    },

    search: function (query, options) {
      return CollectionDoc.root().then( function (rootDoc) {
        var vars = lodash.merge({text: query, profile: 'story', limit: 20}, (options || {}));
        return rootDoc.follow('urn:collectiondoc:query:docs', vars);
      });
    }

  };

  window.CollectionDoc = CollectionDoc;

  return CollectionDoc;
}).factory('$pmp', function ($q, $http, $document) {

  var baseUrl = 'http://support.pmp.io.dev/proxy/sandbox/';

  return {

    root: function () {
      return this.request(baseUrl);
    },

    search: function (query) {
      var searchUrl = baseUrl + 'docs?profile=story&limit=20&text=' + query;
      return this.request(searchUrl);
    },

    follow: function (locationString) {
      var followUrl = baseUrl + this.extractQuery(locationString);
      return this.request(followUrl);
    },

    extractQuery: function (fullUrl) {
      var parser = $document[0].createElement('a');
      parser.href = fullUrl;
      // console.log('parser', fullUrl, parser, parser.pathname, parser.search)
      return (parser.pathname + parser.search);
    },

    request: function (requestUrl) {
      return $http.get(requestUrl);
    }

  }
});

@@ -726,20 +726,24 @@
 nction (
-data
+response
 ) %7B%0A    
@@ -768,28 +768,32 @@
 llectionDoc(
-data
+response
 .data);%0A    
@@ -1668,20 +1668,24 @@
 nction (
-data
+response
 ) %7B retu
@@ -1705,20 +1705,24 @@
 tionDoc(
-data
+response
 .data); 
@@ -1901,21 +1901,16 @@
 nd(this.
-data.
 links, f
@@ -3137,27 +3137,24 @@
 llUrl;%0A     
- //
  console.log
@@ -3212,16 +3212,17 @@
 .search)
+;
 %0A      r

clean up use of data var