// for ff
/*
if( !document.readyState ) {
	// this is necessary because gwt seems to 'hang'
	// for some reason, DOMContentLoaded event never fires
	// correctly in our iframe (need to investigate)
	try
	{
		document.readyState = 'complete';
	}
	catch( e ) {}
}
*/
//var __mrpListingJsCallbacks = [];

var componentManager = {
	lookup: {},
	adjustHeight: function() {
		if( typeof window.adjustIFrameHeight == "function" ) {
			adjustIFrameHeight();
		}
	},
	add: function( id, c ) {
		this.lookup[id] = c;
		var self = this;
		setTimeout( function() {
			self.adjustHeight();
		}, 500 );
	},
	resize: function( id, width, height ) {
		var c = this.lookup[id];
		if( c && ( typeof c.resize == 'function' ) ) {
			if( browser.isIE ) {
				width -= 10;
			}
			var self;
			c.resizeWithCallback( width, height, function() {
				self.adjustHeight();
			} );
		}
	},
	resizeWithCallback: function( id, width, height, callback ) {
		var c = this.lookup[id];
		if( c && ( typeof c.resizeWithCallback == 'function' ) ) {
			if( browser.isIE ) {
				width -= 10;
			}
			var self = this;
			c.resizeWithCallback( width, height, function() {
				callback();
				self.adjustHeight();
			} );
		}
	},
	remove: function( id ) {
		var c = this.lookup[id];
		if( c && ( typeof c.close == 'function' ) ) {
			try
			{
				c.animateAndClose();
				delete this.lookup[id];
				c = null;
				
				var self = this;
				setTimeout( function() {
					self.adjustHeight();
				}, 500 );
			}
			catch( e )
			{
			}
		}
	}
};

try
{
	if( parent && parent != window ) {
		parent.componentManager = componentManager;
	}
}
catch( e )
{
}

var listing_default_form_width = 550;
var listing_inquiry_form_height = 450;
var listing_send_form_height = 480;
var listing_mortgage_form_height = 450;

if( typeof subscribeToBus != 'undefined' ) {
	subscribeToBus( "form.show", null, function( s, m ) {
		showForm( m.url, m.whereId, m.how, m.width, m.height, m );
	}, null );
}

function showForm( url, whereId, how, width, height, opts )
{
try
{
	if( !opts ) {
		opts = {};
	}
	
	var hasParentLightBox = false;
	var doPopup = false;
	if( window.mrp_inline_markup ) {
		doPopup = (window == top );
	}
	
	if( how == "popup" ) {
		try
		{
			if( parent != window && parent.mrplst && parent.mrplst.ui ) {
				hasParentLightBox = true;
			}
		}
		catch( e )
		{
		}
		doPopup = true;
	}
	else if( how == "inline" ) {
		doPopup = false;
	}
	else {
		try
		{
			if( parent != window && parent.mrplst && parent.mrplst.ui ) 
			{
				doPopup = true;
				hasParentLightBox = true;
			}
		}
		catch( e ) {}
		
		try
		{
			if( parent != window && parent._mrpMobileIDX ) {
				doPopup = true;
			}
		}
		catch( e ) { }
	}
	var container = null;
	var idx = -1;
	if( url ) {
		idx = url.indexOf( "?" );
		if( idx == -1 ) {
			url += "?";
		}
		else {
			url += "&";
		}
		url += "ofid=" + whereId;
	}
	
	if( doPopup && hasParentLightBox ) {
		container = new parent.mrplst.ui.LightBox();
		var id = container.show( url, null, width, height );
		if( !parent.componentManager ) {
			parent.componentManager = componentManager;
		}
		parent.componentManager.add( whereId, container );
		
		if( opts["white-border"] ) {
			var e = parent.document.getElementById( id );
			if( e ) {
				e.style.border = "10px solid white";
			}
		}
		if( opts["auto-close"] ) {
			container.setAutoClose(true);
		}
	}
	else if( doPopup && !hasParentLightBox ) {
		container = new mrplst.ui.LightBox();
		if( parent._mrpMobileIDX || opts["tablet"] ) {
			container.setTabletMode( true );
		}
		var id = container.show( url, null, width, height );
		componentManager.add( whereId, container );
		
		if( opts["white-border"] ) {
			var e = document.getElementById( id );
			if( e ) {
				e.style.border = "10px solid white";
			}
		}
		if( opts["auto-close"] ) {
			container.setAutoClose(true);
		}
	}
	else {
		container = new mrplst.ui.LightBox();
		var where = document.getElementById( whereId );
		if( where ) {
			while( where.firstChild != null ) {
				where.firstChild.parentNode.removeChild( where.firstChild );
			}
			if( opts["scrollTo"] ) {
				scrollToAnchor( opts["scrollTo"] );
			}
			//var xy = getXY( where );
			//if( opts["scroll"] ) {
				//window.scrollTo( 0, xy[1] - 150 );
			//}
		}
		if( browser.isIE ) {
			width -= 10;
		}
		container.show( url, whereId, width, height );
		componentManager.add( whereId, container );
	}
}
catch( e )
{
	//alert( e.message );
}
}

function showMediaCenter( cfg, where, what, how, configURL )
{
	if( !__mediaCenterAvailable ) {
		return;
	}
	if( __mc != null ) {
		__mc.dispose();
		__mc = null;
	}
	if( !where ) {
		where = null;
	}
	if( !what ) {
		what = null;
	}
	
	var doPopup = false;
	if( window.mrp_inline_markup ) {
		doPopup = (window == top );
	}
	
	try
	{
		if( "popup" == how && parent != window && parent.mrplst && parent.mrplst.mc ) 
		{
			doPopup = true;
		}
	}
	catch( e ) {}
	
	if( doPopup ) {
		// global window
		__mc = new parent.mrplst.mc.MediaCenterPanel();
		if( cfg == null && configURL != null ) {
			__mc.getConfigAndShow( configURL, null, what, 0, 0 );
		}
		else {
			__mc.show( cfg, null, what, 0, 0 );
		}
		//__mc.show( cfg, null, what, 0, 0 );
		//__mc.fakeGetConfigAndShow( cfg, null, what, 0, 0 );
	}
	else {
		// framed
		__mc = new mrplst.mc.MediaCenterPanel();
		var mcId = null;
		if( cfg == null && configURL != null ) {
			mcId = __mc.getConfigAndShow( configURL, where, what, 0, 0 );
		}
		else {
			mcId = __mc.show( cfg, where, what, 0, 0 );
		}
		//var mcId = __mc.fakeGetConfigAndShow( cfg, where, what, 0, 0 );
		var m = document.getElementById( mcId );
		if( m != null /*&& cfg.overlayImageURL*/ ) {
			m.style.marginTop = "32px";
			
			var xy = getXY( m );
			if( xy ) {
				//window.scrollTo( 0, xy[1] );
			}
		}
		
		if( typeof window.adjustIFrameHeight == "function" ) {
			adjustIFrameHeight();
		}
		
		m = null;
	}
}
__mrpListingJsCallbacks.push( function() {
	__mediaCenterAvailable = true;
});

var __mrpJsAvailable = false;
function __onMrpListingsLoad()
{
	for( var i=0; i<__mrpListingJsCallbacks.length; ++i ) {
		try
		{
			__mrpListingJsCallbacks[i]();
		}
		catch( e )
		{
		}
	}
	__mrpJsAvailable = true;
	publishToBus( "mrp_js_listings.load", {} );
}