﻿/// <reference name="MicrosoftAjax.js"/>

Type.registerNamespace("Adstream");
Adstream.FlashPlayer = function () {
    Adstream.FlashPlayer.initializeBase(this);
    this.FLASH_VERSION = "10.0.0";
    /* FLASH_PLAYER, FLASH_INSTALL and FLASH_AUDIO are global variables, defined in ScriptsStylesIncludingsControl */
    this.FLASH_PLAYER = FLASH_PLAYER;
    this.FLASH_INSTALL = FLASH_INSTALL;
    this.FLASH_AUDIO = FLASH_AUDIO;
    this.PlayerContainer = null;
    this.MessageContainer = null;
    this.MessageControl = null;
    this.NotAdbankedMessage = null;
    this.FileNotAvailableMessage = null;
    this.PlayerLicence = null;
    this.Player = null;
    this.IsPrint = false;
    this.ViewFullscreenMessage = null;
};

Adstream.FlashPlayer.prototype = {
    initialize: function () {
        Adstream.FlashPlayer.callBaseMethod(this, 'initialize');
        // Add custom initialization here
    },

    init: function (playerContaierId, messageContainerId, messageControlId, notAdbankedMsg, fileNotAvailableMsg, playerLicence, isPrint,
                    viewFullscreenMessage) {
        this.PlayerContainer = $get(playerContaierId);

        if (messageContainerId != null && messageContainerId.toString().length > 0) {
            this.MessageContainer = $get(messageContainerId);
        }

        if (messageControlId != null && messageControlId.toString().length > 0) {
            this.MessageControl = $get(messageControlId);
        }

        this.NotAdbankedMessage = notAdbankedMsg;
        this.FileNotAvailableMessage = fileNotAvailableMsg;
        this.PlayerLicence = playerLicence;
        this.IsPrint = isPrint;
        this.ViewFullscreenMessage = viewFullscreenMessage;
    },

    play: function (fileToPlay, assetAdbanked, playerWidth, playerHeight) {
        if (assetAdbanked != true) {
            this.showHideMesssage(this.NotAdbankedMessage);
            return;
        }
        if (fileToPlay != "") {
            this.showHideMesssage("");
            this.playFileWithFlash(fileToPlay, playerWidth, playerHeight, false, true);
        }
        else {
            this.showFileNotAvailable();
        }
    },
    playFile: function (fileToPlay, playerWidth, playerHeight, autostart, showControls, isVideo) {
        if (fileToPlay != "") {
            this.showHideMesssage("");
            this.playFileWithFlash(fileToPlay, playerWidth, playerHeight, autostart, showControls, isVideo);
        }
        else {
            this.showFileNotAvailable();
        }
    },
    showFileNotAvailable: function () {
        this.showHideMesssage(this.FileNotAvailableMessage);
    },
    stop: function () {
        if (this.Player && this.Player.isLoaded()) {
            this.Player.stop();
        }
    },
    playFileList: function (fileListToPlay, playerWidth, playerHeight, autostart, showControls) {
        if (fileListToPlay != null && fileListToPlay.length != 0) {
            this.showHideMesssage("");

            this.playFileListWithFlash(fileListToPlay, playerWidth, playerHeight, autostart, showControls);
        }
        else {
            this.showFileNotAvailable();
        }
    },

    controlsConfig: {
        url: FLASH_PLAYER_CONTROLS,
        bufferColor: '#6C9CBC',
        progressColor: '#2E7892',
        sliderColor: '#C1C1C2',
        timeSeparator: ' / ',
        autoHide: { enabled: false },
        timeFontSize: 11,
        timeBgColor: "#555555",
        timeColor: "#01DAFF",
        durationColor: "#FFFFFF",
        timeBorder: "1px solid rgba(0, 0, 0, 0.3)",
        backgroundColor: "#25353C",
        margins: [2, 2, 2, 2],
        buttonColor: "#FFFFFF",
        height: 25,
        volumeColor: "#000000",
        volumeSliderColor: "#000000",
        volumeSliderHeightRatio: 0.4,
        volumeBarHeightRatio: 1,
        timeBorderRadius: 10,
        timeBgHeightRatio: 0.8,
        scrubberHeightRatio: 0.4,
        scrubberBarHeightRatio: 1,
        tooltipColor: "#5F747C",
        tooltipTextColor: "#FFFFFF"
    },
    showPrint: function (containerId, file) {
        $("#" + containerId).html("<div class='preview-print'><a href='" + file + "' class='hidden' target='_blank'>" + this.ViewFullscreenMessage + "</a><img src='" + file + "' /></div>");
    },
    playFileWithFlash: function (fileToPlay, pWidth, pHeight, autostart, showControls, isVideo) {
        var _self = this;
        isVideo = isVideo || false;
        _self.showItem(_self.PlayerContainer);

        var id = _self.PlayerContainer.id;
        if (this.IsPrint) {
            this.showPrint(id, fileToPlay);
        }
        else {
            var player = $f(id, { src: _self.FLASH_PLAYER, wmode: 'transparent' }, {
                key: _self.PlayerLicence,
                clip: {
                    subTitle: '',
                    autoPlay: autostart,

                    //http://code.google.com/p/flowplayer-core/issues/detail?id=138
                    //https://jira.adstream.com/browse/BC-8959
                    autoBuffering: isVideo,
                    cachebusting: true,
                    url: fileToPlay,
                    title: '',
                    h264: fileToPlay,
                    scaling: "fit"
                },
                onFullscreen: function () { window.PlayerInFullscreen = 1; },
                onFullscreenExit: function () { setTimeout(function () { window.PlayerInFullscreen = null }, 1000); },
                plugins: {
                    audio: {
                        url: _self.FLASH_AUDIO
                    },
                    controls: showControls ? _self.controlsConfig : null
                }
            });

            player.html5({ html5_force: false });

            _self.setPlayerSize(pWidth, pHeight);
            _self.Player = player;
        }
    },
    playFileListWithFlash: function (fileListToPlay, pWidth, pHeight, autostart, showControls) {
        var _self = this;
        this.showItem(this.PlayerContainer);

        var id = this.PlayerContainer.id;
        if (this.IsPrint) {
            this.showPrint(id, fileListToPlay[0].Path);
        } else {
            var player = $f(id, { src: this.FLASH_PLAYER, wmode: 'transparent' }, {
                key: this.PlayerLicence,
                clip: {
                    subTitle: '',
                    autoPlay: autostart,

                    //http://code.google.com/p/flowplayer-core/issues/detail?id=138
                    //https://jira.adstream.com/browse/BC-8959
                    //autoBuffering: true,
                    cachebusting: true,
                    title: '',
                    scaling: "fit"
                },
                onFullscreen: function () { window.PlayerInFullscreen = 1; },
                onFullscreenExit: function () { setTimeout(function () { window.PlayerInFullscreen = null }, 1000); },
                plugins: {
                    audio: {
                        url: this.FLASH_AUDIO
                    },
                    controls: showControls ? _self.controlsConfig : null
                }
            });

            player.html5({ html5_force: false });
            this.setPlayerSize(pWidth, pHeight);

            var playListParams = "[";
            var len = fileListToPlay.length;
            for (i = 0; i < len; i++) {
                playListParams += "{url: '" + fileListToPlay[i].Path + "', title: ''}";
                if (i != len - 1)
                    playListParams += ",";
            }

            playListParams += "]";

            eval("player.play(" + playListParams + ")");
            this.Player = player;
        }
    },

    showHideMesssage: function (message) {
        if (this.MessageContainer == null || !message) {
            return;
        }
        this.hideItem(this.MessageContainer);
        if (message.toString().length > 0) {
            this.hideItem(this.PlayerContainer);
            if (this.MessageControl == null) {
                return;
            }
            this.setText(this.MessageControl, message);
            this.showItem(this.MessageContainer);
        }
        else {
            this.showItem(this.PlayerContainer);
        }
    },

    showNotAvailableMesssage: function () {
        if (this.MessageContainer == null) {
            return;
        }
        this.showItem(this.MessageContainer);
        this.hideItem(this.PlayerContainer);

        if (this.MessageControl == null) {
            return;
        }
        this.setText(this.MessageControl, this.FileNotAvailableMessage);
    },
    showNotAdbankedMesssage: function () {
        if (this.MessageContainer == null) {
            return;
        }
        this.showItem(this.MessageContainer);
        this.hideItem(this.PlayerContainer);
        if (this.MessageControl == null) {
            return;
        }
        this.setText(this.MessageControl, this.NotAdbankedMessage);
    },
    setText: function (control, text) {
        if (control && text) {
            $(control).text(text);
        }
    },
    showItem: function (item) {
        $(item).css("visibility", "visible").show();
    },
    hideItem: function (item) {
        $(item).css("visibility", "hidden").hide();
    },
    removeFlashPlayer: function () {
    },
    setPlayerSize: function (pWidth, pHeight, mode) {
        var $container = $(this.PlayerContainer).css("width", pWidth + "px");
        if (!this.IsPrint) {
            $container.css("height", pHeight + "px");
        } else {
            var $fullscreenLink = $container.find(".preview-print a");
            if (mode == 'Large') {
                $fullscreenLink.show();
            } else {
                $fullscreenLink.hide();
            }
        }
    },
    dispose: function () {
        //Add custom dispose actions here
        Adstream.FlashPlayer.callBaseMethod(this, 'dispose');
    }
};

Adstream.FlashPlayer.registerClass('Adstream.FlashPlayer', Sys.Component, Sys.IDisposable);

if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();

