$(document).ready( function () {
    setTimeout(function () {
		console.log("LOAD TOKEN");
        var checkToken = localStorage.getItem("token");
		console.log(checkToken);
		if (!!checkToken) {
			
			ui.preauthorizeApiKey("Bearer", checkToken);
			setTimeout(function () {
			ui.preauthorizeApiKey("Bearer", checkToken);
			},2000);
			
			console.log("LOGED IN");
		}

		var authButton = document.getElementsByClassName("btn authorize unlocked");
		if (authButton.length > 0) {
			authButton[0].addEventListener("click", () => {
				$(document).ready(function () {
					var submitButton = document.getElementsByClassName("btn modal-btn auth authorize button");
					if (submitButton[0] !== undefined) {
						submitButton[0].addEventListener("click", () => {
							localStorage.removeItem("token");
							var token = $(":text").val();
							localStorage.setItem("token", token);
							//ui.preauthorizeApiKey("Bearer", token);
						});
					}

				});
			});
		}

		var logOutButton = document.getElementsByClassName("btn authorize locked");
		if (logOutButton.length > 0) {
			logOutButton[0].addEventListener("click", () => {
				$(document).ready(function () {
					var submitButton = document.getElementsByClassName("btn modal-btn auth button");
					if (submitButton[0] !== undefined) {
						submitButton[0].addEventListener("click", () => {
							localStorage.removeItem("token");
							//var token = $(":text").val();
							//localStorage.setItem("token", token);
							////ui.preauthorizeApiKey("Bearer", token);
						});
					}

				});
			});
		}
    }, 1000);
	

});