﻿var FOURROADS = FOURROADS ? FOURROADS : function() {
	// private functions and properties
	var _private =
			{
				loggedOutElement: null,
				loggedInElement: null,
				loggedOutMarkup: null,
				loggedInMarkup: null,

				copy: function(source, dest) {
					if (source && source.childNodes)
					// So, first we check if the object is not empty, if the object has child nodes
					{
						//clear contents of destination element
						_private.clear(dest);
						// Loop through children of source
						var children = source.childNodes;
						for (var i = 0; i < children.length; i++) {
							// clone each child of source element and copy to destination element
							var child = children[i].cloneNode(true);
							dest.appendChild(child);
						};
					};
				},

				clear: function(obj) {
					if (obj)
						while (obj.firstChild)
						obj.removeChild(obj.firstChild);
				},

				toggle: function(clearTarget, fillSource, fillTarget) {
					_private.clear(clearTarget);
					_private.copy(fillSource, fillTarget);
					if (clearTarget && clearTarget.style)
						clearTarget.style.display = 'none';
					if (fillTarget && fillTarget.style)
						fillTarget.style.display = 'block';
				}
			}

	var _public =
			{
				onLogin: function() {
					_private.toggle(_private.loggedOutElement, _private.loggedInMarkup, _private.loggedInElement);
					FB.XFBML.Host.parseDomTree();
				},
				onLogout: function() {
					_private.toggle(_private.loggedInElement, _private.loggedOutMarkup, _private.loggedOutElement);
					FB.XFBML.Host.parseDomTree();
				},
				setLoggedInElement: function(el) {
					_private.loggedInElement = el;
				},
				setLoggedOutElement: function(el) {
					_private.loggedOutElement = el;
				},
				init: function(loggedInElement, loggedOutElement) {
					_private.loggedOutElement = document.getElementById(loggedOutElement);
					_private.loggedInElement = document.getElementById(loggedInElement);
					if (_private.loggedInElement && _private.loggedOutElement) {
						_private.loggedOutMarkup = _private.loggedOutElement.cloneNode(true);
						_private.loggedInMarkup = _private.loggedInElement.cloneNode(true);
						_private.clear(_private.loggedInElement);
						_private.clear(_private.loggedOutElement);
					}
				},
				toggleDisplay: function(sourceElement, targetElement) {
					if (sourceElement && targetElement) {
						var style = sourceElement.style.display;

						sourceElement.style.display = targetElement.style.display;
						targetElement.style.display = style;
					}
				}
			};

	return _public;
} ();

// Dean Edwards/Matthias Miller/John Resig
function init() {
	// quit if this function has already been called
	if (arguments.callee.done) return;

	// flag this function so we don't do the same thing twice
	arguments.callee.done = true;

	// kill the timer
	if (_timer) clearInterval(_timer);

	// do our stuff
	if(typeof(domLoad) == 'function')
		domLoad();

};

/* for Mozilla/Opera9 */
if (document.addEventListener) {
	document.addEventListener("DOMContentLoaded", init, false);
}

/* for Internet Explorer */
/*@cc_on@*/
/*@if (@_win32)
document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
var script = document.getElementById("__ie_onload");
script.onreadystatechange = function() {
	if (this.readyState == "complete") {
		init(); // call the onload handler
	}
};
/*@end@*/

/* for Safari */
if (/WebKit/i.test(navigator.userAgent)) { // sniff
	var _timer = setInterval(function() {
		if (/loaded|complete/.test(document.readyState)) {
			init(); // call the onload handler
		}
	}, 10);
}

/* for other browsers */
window.onload = init;