<%@ WebHandler Language="C#" Class="InsertMedia" %>

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.SessionState;

using Bitrix.Services.Js;

public class InsertMedia : IHttpHandler, IRequiresSessionState
{
    
    public void ProcessRequest (HttpContext context) 
	{
		HttpRequest request = context.Request;
		HttpResponse response = context.Response;    
		
		response.ContentType = "text/html";

		Dictionary<string, object> parameters = new Dictionary<string, object>();
		string url = request.QueryString["url"];
		if (string.IsNullOrEmpty(url))
		{
			response.StatusCode = 404;
			response.End();
		}
		parameters["url"] = url;
		parameters["flvskinfolderpath"] = "~/bitrix/controls/main/media.player/flv/skins";
		parameters["flvskinname"] = "bitrix.swf";
		parameters["abouttext"] = "1C-Bitrix Media Player";
        parameters["flvwmode"] = "transparent";
        parameters["wmvwmode"] = "windowless";
        
		int i;
		if (int.TryParse(request.QueryString["width"], out i) && i > 0)
			parameters["width"] = i + "px";
		if (int.TryParse(request.QueryString["height"], out i) && i > 0)
			parameters["height"] = i + "px";
		if (request.QueryString["nofullscreen"] != null)
			parameters["fullscreen"] = false;
		
		response.StatusCode = 200;
	
		string jsPathString = HttpRuntime.AppDomainAppVirtualPath;
		if (jsPathString == "/")
			jsPathString = string.Empty;
		jsPathString = BXJSUtility.Encode(jsPathString);

		response.Write(
@"<html>
<head>
	<script type=""text/javascript"">
		var bitrixWebAppPath = '" + jsPathString + @"'; 
		var APPPath = '" + jsPathString + @"';
		function ResizeIFrame()
		{
			if (!window.frameElement)			
				return;			
			window.frameElement.style.width = document.body.scrollWidth + 'px';			
			window.frameElement.style.height = document.body.scrollHeight + 'px';
		}				
	</script>
	<style type=""text/css"">
	body { margin: 0px; padding: 0px; border: 0px none; }	
	</style>	
"
		);
		RenderScript(response, VirtualPathUtility.ToAbsolute("~/bitrix/js/Main/utils.js"));
		RenderScript(response, VirtualPathUtility.ToAbsolute("~/bitrix/js/Main/utils_net.js"));
		RenderScript(response, VirtualPathUtility.ToAbsolute("~/bitrix/controls/main/media.player/wmv/silverlight.js"));
		RenderScript(response, VirtualPathUtility.ToAbsolute("~/bitrix/controls/main/media.player/wmv/wmvplayer.js"));
		RenderScript(response, VirtualPathUtility.ToAbsolute("~/bitrix/controls/main/media.player/js/player.js"));
		response.Write(
@"</head>
<body>
	<div id=""player""></div>
	<script type=""text/javascript"">
		window.setTimeout(function()
		{
			var player = Bitrix.MediaPlayer.create(
				'player',
				'player',
				Bitrix.MediaPlayerData.create("
		);
		response.Write(BXJSUtility.BuildJSON(parameters));
		response.Write(
@")
			);
			player.addActivationChangeListener(function(a, b)
			{		
				window.setTimeout(ResizeIFrame, 100);
			});
			player.activate();
		}, 100);
	</script>		
</body>
</html>"
		);
    }
	
	static void RenderScript(HttpResponse response, string path)
	{
		response.Write("\t<script src=\"");
		response.Write(HttpUtility.HtmlAttributeEncode(path));
		response.Write("\" type=\"text/javascript\"></script>\r\n");
	}
	
    public bool IsReusable 
	{
        get 
		{
            return true;
        }
    }

}