(function (window, undef) {
	'use strict';

	var version = 1.00;
	var document = window.document;

	window.DocutoneClient = window.DocutoneClient || {};
	window.DocutoneClient.Kintone = window.DocutoneClient.Kintone || {};
	if (window.DocutoneClient.Kintone.version >= version) {
		return;
	}

	window.DocutoneClient.Kintone = (function() {
		var that = {
			version: version,
			_ua: (function(){
				return {
					ltIE6:typeof window.addEventListener == "undefined" && typeof document.documentElement.style.maxHeight == "undefined",
					ltIE7:typeof window.addEventListener == "undefined" && typeof document.querySelectorAll == "undefined",
					ltIE8:typeof window.addEventListener == "undefined" && typeof document.getElementsByClassName == "undefined",
					ltIE9:document.uniqueID && !window.matchMedia,
					gtIE10:document.uniqueID && document.documentMode >= 10,
					Trident:document.uniqueID,
					Gecko:window.sidebar,
					Presto:window.opera,
					Blink:window.chrome,
					Webkit:!window.chrome && typeof document.webkitIsFullScreen != undefined,
					Touch:typeof document.ontouchstart != "undefined",
					Mobile:typeof window.orientation != "undefined"
				};
			})()
		};

		function log(message) {
			if (typeof(console) != "undefined" && console.log) {
				console.log(message);
			}
		}

		function Initializer(kintone) {
			this.loaded = false;
			this.logic = null;

			this.kintone = kintone;
			this.init();
		};

		Initializer.prototype.JQUERY = "https://s.oproarts.com/docutone/jquery/jquery-1.11.0.min.js";
		Initializer.prototype.JQUERY_UI = "https://s.oproarts.com/docutone/ui/1.10.4/jquery-ui.min.js";
		Initializer.prototype.JQUERY_UI_CSS = "https://s.oproarts.com/docutone/ui/1.10.4/black-tie/jquery-ui.css";
		Initializer.prototype.JQGRID = "https://s.oproarts.com/docutone/jquery.jqGrid-4.5.4/js/jquery.jqGrid.min.js";
		Initializer.prototype.JQGRID_LOCALE = "https://s.oproarts.com/docutone/jquery.jqGrid-4.5.4/js/i18n/grid.locale-en.js";
		Initializer.prototype.JQGRID_CSS = "https://s.oproarts.com/docutone/jquery.jqGrid-4.5.4/css/ui.jqgrid.css";
		Initializer.prototype.JSRENDER = "https://s.oproarts.com/docutone/jsRender/jsrender.min.js";
		Initializer.prototype.DOCUTONE_CSS = "https://s.oproarts.com/docutone/docutone.css";
		Initializer.prototype.DOCUTONE_LIBRARY = "https://s.oproarts.com/docutone/docutone_lib-1.5.0.js";
		Initializer.prototype.init = function() {
			var oThis = this;
			oThis.loadStylesheet(oThis.JQUERY_UI_CSS, function () {
				oThis.loadStylesheet(oThis.JQGRID_CSS, function () {
					oThis.loadScript(oThis.JQUERY, function() {
						oThis.loadScript(oThis.JQUERY_UI, function () {
							oThis.loadScript(oThis.JQGRID_LOCALE, function () {
								oThis.loadScript(oThis.JQGRID, function () {
									oThis.loadScript(oThis.JSRENDER, function () {
										oThis.loadStylesheet(oThis.DOCUTONE_CSS, function () {
											oThis.loadScript(oThis.DOCUTONE_LIBRARY, function() {
												oThis.logic = new net.opro.js.docutone.ui.Logic(oThis.kintone, function() { oThis.loaded = true; });
											});
										});
									});
								});
							});
						});
					});
				});
			});
			this.kintone.events.on("app.record.detail.show", function(event) { oThis.onDetailShow(event); });
			this.kintone.events.on("app.record.create.show", function(event) { oThis.onEditShow(event); });
			this.kintone.events.on("app.record.edit.show", function(event) { oThis.onEditShow(event); });

			// 保存時のユーザ制限制御
			this.kintone.events.on("app.record.index.edit.submit", function(event) { return oThis.onBeforeSubmit(event); });
			this.kintone.events.on("app.record.edit.submit", function(event) { return oThis.onBeforeSubmit(event); });
			this.kintone.events.on("mobile.app.record.edit.submit", function(event) { return oThis.onBeforeSubmit(event); });
			this.kintone.events.on("app.record.create.submit", function(event) { return oThis.onBeforeSubmit(event); });
			this.kintone.events.on("mobile.app.record.create.submit", function(event) { return oThis.onBeforeSubmit(event); });
		};

		Initializer.prototype.getLogic = function() {
			return this.logic;
		};

		Initializer.prototype.loadScript =  function(url, success) {
			var script = document.createElement("SCRIPT");
			script.src = url;
			script.type = "text/javascript";
			var head = document.getElementsByTagName('head')[0];
			var completed = false;
			script.onload = script.onreadystatechange = function () {
				if (!completed && (!this.readyState || this.readyState == 'loaded' || this.readyState == 'complete')) {
					completed = true;
					success();
					script.onload = script.onreadystatechange = null;
				}
			};
			head.appendChild(script);
		};

		Initializer.prototype.loadStylesheet = function(url, success) {
			var link = document.createElement("LINK");
			link.rel = "stylesheet";
			link.href = url;
			link.type = "text/css";
			link.media = "screen";
			var head = document.getElementsByTagName('head')[0];
			var completed = false;
			link.onload = link.onreadystatechange = function () {
				if (!completed && (!this.readyState || this.readyState == 'loaded' || this.readyState == 'complete')) {
					completed = true;
					success();
					link.onload = link.onreadystatechange = null;
				}
			};
			head.appendChild(link);
		};

		Initializer.prototype.ready = function(fn) {
			if (this.loaded) {
				$(document).ready(fn);
			} else {
				log("Now loading...");
				var oThis = this;
				setTimeout(function() { oThis.ready(fn); }, 100);
			}
		};

		Initializer.prototype.onDetailShow = function(event) {
			var oThis = this;
			this.ready(function() {
				oThis.logic.prepare(event.record);
			});
		};

		Initializer.prototype.onEditShow = function(event) {
			var oThis = this;
			this.ready(function() {
				oThis.logic.prepareLabel();
			});
		};

		Initializer.prototype.onBeforeSubmit = function(event) {
			const checkGuestSpace = this.logic.checkGuestSpace(event);
			if(checkGuestSpace.error != ""){
				return checkGuestSpace;
			}
			return this.logic.checkMultipleUsersAndUIDs(event);
		};

		that.init = function(kintone) {
			this.initializer = new Initializer(kintone);
			return this.initializer;
		};

		that.getInitializer = function() {
			return this.initializer;
		};

		return that;
	})();


})(window);


DocutoneClient.Kintone.init(kintone);

