


try
{
	if ( document.location.href == parent.document.location.href )
	{
		if ( !/\.(?:com|org)(?:\/(?:index.php)?)?(?:\?.*|$)/i.exec(document.location.href) )
		{
			var url_bits = document.location.href.split(/\//);
			parent.location = '/?' + url_bits[url_bits.length - 1];
		}
	}
}
catch ( err )
{
	//
}



function _initialize( b )
{
	var c = document.getElementById('d_left');
	var i = document.getElementById('i_content');
	var r = document.getElementById('d_right');

	_setAnchorActions(document.getElementById('d_links').childNodes);

	var hash_url = '';
	if ( parent.location.hash.length == 0 )
	{
		//
		// Set hash based on query string
		//
		var url = parent.location.href;
		if ( url.indexOf('?') )
		{
			var q  = url.substr(url.indexOf('?')+1);
			var ps = q.split(/&/);

			for ( var k = 0; k < ps.length; ++k )
			{
				if ( /\.php(?:\?|#|$)/.exec(ps[k]) )
				{
					hash_url = ps[k];
					parent.location.hash = hash_url;
					break;
				}
			}
		}

		if ( hash_url == '' )
		{
			document.getElementById('i_content').setAttribute('src','home.php');
		}
	}
	_monitorURL(hash_url);

	if ( GBrowserIsCompatible() )
	{
		window.gmap = new GMap2(document.getElementById('d_right'));
		window.gmap.addMapType(G_PHYSICAL_MAP);
		window.gmap.addControl(new GHierarchicalMapTypeControl());
		window.gmap.addControl(new GLargeMapControl());
		window.gmap.setCenter(new GLatLng(38.0625,-90.677068),5);
		GEvent.addListener(window.gmap,'moveend',function(){
			_setCookie('mapzoom',this.getZoom());
			_setCookie('mapcenter',this.getCenter().toString());
		});
	}
	else
	{
		parent.location = '#';
	}

	if ( document.cookie )
	{
		var m = /\bnavwidth=(\d+)/.exec(document.cookie);
		if ( m )
		{
			c.style.width                                       = m[1]                   + 'px';
			document.getElementById('d_left_resize').style.left = ( parseInt(m[1]) - 1 ) + 'px';
		}
	}

	//
	// Admittedly, this function seems pretty random, until you get IE trying to
	// push markers to parent frames.
	//
	window.gmap.rsAddIntersectionMarker = function ( i, c )
	{
		var m = new GMarker(new GLatLng(i.latitude,i.longitude));
		m.bindInfoWindow(DOM(c));
		this.addOverlay(m);

		if ( i.setMarker )
		{
			i.setMarker(m);
		}
		else
		{
			i.sandbox_marker = m;
		}
	};

	window.gmap.rsTriggerEvent = function ( obj, evt )
	{
		GEvent.trigger(obj,evt);
	};

	new RSPDragger(
		document.getElementById('d_left_resize'),
		function( d )
		{
			var _width      = c.offsetWidth + d
			_setCookie('navwidth',_width);
			c.style.width   = _width + 'px';
			r.style.width   = ( r.offsetWidth - d ) + 'px';
		}
	);

	window.onresize = function( v )
	{
		_width        = document.documentElement.clientWidth - c.offsetWidth - 1;
		if ( _width < 0 )
		{
			_width      = 0;
		}
		r.style.width  = _width + 'px';
		i.style.height = ( document.documentElement.clientHeight - ( 150 + 32 + 2 * 2 ) ) + 'px';

		//
		// Keep the map centering code happy.
		//
		parent.window.gmap.checkResize();
	};

	window.onresize();

	//
	// This document.cookie check is separate from the previous, so as to
	// instantiate our map with knowledge of size, etc.
	//
	if ( document.cookie )
	{
		var m = /\bmapzoom=(\d+)/.exec(document.cookie);
		var z = null;
		if ( m )
		{
			z = parseInt(m[1]);
		}

		m = /\bmapcenter=(\([\-\d\.\s\,]+\))/.exec(document.cookie);
		if ( m )
		{
			window.gmap.setCenter(eval('new GLatLng'+m[1]),z==null?window.gmap.getZoom():z);
		}
	}

	window.sandbox = {
		clean:           function( v )
			{
				var es = ['clean','manager','map','RSPPaneFactory','showPane','startLoading'];

				if ( !v )
				{
					this.map.clearOverlays();
				}

				this.manager.disposeAll();

				for ( var k in this )
				{
					if ( k.substr(0,1) == 'G' )
					{
						continue;
					}

					var b = false;
					for ( var i = 0; i < es.length; ++i )
					{
						if ( k == es[i] )
						{
							b = true;
							break;
						}
					}

					if ( !b )
					{
						delete this[k];
					}
				}
			},
		GClientGeocoder: GClientGeocoder,
		GDirections:     GDirections,
		GEvent:          GEvent,
		GLatLng:         GLatLng,
		GLatLngBounds:   GLatLngBounds,
		GMarker:         GMarker,
		GPolyline:       GPolyline,
		manager:         new RSPPaneManager(),
		map:             window.gmap,
		RSPPaneFactory:  new RSPPaneFactory(this.manager,document.getElementById('d_right')),
		showPane:        function ( id, attributes )
		{
			if ( !attributes.container )
			{
				attributes.container = document.getElementById('d_right');
			}

			return this.manager.getPane(id,attributes);
		},
		startLoading:    function ( id )
		{
			return new LoadingPane('LOADING_' + id,this.manager,document.getElementById('d_right'));
		}
	};

	//
	// WTF?
	//
	window.sandbox.RSPPaneFactory.manager = window.sandbox.manager;
}



function _getFormURLString( f )
{
	var s = '';

	for ( var i = 0; i < f.childNodes.length; ++i )
	{
		var g = f.childNodes[i];

		if ( g.childNodes && g.childNodes.length > 0 )
		{
			s += _getFormURLString(g);
		}

		if ( g.tagName && g.tagName.toLowerCase() == 'input' && g.getAttribute('name') )
		{
			s += escape(g.getAttribute('name')) + '=' + escape(g.value) + '&';
		}
	}

	return s;
}



function _monitorURL( h )
{
	//
	// Polling frequency, in ms
	//
	var p = 300;

	if ( window.location.hash != h )
	{
		if ( /(?:\/(?:index.php)?|index.php)(?:[\?#]|$)/i.exec(window.location.hash) )
		{
			window.location.hash = 'home.php';
		}

		//
		// set child location
		//
		h = window.location.hash;

		if ( document.getElementById('i_content').getAttribute('src') != h.substr(1) )
		{
			document.getElementById('i_content').setAttribute('src',h.substr(1));
		}
	}

	//
	// Break the function for now.
	//
	return;

	setTimeout(function()
	{
		_monitorURL(h);
	},p);
}



function _objectNVL ( o, n )
{
	if ( typeof o != 'object' )
	{
		o = {};
	}

	for ( k in n )
	{
		if ( typeof o[k] == 'undefined' )
		{
			o[k] = n[k];
		}
	}

	//
	// Unnecessary, as we're pass-by-reference
	//
	return o;
}



function _onload()
{
	parent.window.sandbox.clean();

	if ( !/(index.php|\/$)/.exec(window.location) )
	{
		parent.location.hash = '#' + window.location;
	}

	if ( typeof _iframeload != 'undefined' )
	{
		_iframeload();
	}
}



function _setAnchorAction( m )
{
	return;

	var f = m.onclick;
	var b = f ? true : false;

	/*
	m.setAttribute('target','_parent');

	var u = parent.location.href;
	var h = u.indexOf('#');
	if ( h > -1 )
	{
		u = u.substr(0,h);
	}
	m.setAttribute('href',u + '#' + m.getAttribute('href'));

	return;
	*/

	m.onclick = function ( event )
	{
		parent.location.hash = '#' + this.getAttribute('href');

		if ( b )
		{
			return f(event);
		}

		return true;
	};
}



function _setAnchorActions( n )
{
	for ( var i = 0; i < n.length; ++i )
	{
		var m = n[i];

		if ( m.tagName )
		{
			var t = m.tagName.toLowerCase();

			if ( t == 'a' )
			{
				_setAnchorAction(m);
			}
			else if ( t == 'form' )
			{
				_setFormAction(m);
			}
		}

		if ( m.childNodes && m.childNodes.length > 0 )
		{
			_setAnchorActions(m.childNodes);
		}
	}
}



function _setCookie ( key, value )
{
	var exp_date = new Date();
	exp_date.setTime(exp_date.getTime() + ( 90 * 24 * 3600 * 1000 ));
	document.cookie = key + '=' + value + '; expires=' + exp_date.toGMTString() + '; path=/';
}



function _setFormAction( m )
{
	var f = m.onclick;
	var b = f ? true : false;

	m.onclick = function ( event )
	{
		//
		// Consider making index.php dynamic, based on existing URL
		//
		parent.location.hash = '#' + this.getAttribute('action') + '?' + ( this.getAttribute('method') == 'get' ? _getFormURLString(this) : '' );

		if ( b )
		{
			return f(event);
		}

		return true;
	};
}



function _setNavActive ( nav_subject, id, active )
{
	//
	// hold onto this function for now, but it's before it's time
	//
	return;

	var on  = document.getElementById(id + '_active');
	var off = document.getElementById(id + '_inactive');

	if ( on && off )
	{
		on.style.display  = active ? 'block' : 'none';
		off.style.display = active ? 'none'  : 'block';
	}
}



function _shownav( e )
{
	var t = e.innerHTML;

	var d = e.parentNode;
	for ( var i = 0; i < d.childNodes.length; ++i )
	{
		if ( d.childNodes[i].tagName )
		{
			d.childNodes[i].removeAttribute('style');
		}

		var nav_title   = d.childNodes[i].innerHTML.replace(/<[^>]+>/,'');
		var nav_subject = document.getElementById('_' + nav_title);
		if ( nav_title == t )
		{
			nav_subject.style.display = 'block';
			_setNavActive(nav_subject,nav_title,true);
		}
		else
		{
			nav_subject.style.display = 'none';
			_setNavActive(nav_subject,nav_title,false);
		}
	}

	e.style.backgroundColor = 'white';
	e.style.borderWidth     = '2px 2px 0px 2px';
	e.style.paddingBottom   = '2px';
}



function ANav( t, o )
{
	this.offset = o;
	this.text   = t;
}



function makeNavDOM ( total, offset, per, clickHandler )
{
	if ( total + offset <= per )
	{
		return DOM([['span',null,' ']]);
	}

	var o   = offset / per;
	var nav = [];
	if ( offset > 0 )
	{
		nav.push(new ANav('\u25c4',parseInt(o-1)));
	}

	var min = Math.max(0,o-5);
	var max = Math.min(parseInt((total-1)/per),o+5);

	if ( min > 0 )
	{
		nav.push(new ANav('...'));
	}

	for ( var i = min; i <= max; ++i )
	{
		nav.push(new ANav((i+1)+'',i));
	}

	if ( max < parseInt((total-1)/per) )
	{
		nav.push(new ANav('...'));
	}

	if ( offset + per < total )
	{
		nav.push(new ANav('\u25ba',parseInt(o+1)));
	}
	return DOM([
		[
			'div',
			{
				style: {
					textAlign: 'center'
				}
			},
			nav.map(function(n)
			{
				if ( n.offset == o || n.text == '...' )
				{
					return [
						'span',
						{
							style:   {
								marginRight: '3px'
							}
						},
						n.text
					];
				}
				else
				{
					return [
						'a',
						{
							href:    '#',
							onclick: function ( event ) { clickHandler(n.offset * per); return false; },
							style:   {
								marginRight: '3px'
							}
						},
						n.text
					];
				}
			})
		]
	]);
}



//
// Helpful addenda
//
Array.prototype.filter = function ( f )
{
	var a = [];

	for ( var i = 0; i < this.length; ++i )
	{
		if ( f(this[i]) )
		{
			a.push(this[i]);
		}
	}

	return a;
};



Array.prototype.findIndex = function ( e )
{
	if ( !this.sorted )
	{
		this.rsSort();
	}

	var L = 0;
	var R = this.length;
	var M;

	while ( L <= R )
	{
		M     = parseInt((L+R)/2);
		var c = e.compare(this[M]);

		if ( c == 0 )
		{
			return M;
		}
		else if ( c < 0 )
		{
			R = M - 1;
		}
		else if ( c > 0 )
		{
			L = M + 1;
		}
	}

	if ( e.compare(this[M]) < 0 )
	{
		return M;
	}
	else
	{
		return M + 1;
	}
};



Array.prototype.insertSorted = function ( e, f )
{
	var i = this.findIndex(e);

	if ( e.compare(this[i]) == 0 && f )
	{
		f(this[i],e);
	}
	else
	{
		this.splice(i,0,e);
	}
};



Array.prototype.map = function ( f )
{
	var a = new Array();
	this.map_(function(e)
	{
		a.push(f(e));
	});
	return a;
};



Array.prototype.map_ = function ( f )
{
	for ( var i = 0; i < this.length; ++i )
	{
		f(this[i]);
	}
};



Array.prototype.rsSort = function ()
{
	this.sort(compareToComparator);
	this.sorted = true;
};



Math.range = function ( min, max )
{
	var a = [];

	for ( var i = min; i < max; ++i )
	{
		a.push(i);
	}

	return a;
};



Math.sign = function ( n )
{
	return n < 0 ? -1 : n > 0 ? 1 : 0;
};



function compareToComparator( a, b )
{
	if ( a == null && b == null )
	{
		return 0;
	}
	else if ( a == null )
	{
		return -1;
	}
	else if ( b == null )
	{
		return 1;
	}

	return a.compare(b);
};




