﻿var modalWindow = {
	parent: "body",
	windowId: null,
	content: null,
	left: null,
	top: null,
	width: null,
	height: null,
	hide: function () {
		$(".modal-window").css("display", "none");
		$(".modal-overlay").remove();
	},
	close: function () {
		$(".modal-window").remove();
		$(".modal-overlay").remove();
	},
	closeWithRefresh: function () {
		$(".modal-window").remove();
		setTimeout(delayedReload, 500);
	},
	open: function () {
		var modal = "";
		modal += "<div class=\"modal-overlay\"></div>";
		modal += "<div id=\"" + this.windowId + "\" class=\"modal-window\" style=\"display:none;left:" + this.left + ";top:" + this.top + ";width:" + this.width + "px; height:" + this.height + "px; margin-top:-" + (this.height / 2) + "px; margin-left:-" + (this.width / 2) + "px;\">";
		modal += this.content;
		modal += "</div>";

		$(this.parent).append(modal);

		$(".modal-overlay").click(function () { modalWindow.close(); });
	}
};

function delayedReload() {
	window.location.reload();
}

var openMyModal = function (source, left, top, width, height) {
	modalWindow.windowId = "myModal";
	if (left) { modalWindow.left = left; } else { modalWindow.left = '50%'; }
	if (top) { modalWindow.top = top; } else { modalWindow.top = '50%'; }
	modalWindow.width = width;
	modalWindow.height = height;
	modalWindow.content = "<iframe width='100%' height='100%' frameborder='0' allowtransparency='true' scrolling='no' src='" + source + "' OnLoad='OnWindowIsLoad();'></iframe>";
	modalWindow.open();
};

var OnWindowIsLoad = function () {
	$(".modal-window").css("display", "");
}

function informOnSignedIn() {
	if (typeof(onUserInfoChangeState) === "function") {
		onUserInfoChangeState();
	}
}

var OnAuthorized = function (email) {
	informOnSignedIn();
}

OnAuthorizedWithClose = function (email) {
	OnAuthorized(email);
	modalWindow.close();
	informOnSignedIn();
}

var Logout = function (myRoot) {
	modalWindow.close();
	openMyModal(myRoot + '/Pages/My/Logout.aspx', null, null, 517, 361);
}
