﻿if (typeof (__assetPopupTimer) == "undefined") {
    __assetPopupTimer = null;
}

function BindFaderClick($fader)
{
    $fader.click(function (event)
    {
        if (event && event.stopPropagation)
        {
            event.stopPropagation();
        }
    });
}

function BindCloseClick($popup, $close, faderId)
{
    $close.click(function (event)
    {
        HideAssetShortInfo($popup, faderId);
        if (event && event.stopPropagation)
        {
            event.stopPropagation();
        }
    });
}

function InitializeAssetPopup(linkId, assetId, faderId, showPopupNearPointer)
{
    var $link = $("#" + linkId);
    var $popup = $("div[linkId=" + linkId + "]");
    var popupId = $popup.attr("id");
    var $close = $popup.find(".close:first");
    var $head = $popup.find(".head:first");
    var $fader = $("#" + faderId);
    
    ReplaceLinkWithSpan($link, popupId, faderId, showPopupNearPointer);
    
    BindFaderClick($fader);
    BindCloseClick($popup, $close, faderId);
    Draggable.Bind($popup, $head);
}

function ReplaceLinkWithSpan($link, popupId, faderId, showPopupNearPointer) {
    var spanId = $link.attr("id") + "_span";
    var text = $link.html();
    $link.parent().append("<span id='" + spanId + "' class='block-center'>" + text + "</span>");
    var $span = $link.siblings("#" + spanId);
    $span.css("cursor", "url(" + zoomCursorUrl + "), pointer");
    $span.click(function () { ShowAssetShortInfo(popupId, false, faderId, null, null, showPopupNearPointer); return false; });
    $span.show();
    $link.remove();
}

function ShowAssetShortInfo(popupid, calledFromTimer, faderId, mouseX, mouseY, showPopupNearPointer)
{
    if (calledFromTimer == true)
    {
        var $popup = $("#" + popupid);

        /* there is possible to show popups only from timer callback */
        $("div .shortAssetInfopopup").hide();
        LoadAssetPopupContent($popup, popupid);
        SetAssetPopupPosition($popup, mouseX, mouseY, showPopupNearPointer)

        if (faderId)
        {
            $("#" + faderId).show();
            $("body").css("overflow", "hidden");
        }
        BindEscClick($popup, faderId);

        /*show popup*/
        $popup.fadeIn("fast");
    }
    else
    {
        ClearAssetTimer();
        __assetPopupTimer = setTimeout(function () { ShowAssetShortInfo(popupid, true, faderId, window.mouseXPos, window.mouseYPos, showPopupNearPointer); }, 200);
    }
}

function BindEscClick($popup, faderId)
{
    //activePlayerElement = $("#" + popupid + " .flash-player-control").get(0);

    $(document)
    .one('keyup', function (event)
    {
        var fullscreen = window.PlayerInFullscreen;
        if (event.keyCode == 27 && !fullscreen)
        {
            HideAssetShortInfo($popup, faderId);
        }
        else
        {
            BindEscClick($popup, faderId);
        }
    });
}

function LoadAssetPopupContent($popup, popupid)
{
    var $content = $popup.find("#" + popupid + "-content");
    if ($popup.attr("notinitialized") == "true")
    {
        if ($popup.attr("initializing") != "true")
        {
            $popup.attr("initializing", "true");
            var assetguid = $popup.attr("assetguid");
            $content.load(shortAssetPopupAjaxPageUrl + assetguid,
            function (responseContent)
            {
                $popup.removeAttr("notinitialized");
                $popup.removeAttr("initializing");
                var responseContent = $(responseContent).find(".clock-number").text();
                $popup.find(".title").text(responseContent);
            });
        }
    }
}

function SetAssetPopupPosition($popup, mouseX, mouseY, showPopupNearPointer)
{
    var marginLeft = 0;
    var marginTop = 0;
    var widgetWidth = 550;
    var widgetHeight = 350;
    var screenWidth = $(window).width();
    var screenHeight = $(window).height();

    if (showPopupNearPointer == true)
    {
        if (widgetWidth > screenWidth - mouseX)
        {
            marginLeft = (screenWidth - mouseX) - widgetWidth;
        }
        if (widgetHeight > screenHeight - mouseY)
        {
            marginTop = (screenHeight - mouseY) - widgetHeight;
        }

        $popup.css("left", mouseX);
        $popup.css("top", mouseY);
        $popup.css("margin-left", marginLeft);
        $popup.css("margin-top", marginTop);
    }
    else
    {
        $popup.css("left", screenWidth / 2);
        $popup.css("top", "100px");
        $popup.css("margin-left", "-275px");
        $popup.css("margin-top", "0");
    }
}

function HideAssetShortInfo($popup, faderId)
{
    ClearAssetTimer();
    if (faderId)
    {
        $("#" + faderId).hide();
        $("body").css("overflow", "auto");
    }
    var player = $popup.find(".flash-player-control").get(0);
    if(player && player.control)
    {
        var flashPlayer = player.control.FlashPlayer;
        if(flashPlayer && $.isFunction(flashPlayer.stop))
        {
            flashPlayer.stop();
        }
    }
    $popup.hide();
}

function ClearAssetTimer()
{
    try
    {
        clearTimeout(__assetPopupTimer);
    }
    catch (e) { /* ignore all errors */ }
}

function ShowAssetPlayer(playerId)
{
    var popupPlayer = $find(playerId);
    if (popupPlayer)
    {
        popupPlayer.showFirstFile();
        popupPlayer.toggleViewingMode('Small');
    }
}

/* this method binds ALL popup links and controls. It should be only in UpdatePanel's postback */
function RefreshAssetPopupBindings()
{
    $(".shortAssetInfopopup").each(function (index, item)
    {
        var $item = $(item);
        var linkid = $item.attr("linkid");
        var assetguid = $item.attr("assetguid");
        var id = $item.attr("id");
        var faderid = id + "-fader";

        if (linkid && assetguid && id && faderid)
        {
            InitializeAssetPopup(linkid, assetguid, faderid);
        }
    });
}

