/* global
    UALinkModule: false,
    UALinkPlugin: true,
    UALog: false,
    UAPlugin: false,
    UserAccounts: false
*/
'use strict';


// ------------------------------------------
//  Logs the start of execution for this file
// ------------------------------------------
UALog.trace('Loading link_plugin.js');


// define the UALinkPlugin class
UALinkPlugin = function () {
  // Call the parent constructor
  UAPlugin.call(this);

  this._id = 'links';
};


// inherit UAPlugin
UALinkPlugin.prototype = new UAPlugin();

_.extend(UALinkPlugin.prototype, {
  // correct the constructor pointer because it points to UAModule
  constructor: UALinkPlugin,

  _clickCallbacks: [],

  _modules: [],

  configure: function(options) {
    UALog.trace('configure ' + this._id);
    // console.log(options);

    this.text = _.defaults(options.text || {}, this.text);
  },

  init: function() {
    UALog.trace('Creating signInLink module');

    var self = this;

    var signInLink = new UALinkModule(
      'signInLink',
      60,
      'sign-in-link',
      'signIn',
      {
        default: {
          prefix: 'If you already have an account',
          suffix: '',
          text: 'sign in',
        }
      }
    );

    UALog.trace('Creating signUpLink module');
    var signUpLink = new UALinkModule(
      'signUpLink',
      70,
      'sign-up-link',
      'signUp',
      {
        default: {
          prefix: 'Don\'t have an account?',
          suffix: '',
          text: 'Register',
        },
      }
    );

    self._modules.push(signInLink);
    self._modules.push(signUpLink);

    UALog.trace('Registering modules');
    _.each(self._modules, function(module) {
      UserAccounts.registerModule(module);
    });

    if (Meteor.isClient) {
      Template.uaForm.events({
        'click .ua-link a': function(e){
          e.preventDefault();
          self.linkClick(this.instance, this.module);
        }
      });
    }
  },

  linkClick: function(uaTmpl, module) {
    var route = module.targetState;
    var callbacks = _.map(UserAccounts.links._clickCallbacks, function(cb) {
      return cb(uaTmpl, route);
    });
    var callback = _.find(callbacks, function(cb) {return cb;});
    if (callback) {
      callback();
    }
    else {
      uaTmpl.setState(module.targetState);
    }
  },

  onClick: function(callback) {
    check(callback, Function);
    this._clickCallbacks.push(callback);
  },

  uninit: function(){
    UALog.trace('Removing modules');
    _.each(this._modules, function(module) {
      UserAccounts.removeModule(module._id);
    });

    if (Meteor.isCLient) {
      delete Template.uaForm.__eventMaps['click .ua-link a'];
    }
  }
});

@@ -374,45 +374,45 @@
 ion 
-() %7B%0A  // Call the parent constructor
+_UALinkPlugin() %7B%0A  var self = this;%0A
 %0A  U
@@ -428,22 +428,22 @@
 all(
-this
+self
 );%0A%0A  
-this
+self
 ._id
@@ -715,16 +715,26 @@
 function
+ configure
 (options
@@ -890,21 +890,88 @@
 function
+ init
 () %7B%0A
+    var self = this;%0A    var signInLink;%0A    var signUpLink;%0A%0A
     UALo
@@ -1018,34 +1018,8 @@
     
-var self = this;%0A%0A    var 
 sign
@@ -1121,39 +1121,41 @@
       %7B%0A        
+'
 default
+'
 : %7B%0A          pr
@@ -1244,32 +1244,33 @@
 n in',%0A        %7D
+,
 %0A      %7D%0A    );%0A
@@ -1317,28 +1317,24 @@
 dule');%0A    
-var 
 signUpLink =
@@ -1436,23 +1436,25 @@
         
+'
 default
+'
 : %7B%0A    
@@ -1710,32 +1710,42 @@
 odules, function
+ regModule
 (module) %7B%0A     
@@ -1889,11 +1889,24 @@
 tion
+ clickUALink
 (e)
+ 
 %7B%0A  
@@ -1992,24 +1992,25 @@
 );%0A        %7D
+,
 %0A      %7D);%0A 
@@ -2041,16 +2041,26 @@
 function
+ linkClick
 (uaTmpl,
@@ -2066,24 +2066,61 @@
 , module) %7B%0A
+    var callback;%0A    var callbacks;%0A
     var rout
@@ -2143,24 +2143,21 @@
 tState;%0A
+%0A
     
-var 
 callback
@@ -2206,24 +2206,28 @@
 ks, function
+ lcb
 (cb) %7B%0A     
@@ -2261,28 +2261,24 @@
     %7D);%0A    
-var 
 callback = _
@@ -2302,16 +2302,20 @@
 function
+ fcb
 (cb) %7Bre
@@ -2322,16 +2322,17 @@
 turn cb;
+ 
 %7D);%0A    
@@ -2370,20 +2370,16 @@
 );%0A    %7D
-%0A   
  else %7B%0A
@@ -2452,16 +2452,24 @@
 function
+ onClick
 (callbac
@@ -2569,18 +2569,26 @@
 function
+ uninit
 ()
+ 
 %7B%0A    UA
@@ -2652,16 +2652,23 @@
 function
+ remMod
 (module)
@@ -2822,13 +2822,14 @@
    %7D%0A  %7D
+,
 %0A%7D);%0A

use of self and linting