window.mailster = window.mailster || {};

// block localization
mailster = (function (mailster, $, window, document) {
	'use strict';

	mailster.l10n = window.mailster_l10n;

	return mailster;
})(mailster || {}, jQuery, window, document);
// end localization

// events
mailster = (function (mailster, $, window, document) {
	'use strict';

	var triggertimeout,
		isEnabled = !$('#mailster_disabled').val(),
		events = {
			documentReady: [],
			windowLoad: [],
		},
		last;

	mailster.events = mailster.events || false;

	mailster.status = {
		documentReady: false,
		windowLoad: false,
		windowLoadPending: false,
	};

	//already events registered
	if (mailster.events) {
		for (var i in mailster.events) {
			mailster.log(i, mailster.events[i]);
			if (typeof mailster.events[i] == 'string') {
				last = mailster.events[i];
				events[last] = events[last] || [];
				continue;
			}
			events[last].push(mailster.events[i]);
		}
	}

	mailster.events = events;

	$(document).ready(documentReady);
	$(window).on('load', windowLoad);

	function documentReady(context) {
		context = typeof context === typeof undefined ? $ : context;
		events.documentReady.forEach(function (component) {
			component(context);
		});
		mailster.status.documentReady = true;
		if (mailster.status.windowLoadPending) {
			windowLoad(setContext());
		}
	}

	function windowLoad(context) {
		if (mailster.status.documentReady) {
			mailster.status.windowLoadPending = false;
			context = typeof context === 'object' ? $ : context;
			events.windowLoad.forEach(function (component) {
				component(context);
			});
			mailster.status.windowLoad = true;
		} else {
			mailster.status.windowLoadPending = true;
		}
	}

	function debug(data, type) {
		if (console) {
			for (var i = 0; i < data.length; i++) {
				console[type](data[i]);
			}
		}
	}

	function setContext(contextSelector) {
		var context = $;
		if (typeof contextSelector !== typeof undefined) {
			return function (selector) {
				return $(contextSelector).find(selector);
			};
		}
		return context;
	}

	mailster.events.push = function () {
		var params = Array.prototype.slice.call(arguments),
			event = params.shift(),
			callbacks = params || null;

		mailster.events[event] = mailster.events[event] || [];

		for (var i in callbacks) {
			mailster.events[event].push(callbacks[i]);
		}

		return true;
	};

	mailster.trigger = function () {
		var params = Array.prototype.slice.call(arguments),
			triggerevent = params.shift(),
			args = params || null;

		if (mailster.events[triggerevent]) {
			for (var i = 0; i < mailster.events[triggerevent].length; i++) {
				mailster.events[triggerevent][i].apply(mailster, args);
			}
		} else {
			//events[triggerevent] = [];
		}
	};

	mailster.log = function () {
		debug(arguments, 'log');
	};

	mailster.error = function () {
		debug(arguments, 'error');
	};

	mailster.warning = function () {
		debug(arguments, 'warn');
	};

	return mailster;
})(mailster || {}, jQuery, window, document);

mailster = (function (mailster, $, window, document) {
	'use strict';

	mailster.user = mailster.user || {};
	mailster.local = mailster.local || {};
	mailster.session = mailster.session || {};

	mailster.user.get = function (key, def) {
		key = 'mailster_' + key;
		if (window.getUserSetting !== 'undefined') {
			var raw = window.getUserSetting(key, def);
			if (raw === 'true') {
				return true;
			} else if (raw === 'false') {
				return false;
			}
			return raw;
		}
		return null;
	};
	mailster.user.set = function (key, value) {
		key = 'mailster_' + key;
		if (window.setUserSetting !== 'undefined') {
			if (value === true) {
				value = 'true';
			} else if (value === false) {
				value = 'false';
			}
			return window.setUserSetting(key, value);
		}
		return null;
	};

	mailster.local.get = function (key) {
		key = 'mailster_' + key;
		if (localStorage.getItem(key) === undefined) return {};

		return JSON.parse(localStorage.getItem(key));
	};
	mailster.local.set = function (key, value) {
		key = 'mailster_' + key;
		return localStorage.setItem(key, JSON.stringify(value));
	};

	mailster.session.get = function (key) {
		key = 'mailster_' + key;
		if (sessionStorage.getItem(key) === undefined) return {};

		return JSON.parse(sessionStorage.getItem(key));
	};
	mailster.session.set = function (key, value) {
		key = 'mailster_' + key;
		return sessionStorage.setItem(key, JSON.stringify(value));
	};

	return mailster;
})(mailster || {}, jQuery, window, document);

mailster = (function (mailster, $, window, document) {
	'use strict';

	mailster.util = mailster.util || {};

	mailster.util.requestAnimationFrame =
		window.requestAnimationFrame ||
		window.mozRequestAnimationFrame ||
		window.webkitRequestAnimationFrame ||
		window.msRequestAnimationFrame;

	mailster.util.ajax = function (
		action,
		data,
		callback,
		errorCallback,
		dataType
	) {
		if ('function' === typeof data) {
			if ('function' === typeof callback) {
				errorCallback = callback;
			}
			callback = data;
			data = {};
		}

		dataType = dataType ? dataType : 'JSON';
		return $.ajax({
			type: 'POST',
			url: mailster.ajaxurl,
			data: $.extend(
				{
					action: 'mailster_' + action,
					_wpnonce: mailster.wpnonce,
				},
				data
			),
			success: function (data, textStatus, jqXHR) {
				callback && callback.call(this, data, textStatus, jqXHR);
			},
			error: function (jqXHR, textStatus, errorThrown) {
				var response = mailster.util.trim(jqXHR.responseText);
				if (textStatus == 'error' && !errorThrown) return;
				mailster.log(response, 'error');
				if ('JSON' == dataType) {
					var maybe_json = response.data
						? response.data.match(/{(.*)}$/)
						: false;
					if (maybe_json && callback) {
						try {
							callback.call(this, JSON.parse(maybe_json[0]));
						} catch (e) {
							mailster.log(e, 'error');
						}
						return;
					}
				}
				errorCallback &&
					errorCallback.call(this, jqXHR, textStatus, errorThrown);
				console.error(
					textStatus +
						' ' +
						jqXHR.status +
						': ' +
						errorThrown +
						'\n\n' +
						mailster.l10n.common.check_console
				);
			},
			dataType: dataType,
		});
	};

	mailster.util.rgb2hex = function (str) {
		if (!str) return str;
		var colors = str.match(/rgb\((\d+), ?(\d+), ?(\d+)\)/);

		function nullify(val) {
			val = parseInt(val, 10).toString(16);
			return val.length > 1 ? val : '0' + val; // 0 -> 00
		}
		return colors
			? '#' + nullify(colors[1]) + nullify(colors[2]) + nullify(colors[3])
			: str;
	};

	mailster.util.sanitize = function (string) {
		return mailster.util
			.trim(string)
			.toLowerCase()
			.replace(/ /g, '_')
			.replace(/[^a-z0-9_-]*/g, '');
	};

	mailster.util.sanitizeTag = function (string) {
		var tag = mailster.util
			.trim(string)
			.toLowerCase()
			.replace(/ /g, '-')
			.replace(/[^a-z0-9_-]*/g, '')
			.replace(/^[_]*/, '')
			.replace(/[_]*$/, '');
		return tag;
	};

	mailster.util.sprintf = function () {
		var a = Array.prototype.slice.call(arguments),
			str = a.shift(),
			total = a.length,
			reg;
		for (var i = 0; i < total; i++) {
			reg = new RegExp('%(' + (i + 1) + '\\$)?(s|d|f)');
			str = str.replace(reg, a[i]);
		}
		return str;
	};

	mailster.util.trim = function (string) {
		if ('string' !== typeof string) {
			return string;
		}
		return string.trim();
	};

	mailster.util.isWebkit = 'WebkitAppearance' in document.documentElement.style;
	mailster.util.isMozilla = /firefox/i.test(navigator.userAgent);
	mailster.util.isMSIE = /msie|trident/i.test(navigator.userAgent);
	mailster.util.isTouchDevice = 'ontouchstart' in document.documentElement;

	mailster.util.codemirrorargs = {
		mode: {
			name: 'htmlmixed',
		},
		tabMode: 'indent',
		lineNumbers: true,
		viewportMargin: Infinity,
		autofocus: true,
	};

	mailster.util.top = function () {
		return $('html,body').scrollTop() || document.scrollingElement.scrollTop;
	};

	mailster.util.scroll = function (pos, callback, speed) {
		var t;
		pos = Math.round(pos);
		if (isNaN(speed)) speed = 200;
		if (!mailster.util.isMSIE && mailster.util.top() == pos) {
			callback && callback();
			return;
		}
		$('html,body')
			.stop()
			.animate(
				{
					scrollTop: pos,
				},
				speed,
				function () {
					//prevent double execution
					clearTimeout(t);
					t = setTimeout(callback, 0);
				}
			);
	};

	mailster.util.jump = function (val, rel) {
		val = Math.round(val);
		if (rel) {
			window.scrollBy(0, val);
		} else {
			window.scrollTo(0, val);
		}
	};

	mailster.util.inViewport = function (el, offset) {
		var rect = el.getBoundingClientRect();

		if (!offset) offset = 0;

		//only need top and bottom
		return (
			rect.top + offset >= 0 &&
			rect.top - offset <=
				(window.innerHeight ||
					document.documentElement.clientHeight) /*or $(window).height() */
		);
	};

	mailster.util.unique = function (array) {
		return array.filter(function (value, index, self) {
			return self.indexOf(value) === index;
		});
	};

	mailster.util.debounce = function (callback, delay) {
		return mailster.util.throttle(callback, delay, true);
	};

	mailster.util.throttle = function (callback, delay, debounce) {
		var timeout,
			last = 0;

		if (delay === undefined) delay = 250;

		function api() {
			var that = this,
				elapsed = +new Date() - last,
				args = arguments;

			function run() {
				last = +new Date();
				callback.apply(that, args);
			}

			function clear() {
				timeout = undefined;
			}

			if (debounce && !timeout) {
				run();
			}

			timeout && clearTimeout(timeout);

			if (debounce === undefined && elapsed > delay) {
				run();
			} else {
				timeout = setTimeout(
					debounce ? clear : run,
					debounce === undefined ? delay - elapsed : delay
				);
			}
		}

		return api;
	};

	return mailster;
})(mailster || {}, jQuery, window, document);

mailster = (function (mailster, $, window, document) {
	'use strict';

	mailster.$ = {};
	mailster.dom = {};

	mailster.$.window = $(window);
	mailster.$.document = $(document);
	mailster.$.body = $('body');

	mailster.util.tb_position = function () {
		if (!window.TB_WIDTH || !window.TB_HEIGHT) return;
		$('#TB_window').css({
			marginTop: '-' + parseInt(TB_HEIGHT / 2, 10) + 'px',
			marginLeft: '-' + parseInt(TB_WIDTH / 2, 10) + 'px',
			width: TB_WIDTH + 'px',
		});
	};

	mailster.util.tb_show = function (caption, url, imageGroup) {
		var tb_position = window.tb_position;
		window.tb_position = mailster.util.tb_position;
		tb_show(caption, url, imageGroup);
		window.tb_position = tb_position;
	};

	mailster.events.push('documentReady', function () {
		//open externals in a new tab
		mailster.$.document.on('click', 'a.external', function () {
			window.open(this.href, '_blank', 'noopener,noreferrer');
			return false;
		});
		for (var i in mailster.$) {
			mailster.dom[i] = mailster.$[i][0];
		}
	});
	return mailster;
})(mailster || {}, jQuery, window, document);
