﻿$(function() {
    $(window).resize(function() {
        AdjustModal();
    });
    $("#modalWindow #exit").click(function() {
    CloseModal("modalContent");
    })
});

function ShowModal(playerUrl, videoUrl, containerId, autoSt) {
    EmbedVideo(playerUrl, videoUrl, containerId,autoSt);
    AdjustModal();
    ShowModalOverlay();
}

function ShowModalOverlay() {
    $("#modalOverlay").fadeIn("slow", ShowModalWindow);
}

function ShowModalWindow() {
    $("#modalWindow").fadeIn("fast");
}

function CloseModal(containerId) {
    Stop(containerId);
    RemoveVideo();
    $("#modalWindow").fadeOut("fast");
    $("#modalOverlay").fadeOut("slow");
}

function AdjustModal() {
    var winH = $(window).height();
    var winW = $(window).width();
    var bodyH =$("body").height();
    var bodyW = $("body").width();

    $("#modalOverlay").width((winW > bodyW ? winW : bodyW));
    $("#modalOverlay").height((winH > bodyH ? winH : bodyH));
    $("#modalOverlay").css({ opacity: 0.6 });

    $("#modalWindow").css('top', winH / 2 - $("#modalWindow").height() / 2);
    $("#modalWindow").css('left', winW / 2 - $("#modalWindow").width() / 2);
}

function EmbedVideo(playerUrl, videoUrl, containerId, autoSt) {

    jwplayer(containerId).setup({
        flashplayer: playerUrl,
        autostart: autoSt,
        file: videoUrl,
        height: "360",
        width: "640"
    });
}

function Play(containerId) {
    jwplayer(containerId).play();
}

function Stop(containerId) {
    jwplayer(containerId).stop();
}

function RemoveVideo() {
    $("#modalContent").empty();
}
