﻿var popupStatus = 0;

function loadPopup() {
	if (popupStatus == 0) {
		$("#popupBackground").css({
			"opacity": "0.7"
		});
		$("#popupBackground").fadeIn("fast");
		$("#popupContainer").fadeIn("normal");
		popupStatus = 1;
	}
}

function disablePopup() {
	if (popupStatus == 1) {
		$("#popupContainer").fadeOut("fast");
		$("#popupBackground").fadeOut("normal");
		popupStatus = 0;
	}
}

function centerPopup() {
	var windowHeight = document.documentElement.clientHeight;
	var windowWidth = document.documentElement.clientWidth;
	var popupHeight = $("#popupContainer").height();
	var popupWidth = $("#popupContainer").width();
	//alert($(window).scrollTop() + ' ' + windowHeight + ' ' + popupHeight + ' ' + ((windowHeight / 2) - (popupHeight / 2) - 100));
	var top = ($(window).scrollTop() + ((windowHeight / 2) - (popupHeight / 2) - 100));
	if (top < $(window).scrollTop())
		top = $(window).scrollTop();
	//alert(top);
	$("#popupContainer").css({
		"position": "absolute",
		"top": top,
		"left": ((windowWidth / 2) - (popupWidth / 2))
	});
	$("#popupBackground").css({
		"height": $(document).height()
	});
}

function openAlert(heading, contents) {
    if (popupStatus == 0) {
        if (heading != '')
            $("#popupContent").html('<h3>' + heading + '</h3><p>' + contents + '</p>');
        else
            $("#popupContent").html('<p>' + contents + '</p>');
        $("#popupContainer").width("400px");
        centerPopup();
        loadPopup();

        $(window).resize(function () { centerPopup(); });
        $(window).scroll(function () { centerPopup(); });
    }
}

function openAlertWide(heading, contents) {
	if (heading != '')
		$("#popupContent").html('<h3>' + heading + '</h3><p>' + contents + '</p>');
	else
		$("#popupContent").html('<p>' + contents + '</p>');
	$("#popupContainer").width("650px");
	centerPopup();
	loadPopup();

	$(window).resize(function() { centerPopup(); });
	$(window).scroll(function() { centerPopup(); });
}

function openAlertWideTop(heading, contents) {
	if (heading != '')
		$("#popupContent").html('<h3>' + heading + '</h3><p>' + contents + '</p>');
	else
		$("#popupContent").html('<p>' + contents + '</p>');
	$("#popupContainer").width("650px");
	centerPopup();
	//$("#popupContainer").css({ "top": 10 });
	loadPopup();

	$(window).resize(function() { centerPopup(); });
	$(window).scroll(function() { centerPopup(); });
}

function openDownload(articleId) {
	var iframehtml = '<iframe id="downloadfram" src="/Site/Pages/Loading.html?articleId=' + articleId + '" frameborder="0" marginheight="0" marginwidth="0" scrolling="auto" style="width:380px;height:400px;border:0;padding:0;margin:0;"></iframe>';
	$("#popupContent").html(iframehtml);
	$("#popupContainer").width("400px");
	centerPopup();
	loadPopup();

	$(window).resize(function() { centerPopup(); });
	$(window).scroll(function() { centerPopup(); });
}

function openDownloadFormat(articleId, formatId) {
	var iframehtml = '<iframe id="downloadfram" src="/Site/Pages/Loading.html?articleId=' + articleId + '&formatId=' + formatId + '" frameborder="0" marginheight="0" marginwidth="0" scrolling="auto" style="width:380px;height:400px;border:0;padding:0;margin:0;"></iframe>';
	$("#popupContent").html(iframehtml);
	$("#popupContainer").width("400px");
	centerPopup();
	loadPopup();

	$(window).resize(function() { centerPopup(); });
	$(window).scroll(function() { centerPopup(); });
}

function openDownloadFormatLink(articleId, formatId, linkId) {
	var iframehtml = '<iframe id="downloadfram" src="/Site/Pages/Loading.html?articleId=' + articleId + '&formatId=' + formatId + '&linkId=' + linkId + '" frameborder="0" marginheight="0" marginwidth="0" scrolling="auto" style="width:380px;height:400px;border:0;padding:0;margin:0;"></iframe>';
	$("#popupContent").html(iframehtml);
	$("#popupContainer").width("400px");
	centerPopup();
	loadPopup();

	$(window).resize(function() { centerPopup(); });
	$(window).scroll(function() { centerPopup(); });
}

function openHtmlWidget(heading, contents, articleId) {
	var html = '';
	if (heading != '')
		html += '<h3>' + heading + '</h3>'
	if (contents != '')
		html += '<p>' + contents + '</p>';
	$("#popupContent").html(html + '<textarea id="widgetarea" style="width:395px;height:80px;font-family:Consolas,Courier New;font-size:12px;padding:0;margin:10px 0;border:1px solid #dddddd;"><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="405" height="144" id="Player" align="middle"><param name="movie" value="http://www.laudio.se/Site/Flash/FacebookPlayer.swf?articleId=' + articleId + '" /><param name="quality" value="high" /><param name="bgcolor" value="#ffffff" /><param name="play" value="true" /><param name="loop" value="true" /><param name="wmode" value="window" /><param name="scale" value="showall" /><param name="menu" value="true" /><param name="devicefont" value="false" /><param name="salign" value="" /><param name="allowScriptAccess" value="sameDomain" /><!--[if !IE]>--><object type="application/x-shockwave-flash" data="http://www.laudio.se/Site/Flash/FacebookPlayer.swf?articleId=' + articleId + '" width="405" height="144"><param name="movie" value="http://www.laudio.se/Site/Flash/FacebookPlayer.swf?articleId=' + articleId + '" /><param name="quality" value="high" /><param name="bgcolor" value="#ffffff" /><param name="play" value="true" /><param name="loop" value="true" /><param name="wmode" value="window" /><param name="scale" value="showall" /><param name="menu" value="true" /><param name="devicefont" value="false" /><param name="salign" value="" /><param name="allowScriptAccess" value="sameDomain" /><!--<![endif]--><a href="http://www.adobe.com/go/getflash"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /></a><!--[if !IE]>--></object><!--<![endif]--></object></textarea>');
	$("#popupContainer").width("400px");
	centerPopup();
	loadPopup();

	$("#widgetarea").focus();
	$("#widgetarea").select();

	$(window).resize(function () { centerPopup(); });
	$(window).scroll(function () { centerPopup(); });
}

$(document).ready(function() {
	$("#popupBackground").click(function() { disablePopup(); });
	$(document).keypress(function(e) {
		if (e.keyCode == 27 && popupStatus == 1)
			disablePopup();
	});
});
