﻿
var HIWOverlay = null;
var HIWOverlayActive = false;

// --
// Hides overlay and passed in element
function closeDialog(diag) {
	HIWOverlayActive = false;
	HIWOverlay
		.data("current-diag", diag)
		.animate({
			opacity: 0
		}, 200, null, function () {
			var jThis = $(this);
			jThis
				.css("display", "none")
				.data("current-diag")
					.css("display", "none");
		});
	}

// --
// Shows and centeres an element
function showDialog(diag) {
	var doc = $(window);

	diag
		.css("left", ((doc.width() / 2) - (diag.outerWidth() / 2)))
		.css("top", ((doc.height() / 2) - (diag.outerHeight() / 2)) + doc.scrollTop())
		.css("display", "block");
}

// --
// Shows overlay, and shows (and centers) passed in element
function openDialog(diag) {
	var jThis = $(this);
	var dialogSelector = jThis.attr("rel");

	try {
		if (!HIWOverlayActive) {
			HIWOverlayActive = true;

			// display overlay
			HIWOverlay
				.data("current-diag", diag)
				.css("opacity", 0).css("display", "block")
				.animate({
					opacity: .7
				}, 200, null, function () {
					showDialog($(this).data("current-diag"));
				});
		} else {
			HIWOverlay
				.data("current-diag")
					.css("display", "none");
			HIWOverlay
				.data("current-diag", diag);
			showDialog(diag);
		}
	} catch (ex) {
		alert(ex);
	}

	return false;
}

$(document).ready(function () {

	HIWOverlay =
		$("<div id='overlay-darken' />")
		.click(function () {
			closeDialog($(this).data("current-diag"));
		})
		.appendTo(document.body);

	$(".overlay > a.close").click(function () {
		closeDialog($(this).parent());
		return false;
	});

	$("a.read-more[rel], a.next[rel]").click(function () {
		openDialog($($(this).attr("rel")));
		return false;
	});

});
