(function() {
  'use strict';
  angular.module('cm-google-api').provider('googleClient', function () {
    var apisToLoad = 0;
    var googleApis = [];
    var cloudEndpoints = [];

    var apiLoadingPromise;
    var apiLoaded = false;
    var apiLoading = false;

    var clientLoadingPromise;
    var clientLoaded = false;
    var clientLoading = false;

    var apiLoadCallback = function(deferred) {
      if (--apisToLoad === 0) {
        apiLoaded = true;
        apiLoading = false;
        deferred.resolve();
      }
    };

    this.addApi = function(api, version, baseUrl){
      console.log(api);
      console.log(version);
      console.log(baseUrl);
      var obj = {};
      obj.api = api;
      obj.version = version;
      apisToLoad++;
      if(baseUrl === 'undefined'){
        googleApis.push(obj);
      }else{
        obj.baseUrl = baseUrl;
        cloudEndpoints.push(obj);
      }
    };

    this.$get = ['$q', '$window', function ($q, $window) {
      return{
        afterClientLoaded: function(){
          if(!clientLoaded && !clientLoading){
            clientLoading = true;
            clientLoadingPromise = $q.defer();
            console.log('loading client');
            var randomId = new Date().getTime();
            var callbackName = '_gapiServiceInitCallback'+randomId;
            $window[callbackName] = function(){
              clientLoaded = true;
              clientLoading = false;
              clientLoadingPromise.resolve();
            };
            var s = document.createElement('script');
            s.src = 'https://apis.google.com/js/client.js?onload='+callbackName;
            document.body.appendChild(s);
          }
          return clientLoadingPromise.promise;
        },
        afterApiLoaded: function(){
          if(!apiLoaded && !apiLoading){
            apiLoadingPromise = $q.defer();
            this.afterClientLoaded().then(function(){
              apiLoading = true;
              console.log('loading api');
              angular.forEach(cloudEndpoints, function(endpoint){
                gapi.client.load(endpoint.api, endpoint.version, function(){apiLoadCallback(apiLoadingPromise);}, endpoint.baseUrl);
              });
              angular.forEach(googleApis, function(api){
                gapi.client.load(api.api, api.version, function(){apiLoadCallback(apiLoadingPromise);});
              });
            });
          }
          return apiLoadingPromise.promise;
        }
      };
    }];
  });
})();

@@ -92,24 +92,141 @@
 nction () %7B%0A
+    var clientId;%0A    //FIXME: Set() is not compatible to all browser, so I'll have to wait...%0A    var scopes = %7B%7D;%0A%0A
     var apis
@@ -290,16 +290,50 @@
 ts = %5B%5D;
+%0A    var tryAutomaticAuth = false;
 %0A%0A    va
@@ -646,101 +646,328 @@
     
-deferred.resolve();%0A      %7D%0A    %7D;%0A%0A    this.addApi = function(api, version, baseUrl)
+if(tryAutomaticAuth)%7B%0A          var scopesStrings = '';%0A          angular.forEach(scopes, function(value, key) %7B%0A            scopesStrings += ' ' + key;%0A          %7D);%0A          gapi.auth.authorize(%7B'client_id': clientId, 'scope': scopesStrings, 'immediate': true%7D, function()%7Bdeferred.resolve();%7D);%0A        %7Delse
 %7B%0A      
 cons
@@ -966,80 +966,107 @@
     
+ 
-console.log(api);%0A      console.log(version);%0A      console.log(
+   deferred.resolve();%0A        %7D%0A      %7D%0A    %7D;%0A%0A    this.addApi = function(api, version, 
 baseUrl)
 ;%0A  
@@ -1057,25 +1057,25 @@
 on, baseUrl)
-;
+%7B
 %0A      var o
@@ -1162,16 +1162,23 @@
      if(
+typeof 
 baseUrl 
@@ -1303,32 +1303,405 @@
 h(obj);%0A      %7D%0A
+      return this;%0A    %7D;%0A%0A    this.setAutomaticAuth = function()%7B%0A      tryAutomaticAuth = true;%0A      this.addScope('https://www.googleapis.com/auth/userinfo.email');%0A      return this;%0A    %7D;%0A%0A    this.addScope = function(scope)%7B%0A      scopes%5Bscope%5D = true;%0A      return this;%0A    %7D;%0A%0A    this.setClientId = function(client)%7B%0A      clientId = client;%0A      return this;%0A
     %7D;%0A%0A    this
@@ -1936,51 +1936,8 @@
 ();%0A
-            console.log('loading client');%0A
     
@@ -2689,50 +2689,8 @@
 ue;%0A
-              console.log('loading api');%0A
     

add missing parameters