(function($) {
	$.overlayIframe =  {
		webroot: false,
		visible: false,
		iframe: false,
		interval: false,
		ifdoc: false,
		createCookie: function() {
			var d = new Date();
			d.setTime(d.getTime()+2000);
			document.cookie = "Iframe=true; expires="+d.toGMTString()+"; path=/";
		},
		hide: function() {
			if (!this.visible) return true;
			var idb = this.iframe.contents().find('body');
			
			this.iframe.animate({height:150},false,false,function(){
				$(this).contents().find('div#body').hide();
			}).css('overflowY','scroll');
			this.visible = false;
		},
		show: function() {
			if (this.visible) return true;
			this.iframe.contents().find('div#body').show();
			this.iframe.animate({height:'100%'}).css('overflowY','scroll');
			this.visible = true;
		},
		load: function(href) {
			window.clearInterval($.overlayIframe.interval);
			$.overlayIframe.iframe.attr('src','http://'+document.location.host+href+'?iframe=true');
			$.overlayIframe.createCookie();
			document.location.hash = href;
		},
		checkLocation: function() {
			if (document.location.hash.replace(/^#/,"") != $.overlayIframe.ifdoc.location.pathname) {
					window.clearInterval($.overlayIframe.interval);
					$.overlayIframe.createCookie();
					$.overlayIframe.ifdoc.location.pathname = document.location.hash.replace(/^#/,"");
			}
		}
	};
	
	$.fn.overlayIframe = function(opts) {
		$.overlayIframe.webroot = opts.webroot;
		$.overlayIframe.iframe = $(this.get(0));
		$.overlayIframe.iframe.css({height:150});
		
		$.overlayIframe.iframe.load(function() {
			var $ifmd = $(this).contents();
			
			$.overlayIframe.ifdoc = $ifmd.get(0);
			if (document.location.hash.replace(/^#/,"") != $.overlayIframe.ifdoc.location.pathname) {
				document.location.hash = $.overlayIframe.ifdoc.location.pathname;
			}
			
			$.overlayIframe.interval = window.setInterval($.overlayIframe.checkLocation,500);

			$ifmd.find('html').click(function(ev){
				if (this == ev.target || $(ev.target).hasClass('showMap')) {
					if ($.overlayIframe.visible) {
						$.overlayIframe.hide();
					} else {
						$.overlayIframe.show();
					}
				}
			});

			$ifmd.find('a').click(function(){
				window.clearInterval($.overlayIframe.interval);
				var rel = $(this).attr('rel');
				if ( rel == "ext" || rel == "thumb") return true;
				if (rel == "admin" || rel == "replace") 
						document.location.href = $(this).attr('href');
				$.overlayIframe.createCookie();
			});

			$ifmd.find('form').submit(function(){
				$.overlayIframe.createCookie();
			});
			
			$.overlayIframe.show();
		});

		if (document.location.pathname != $.overlayIframe.webroot) {
			// Need to reload page, move pathname to hash
			document.location = 'http://'+document.location.host+$.overlayIframe.webroot+'#'+document.location.pathname;
		} else if (document.location.hash == "") {
			// Load document root into iframe
			$.overlayIframe.load($.overlayIframe.webroot);
		} else {
			// Load location.hash into iframe
			$.overlayIframe.load(document.location.hash.substr(1));
		}

		
		return this;
	};
})(jQuery);
