var isPopoverVisible = false;

function showPopover(options) {
	if (!isPopoverVisible) {
		$(document.body).append([
			"<div class='overlay-wrapper'><", "/div>"
		].join(""));
		$(".overlay-wrapper").css({ opacity: 0, position: 'fixed', top: 0, left: 0, right: 0, bottom: '36px', zIndex: 20 });
		$(".overlay-wrapper").append([
			// "<div class='overlay-bg'><", "/div>",
			"<div class='overlay-close'><", "/div>",
			"<div class='overlay-prev'><", "/div>",
			"<div class='overlay-next'><", "/div>",
			"<div class='overlay-body'>",
				"<iframe width='800' height='475' scrolling='no' src='about:blank'><" + "/iframe>",
			"<", "/div>"
		].join(""));
		$(".overlay-body iframe").css({ opacity: 0 });
	}
		
	var methods = {
		showContent: function(url) {
			$.get(url, function(r) {
				if (true || /MSIE [78]/.test(navigator.userAgent) || (/MSIE 9/.test(navigator.userAgent) && document.documentMode < 9))
				{
					var w = $(".overlay-body iframe")[0];
					w.onload = function() {
						var dh = $(this.contentWindow.document.body).height();
						var h = Math.max(475, dh);
						$('.overlay-body iframe').stop().animate({ opacity: 1, height: h }, { duration: 400 });
						$('.overlay-body').animate({ marginTop: (0 - (h / 2)) + 'px' });					
					}
					w.contentWindow.location.href = url;
					return;
				}
				
				var d = $(".overlay-body iframe")[0].contentWindow.document;
				d.location.href = url;
				
				d.open('text/html');
				if (url.indexOf('#') != -1) {
					d.location.hash = '#' + url.split('#').pop();
				}
				d.write(r);
				d.close();
				var dh = $(d.body).height();
				var h = Math.max(475, dh);
				$('.overlay-body iframe').stop().animate({ opacity: 1, height: h }, { duration: 400 });
				$('.overlay-body').animate({ marginTop: (0 - (h / 2)) + 'px' });
			});
		},
		getIFrameWindow: function() {
			return $(".overlay-body iframe")[0].contentWindow;
		},
		getIFrameDocument: function() {
			return this.getIFrameWindow().document;
		}
	}
	
	if (options.previous) {
		$(".overlay-prev").click(function() {
			options.previous(methods);
		});
	} else $('.overlay-prev').remove();
	if (options.next) {
		$(".overlay-next").click(function() {
			options.next(methods);
		});
	} else $('.overlay-next').remove();
	$(".overlay-close, .overlay-bg").click(function() {
		var d = $(".overlay-body iframe")[0].contentDocument;
		$("object, embed", d).css({ display: 'none' });
		$(".overlay-wrapper").animate({ opacity: 0 },  {duration: 400, complete: function() {
			$(".overlay-wrapper").remove();
		} });
		$("#container").css({ top: (0 - $(window).height()) + 'px' }).animate({ top: '0px' }, { duration: 800 });
		$("#bgImage").css({ opacity: 0 }).animate({ opacity: 1 }, { duration: 800 });
		$("#bgImagePopover").animate({ bottom: 0 - (2 * $(window).height()) }, { duration: 800 });
		isPopoverVisible = false;
	});
	options.init(methods);
	
	if (!isPopoverVisible) {
		$(".overlay-wrapper").animate({ opacity: 1 }, { duration: 400 });
		$("#container").css({position:'relative', top: '0'}).animate({ top: (0 - $(window).height()) + 'px' }, { duration: 800, complete: function() {
			$(this).css({ top: '-10000px' });
		} });
		$("#bgImage").animate({ opacity: 0 }, { duration: 800, complete: function() {
			// $(this).css({ height: '10000px' });
		} });
		$("#bgImagePopover").css({ bottom: 0 - (2 * $(window).height()) }).animate({ bottom: 0 }, { duration: 800 });
	}
	isPopoverVisible = true;
}

