﻿var popupStatus = 0;

//loading popup with jQuery magic!
function loadPopup() {
    //loads popup only if it is disabled
    if (popupStatus == 0) {
        $("#backgroundPopup").css({
            "opacity": "0.7"
        });
        $("#backgroundPopup").fadeIn("slow");
        $("#popupContact").fadeIn("slow");
        popupStatus = 1;
    }
}

//centering popup
function centerPopup() {
    //request data for centering
    var windowWidth = $(window).width();
    var windowHeight = $(window).height(); 
    var popupHeight = $("#popupContact").height();
    var popupWidth = $("#popupContact").width();
    //centering
    $("#popupContact").css({
        "position": "absolute",
        "top": windowHeight / 2 - popupHeight / 2,
        "left": windowWidth / 2 - popupWidth / 2
    });

    var docHeight = $(document).height();
    $("#backgroundPopup").css({
        "height": docHeight
    });

}

//disabling popup with jQuery magic!
function disablePopup() {
    //disables popup only if it is enabled
    if (popupStatus == 1) {
        $("#backgroundPopup").fadeOut("slow");
        $("#popupContact").fadeOut("slow");
        popupStatus = 0;
    }
}

//Press Escape event!
$(document).keypress(function(e) {
    if (e.keyCode == 27 && popupStatus == 1) { disablePopup(); }
});

function setHtmlOnly() {
    setCookie("enableSilverlight", "false", 28);
    if (popupStatus == 1) {
        disablePopup();
    }
    else {
        document.location = "/Pages/default.aspx";
    }
}

function setSilverlight() {
    if (!Silverlight.isInstalled("2.0")) {
        loadPopup();
        centerPopup();
    } 
    else {
        setCookie("enableSilverlight", "true", 28);
        document.location = "/";
    }
}

Silverlight.onSilverlightInstalled = function() {
    setCookie("enableSilverlight", "true", 28);
    document.location = "/"; 
};

function clearSilverlight() {
    setCookie("enableSilverlight", "", 28);
    document.location = "/";
}

function setCookie(name, value, expiredays) {
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + expiredays);
    document.cookie = name + "=" + escape(value) + ((expiredays == null) ? "" : ";path=/;expires=" + exdate.toGMTString());
}

function getCookie(name) {
    if (document.cookie.length > 0) {
        start = document.cookie.indexOf(name + "=");
        if (start != -1) {
            start += name.length + 1;
            end = document.cookie.indexOf(";", start);
            if (end == -1) end = document.cookie.length;
            return unescape(document.cookie.substring(start, end));
        }
    }
    return "";
}
