let Vozzi = {
	is_open: false,
	token: null,

	init: function () {
		Vozzi.style();

		//let html = '<div id="vozzi-opener"><img src="https://getvozzi.com/pix/vozzi-text-chat.png?t=1786917266" id="vozzi-chat" onclick="Vozzi.open();"><img src="https://getvozzi.com/pix/vozzi-text-chat-close.png" id="vozzi-close" onclick="Vozzi.close();" style="display: none;"></div>';

		let html = '<div id="vozzi-opener"><div id="vozzi-chat" onclick="Vozzi.open();"><svg style="margin-right: 5px; margin-top: -2px;" width="22.9px" height="18.5px" viewBox="0 0 18 26" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlnsXlink="http://www.w3.org/1999/xlink" alt="Open Web Text"><g stroke="none" stroke-width="1" fill="none" fill-rule="evenodd"><g transform="translate(-303.000000, -368.000000)" stroke="#fff" stroke-width="1.66"><g transform="translate(304.000000, 369.000000)"><path d="M13.6047001,23.436913 L2.47166739,23.436913 C1.23300558,23.436913 0.231463758,22.4998207 0.231463758,21.3427826 L0.231463758,2.26327174 C0.231463758,1.10778261 1.23300558,0.170690217 2.47166739,0.170690217 L13.6047001,0.170690217 C14.8417038,0.170690217 15.8465619,1.10778261 15.8465619,2.26327174 L15.8465619,21.3427826 C15.8465619,22.4998207 14.8417038,23.436913 13.6047001,23.436913 Z"></path><path d="M0.230800485,4.44383152 L15.8458987,4.44383152"></path><path d="M0.230800485,18.8431467 L15.8458987,18.8431467"></path></g></g></g></svg> Text Us</div></div><div id="vozzi-closer"><div id="vozzi-close" onclick="Vozzi.close();"><svg width="18px" height="17px" viewBox="0 0 19 18" version="1.1"><g stroke="none" stroke-width="1" fill="#fff" fill-rule="evenodd"><path d="M9.5,6.95724708 L15.9393398,0.756401316 C16.5251263,0.192310672 17.4748737,0.192310672 18.0606602,0.756401316 C18.6464466,1.32049196 18.6464466,2.2350636 18.0606602,2.79915424 L11.6213203,9 L18.0606602,15.2008458 C18.6464466,15.7649364 18.6464466,16.679508 18.0606602,17.2435987 C17.4748737,17.8076893 16.5251263,17.8076893 15.9393398,17.2435987 L9.5,11.0427529 L3.06066017,17.2435987 C2.47487373,17.8076893 1.52512627,17.8076893 0.939339828,17.2435987 C0.353553391,16.679508 0.353553391,15.7649364 0.939339828,15.2008458 L7.37867966,9 L0.939339828,2.79915424 C0.353553391,2.2350636 0.353553391,1.32049196 0.939339828,0.756401316 C1.52512627,0.192310672 2.47487373,0.192310672 3.06066017,0.756401316 L9.5,6.95724708 Z"></path></g></svg></div></div>';
		//Will switch to this one for better look on all pages but need to confirm with live teams first
		//let html = '<div id="vozzi-opener"><div id="vozzi-chat" style="background-color: black; border: 2px solid white; padding: 5px; width: 75%; font-size: .7rem;" onclick="Vozzi.open();">Text Us <svg style="margin-left: 5px; margin-top: -2px;" xmlns="http://www.w3.org/2000/svg" width="22.9px" height="18.5px" viewBox="0 0 24 24" alt="Open Web Text" fill="none" stroke="#ffffff" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z"></path></svg></div></div><div id="vozzi-closer"><div id="vozzi-close" onclick="Vozzi.close();"><svg width="18px" height="17px" viewBox="0 0 19 18" version="1.1"><g stroke="none" stroke-width="1" fill="#fff" fill-rule="evenodd"><path d="M9.5,6.95724708 L15.9393398,0.756401316 C16.5251263,0.192310672 17.4748737,0.192310672 18.0606602,0.756401316 C18.6464466,1.32049196 18.6464466,2.2350636 18.0606602,2.79915424 L11.6213203,9 L18.0606602,15.2008458 C18.6464466,15.7649364 18.6464466,16.679508 18.0606602,17.2435987 C17.4748737,17.8076893 16.5251263,17.8076893 15.9393398,17.2435987 L9.5,11.0427529 L3.06066017,17.2435987 C2.47487373,17.8076893 1.52512627,17.8076893 0.939339828,17.2435987 C0.353553391,16.679508 0.353553391,15.7649364 0.939339828,15.2008458 L7.37867966,9 L0.939339828,2.79915424 C0.353553391,2.2350636 0.353553391,1.32049196 0.939339828,0.756401316 C1.52512627,0.192310672 2.47487373,0.192310672 3.06066017,0.756401316 L9.5,6.95724708 Z"></path></g></svg></div></div>';

		document.body.insertAdjacentHTML("beforeend", html);

		setTimeout(function () {
			document.getElementById('vozzi-opener').style.opacity = 1;
		}, 250);
		
		if (Vozzi.get_cookie('vozzi-web-text')) {
			Vozzi.token = Vozzi.get_cookie('vozzi-web-text');
		}
	},
	
	fetch_token: function () {
		return (Vozzi.token !== null ? Vozzi.token : (Vozzi.get_cookie('vozzi-web-text')));
	},

	style: function () {
		let styles = document.createElement('link');
		styles.rel = 'stylesheet';
		styles.type = 'text/css';
		styles.media = 'screen';
		styles.href = 'https://getvozzi.com/chat/vc-fe39b277-a062-518f-b0c3-ad02a74ce407/css';
		document.getElementsByTagName('head')[0].appendChild(styles);
	},

	request: function (url, input, method, data_type, success) {
		failure = arguments[4] ? arguments[4] : new Function;
		complete = arguments[5] ? arguments[5] : new Function;
		
		let httpRequest = new XMLHttpRequest();

		if (!httpRequest) {
			return false;
		}
		
		url = url + ((input && input.length > 0 && !(input instanceof FormData)) ? Object.keys(input).map(key => key + '=' + input[key]).join('&') : '');
		
		
	
		httpRequest.onreadystatechange = handle_response;
		httpRequest.open(method, url);
		httpRequest.send((input instanceof FormData ? input : null));

		function handle_response () {
			if (httpRequest.readyState === XMLHttpRequest.DONE) {
				let data = httpRequest.responseText;
				if (data_type.toLowerCase() === 'json') {
					try {
						data = JSON.parse(data);
					}
					catch (error) {
						let c = document.getElementsByClassName('vozzi-container');
						if (c && typeof c[0] !== 'undefined') c[0].innerHTML = '<div class="vozzi-feature">An error occurred. We cannot currently process your request. Please try again later.</div>';

						let btn = document.getElementById('vozzi-send');	
						btn.disabled = true;
					}
				}

				if (httpRequest.status === 200) success(data);
				else failure(data);
				
				complete(data);
			}
		}
	},

	open: function () {
		let close = document.getElementById('vozzi-closer');
		let opener = document.getElementById('vozzi-opener');
		let open = document.getElementById('vozzi-chat');
		Vozzi.is_open = true;
		



		
		setTimeout(function () {

			opener.style.display = 'none';
		}, 100);

		let height = Math.min(500, window.screen.availHeight - 200);
		let width = 550;
		let top = (window.screen.availHeight / 2) - (height / 2);
		let left = (window.screen.availWidth / 2) - (width / 2);

		let voz = document.getElementById('vozzi-window');
		if (!voz || voz.length <= 0) {
			Vozzi.request('https://getvozzi.com/chat/vc-fe39b277-a062-518f-b0c3-ad02a74ce407/text', {token: Vozzi.fetch_token()}, 'get', 'json', function (result) {
				close.style.display = 'block';
				if (result && typeof result !== 'undefined') {
					if (result.status === 'success') {
						document.body.insertAdjacentHTML("beforeend", '<div id="vozzi-window" style="height:' + height + 'px;">' + result.form + '</div>');
						document.getElementById('vozzi_chat_name').focus();
						Vozzi.bind_events();
						Vozzi.token = result.token;
						Vozzi.set_cookie('vozzi-web-text', '/', 'denverpioneers.com', result.token);
					}
					else if (result.status === 'warning') {
						document.body.insertAdjacentHTML("beforeend", '<div id="vozzi-window" style="height:' + height + 'px;">' + result.message + '</div>');
					}
				}
			});
		}
		else {
			close.style.display = 'block';
			voz.style.display = 'block';
			let n = document.getElementById('vozzi_chat_name');
			if (n) n.focus();
		}
	},

	bind_events: function () {
		document.getElementById('vozzi_chat_name').onkeyup = Vozzi.validate_input;
		document.getElementById('vozzi_chat_phone').onkeyup = Vozzi.validate_input;
		document.getElementById('vozzi_chat_message').onkeyup = Vozzi.validate_input;
		document.getElementById('vozzi_chat_name').blur = Vozzi.validate_input;
		document.getElementById('vozzi_chat_phone').blur = Vozzi.validate_input;
		document.getElementById('vozzi_chat_message').blur = Vozzi.validate_input;
	},
	
	input_clear: false,

	validate_input: function () {
		let n = document.getElementById('vozzi_chat_name');
		let p = document.getElementById('vozzi_chat_phone');
		let m = document.getElementById('vozzi_chat_message');
		let send = document.getElementById('vozzi-send');

		if (
			(typeof n !== 'undefined' && n.value !== '' && n.value.length > 1)
			&& (typeof p !== 'undefined' && p.value !== '' && p.value.length >= 10)
			&& (typeof m !== 'undefined' && m.value !== '' && m.value.length >= 5)
		) {
			Vozzi.input_clear = true; send.style.opacity = 1; send.disabled = false;

		}
		else {
			Vozzi.input_clear = false; send.style.opacity = 0.5; send.disabled = true;

		}
	},
	
	close: function () {
		Vozzi.is_open = false;
// 		document.body.removeEventListener('click', Vozzi.close);

		let w = document.getElementById('vozzi-window');
		document.getElementById('vozzi-closer').style.display = 'none';
// 		document.getElementById('vozzi-chat').style.display = 'block';
		document.getElementById('vozzi-opener').style.display = 'block';
		if (w) w.style.display = 'none';
	},

	set_cookie: function (cookie_name, cookie_path, cookie_domain, cookie_value) {
		document.cookie = '"' + cookie_name + "=;samesite=strict;secure;expires=Sun, 16 Aug 2027 00:00:01 GMT;max-age=0" + ((cookie_path) ? ";path=" + cookie_path : "") + ((cookie_domain) ? ";domain=" + cookie_domain : "") + ';value=' + cookie_value + ';"';
	},

	get_cookie: function (cookie_name) {
		var regex = new RegExp("(?:(?:^|.*;\\s*)" + cookie_name + "\\s*\=\\s*([^;]*).*$)|^.*$", 'g');
		return document.cookie.replace(regex, "$1");
	},

	delete_cookie: function (cookie_name, path, domain) {
		document.cookie = cookie_name + "=;expires=Thu, 01 Jan 1970 00:00:01 GMT;max-age=0" + ((path) ? ";path=" + path : "") + ((domain) ? ";domain=" + domain : "") + ";";
	},

	fade_i_interval: null,
	fade_o_interval: null,

	fade_out: function (element) {
		Vozzi.fade_o_interval = setInterval(Vozzi.hide(element), 200);
	},

	fade_in: function (element) {
		Vozzi.fade_i_interval = setInterval(Vozzi.hide(element), 200);
	},

	hide: function (element) {
		let e = document.getElementById(element);
		opacity = Number(window.getComputedStyle(e).getPropertyValue('opacity'));
  		if (opacity > 0) {
			opacity = opacity - 0.1;
			e.style.opacity = opacity;
		}
		else {
			clearInterval(Vozzi.fade_o_interval);
		}
	},

	show: function (element) {
		let e = document.getElementById(element);
		opacity = Number(window.getComputedStyle(e).getPropertyValue('opacity'));
  		if (opacity <= 100) {
			opacity = opacity + 0.1;
			e.style.opacity = opacity;
		}
		else {
			clearInterval(Vozzi.fade_i_interval);
		}
	},
	
	send: function () {
		if (Vozzi.input_clear === true) {
			let btn = document.getElementById('vozzi-send');	
			btn.disabled = true;
			
			let data = new FormData(document.getElementById('vozzi-web-text'));
			data.append('token', Vozzi.fetch_token());
			Vozzi.request('https://getvozzi.com/chat/vc-fe39b277-a062-518f-b0c3-ad02a74ce407/text', data, 'post', 'json', function (result) {
// 				console.log('result:', result);

				if (result.status === 'warning') {
					document.getElementById('vozzi-int').html = result.message;
					document.getElementById('vozzi_chat_name').focus();
				}
				else if (result.status === 'failure') {
					alert('An error occurred: ' + result.message);
				}
				else if (result.status === 'success') {
					let c = document.getElementsByClassName('vozzi-container');
					if (c && typeof c[0] !== 'undefined') c[0].innerHTML = '<div class="vozzi-feature">' + result.message + '</div>';
				}
			});
		}
		else {
			alert('Required field missing!');
		}
	}
};

Vozzi.init();


function vozzi_has_class (el, class_name) {
    if (el.classList) return el.classList.contains(class_name);
    return !!el.class_name.match(new RegExp('(\\s|^)' + class_name + '(\\s|$)'));
}

function vozzi_add_class (el, class_name) {
    if (el.classList) el.classList.add(class_name)
    else if (!vozzi_has_class(el, class_name)) el.class_name += " " + class_name;
}

function vozzi_remove_class (el, class_name) {
    if (el.classList) el.classList.remove(class_name)
    else if (vozzi_has_class(el, class_name)) {
		var reg = new RegExp('(\\s|^)' + class_name + '(\\s|$)');
		el.class_name = el.class_name.replace(reg, ' ');
	}
}


