jQuery(function() {
	jQuery(window).resize(resizeIframe);
	resizeIframe();
	iframeMgr.start;
})
function resizeIframe() {
	var currHeight = jQuery(window).height();
	var footerHeight = 86;
	var iframeTop = jQuery('#icontent').position()['top'];
	jQuery('#icontent').css('height', (currHeight - iframeTop - footerHeight));
}

var iframeMgr = {
	currUrl: "",
	currTitle: "",
	originalUrl: "",
	originalTitle: "",
	timer:null,
	start: function() {
		iframeMgr.originalUrl = iframeMgr.getOriginalUrl();
		iframeMgr.originalTitle = iframeMgr.getOriginalTitle();
		// determine if the user put in a url with a hash that has
		// a second url in it, if so reset the iframe's src attribute
		iframeMgr.processHashRedirect();
		// let's grab the current URL info
		try {
			iframeMgr.currUrl = iframeMgr.getCurrUrl();
		} catch (e) {
			// let's do nothing this first time around, let things trickle through
		}
		timer = setInterval(iframeMgr.checkUrl, 100);
	},
	checkUrl: function() {
		try {
			var currUrl = iframeMgr.getCurrentUrl();
			if (currUrl != iframeMgr.currUrl && currUrl != "") {
				if (currUrl == iframeMgr.originalUrl) {
					// clear out the hash, it is the original page
					var cleanUrl = location.href.replace(/#.*/, "");
					if (cleanUrl != location) {
						location.replace(cleanUrl);
					}
				} else {
					// new location, update accordingly
					location.replace("#" + currUrl);
				}
				iframeMgr.currUrl = currUrl;
			}
			iframeMgr.updateTitle();
		} catch (e) {
			// going to a different domain site, let's just move along
			// in hopes that they eventually return to the same domain,
			// but if not, no big deal, only checking 10 times per second.
		}
	},
	/**
	 * this gets the value from the iframe's src attribute
	 **/
	getOriginalUrl: function() {
		var origUrl = document.getElementById("icontent").src;
		if (origUrl == location) {
			// firefox bug: reports parent URL when no src is set on iframe
			origUrl = "";
		}
		return origUrl;
	},
	/**
	* this gets the value from the #iframe_title <h1> tag if any
	**/
	getOriginalTitle: function() {
		return document.getElementById("iframe_title").innerHTML;
	},
	/**
	* this attempts to get the value from the iframe's document.location
	**/
	getCurrentUrl: function() {
		var currUrl = window.frames[0].location.href;
		if (currUrl == "about:blank") {
			currUrl = "";
		}
		return currUrl;
	},
	updateTitle: function() {
		var titleHtml = document.getElementById("iframe_title");
		var newTitle = window.frames[0].document.title;
		if (newTitle != "" && titleHtml.innerHTML != newTitle) {
			// change the title of the page
			titleHtml.innerHTML = newTitle;
			document.title = newTitle;
		}
	},
	processHashRedirect: function() {
		var hashUrl = location.hash.replace(/#/, "");
		if (hashUrl != "") {
			// go ahead and reset the src of the iframe
			document.getElementById("icontent").src = hashUrl;
		}
	}
}