﻿/// <reference name="MicrosoftAjax.js"/>
//var Players = new Array();

Type.registerNamespace('AdStream');

AdStream.ElementViewer =
    function ElementViewer(element) {
        AdStream.ElementViewer.initializeBase(this, [element]);
        this.ControlID = element.id;
        this.StoryboardPanel = null;
        this.StoryboardPanelSingle = null;
        this.vplayerContainer = null;
        this.messageContainer = null;
        this.messageControl = null;
        this.FileList = null;
        this.StoryBoardList = null;
        this.FlashPlayer = null;
        this.OnLoadHandler = null;
        this.Adbanked = false;
        this.CurrentViewingMode = 'Large';
        this.SizeModes = null;
        this.IsPrint = null;
        this.ViewFullscreenMessage = null;
    };

    AdStream.ElementViewer.prototype = {
        initialize: function () {
            AdStream.ElementViewer.callBaseMethod(this, "initialize");
            this.StoryboardPanel = $get(this.ControlID + '_StoryboardPanel');
            this.StoryboardPanelSingle = $get(this.ControlID + '_StoryboardPanelSingle');
            this.vplayerContainer = $get(this.ControlID + '_vplayerContainer');
            this.OnLoadHandler = Function.createDelegate(this, this.imageLoad);
        },
        init: function (fileList, adbanked, sizeModes,
                        currSizeMode, messageContainerID, messageControlID,
                        notAdbankedMessage, notAvailableMessage, playerLicence,
                        storyBoardList, events, isPrint, viewFullscreenMessage) {

            this.IsPrint = isPrint;
            this.ViewFullscreenMessage = viewFullscreenMessage;
            this.FileList = fileList;
            this.StoryBoardList = storyBoardList;
            this.SizeModes = sizeModes;
            this.CurrentViewingMode = currSizeMode;
            if (messageContainerID) {
                this.messageContainer = $get(messageContainerID);
            }
            if (messageControlID) {
                this.messageControl = $get(messageControlID);
            }
            this.FlashPlayer = $create(Adstream.FlashPlayer);
            this.Adbanked = adbanked;
            if (this.messageContainer != null) {
                Sys.UI.DomElement.setVisible(this.messageContainer, false);
            }
            if (events) {
                if ($.isFunction(events.initStoryBoardDone)) {
                    this.initStoryBoardDone = events.initStoryBoardDone;
                }
                if ($.isFunction(events.initStoryBoardSingleDone)) {
                    this.initStoryBoardSingleDone = events.initStoryBoardSingleDone;
                }
            }

            this.FlashPlayer.init(this.vplayerContainer.id, messageContainerID,
                messageControlID, notAdbankedMessage,
                notAvailableMessage, playerLicence, isPrint, viewFullscreenMessage);
        },
        initStoryBoard: function () {

        },
        initStoryBoardDone: function () {

        },
        initStoryBoardSingleDone: function () {

        },
        imageLoad: function () {
        },
        getPlayerHeight: function (file) {
            if (this.FileList != null) {

                var arrLength = this.FileList.length;
                var selectedFile = null;
                for (var i = 0; i < arrLength; i++) {
                    if (this.FileList[i].Path == file) {
                        selectedFile = this.FileList[i];
                        break;
                    }
                }
                if (selectedFile && selectedFile.AssetType == 'Audio') {
                    return 25;
                }

                if (this.SizeModes != null) {
                    var arrLength = this.SizeModes.length;
                    var selectedFile = null;
                    for (var i = 0; i < arrLength; i++) {
                        if (this.SizeModes[i].Mode == this.CurrentViewingMode) {
                            return this.SizeModes[i].Height;
                        }
                    }
                }
            }
            return 180;
        }, getPlayerWidth: function () {
            if (this.SizeModes != null) {
                var arrLength = this.SizeModes.length;
                var selectedFile = null;
                for (var i = 0; i < arrLength; i++) {
                    if (this.SizeModes[i].Mode == this.CurrentViewingMode) {
                        return this.SizeModes[i].Width;
                    }
                }
            }
            return 240;
        },
        getFileType: function (file) {
            if (this.FileList == null) {
                return null;
            }
            var arrLength = this.FileList.length;

            var selectedFile = null;
            for (var i = 0; i < arrLength; i++) {
                if (this.FileList[i].Path == file) {
                    selectedFile = this.FileList[i];
                    break;
                }
            }
            return selectedFile != null ? selectedFile.AssetType : null;
        },
        showFirstFile: function () {
            if (this.Adbanked != true) {
                this.FlashPlayer.showNotAdbankedMesssage();
                return;
            }
            if (this.FileList == null || this.FileList.length == 0) {
                this.FlashPlayer.showNotAvailableMesssage();
                return;
            }
            this.showFileEx(this.FileList[0].Path, false);
        },

        playFileList: function (autostart, showControls) {
            if (this.Adbanked != true) {
                this.FlashPlayer.showNotAdbankedMesssage();
                return;
            }

            if (this.FileList == null || this.FileList.length == 0) {
                this.FlashPlayer.showNotAvailableMesssage();
                return;
            }
            this.FlashPlayer.playFileList(this.FileList, this.getPlayerWidth(), this.getPlayerHeight(this.FileList[0]), autostart, showControls);
        },
        stop: function () {
            if (this.FlashPlayer && $.isFunction(this.FlashPlayer.stop)) {
                this.FlashPlayer.stop();
            }
        },
        showFileEx: function (file, autostart) {
            var showControls = true;
            if (this.Adbanked != true) {
                this.FlashPlayer.showNotAdbankedMesssage();
                return;
            }

            if (this.StoryboardPanel && this.StoryboardPanelSingle) {
                // Sys.UI.DomElement.setVisible(this.StoryboardPanel, false);
                this.showStoryBoard(file);
            }

            if (this.vplayerContainer) {
                Sys.UI.DomElement.setVisible(this.vplayerContainer, false);
            }
            if (this.messageContainer) {
                Sys.UI.DomElement.setVisible(this.messageContainer, false);
            }
            if (this.getFileType(file) == 'Image') {
                showControls = false;
            }

            if (file == "Storyboard") {
                this.FlashPlayer.showNotAvailableMesssage();
            }
            else {
                this.FlashPlayer.playFile(file, this.getPlayerWidth(), this.getPlayerHeight(file), autostart, showControls, this.getFileType(file) != "Audio");

            }
        },
        showFile: function (file) {
            this.showFileEx(file, true);
        },
        showStoryBoard: function (file) {
            if (this.StoryBoardList != null) {
                var arrLength = this.StoryBoardList.length;
                var selectedStoryDoard = null;
                for (var i = 0; i < arrLength; i++) {
                    if (this.StoryBoardList[i].Path == file) {
                        selectedStoryDoard = this.StoryBoardList[i];
                        break;
                    }
                }

                $(this.StoryboardPanel).hide();
                $(this.StoryboardPanelSingle).hide();

                if (selectedStoryDoard != null) {
                    if (selectedStoryDoard.ImageCount == 1) {
                        var $img = $(this.StoryboardPanelSingle).find('#SBImg');
                        var sbImage = selectedStoryDoard.Images[0];
                        $img.attr("src", sbImage.ImageUrl);
                        $img.attr("alt", sbImage.AlternateText);
                        if (sbImage.Width > 0 && sbImage.Height > 0) {
                            $img.css("width", sbImage.Width).css("height", sbImage.Height);
                        }

                        $(this.StoryboardPanelSingle).fadeIn(700);

                        if (this.initStoryBoardSingleDone) this.initStoryBoardSingleDone();
                    } else {
                        $(this.StoryboardPanel).find('img').each(function (index, e) {
                            if (index <= selectedStoryDoard.ImageCount - 1
                                && selectedStoryDoard.Images != null
                                && selectedStoryDoard.Images[index] != null) {
                                var sbImage = selectedStoryDoard.Images[index];
                                $(e).attr("src", sbImage.ImageUrl);
                                $(e).attr("alt", sbImage.AlternateText);
                                if (sbImage.Width > 0 && sbImage.Height > 0) {
                                    $(e).css("width", sbImage.Width).css("height", sbImage.Height);
                                }
                            } else {
                                $(e).hide();
                            }
                        });

                        $(this.StoryboardPanel).fadeIn(700);

                        if (this.initStoryBoardDone) this.initStoryBoardDone();
                    }
                }
            }
        },
        getViewingMode: function () {
            return this.CurrentViewingMode;
        },
        setViewingMode: function (mode) {
            this.CurrentViewingMode = mode;
            if (typeof (this.FileList[0]) != 'undefined') {
                this.FlashPlayer.setPlayerSize(this.getPlayerWidth(), this.getPlayerHeight(this.FileList[0].Path), mode);
            }
        },
        toggleViewingMode: function (mode) {
            if (mode == 'Large' || mode == 'Small') {
                /*set mode if it has been specified*/
                this.CurrentViewingMode = mode;
            }
            else {
                /*toggle View mode, if mode parameter doesn't contain correct value*/
                if (this.CurrentViewingMode == 'Large') {
                    this.CurrentViewingMode = 'Small';
                }
                else {
                    this.CurrentViewingMode = 'Large';
                }
            }
            if (typeof (this.FileList[0]) != 'undefined') {
                this.FlashPlayer.setPlayerSize(this.getPlayerWidth(), this.getPlayerHeight(this.FileList[0].Path), this.CurrentViewingMode);
            }
        },
        dispose: function () {
            this.StoryboardPanel = null;
            this.StoryboardPanelSingle = null;
            this.vplayerContainer = null;
            this.messageContainer = null;
            this.messageControl = null;
            this.FileList = null;
            this.StoryBoardList = null;
            this.CurrentViewingMode = null;
            this.SizeModes = null;
            if (this.FlashPlayer != null) {
                this.FlashPlayer.dispose();
            }
            AdStream.ElementViewer.callBaseMethod(this, "dispose");
        }
    }

AdStream.ElementViewer.registerClass('AdStream.ElementViewer', Sys.UI.Control, Sys.IDisposable);

function createElementViewer(control) {
    $create(AdStream.ElementViewer, {}, {}, {}, control);
}

//Added to satisfy new notifyScriptLoaded() requirement
if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();


