function __mrpInit()
{
	var s = document.getElementById( "mrpscript" );
	var p = s.parentNode;
	
	var div = document.createElement( "div" );
	
	var url = "http://listings.myrealpage.com/wps/myofficelistings/13511/";
	if( url.indexOf( '?' ) == -1 ) {
		url += "?_fcc=" + Math.random();
	}
	else {
		url += "&_fcc=" + Math.random();
	}
	
	try
	{
		var h = __mrp_getParamStartingWith( "http://" );
		
		if( h && ( h.indexOf( "http://listings.myrealpage.com/wps/" ) == 0 ||
			h.indexOf( "http://res1.myrealpage.com/wps/" ) == 0 ||
			h.indexOf( "http://idx.myrealpage.com/wps/" ) == 0 ||
			h.indexOf( "http://idx-beta.myrealpage.com/wps/" ) == 0 ||
			h.indexOf( "http://reference.myrealpage.com:8080/wps/" ) == 0  ) ) {
			url = h;
		}
	}
	catch( e )
	{
		//alert( e.message );
	}
	
	var iframeWidth = 560;
	if( url && url.indexOf( "facebook~" ) == -1 ) {
		iframeWidth += 30;
	}
	
	div.innerHTML = "<iframe width='" + iframeWidth + "' height='700' name='__mrp_listings' " +
		"id='__mrp_listings' " +
		"marginheight='0' mrginwidth='0' hspace='0' vspace='0' " +
		"frameborder='0' scrolling='no' ALLOWTRANSPARENCY='false' " +
		"class='mrp-listings-iframe' " +
		"src='" + url + "'></iframe>";
	p.insertBefore( div, s );
	
	var divH = document.createElement( "div" );
	divH.style.position = "absolute";
	divH.style.left = "-1000px";
	divH.style.top = "0";
	
	divH.innerHTML = "<iframe width='0' height='0' marginheight='0' " +
		"frameborder='0' marginwidth='0' hspace='0' vspace='0' scrolling='no' " +
		"name='__mrp_comm' " +
		"src='http://listings.myrealpage.com/wps/myofficelistings/13511/idx-comm.form?" +
		escape(window.location.href) +
		"'></iframe>";
		
	p.insertBefore( divH, s );
	
	var ngPres = document.createElement( "script" );
	ngPres.id = "__mrp_ng_pres";
	ngPres.src = "http://listings.myrealpage.com/wps/-/js~1/rest/13511/res/listing-presentation.js?x1";
	ngPres.type = "text/javascript";
	
	window.__mrpListingPresInited = false;
	
	if( typeof ngPres.onreadystatechange != "undefined" ) {
		ngPres.onreadystatechange = function() {
			if( ( this.readyState == 'loaded' || this.readyState == 'complete' ) && window.__mrpListingPresInited === false ) {
				window.__mrpListingPresInited = true;
				mrpInitEmbed();
			}
		};
	}
	else {
		ngPres.onload = function() {
			window.__mrpListingPresInited = true;
			mrpInitEmbed();
		};
	}
	p.appendChild( ngPres );
	
}

setTimeout( __mrpInit, 100 );

var __mrp_lastHash = null;
var __mrp_lastURL = null;

function __mrp_getParamStartingWith( s )
{
	var hash = unescape(window.location.hash);
	if( hash && hash.indexOf( "#" ) == 0 ) {
		hash = hash.substring( 1 );
	}
	
	if( s.indexOf( "http://" ) == 0 ) {
		return hash;
	}
	var idx = hash.indexOf( "," );
	var tks = hash.split( "," );
	for( var i=0; i<tks.length; ++i ) {
		if( tks[i].indexOf( s ) == 0 ) {
			return tks[i];
		}
	}
	return null;
}

function __mrp_poll()
{
	try
	{
		var hash = __mrp_getParamStartingWith( "mrph-" );
		
		if( hash == null ) {
			return;
		}
		
		hash = hash.substring( "mrph-".length );
		
		if( hash == __mrp_lastHash ) {
			return;
		}
		
		__mrp_lastHash = hash;
		
		var h = parseInt( hash );
		if( !isNaN( h ) ) {
			document.getElementById( "__mrp_listings" ).style.height = h + "px";
		}
	}
	catch( e )
	{
	}
}

function __mrp_process_msg( msg )
{
	if( msg.origin && ( msg.origin.indexOf( "http://listings.myrealpage.com" ) == 0 ||
			msg.origin.indexOf( "http://res1.myrealpage.com" ) == 0 ||
			msg.origin.indexOf( "http://idx.myrealpage.com" ) == 0 ||
			msg.origin.indexOf( "http://idx-beta.myrealpage.com" ) == 0 ||
			msg.origin.indexOf( "http://reference.myrealpage.com:8080" ) == 0 ||
			msg.origin == ( "http://" + document.domain )
			 
			 ) ) {
			
		var d = msg.data;
		if( d && d.indexOf( "mrph-" ) == 0 ) {
			d = d.substring( "mrph-".length );
			var h = parseInt( d );
			if( !isNaN( h ) ) {
				document.getElementById( "__mrp_listings" ).style.height = h + "px";
			}
		}
	}
}

if( ( typeof window.postMessage == 'function' ) 
	/* IE8 > || ( typeof window.postMessage == 'object' ) */ ) {
	if (typeof window.addEventListener != 'undefined') { 
		window.addEventListener('message', __mrp_process_msg, false); 
	} else if (typeof window.attachEvent != 'undefined') { 
		window.attachEvent('onmessage', __mrp_process_msg); 
	}
}
else {
	setInterval( function() { __mrp_poll(); }, 500 );
}
