var config = require('./config');
var domain = config.domain; // domain name
var urlpath = config.urlpath; // sub url path, like: www.example.com/<urlpath>
var debug = config.debug;
var GOOGLE_API_KEY = config.GOOGLE_API_KEY;
var GOOGLE_CLIENT_ID = config.GOOGLE_CLIENT_ID;
var DROPBOX_APP_KEY = config.DROPBOX_APP_KEY;

//common
var port = window.location.port;
window.serverurl = window.location.protocol + '//' + (domain ? domain : window.location.hostname) + (port ? ':' + port : '') + (urlpath ? '/' + urlpath : '');
var noteid = urlpath ? window.location.pathname.slice(urlpath.length + 1, window.location.pathname.length).split('/')[1] : window.location.pathname.split('/')[1];
var noteurl = serverurl + '/' + noteid;

var version = '0.4.5';

var checkAuth = false;
var profile = null;
var lastLoginState = getLoginState();
var lastUserId = getUserId();
var loginStateChangeEvent = null;

function resetCheckAuth() {
    checkAuth = false;
}

function setLoginState(bool, id) {
    Cookies.set('loginstate', bool, {
        expires: 365
    });
    if (id) {
        Cookies.set('userid', id, {
            expires: 365
        });
    } else {
        Cookies.remove('userid');
    }
    lastLoginState = bool;
    lastUserId = id;
    checkLoginStateChanged();
}

function checkLoginStateChanged() {
    if (getLoginState() != lastLoginState || getUserId() != lastUserId) {
        if(loginStateChangeEvent)
            loginStateChangeEvent();
        return true;
    } else {
        return false;
    }
}

function getLoginState() {
    var state = Cookies.get('loginstate');
    return state === "true" || state === true;
}

function getUserId() {
    return Cookies.get('userid');
}

function clearLoginState() {
    Cookies.remove('loginstate');
}

function checkIfAuth(yesCallback, noCallback) {
    var cookieLoginState = getLoginState();
    if (checkLoginStateChanged())
        checkAuth = false;
    if (!checkAuth || typeof cookieLoginState == 'undefined') {
        $.get(serverurl + '/me')
            .done(function (data) {
                if (data && data.status == 'ok') {
                    profile = data;
                    yesCallback(profile);
                    setLoginState(true, data.id);
                } else {
                    noCallback();
                    setLoginState(false);
                }
            })
            .fail(function () {
                noCallback();
            })
            .always(function () {
                checkAuth = true;
            });
    } else if (cookieLoginState) {
        yesCallback(profile);
    } else {
        noCallback();
    }
}

module.exports = {
  domain: domain,
  urlpath: urlpath,
  debug: debug,
  GOOGLE_API_KEY: GOOGLE_API_KEY,
  GOOGLE_CLIENT_ID: GOOGLE_CLIENT_ID,
  DROPBOX_APP_KEY: DROPBOX_APP_KEY,
  port: port,
  serverurl: serverurl,
  noteid: noteid,
  noteurl: noteurl,
  version: version,
  checkAuth: checkAuth,
  profile: profile,
  lastLoginState: lastLoginState,
  lastUserId: lastUserId,
  loginStateChangeEvent: loginStateChangeEvent,

  /* export functions */
  resetCheckAuth: resetCheckAuth,
  setLoginState: setLoginState,
  checkLoginStateChanged: checkLoginStateChanged,
  getLoginState: getLoginState,
  getUserId: getUserId,
  clearLoginState: clearLoginState,
  checkIfAuth: checkIfAuth
};

@@ -2646,16 +2646,18 @@
 rts = %7B%0A
+  
   domain
@@ -2668,16 +2668,18 @@
 main,%0A  
+ 
+ 
 urlpath:
@@ -2690,16 +2690,18 @@
 path,%0A  
+ 
+ 
 debug: d
@@ -2706,16 +2706,18 @@
  debug,%0A
+  
   GOOGLE
@@ -2744,16 +2744,18 @@
 _KEY,%0A  
+ 
+ 
 GOOGLE_C
@@ -2782,16 +2782,18 @@
 ENT_ID,%0A
+  
   DROPBO
@@ -2822,16 +2822,18 @@
 _KEY,%0A  
+ 
+ 
 port: po
@@ -2842,30 +2842,8 @@
 ,%0A  
-serverurl: serverurl,%0A
   no
@@ -2856,16 +2856,18 @@
 noteid,%0A
+  
   noteur
@@ -2878,16 +2878,18 @@
 oteurl,%0A
+  
   versio
@@ -2900,16 +2900,18 @@
 ersion,%0A
+  
   checkA
@@ -2926,16 +2926,18 @@
 ckAuth,%0A
+  
   profil
@@ -2948,16 +2948,18 @@
 rofile,%0A
+  
   lastLo
@@ -2984,16 +2984,18 @@
 nState,%0A
+  
   lastUs
@@ -3012,16 +3012,18 @@
 UserId,%0A
+  
   loginS
@@ -3063,16 +3063,18 @@
 Event,%0A%0A
+  
   /* exp
@@ -3090,16 +3090,18 @@
 ions */%0A
+  
   resetC
@@ -3126,16 +3126,18 @@
 ckAuth,%0A
+  
   setLog
@@ -3156,24 +3156,26 @@
 LoginState,%0A
+  
   checkLogin
@@ -3212,16 +3212,18 @@
 hanged,%0A
+  
   getLog
@@ -3246,16 +3246,18 @@
 nState,%0A
+  
   getUse
@@ -3272,16 +3272,18 @@
 UserId,%0A
+  
   clearL
@@ -3310,16 +3310,18 @@
 nState,%0A
+  
   checkI

Remove uneccesasry serverurl exporting