	var adsfin_places = adsfin_places ? adsfin_places : [];
	var adsfin_view_id = adsfin_view_id ? adsfin_view_id : '172981426721313126';
	
	if ( !adsfin_place_obj ) var adsfin_place_obj = function( place_id, params, site_id, publisher_id, utm_passthrough ){
		
		this.params = params;
				
		this.loadLocationParams = function(){
			sp1 = window.location.search.split("?");
			if ( sp1.length==2 ) { sp2 = sp1[1].split("&");
				for ( str in sp2 ) { sp3 = sp2[str].split("=");
					if ( sp3.length == 2 ) { key = sp3[0]; val = sp3[1];
						if ( key in {
							'utm_source':'', 'utm_medium':'', 'utm_campaign':'', 'utm_content':'', 'utm_term':'',
							'subid':'', 'subid1':'', 'subid2':'', 'subid3':'', 
							'src':'', 'src1':'', 'src2':'', 'src3':''
						} ) { this.params[key] = val; } 
					}
				}
			}
		}
		
		this.getCookie = function( name ){
			let matches = document.cookie.match( new RegExp( "(?:^|; )" + name.replace(/([.$?*|{}()\[\]\\\/+^])/g, '\\$1') + "=([^;]*)" ) );
			if ( matches ){ let res = decodeURIComponent( matches[1] ); return res; }
			return undefined;			
		}
		
		this.setCookie = function( name, value, options = { path: '/' } ){
			options = options || {};
			let updatedCookie = encodeURIComponent(name) + "=" + encodeURIComponent(value);
			for (let optionKey in options) {
				updatedCookie += "; " + optionKey;
				let optionValue = options[optionKey];
				if (optionValue !== true) { updatedCookie += "=" + optionValue; }
			}
			document.cookie = updatedCookie;
		}

		this.place_id = place_id; 
		this.render_id = this.place_id + Math.floor(Math.random()*1000000);
		this.d= location.hostname;
		this.u = this.getCookie('dsu');
		this.site_id = site_id;
		this.publisher_id = publisher_id;
		this.l = (navigator.language || navigator.languages[0]).split('-')[0].toLowerCase();
		
		if ( utm_passthrough == "1" ) { this.loadLocationParams(); } 
		
		if ( this.u == undefined ) {
			this.setCookie( 'dsu', adsfin_view_id, {"path":"/", "max-age":86400} );
			this.u = adsfin_view_id;
		} 
		
		this.gy = function(o){ 
			var y=0; 
			if ( o ) do { 
				y += o.offsetTop; 
				o = o.offsetParent; 
			} while( o ); 
			return y; 
		}
		
		this.gh = function(o){ 
			var hh=-1;
			try{
				if( o.offsetHeight ){
					hh = o.offsetHeight;
				} else if( o.firstChild ){
					hh = o.firstChild.offsetHeight;
				} else {
					hh=0;
				}
			} catch(e) {}
			return( hh );
		}
		
		this.sendAnimate = function(){
			if ( ! this.animateSended ) {
				var iframe = document.getElementById( this.render_id );
				iframe.contentWindow.postMessage( JSON.stringify( { "adsfin_message": { "action": "animate" } } ), '*' );
				this.animateSended = true;
			} 
		}
				
		this.clearAnimate = function(){
			this.animateSended = false;
		}
				
		this.sendView = function(){
			if ( 
				( ! this.viewSended ) && 
				( this.place_id != undefined ) && 
				( this.banner_id != undefined ) && 
				( adsfin_view_id != undefined ) 
			) {
				this.variant = this.variant ?? 0;
				this.setup_experiment_version = this.setup_experiment_version ?? 0;
				//console.log( 'View: ' + this.place_id + ' ' + this.banner_id + ' ' + this.campaign_id + ' ' + this.advertiser_id );
				var XHR = ("onload" in new XMLHttpRequest()) ? XMLHttpRequest : XDomainRequest;
				var xhr = new XHR();
				xhr.parent = this;
				xhr.open( 'GET', '//reg.adsfin.net/event/set_view/' + 
					this.place_id + '/' + 
					this.banner_id + '/' + 
					adsfin_view_id + '/' + 
					this.publisher_id + '/' + 
					this.advertiser_id + '/' + 
					this.site_id + '/' + 
					this.campaign_id + '/' +
					this.u + '/' +
					this.cost_view + '/' +
					this.a_rate  + '/' +
					this.variant + '/' + 
					this.setup_experiment_version
				, true );
				xhr.onload = function() {
					//console.log( 'Response:' );
					//console.log( this.responseText );
					var resp = JSON.parse( this.responseText );
					this.parent.viewUID = resp.data;
					var iframe = document.getElementById( this.parent.render_id );
					iframe.contentWindow.postMessage( JSON.stringify( { "adsfin_message": { "action": "view", "id": this.parent.viewUID } } ), '*' );
				}
				xhr.send();
				this.viewSended = true;
			}			
		}
		
		this.sendPlaceView = function(){
			
			var data = { 
				session_id: this.u, 
				view_id: adsfin_view_id,
				publisher_id: this.publisher_id,
				site_id: this.site_id,
				place_id: this.place_id,
				location: document.location.href,
				lang: this.l
			};
			
			var params = Object.keys(data)
			  .map(key => encodeURIComponent(key) + '=' + encodeURIComponent(data[key]))
			  .join('&');

			var xhr = new XMLHttpRequest();
			
				xhr.open("POST", "//reg.adsfin.net/event/set_place_view/", true);
				xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

				xhr.onload = function() {
					console.log( 'Send Place View Response:' );
					console.log( this.responseText );
				}
			
			xhr.send( params );
			
		}
		
		this.viewSended = false;
		this.animateSended = false;
		this.viewUID = null;
		adsfin_places[this.render_id] = this;
		
		this.place = document.getElementById( 'place_' + this.place_id ); 
			
		if ( this.place ) {
			
			
			hh = parseInt( this.place.getAttribute('height') );
			this.place_custom_height = ( this.place.getAttribute('height') == 'auto' ) ? 'auto' : ( isNaN( hh ) ? 'auto' : hh );
			this.ow = this.place.parentNode.offsetWidth || window.innerWidth;
			this.cw = this.place.parentNode.clientWidth || window.innerWidth;
			
			this.yametric = this.place.getAttribute('yametric');
			this.yatarget = this.place.getAttribute('yatarget');
			this.delay = parseInt( this.place.getAttribute('delay') );
				this.delay = this.delay>0 ? this.delay : 0;
				
			setTimeout( ()=>{
				
				var paramstr = '';
				for( const key in this.params ) { paramstr += '&' + key + '=' + this.params[ key ]; }
				
				this.place.outerHTML = 
					'<style>.frm_' + this.place_id + ' {	width: 100%;	height: 0px; background-color: red;}</style>' + 
					'<div style="text-align: center;">'+
						'<iframe class="frm_' + this.place_id + '" id="' + this.render_id + '" frameborder="0" data-height="' + this.place_custom_height + '" data-yametric="' + this.yametric + '" data-yatarget="' + this.yatarget + '" src="//front.adsfin.net/auc/roll/' + this.place_id + '/?' + 'r=' + this.render_id + '&u=' + this.u + '&v=' + adsfin_view_id + '&d=' + this.d + '&l=' + this.l + '&s=' + this.site_id + '&p=' + this.publisher_id + '&ow=' + this.ow + '&cw=' + this.cw +  paramstr + '" style="background: transparent;"></iframe>' + 
					'</div>';

			}, ( this.delay * 1000 + 100 ) );
			
			this.sendPlaceView();
			
		}
		
	}
	
	var adsfin_event_listed = adsfin_event_listed ? adsfin_event_listed : false;
	
	if( ! adsfin_event_listed ) {
		
		window.addEventListener( "message", function( event ) { 
			
			var gh = function (o){
				var hh=-1;
				try{
					if ( o.offsetHeight ){ 
						hh = o.offsetHeight;
					} else if ( o.firstChild ){ 
						hh = o.firstChild.offsetHeight; 
					} else { 
						hh=0; 
					} 
				} catch( e ){}
				return( hh );
			}
			
			let json = null;
			
			if ( typeof event.data === 'object' ) {
				json = event.data;
			} else if ( typeof event.data === 'string' ) {
				if ( event.data.substring(0,1) == '{' ) {
					json =  JSON.parse( event.data );
				} 
			}
			
			if ( json && typeof json === 'object' ) {
				
				let adsfin_message = json.adsfin_message;
				
				if ( typeof adsfin_message === 'object' ) { 
					
					let render_id = adsfin_message.render_id;
					let action = adsfin_message.action;
					let banner = adsfin_message.banner;
					
					if ( typeof banner === 'object' ) {
						
						let place_id = banner.place_id;
						let banner_id = banner.banner_id;
						let advertiser_id = banner.advertiser_id;
						let campaign_id = banner.campaign_id;
						let scrollHeight = banner.scrollHeight;
						let cost_view = banner.cost_view;
						let a_rate = banner.a_rate;
						let variant = banner.variant;
						let setup_experiment_version = banner.setup_experiment_version;
						
						if ( render_id != undefined ) {
							
							let fr = document.getElementById( render_id );
							let place = ( typeof adsfin_places !== 'undefined' ) ? adsfin_places[ render_id ] : null;
							let is_over = false;
								if ( !place || place.render_id != render_id ) {
									place = ( typeof adsfin_overlay_0 !== 'undefined' ) ? adsfin_overlay_0 : null;
									is_over = true;
								}
									if ( !place || place.render_id != render_id ) {
										place = ( typeof adsfin_overlay_1 !== 'undefined' ) ? adsfin_overlay_1 : null;
										is_over = true;
									}
							
							if ( fr && place ) {
								
								place.banner_id = banner_id;
								place.advertiser_id = advertiser_id;
								place.campaign_id = campaign_id;
								place.cost_view = cost_view;
								place.a_rate = a_rate;	
								place.variant = variant;	
								place.setup_experiment_version = setup_experiment_version;	
								
								/* RENDER */
								if ( action == 'render' ) {
									
									if ( !is_over ) {
										
										auto_height = ( fr.getAttribute('data-height') == 'auto' );
										
										if ( auto_height ) {
											custom_height = parseInt( fr.parentNode.parentNode.style.maxHeight );
											if ( isNaN(custom_height) || ( custom_height < 250 ) ) {
												custom_height = place.gh( fr.parentNode.parentNode ); 
											}
										} else {
											custom_height = parseInt( fr.getAttribute('data-height') );
												custom_height = isNaN( custom_height ) ? 0 : custom_height;
										}
										
										if ( custom_height >= 250 && custom_height <= 800 ) {
											fr.style.height = custom_height + 'px';
										} else {
											fr.style.height = banner.scrollHeight + 'px';
										}
										
										fr.style.minHeight = '250px';

									} 
									
									if ( !place.viewSended ) {
										if ( ( place.gy( fr ) >= window.scrollY ) && ( ( place.gy( fr ) + 250 ) <= ( window.scrollY + window.innerHeight ) ) ) {
											place.sendView();
											place.animateSended = true;
										}
									} 
								
								/* CLICK */
								} else if ( action == 'click' ) {
									
									let yametric = fr.getAttribute('data-yametric');
									let yatarget = fr.getAttribute('data-yatarget');
									
									if ( ( yametric != null ) && ( yametric != 'null' ) && ( yatarget != 'null' ) && ( yatarget != null ) && typeof ym === 'function' ) {
										try { ym( yametric, 'reachGoal', yatarget ); } catch (err) {}
									} 
									
								}
								
							}
							
						}
						
						if ( typeof adsfin_overlay_0 !== 'undefined' ) {
							adsfin_overlay_0.banner_id = banner_id;
						}							
						
					}
					
				}
				
			} 
			
		} );
		
		document.addEventListener( 'scroll', function( event ) {
			if ( typeof adsfin_places !== 'undefined'  ) for ( let render_id in adsfin_places ) {
				let place = adsfin_places[ render_id ];
				let  fr = document.getElementById( render_id );
				if ( ( place.gy( fr ) >= window.scrollY ) && ( ( place.gy( fr ) + 250 ) <= ( window.scrollY + window.innerHeight ) ) ) {
					setTimeout( ()=>{
						place.sendView();
						place.sendAnimate();
					}, 600 );
				} else {
					setTimeout( ()=>{
						place.clearAnimate();
					}, 600 );
				}
			}
		} );
		
	} 
		
	
	adsfin_event_listed = true;
	
	var last_place = new adsfin_place_obj( '111237839547682110', {"utm_source":"sms_mass"}, '111153736138910110', '109621571381628113', '0' );
		
