if( !window._mrp_tmpl2_inited ) {

	var __LazyLoad=function(a){function h(b,c){var e,d=a.createElement(b);for(e in c)c.hasOwnProperty(e)&&d.setAttribute(e,c[e]);return d}function i(a){var c,g,b=d[a];b&&(c=b.callback,g=b.urls,g.shift(),e=0,g.length||(c&&c.call(b.context,b.obj),d[a]=null,f[a].length&&k(a)))}function j(){var c=navigator.userAgent;b={async:a.createElement("script").async===!0},(b.webkit=/AppleWebKit\//.test(c))||(b.ie=/MSIE|Trident/.test(c))||(b.opera=/Opera/.test(c))||(b.gecko=/Gecko\//.test(c))||(b.unknown=!0)}function k(e,g,k,n,o){var s,t,u,v,w,x,p=function(){i(e)},q="css"===e,r=[];if(b||j(),g)if(g="string"==typeof g?[g]:g.concat(),q||b.async||b.gecko||b.opera)f[e].push({urls:g,callback:k,obj:n,context:o});else for(s=0,t=g.length;s<t;++s)f[e].push({urls:[g[s]],callback:s===t-1?k:null,obj:n,context:o});if(!d[e]&&(v=d[e]=f[e].shift())){for(c||(c=a.head||a.getElementsByTagName("head")[0]),w=v.urls.concat(),s=0,t=w.length;s<t;++s)x=w[s],q?u=b.gecko?h("style"):h("link",{href:x,rel:"stylesheet"}):(u=h("script",{src:x}),u.async=!1),u.className="lazyload",u.setAttribute("charset","utf-8"),b.ie&&!q&&"onreadystatechange"in u&&!("draggable"in u)?u.onreadystatechange=function(){/loaded|complete/.test(u.readyState)&&(u.onreadystatechange=null,p())}:q&&(b.gecko||b.webkit)?b.webkit?(v.urls[s]=u.href,m()):(u.innerHTML='@import "'+x+'";',l(u)):u.onload=u.onerror=p,r.push(u);for(s=0,t=r.length;s<t;++s)c.appendChild(r[s])}}function l(a){var b;try{b=!!a.sheet.cssRules}catch(c){return e+=1,void(e<200?setTimeout(function(){l(a)},50):b&&i("css"))}i("css")}function m(){var b,a=d.css;if(a){for(b=g.length;--b>=0;)if(g[b].href===a.urls[0]){i("css");break}e+=1,a&&(e<200?setTimeout(m,50):i("css"))}}var b,c,d={},e=0,f={css:[],js:[]},g=a.styleSheets;return{css:function(a,b,c,d){k("css",a,b,c,d)},js:function(a,b,c,d){k("js",a,b,c,d)}}}(this.document);

	window.mrp_jquery_ready = function( callback ) {
		if( window.mrp_jquery_loaded ) {
			callback();
		}
		else {
			document.addEventListener( "mrp_jquery_ready", callback );
		}
	};
	
	window.mrp_vendor_ready = function( callback ) {
		if( window.mrp_vendor_loaded ) {
			callback();
		}
		else {
			document.addEventListener( "mrp_vendor_ready", callback );
		}
	};
	
	window.mrp_v2_ready = function( callback ) {
	
		if( document.mrp_v2_loaded ) {
			callback();
		}
		else {
			document.addEventListener( "mrp_v2_ready", callback );
		}
	};
	
	window.mrp_v2_map_ready = function( callback ) {
	
		if( document.mrp_v2_map_loaded ) {
			callback();
		}
		else {
			document.addEventListener( "mrp_v2_map_ready", callback );
		}
	};

	window._mrp_tmpl2_inited = true;
	
			window.mrp_vowLoggedIn = false;

	function checkjQuery() {
	
		function checkVersion( ver ) {

			// if jQuery is present by version is not, we are past 3.x
			if( window.jQuery && !ver ) {
				return true;
			}

			if( !ver ) {
				return false;
			}
			var tks = ver.split( "." );
			if( tks.length == 0 ) {
				return false;
			}
			// noinspection RedundantIfStatementJS
			if( parseInt( tks[0] ) > 1 || (parseInt( tks[0] ) == 1 && parseInt( tks[1] ) >= 10 )) {
				return true;
			}
			else {
				return false;
			}
		}

		if( typeof jQuery == "undefined" ) {
			return false;
		}
		
		if( window.$ ) {
			// we shouldn't really be doing this, but some sites load multiple jQuery instances
			// and if these are different it wreaks havoc on our code; the 'onload' event ends up
			// firing on a different instance of jquery; one we don't have access to.
			window.jQuery = window.$;
		}
		return true;
	}
	
	function hasWebGL () {
		if( isIE() ) {
			return false;
		}
		try {
			var canvas = document.createElement("canvas");
			var gl = canvas.getContext("webgl") || canvas.getContext("experimental-webgl");
			return gl && gl instanceof WebGLRenderingContext;
		}
		catch( e ) {
			return false;
		}
	}
	
	function jQueryPromise( callback ) {
	
		function fireReady() {
			
			window.mrp_jquery_loaded = true;
			
			try {
				callback();
			}
			catch( e ) {
				console.error( e );
			}
			
			try {
				if(document.createEventObject) {
					document.fireEvent("mrp_jquery_ready");
				} 
				else {
					var evt = new Event("mrp_jquery_ready");
					//var evt = document.createEvent("HTMLEvents");
					//evt.initEvent("mrp_jquery_ready", false, true);
					document.dispatchEvent(evt);
				}
			}
			catch( e ) {
				console.error( e );
			}
		}
		
		if( !checkjQuery()  ) {
			__LazyLoad.js( '//res.myrealpage.com/wps/js/jquery-1.10.2.min.js', function() {
				fireReady();
			});
		}
		else {
			fireReady();
		}
	}
	
	function isIE() {

		var ua = window.navigator.userAgent;
		var msie = ua.indexOf("MSIE ");

		// noinspection RedundantIfStatementJS,RegExpRedundantEscape
		if (msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./))  // If Internet Explorer, return version number
		{
        	return true;
		}
		else  // If another browser, return 0
		{
        	return false;
		}
	}
	
	if( /loaded|complete/.test(document.readyState) ) {
		// too late
		console.log( "cannot use mrp scripts in a loaded document" );
	}
	else {
		var stage1_scripts = [];
		var stage2_scripts = [];
		var stage3_scripts = [];
		var stage4_scripts = [];
		
		var stage5_scripts = [];
		
		/*
		if( !checkjQuery()  ) {
			stage1_scripts.push( "//res.myrealpage.com/wps/js/jquery-1.10.2.min.js" );
		}
		*/
		
		
		const prevGlobalHistoryPushState = window.history.pushState;
		window.history.pushState = function( state, title, url ) {
			// this exists in order to account for a discrepancy in mrp
			// <base> tag and visible pathname. when invoked page('#!foo')
			// the browser's default impl will do /[base-tag-url]#!foo
			// but we need it go actually use /pathname#!foo
		
			// if condition below ensures there is a base tag and URL looks like MRP's
			if( url
				&& url.indexOf( "#!" ) === 0
				&& document.querySelector( "base" ) != null 
				&& window.location.pathname 
				&& window.location.pathname.indexOf( ".html" ) != -1 ) {
				url = window.location.pathname + url;
			}
			prevGlobalHistoryPushState.call( window.history, state, title, url );
		};
				
		var initNode = document.createElement( "script" );
		initNode.className = "mrp-init";
		initNode.type = "text/plain";
		initNode.setAttribute( "RES_URL", "//res.myrealpage.com/wps/" );
		initNode.setAttribute( "ROOT_URL", "//res.myrealpage.com/wps/" );
		initNode.setAttribute( "ACCOUNT_ID", "59872" );
		initNode.setAttribute( "CONTEXT", "mylistings" );
		initNode.setAttribute( "ROOT_PATH", "/wps" );
		initNode.setAttribute( "country", "CA" );
		initNode.setAttribute( "map", "ONTARIO" );
		initNode.setAttribute( "region", "ONTARIO" );
		initNode.setAttribute( "LISTINGS_SERVER", "listings.myrealpage.com" );

		initNode.setAttribute( "VOWP_IS", "");
		initNode.setAttribute( "VOWP_RT", "");
		initNode.setAttribute( "VOWP_RI", "");
		initNode.setAttribute( "VOWP_FF", "");
		initNode.setAttribute( "VOWP_DV", "");
		initNode.setAttribute( "VOW_SIGNUP_TYPE", "");

		document.body.appendChild( initNode );
				
		
		stage1_scripts.push( "//res.myrealpage.com/wps/js/ng/v2/listing-vendor-combined.js?v=7" );
				
		stage3_scripts.push( "//res.myrealpage.com/wps/js/ng/v2/listings/listing-common.js?v=102" );
		
		stage4_scripts.push( "//res.myrealpage.com/wps/js/ng/v2/listings/listing-vow.js?v=91" );
		stage4_scripts.push( "//res.myrealpage.com/wps/js/ng/v2/listings/listing-results.js?v=33" );
		stage4_scripts.push( "//res.myrealpage.com/wps/js/ng/v2/listings/listing-details.js?v=61" );
		
		
		function buildIdxURLs() {
		
			var idxURL = "//res.myrealpage.com/wps/js/ng/v2/listings/listing-idx-form.js?v=18";
			
			var isGoogleMap = ( window.location.hash && window.location.hash.indexOf( "googlemap=true" ) != -1 ) || window.googlemap_idx || !hasWebGL();
			
			var jsURLs = [];
			var cssURLs = [];
			
			if( !isGoogleMap ) {
				jsURLs.push( "https://api.tiles.mapbox.com/mapbox-gl-js/v0.54.1/mapbox-gl.js" );
				jsURLs.push( "//res.myrealpage.com/wps/js/ng/v2/listings/listing-idx-form-v2.js?v93" );
			}
			else {
				jsURLs.push( "//res.myrealpage.com/wps/js/ng/v2/listings/listing-idx-form.js?v=17" );
			}
			
			return { jsURLs: jsURLs, cssURLs: cssURLs };
		}
		
		
		if( !window.mrp_no_idx_scripts ) {
		
			var urls = buildIdxURLs();
			
			for( var i=0; i < urls.jsURLs.length; ++i ) {
				stage4_scripts.push( urls.jsURLs[i] );
			}
			
			if( urls.cssURLs.length > 0 ) {
				__LazyLoad.css( urls.cssURLs );
			}
		}	
		
		
		// https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css
		// brands font loaded from mrp-bootstrap.less
		// __LazyLoad.css( "//res.myrealpage.com/wps//fontawesome-free-5.4.1-web/css/all.css" );
			
		jQueryPromise( function() {
			__LazyLoad.js( stage1_scripts, function() {
			
				// mrp_vendor_ready
				window.mrp_vendor_loaded = true;
				try {
					if(document.createEventObject) {
						document.fireEvent("mrp_vendor_ready");
					} 
					else {
						var evt = new Event("mrp_vendor_ready");
						document.dispatchEvent(evt);
					}
				}
				catch( e ) {
					console.error( e );
				}
				
				__LazyLoad.js( stage3_scripts, function() {
					__LazyLoad.js( stage4_scripts, function() {
					
						_gmrp.accountId = 59872;
						
						_gmrp.loadBaseCSS = function( callback ) {
	
							var styles = [];
							styles.push( "//res.myrealpage.com/wps/less/v2/mrp-bootstrap.less?v1.2" );
							styles.push( "//res.myrealpage.com/wps/less/v2/listings/common.less?v4.1" );
							styles.push( "https://fonts.googleapis.com/css?family=Open+Sans:400italic,700,400" );
							
							_gmrp.MRP_IDX.loadStylesheets( styles, callback );
						};
						
						function loadPostMapbox( callback ) {
						
							function _done() {
								if(document.createEventObject) {
									document.fireEvent("mrp_v2_map_ready");
								} 
								else {
									var evt = new Event("mrp_v2_map_ready");
									//var evt = document.createEvent("HTMLEvents");
									//evt.initEvent("mrp_v2_ready", false, true);
									document.dispatchEvent(evt);
								}
								document.mrp_v2_map_loaded = true;
								
								if( callback ) {
									callback();
								}
							}
						
							if( window.mapboxgl ) {
								window.mapboxgl.accessToken = "pk.eyJ1IjoibXlyZWFscGFnZSIsImEiOiJjamd3dTBvd24wMDV3MnFuOXg2NTB6eDFrIn0.sKvYZK1dR9Jz7VUfTebaAA";
window._gmrp_geoFilter = function(item,index,items) {

	var overrides = {
		"Greenwood, Nova Scotia, Canada" : { // Greenwood, Nova Scotia
			center: [-64.934722, 44.971944],
			// sw, ne trigge 'mrp.debug.map.bounds' on document to dump
			bbox : [-64.98192887852869,44.95217585223236,-64.87687211583203,44.990491318266294]
		},
		"neighborhood.7899298212961420" : {
			// replacing "Scarborough City Centre, M1P 4P5, Etobicoke, Ontario, Canada"
			// with more appropriate "Scarborough, Toronto"
			text: "Scarborough, Toronto",
			place_name: "Scarborough, Toronto",
			center: [-79.3128433, 43.7589442 ],
			bbox: [ -79.36823308452246, 43.707401993492596, -79.09305989726784, 43.81241425080441 ]
		}
	};


	if( item.place_name ) {
		var override = overrides[item.place_name];
		if( override ) {
			item.center = override.center;
			item.bbox = override.bbox;
		}
		override = overrides[item.id];
		if( override ) {
			item.place_name = override.place_name;
			item.text = override.text || item.text;
			item.center = override.center;
			item.bbox = override.bbox;
		}
	}
	return true;
}
								
								_done();
							}
						}
						
						loadPostMapbox();
						
						_gmrp.loadMap = function( callback ) {
						
							callback = callback || function() {};
							
							if( document.mrp_v2_map_loaded ) {
								return callback();
							}
							
							var urls = buildIdxURLs();
							
							if( urls.cssURLs.length > 0 ) {
								__LazyLoad.css( urls.cssURLs );
							}
				
							if( urls.jsURLs.length > 0 ) {
								__LazyLoad.js( urls.jsURLs, function() {
									loadPostMapbox( callback );
								});
							}
						}
						
						_gmrp.settings.isWP = false;
						_gmrp.settings.hasRecip = true;
						_gmrp.settings.hasVow = true;
						_gmrp.settings.hasOmnibox = true;
						_gmrp.settings.defaultSoldOnOff = "";
						_gmrp.settings.defaultSoldRange = "";
						_gmrp.settings.vowName = "VIP Access";
						// FULL INIT

						if(document.createEventObject) {
							document.fireEvent("mrp_v2_ready");
						} 
						else {
							var evt = new Event("mrp_v2_ready");
							//var evt = document.createEvent("HTMLEvents");
							//evt.initEvent("mrp_v2_ready", false, true);
							document.dispatchEvent(evt);
						}
						
						//console.log( "ALL LOADED" );
						//document.dispatchEvent( new Event( "mrp_v2_ready" ) );
						document.mrp_v2_loaded = true;
						

						try {
							// mrpt
							var __s = document.querySelector( "[data-rel='mrpt']" );
    						if( !__s ) {
								var mrpt = document.createElement("script");
								mrpt.setAttribute("src", "/wps/js/_mrpt.js?1");
								mrpt.setAttribute("async", "");
								mrpt.setAttribute("data-rel", "mrpt");
								mrpt.setAttribute("data-a", "59872");
								document.body.appendChild(mrpt);
							}
							else {
								console.log( "_mrpt skipped; already exists" );
							}
						}
						catch( e ) {
						}
						
						if( window.location.href.indexOf( "showsignup" ) != -1 || window.location.hash == "#showsignup" ) {
							let params = _gmrp.Utils.getURLParams();
							_gmrp.loadBaseCSS( function() {
								var accountId = _gmrp.accountId;
								if( !accountId ) {
									return;
								}
								var form = new _gmrp.VowSignupForm( {
									accountId : accountId,
									prefill: params
								} ).open();
								
								console.log( "Params", params );
							});
						}
						
						else if( window.location.href.indexOf( "showlogin" ) != -1 || window.location.hash == "#showlogin" ) {
							if( document.querySelector('html').classList.contains('vow-ok')) {
								return;
							}
							let params = _gmrp.Utils.getURLParams();
							_gmrp.loadBaseCSS( function() {
								var accountId = _gmrp.accountId;
								if( !accountId ) {
									return;
								}
								var form = new _gmrp.VowLoginForm( {
									accountId : accountId,
									prefill: params
								} ).open();
								
								console.log( "Params", params );
							});
						}
									
						else if( window.location.href && window.location.href.indexOf( 'offmarket' ) != -1 ) {
							
							if( _gmrp.$( ".mrp-popup" ).length > 0 ) {
							 	// bill; not to conflict with default IDX one at times
							 	return;
							}
							if( _gmrp.$( ".sp-listing-details" ).length > 0 ) {
								// bill: not to conflict with single property listing details block
								return;
							}
							
							
							var styles = [];
							styles.push( "//res.myrealpage.com/wps/less/v2/mrp-bootstrap.less?v1.2" );
							styles.push( "//res.myrealpage.com/wps/less/v2/listings/common.less?v4.1" );
							styles.push( "https://fonts.googleapis.com/css?family=Open+Sans:400italic,700,400" );
							
							_gmrp.MRP_IDX.loadStylesheets( styles, function() {
								
								new _gmrp.MessageBox( "Sorry, the listing you are looking for is no longer available", {
										hasCloseButton : true,
										customClass: "offmarket-popup",
									}).show();
							});
						}
			
					}); // __LazyLoad.js( stage4_scripts
				}); // __LazyLoad.js( stage3_scripts
			});	// __LazyLoad.js( stage1_scripts
		}); // jQueryPromise
	}
}

