// Generated by CoffeeScript 1.6.2
(function() {
  var AjaxAuthBaseController, AjaxAuthController, AjaxAuthFbController, AjaxAuthGoogleController, d,
    __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
    __hasProp = {}.hasOwnProperty,
    __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };

  d = document;

  AjaxAuthBaseController = (function() {
    function AjaxAuthBaseController() {
      this.login_failed = __bind(this.login_failed, this);
      this.login_succeded = __bind(this.login_succeded, this);
      this.server_responded = __bind(this.server_responded, this);      this.form = $('form#ajax-login');
      this.username = this.form.find('input[name=_username]');
      this.password = this.form.find('input[name=_password]');
    }

    AjaxAuthBaseController.prototype.server_responded = function(response) {
      this[response.result](response);
      return this.form.trigger('done');
    };

    AjaxAuthBaseController.prototype.login_succeded = function(response) {
      return this.redirect(response.redirect_url);
    };

    AjaxAuthBaseController.prototype.login_failed = function(response) {
      return this.invalid($(this.username, this.password), response.message);
    };

    AjaxAuthBaseController.prototype.redirect = function(url) {
      return window.location.href = url;
    };

    AjaxAuthBaseController.prototype.invalid = function(input, msg) {
      return input.trigger({
        type: 'invalid',
        message: msg
      });
    };

    return AjaxAuthBaseController;

  })();

  AjaxAuthController = (function(_super) {
    __extends(AjaxAuthController, _super);

    function AjaxAuthController() {
      this._ajax = __bind(this._ajax, this);
      this._onSubmit = __bind(this._onSubmit, this);
      this._beforeSubmit = __bind(this._beforeSubmit, this);      AjaxAuthController.__super__.constructor.apply(this, arguments);
      this.submitURL = this.form.attr('action');
      this.form.bind('submit', this._onSubmit);
    }

    AjaxAuthController.prototype._beforeSubmit = function(_callback) {
      if (!this.username.val().length) {
        return this.invalid(this.username, 'Type your username');
      }
      if (!this.password.val().length) {
        return this.invalid(this.password, 'Type your password');
      }
      return _callback.apply(this);
    };

    AjaxAuthController.prototype._onSubmit = function(e) {
      e.preventDefault();
      return this._beforeSubmit(this._ajax);
    };

    AjaxAuthController.prototype._ajax = function() {
      this.form.trigger('loading');
      return $.ajax({
        url: this.submitURL,
        type: 'POST',
        dataType: 'json',
        data: {
          _username: this.username.val(),
          _password: this.password.val()
        },
        success: this.server_responded
      });
    };

    return AjaxAuthController;

  })(AjaxAuthBaseController);

  AjaxAuthFbController = (function(_super) {
    __extends(AjaxAuthFbController, _super);

    function AjaxAuthFbController(loginRoute, appId) {
      var _this = this;

      this.loginRoute = loginRoute;
      this.appId = appId;
      this.fbReady = __bind(this.fbReady, this);
      this.loadFBApi = __bind(this.loadFBApi, this);
      this.fbCallback = __bind(this.fbCallback, this);
      AjaxAuthFbController.__super__.constructor.apply(this, arguments);
      this.loadFBApi();
      this.fBtn = $('#ajax-fb-btn');
      this.fBtn.bind('click', function() {
        return FB.login(_this.fbCallback, {
          scope: "email"
        });
      });
    }

    AjaxAuthFbController.prototype.fbCallback = function(response) {
      if (response.status === 'connected') {
        return $.ajax({
          url: this.loginRoute,
          type: 'POST',
          dataType: 'json',
          data: {
            token: response.authResponse.accessToken
          },
          success: this.server_responded
        });
      }
    };

    AjaxAuthFbController.prototype.loadFBApi = function() {
      var id, js, ref;

      if (window.facebookReady !== true) {
        if (d.getElementById(id = 'facebook-jssdk')) {
          return;
        }
        ref = d.getElementsByTagName('script')[0];
        js = d.createElement('script');
        js.id = id;
        js.async = true;
        js.src = "//connect.facebook.net/en_US/all.js";
        ref.parentNode.insertBefore(js, ref);
        return window.fbAsyncInit = this.fbReady;
      }
    };

    AjaxAuthFbController.prototype.fbReady = function() {
      FB.init({
        appId: this.appId,
        status: true,
        cookie: true,
        xfbml: true
      });
      window.facebookReady = true;
      return $(window).trigger('facebookReady');
    };

    return AjaxAuthFbController;

  })(AjaxAuthController);

  AjaxAuthGoogleController = (function(_super) {
    __extends(AjaxAuthGoogleController, _super);

    function AjaxAuthGoogleController(loginRoute, clientId) {
      var _this = this;

      this.loginRoute = loginRoute;
      this.clientId = clientId;
      this.loadGApi = __bind(this.loadGApi, this);
      this.renderSignInButton = __bind(this.renderSignInButton, this);
      this.doLogin = __bind(this.doLogin, this);
      this.saveResponse = __bind(this.saveResponse, this);
      AjaxAuthGoogleController.__super__.constructor.apply(this, arguments);
      this.loadGApi();
      this.gBtn = $('#ajax-g-btn');
      this.gBtn.bind('click', function() {
        return _this.login = true;
      });
    }

    AjaxAuthGoogleController.prototype.saveResponse = function(response) {
      this.response = response;
      if (this.login) {
        return this.doLogin();
      }
    };

    AjaxAuthGoogleController.prototype.doLogin = function() {
      var response;

      response = this.response;
      if ((response.code != null) && response.code && response.state === this.token && (response.error == null)) {
        return $.ajax({
          url: this.loginRoute,
          type: 'POST',
          dataType: 'json',
          data: {
            token: response.access_token
          },
          success: this.server_responded
        });
      }
    };

    AjaxAuthGoogleController.prototype.renderSignInButton = function() {
      this.googleBtn = {
        'callback': this.saveResponse,
        'clientid': this.clientId + '.apps.googleusercontent.com',
        'cookiepolicy': 'single_host_origin',
        'requestvisibleactions': 'http://schemas.google.com/AddActivity',
        'scope': 'https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile',
        'state': (this.token = 'test123_merely_is_a_token')
      };
      return gapi.signin.render('ajax-g-btn', this.googleBtn);
    };

    AjaxAuthGoogleController.prototype.loadGApi = function() {
      var c, s,
        _this = this;

      if (window.gApiReady !== true) {
        window.gApiReady = function() {
          window.gApiReady = true;
          return _this.renderSignInButton();
        };
        s = d.createElement('script');
        s.type = 'text/javascript';
        s.async = true;
        s.src = 'https://apis.google.com/js/client:plusone.js?onload=gApiReady';
        c = d.getElementsByTagName('script')[0];
        return c.parentNode.insertBefore(s, c);
      }
    };

    return AjaxAuthGoogleController;

  })(AjaxAuthController);

  new AjaxAuthController;

  new AjaxAuthFbController;

  new AjaxAuthGoogleController;

}).call(this);
