/// <reference path="../../ScriptReference/jquery-1.5.2.js" />
/// <reference path="../../Scripts/Mediasite.Core.js"/>
/// <reference path="../../Scripts/Mediasite.Player.Core.js"/>
/// <reference path="../../Scripts/Mediasite.Player.Localization.js"/>
/*global Mediasite, Silverlight*/

Mediasite.namespace("MediaPlayer");

Mediasite.MediaPlayer.NewStandardViewHtml5 = function (showControls) {
    var $ = jQuery;
    var element;
    var self = this;

    var notifier = new Mediasite.MediaPlayer.ViewEventNotifier();
    this.AddListener = function (list) {
        notifier.AddListener(list);
    };

    this.CreateMediaElement = function () {
        var $mediaElementArea = $('#mediaElementArea');
        var $wrapper = $('<div id="MediaWrapper"><div id="FullScreenPrompt"/></div>').appendTo($mediaElementArea);

        element = $('<video id="MediaElement" webkit-playsinline>This browser does not support HTML5 video</video>').appendTo($wrapper);

        if (showControls) {
            element.attr('controls', 'controls');
        }

        // iOS and mobile devices are just... special.
        // Add a play button to the video area because the actual play button may be pretty small.
        var IsMobile = Mediasite.BrowserDetect.Mobile();
        if (IsMobile) {
            var playCover = $('<div class="iosPlayCover"></div>');
            var playButton = $('<button></button>')
                .text(Mediasite.Player.Localization.Buttons.Play)
                .appendTo(playCover);

            element.bind({
                'loadstart': function (e) {
                    playCover.appendTo($wrapper);
                    playCover.bind('touchstart', function (e) { e.stopPropagation(); });
                    playButton.bind({
                        'touchstart': function (e) {
                            e.stopPropagation();

                            if (element.size() > 0 && element.get(0).play) {
                                element.trigger('playCoverPressed');
                            }
                        }
                    });
                },
                'play': function (e) {
                    playCover.remove();
                }
            });
        }

        // Hack in a way for model to destroy and recreate the media element.
        element.get(0).Rebuild = function () {
            $('#MediaWrapper').remove();

            var newElement = self.CreateMediaElement();

            return newElement;
        };

        return element;
    };
};

Mediasite.MediaPlayer.NewStandardViewSilverlight = function (showControls) {
    var $ = jQuery;
    var element;
    var slCanvas;

    var notifier = new Mediasite.MediaPlayer.ViewEventNotifier();

    this.AddListener = function (list) {
        notifier.AddListener(list);
    };

    function afterCanvasInit() {
        var pluginContent = element.get(0).content;

        pluginContent.OnResize = function () {
            var media = pluginContent.findName('media');
            media.width = element.width();
            media.height = element.height();
        };
    }

    function waitForCanvasInit() {
        slCanvas = $('#MediaElement').get(0).content;
        if (!slCanvas || !slCanvas.findName("videoArea")) {
            setTimeout(waitForCanvasInit, 200);
        }
        else {
            afterCanvasInit();
        }
    }

    this.CreateMediaElement = function () {

        var versionRequired = '1.0';

        if (Silverlight.isInstalled("3.0.40624")) {
            versionRequired = "3.0.40723";  //force SL3 GDR 1 if RTM is found
        }

        Silverlight.createObjectEx({
            source: 'Javascript/Players/NewStandardPlayer/SilverlightMedia.xaml',
            parentElement: $('#mediaElementArea').get(0),
            id: 'MediaElement',
            properties:
            {
                width: $('#mediaElementArea').first().width(),
                height: $('#mediaElementArea').first().height(),
                background: "black",
                isWindowless: "true",
                framerate: "24",
                version: versionRequired
            },
            context: null
        });

        waitForCanvasInit();

        element = $('#MediaElement');



        //		// handle window resizes to keep MediaElement filling entire area
        //		$(window).resize(function () {
        //			var newWidth = $(this).width();
        //			var newHeight = $(this).height();

        //			// resize silverlight object
        //			$("#MediaElement").width(newWidth).height(newHeight);

        //			// resize mediaelement
        //			var media = $('#MediaElement').get(0).content.findName("media");

        //			if (media) {
        //				media.width = newWidth;
        //				media.height = newHeight;
        //			}
        //		});

        return element;
    };

      };

Mediasite.MediaPlayer.NewStandardViewWindowsMedia = function (mediaPlayerType) {
   var $ = jQuery;

   var notifier = new Mediasite.MediaPlayer.ViewEventNotifier();
   this.AddListener = function (list) {
      notifier.AddListener(list);
   };

   this.CreateMediaElement = function () {
      if (mediaPlayerType === Mediasite.Player.MediaPlayerType.Port25) {
      	// using windowless video breaks the layout, just use normal windowed and realize overlays will not work...
      	return $('<object id="MediaElement" type="application/x-ms-wmp"><param name="enableContextMenu" value="' + Mediasite.GlobalOptions.EnableContextMenuForPlayer + '"/><param name="stretchToFit" value="true"/><param name="windowlessVideo" value="false"/><param name="uiMode" value="none"/></object>').appendTo('#mediaElementArea');
      }
      else {
        // using windowless = true here

        var wm7Player = document.createElement("OBJECT");

        // Add the param elements before setting the classid attribute. This seems to make IE7 through IE9 happy.
        wm7Player.appendChild(makeParam('windowlessVideo', 'true'));
        wm7Player.appendChild(makeParam('uiMode', 'none'));
        wm7Player.appendChild(makeParam('enableContextMenu', 'true'));
        wm7Player.appendChild(makeParam('stretchToFit', 'true'));

        wm7Player.setAttribute("id", "MediaElement");
        wm7Player.setAttribute("classid", "clsid:6BF52A52-394A-11d3-B153-00C04F79FAA6");

        document.getElementById("mediaElementArea").appendChild(wm7Player);

        return $(wm7Player);
      }
    };



    function makeParam(name, value) {
        var p = document.createElement('param');
        p.setAttribute('name', name);
        p.setAttribute('value', value);
        return p;
    }

};
