<%@ Reference Control="~/bitrix/components/bitrix/media.gallery.element.detail/component.ascx" %>
<%@ Control Language="C#" AutoEventWireup="false" Inherits="Bitrix.IBlock.Components.MediaGalleryElementDetailTemplate" %>
<%@ Import Namespace="System.Collections.Generic" %>
<%@ Import Namespace="Bitrix.UI" %>
<%@ Import Namespace="Bitrix.Services.Js" %>
<%@ Import Namespace="Bitrix.IBlock.Components" %>
<script runat="server">
    protected string GetPlayerID()
    {
        return string.Concat(Component.ClientID, "_Player");
    }
    protected string GetPlayerContainerID()
    {
        return "playerContainer";
    }

    protected string GetPlayerContainerStyle()
    {
        string screenColor = Component.PlayerScreenColor;
        return string.Format(
            "width:{0}; height:{1}; background-color:#{2};",
            Component.PlayerWidth,
            Component.PlayerHeight,
            !string.IsNullOrEmpty(screenColor) ? screenColor : "000000"
            );
    }    
    
    protected override void OnPreRender(EventArgs e)
    {
        base.OnPreRender(e);

        string playerScriptVirtualPath = "~/bitrix/controls/main/media.player/js/player.js";
        BXPage.RegisterScriptInclude("~/bitrix/js/Main/utils_net.js");

        BXPage.RegisterScriptInclude("~/bitrix/controls/main/media.player/wmv/silverlight.js");
        BXPage.RegisterScriptInclude("~/bitrix/controls/main/media.player/wmv/wmvplayer.js");
        BXPage.RegisterScriptInclude(string.Concat(VirtualPathUtility.ToAbsolute(playerScriptVirtualPath), "?t=", HttpUtility.UrlEncode(System.IO.File.GetLastWriteTimeUtc(Request.MapPath(playerScriptVirtualPath)).Ticks.ToString())));

        string playbackUrl = Component.ElementFileUrl;
        string downloadingUrl = string.Empty;
        if (Component.PlayerEnableDownloading) 
            downloadingUrl = Component.ElementPropertyForDownloadingFilePath;

        ScriptManager.RegisterStartupScript(
            this,
            GetType(),
            "setMessages",
            string.Format("Bitrix.MediaPlayer.messages = {{ installFlashPlayer:'{0}', couldntCreatePlayer:'{1}', couldntCreateFlashPlayer:'{2}', couldntCreateSilverlightPlayer:'{3}' }};",
                GetMessageJS("InstallFlashPlayer"),
                GetMessageJS("CouldntCreatePlayerErrorMsg"),
                GetMessageJS("CouldntCreateFlashPlayerErrorMsg"),
                GetMessageJS("CouldntCreateSilverlightPlayerErrorMsg")
                ), 
            true
            );        
        
        ScriptManager.RegisterStartupScript(
            this,
            GetType(),
            "register",
            string.Format(
            "Bitrix.MediaPlayer.create('{0}', '{1}', Bitrix.MediaPlayerData.create({{ file:'{2}', image:'{3}', flvwmode:'opaque', wmvwmode:'windowless', width:'{4}', height:'{5}', stretching:'{6}', bufferlength:{7}, controlpanelbackgroundcolor:'{8}', controlscolor:'{9}', controlsovercolor:'{10}', screencolor:'{11}', link:'{12}', linktarget:'{13}', showcontrolpanel:{14}, enabledownloading:{15}, fullscreen:{16}, volume:{17}, repeat:{18}, autostart:{19} }})).activate();",
                GetPlayerID(),
                GetPlayerContainerID(),
                BXJSUtility.JSHEncode(playbackUrl),
                BXJSUtility.JSHEncode(Component.ElementPlayerPreviewImageFileUrl),
                Component.PlayerWidth,
                Component.PlayerHeight,
				Component.PlayerStretching,
                Component.PlayerBufferLengthInSeconds,
                Component.PlayerControlPanelBackgroundColor,
                Component.PlayerControlsColor,
                Component.PlayerControlsOverColor,
                Component.PlayerScreenColor,
                string.IsNullOrEmpty(downloadingUrl) ? string.Empty : BXJSUtility.JSHEncode(downloadingUrl),
                string.IsNullOrEmpty(downloadingUrl) ? string.Empty : Component.PlayerDownloadingLinkTargetWindow,
                Component.PlayerShowControlPanel.ToString().ToLowerInvariant(),
                Component.PlayerEnableDownloading.ToString().ToLowerInvariant(),
                Component.PlayerEnableFullScreenModeSwitch.ToString().ToLowerInvariant(),
                Component.PlayerVolumeLevelInPercents.ToString(),
                Component.PlayerEnableRepeatMode.ToString().ToLowerInvariant(),
                Component.PlayerEnableAutoStart.ToString().ToLowerInvariant()             
                ),                
            true);                                       
    }         
</script>

<%
if (Component.ComponentErrors != MediaGalleryElementDetailComponent.ComponentError.ErrNone)
{ 
    string[] errorNames = Component.ComponentErrors.ToString().Split(',');
    int errorNamesCount = errorNames != null ? errorNames.Length : 0;
    for (int i = 0; i < errorNamesCount; i++)
    {
        %><span class="errortext"><%= GetMessage(string.Concat("MediaGalleryElementDetailComponent", errorNames[i]))%></span><br /><%
    }
}
else if (Component.Element != null)
{
	%><div class="catalog-element">
	
	<table width="100%" cellspacing="0" cellpadding="2" border="0">
	<tr>
	    <td width="100%" valign="top" colspan="2" align="left">
	        <div id="<%= GetPlayerContainerID() %>"  style="<%= GetPlayerContainerStyle() %>"></div>
	    </td>
    </tr>	
		<%
        foreach (MediaGalleryElementDetailComponent.ElementDetailProperty property in Component.Properties)
		{
			if (!String.IsNullOrEmpty(property.DisplayValue) && Component.ShowProperties.Contains(property.Code))
			{
				%><tr width="100%"><td width="0%" valign="top"><%=property.Name%>:</td><td width="100%" valign="top"><b><%=property.DisplayValue%></b></td></tr><%
			}
		}
		%>
	</table>
	<br />
	<%
	if (Component.Element.DetailText.Length > 0)
	{
		%><%= Component.Element.DetailText%><br /><%
	}
	else if (Component.Element.PreviewText.Length > 0)
	{
		%><%= Component.Element.PreviewText%><br /><%
	}
	
	%></div><%
} 
%>