window.f230roGlobal = { 'domain':'https://formular230.ro','id':'d79cfa2' };
window.f230ro = {
	pop: null,
	frame: null,
	popBox: null,
	closeBut: null,
	powerUp: function() {
		css = document.createElement("link");
		target = document.head;
		css.rel = 'stylesheet';
		css.type = 'text/css';
		css.href = window.f230roGlobal.domain + '/erp/deploy/client.css';
		css.addEventListener('load', f230ro.initAll);
		window.addEventListener('message', f230ro.comResize);
		window.addEventListener('resize' , f230ro.comPortrait);
		target.appendChild(css);
	},
	initAll: function() {
		f230ro.initPop();
		// FORMULAR
		var c = document.body.querySelector('.f230ro-formular');
		if (!!c && c instanceof Element) {
			f230ro.getForm(c);
			// RESIZE EVENTS
			window.addEventListener('resize' , f230ro.comOffset);
			document.addEventListener('scroll', f230ro.comOffset);
			return; }
		// BUTON POPUP
		c = document.body.querySelectorAll('.f230ro-lansare');
		if (!!c && c.length > 0) {
			for (var i=0,n=c.length; i<n; i++)
				c[i].addEventListener('click', function() { f230ro.getForm(); });
			return; }
		// POP-UP INSTANT
		f230ro.getForm();
	},
	div: function(cname, parent) {
		div = document.createElement('div');
		div.className = cname;
		if (!!parent) parent.appendChild(div);
		return div;
	},
	highestZ: function(element) {
		if (!element) return;
		var z, max = 1, elems = document.getElementsByTagName('*');
		for (var i=0, n=elems.length; i<n; i++) {
			z = Number.parseInt( document.defaultView.getComputedStyle(elems[i], null).getPropertyValue("z-index"), 10);
			if (elems[i] != element && z > max) max = z;
		}
		if (!element.style.zIndex || element.style.zIndex/1 < max/1) element.style.zIndex = ++max;
		return max;
	},
	getForm: function(target) {
		if (!target) {
			target = f230ro.popBox;
			f230ro.pop.className = f230ro.pop.className.replace(' f230ro-show', '');
			f230ro.pop.style.display = 'none';
		}
		if (!!f230ro.frame) f230ro.frame.parentNode.removeChild(f230ro.frame);
		f230ro.frame = document.createElement('iframe');
		f230ro.frame.addEventListener('load', f230ro.frameLoad);
		f230ro.frame.src = window.f230roGlobal.domain + '/share/' + window.f230roGlobal.id + '/form';
		// APPEND
		target.appendChild(f230ro.frame);
	},
	frameLoad: function(e) {
		// RESIZE INFO EVENT
		f230ro.comRegister();
		// CSS
		var p = e.target.parentNode;
		if (p.className.indexOf('formular') < 0) {
			setTimeout(f230ro.comResizeRequest, 100);
			f230ro.showPop();
		} else {
			setTimeout(f230ro.comInit, 100);
			p.className+= ' f230ro-show';
		}
	},
	initPop: function() {
		f230ro.pop = f230ro.div('f230ro-pop-bg', document.body);
		f230ro.highestZ(f230ro.pop);
		f230ro.pop.style.display = 'none';
		f230ro.popBox = f230ro.div('f230ro-pop', f230ro.pop);
		f230ro.closeBut = f230ro.div('f230ro-pop-close', f230ro.popBox);
		f230ro.closeBut.addEventListener('mousedown', f230ro.closePopDown);
		f230ro.pop.addEventListener('mousedown', f230ro.closePopDown);
		f230ro.closeBut.addEventListener('mouseup', f230ro.closePopUp);
		f230ro.pop.addEventListener('mouseup', f230ro.closePopUp);
	},
	canClose: false,
	closePopDown: function(e) {
		f230ro.canClose = e.target === f230ro.pop || e.target === f230ro.closeBut;
	},
	closePopUp: function(e) {
		if (f230ro.canClose) f230ro.closePop();
		f230ro.canClose = false;
	},
	closePop: function() {
		f230ro.pop.className = f230ro.pop.className.replace(' f230ro-show', '');
		setTimeout(f230ro.closedPop, 600);
	},
	closedPop: function() {
		f230ro.pop.style.display = 'none';
	},
	showPop: function() {
		f230ro.pop.className = f230ro.pop.className.replace(' f230ro-show', '');
		f230ro.pop.style.display = 'block';
		setTimeout(f230ro.shownPop, 50);
	},
	shownPop: function() {
		if (f230ro.pop.className.indexOf(' f230ro-show') < 0)
			f230ro.pop.className+= ' f230ro-show';
	},
	// RESIZE EVENT COMMUNICATION for DIV
	comPortraitLast: false,
	comOffsetTimeout: null,
	comRegister: function() {
		f230ro.frame.contentWindow.postMessage(0, window.f230roGlobal.domain);  },
	comInit: function() {
		f230ro.comResizeRequest();
		f230ro.comPortrait();
		f230ro.comOffset();
	},
	comResizeRequest: function() {
		f230ro.frame.contentWindow.postMessage(1, window.f230roGlobal.domain);  },
	comResize: function(e) {
		if (!f230ro.frame) return;
		if (!f230ro.frame || !e.origin || e.origin !== window.f230roGlobal.domain || e.data.indexOf('px') < 0) return;
		f230ro.frame.style.height = e.data;  },
	comOffset: function() {
		if (!f230ro.frame) return;
		if (!f230ro.comOffsetTimeout)
			f230ro.comOffsetTimeout = window.setTimeout(f230ro.comOffset, 400);
		var r = f230ro.frame.getBoundingClientRect();
		f230ro.frame.contentWindow.postMessage([2, r.y, window.innerHeight], window.f230roGlobal.domain);  },
	comPortrait: function() {
		var portrait = window.innerHeight > window.innerWidth;
		if (portrait == f230ro.comPortraitLast) return;
		f230ro.comPortraitLast = portrait;
		f230ro.frame.contentWindow.postMessage([3, portrait], window.f230roGlobal.domain);  }
};
f230ro.powerUp();