(function(w, d){
	var rid         = 'ota_http_request_id',
		v           = 'version',
		dl          = 'dataLayer',
		source      = (d.currentScript || {src: '//cnt.worldota.net/'}).src,
		source_host = (source.split('//') || [null, ''])[1].split('/')[0],
		qs          = (source.split('?')[1] || '')
		.split('&')
		.map(function(param){
			return param.split('=');
		}).reduce(function(result, param){
			return result[param[0]] = param[1], result;
		}, {}),
		request_id  = qs[rid],
		version     = qs[v],
		page_host   = d.location.hostname.replace('www.', '');

	w[rid] = w[rid] || request_id;
	w[dl]  = w[dl]  || [];

	function inject(src, is){
		is && is.apply(this, Array.prototype.slice.call(arguments, 2));

		var l = d.createElement('link');
		l.rel  = 'preload';
		l.as   = 'script';
		l.href = src;

		d.head.appendChild(l);

		var s = d.createElement('script');
		s.src = src;

		d.head.appendChild(s);
	}

	function gtm(domain, cb){
		var ids = {
				'ostrovok.ru':                  'K3QKHW',
				'academy.ostrovok.ru':          'K3QKHW',
				'career.ostrovok.ru':           'K3QKHW',
				'zenhotels.com':                'K3QKHW',
				'b2b.ostrovok.ru':              'THPBPLQ',
				'corp.ostrovok.ru':             'THPBPLQ',
				'aparts.ostrovok.ru':           'MP9LNJ7',
				'ratehawk.com':                 'WJF4RP3',
				'roundtrip.travel':             'KS5GMWN',
				'culture.etg.team':             'PN2H4S9R',
				'adventure.emergingtravel.com': 'NZH2W2K',
				'emergingtravel.com':           'NZH2W2K',
				'blog.emergingtravel.com':      'MSWNZVV',
				'ostrovok.tech':                'P4VH7KXC'
			},
			id  = ids[domain];

		id && cb('//www.googletagmanager.com/gtm.js?id=GTM-' + id + (version ? '&' + version : ''));
	}

	function funnel(domain, request_id, cb){
		cb('//' + domain + '/hc/static/master/funnel-loader.js' + (request_id ? '?' + rid + '=' + request_id : ''));
	}

	funnel(source_host, w[rid], function(source){
		inject(source, function(n){
			w.HcFunnelGlobal = n;

			w[n] = w[n] || function(){
			  (w[n].q || (w[n].q = [])).push(arguments);
			};
			w[n].t = w[n].t || Date.now();
		}, 'hcfunnel');
	});
	gtm(page_host, function(source){
		inject(source, function(n){
			w[n].push({
				'gtm.start': Date.now(),
				'event':     'gtm.js'
			});
		}, dl);
	});
})(window, document);