﻿/* Do not remove this commented code. This is compiled by Expression Encoder at template compile time.



 
*/

// Global var
var customAutoPlay = false;
var expressionObj;

function rgbToHex(rgb)
{
	var strHex="0123456789ABCDEF";
	var ichBracket=rgb.indexOf("(");
	if (ichBracket!=-1) {
		var rgstr = rgb.substr(ichBracket+1, rgb.length-ichBracket-2).split(',');
		rgb = "#"+ strHex[parseInt(rgstr[0])>>4]+strHex[parseInt(rgstr[0])%16]+
			   strHex[parseInt(rgstr[1])>>4]+strHex[parseInt(rgstr[1])%16]+
			   strHex[parseInt(rgstr[2])>>4]+strHex[parseInt(rgstr[2])%16];
	}
	return rgb;
}


function StartPlayer_0(parentId) {
    try {
    	eval('document.body.style.backgroundColor="#000000"');
    } catch(e){}

    this._hostname = ExpressionPlayer.Player._getUniqueName("xamlHost");
    Silverlight.createObjectEx( {   source: 'player.xaml', 
                                    parentElement: $get(parentId ||"divPlayer_0"), 
                                    id:this._hostname, 
                                    properties:{ width:'100%', height:'100%', version:'1.0', background: 'Transparent', isWindowless: 'true', pluginbackground: 'Transparent',inplaceInstallPrompt:true }, 
                                    events:{ onLoad:Function.createDelegate(this, this._handleLoad) } } );
    this._currentMediainfo = -1;     
}

StartPlayer_0.prototype = {
    _handleLoad: function() {
        this._player = $create(ExtendedPlayer.Player,
                                  { // properties
                                      autoPlay: false,
                                      autoLoad: true,
                                      scaleMode: this.scaleModeParam(),
                                      muted: this.mutedParam(),
                                      enableCaptions: this.enableCaptionsParam(),
                                      volume: 1.0
                                  },
                                  { // event handlers
                                      mediaEnded: Function.createDelegate(this, this._onMediaEnded),
                                      mediaFailed: Function.createDelegate(this, this._onMediaFailed),
                                      playPreviousVideo: Function.createDelegate(this, this._onPlayPreviousVideo),
                                      playNextVideo: Function.createDelegate(this, this._onPlayNextVideo)
                                  },
                                  null, $get(this._hostname));
                                  
        this._playlist = [];
        try {
            var playText = 'this._playlist=[';

            for (var i = 1; i < vidply.length; i++) {
                if (i + 1 == vidply.length) {
                    // Last line - no comma
                    playText = playText + '{"mediaSource":"' + vidply[i][1] + '",' +
                                          '"placeholderSource":"",' +
		                                  '"chapters":' +
			                              '[' +
			                              ']}'
                }
                else {
                    playText = playText + '{"mediaSource":"' + vidply[i][1] + '",' +
                                          '"placeholderSource":"",' +
		                                  '"chapters":' +
			                              '[' +
			                              ']},'
                }
            }
            playText = playText + '];';
            eval(playText);

        }
        catch (e) { alert(e); }

        this._galleryItems = [];
        try {
            var galleryText = 'this._galleryItems=[';

            for (var j = 1; j < vidply.length; j++) {
                if (j + 1 == vidply.length) {
                    // Last line - no comma
                    galleryText = galleryText + 'new ExpressionPlayer.GalleryItem("' + vidply[j][2] + '","")';
                }
                else {
                    galleryText = galleryText + 'new ExpressionPlayer.GalleryItem("' + vidply[j][2] + '",""),';
                }
            }

            galleryText = galleryText + ']';

            eval(galleryText);
        }
        catch (e) { alert(e); }

        this._player.set_galleryInfo(this._galleryItems, Function.createDelegate(this, this._onClickGalleryItem));

        //setTimeout('this.playByQS();', 300);
        //this.playByQS();

        this._onPlayNextVideo(null, null);
    },

    _onClickGalleryItem: function(galleryItemIndex) {
        this._player.set_mediainfo(this._playlist[galleryItemIndex]);
        this._currentMediainfo = galleryItemIndex + 1;
    },

    _onMediaEnded: function(sender, eventArgs) {
        window.setTimeout(Function.createDelegate(this, this._onPlayNextVideo), 1000);
    },

    _onMediaFailed: function(sender, eventArgs) {
        alert(String.format(Sys.UI.Silverlight.MediaPlayer.Res.mediaFailed, this._player.get_mediaSource()));
    },


    _onPlayPreviousVideo: function(sender, eventArgs) {
        if (this._playlist != null) {
            if (this._currentMediainfo > 0) {
                this._player.set_mediainfo(this._playlist[--this._currentMediainfo]);
            }
        }
    },

    _onPlayNextVideo: function(sender, eventArgs) {
        if (this._playlist != null) {
            if (this._currentMediainfo < this._playlist.length - 1) {
                this._player.set_mediainfo(this._playlist[++this._currentMediainfo]);
            }
        }
    },

    autoLoadParam: function() {
        var autoLoad = true;
        try {
            eval("autoLoad=('True'!=='False')");
        } catch (e) { }
        return autoLoad;
    },

    autoPlayParam: function() {
        var autoPlay = false;
        try {
            eval("autoPlay=('True'!=='False')");
        } catch (e) { }
        return autoPlay;
    },

    scaleModeParam: function() {
        var scaleMode = 1/*Normal*/;
        try {
            eval("scaleMode=1;");
        } catch (e) { }
        return scaleMode;
    },

    enableCaptionsParam: function() {
        var enableCaptions = true;
        try {
            eval("enableCaptions=('True'!=='False');");
        } catch (e) { }
        return enableCaptions;
    },

    mutedParam: function() {
        var muted = false;
        try {
            eval("muted=('False'!=='False');");
        } catch (e) { }
        return muted;
    },

    playByQS: function(posn) {
        var argVal = $getArgument("v", 1);

        if (posn != null) {
            argVal = posn;
        }

        this._player.set_mediainfo(this._playlist[argVal - 1]);
        this._currentMediainfo = this.getVideoPos(argVal - 1);

        ///////////////////////// Custom code ///////////////////////////////////////////////////
        expressionObj.set_autoPlay($getArgument(ExpressionPlayer.UrlParam.autoplay, customAutoPlay));

        // After the first load all videos should autoplay
        customAutoPlay = true;
        ///////////////////////// End of Custom code ///////////////////////////////////////////////////
    },

    getVideoPos: function(argVal) {
        var pos = 0;    // Default position
        for (var i = 1; i < vidply.length; i++) {
            if (vidply[i][0] == argVal) {
                pos = i - 1;
                break;
            }
        }
        return pos;
    },

    playByChoice: function(selectedVideo) {
        var argVal = selectedVideo;

        if ((argVal > this._playlist.length - 1) || (argVal < 0))
            argVal = 0;

        this._player.set_mediainfo(this._playlist[argVal]);
        this._currentMediainfo = argVal + 1;
    }

}
function topStartPlayer_0(parentId) {
    try {
        eval('document.body.style.backgroundColor="#000000"');
    } catch (e) { }

    this._hostname = ExpressionPlayer.Player._getUniqueName("xamlHost");
    Silverlight.createObjectEx({ source: 'player.xaml',
        parentElement: $get(parentId || "topdivPlayer0"),
        id: this._hostname,
        properties: { width: '100%', height: '100%', version: '1.0', background: 'Transparent', isWindowless: 'true', pluginbackground: 'Transparent', inplaceInstallPrompt: true },
        events: { onLoad: Function.createDelegate(this, this._handleLoad)}
    });
    this._currentMediainfo = -1;

}
topStartPlayer_0.prototype = {
    _handleLoad: function() {
        this._player = $create(ExtendedPlayer.Player,
                                  { // properties
                                      autoPlay: true,
                                      autoLoad: this.autoLoadParam(),
                                      scaleMode: this.scaleModeParam(),
                                      muted: this.mutedParam(),
                                      enableCaptions: this.enableCaptionsParam(),
                                      volume: 1.0
                                  },
                                  { // event handlers
                                      mediaEnded: Function.createDelegate(this, this._onMediaEnded),
                                      mediaFailed: Function.createDelegate(this, this._onMediaFailed),
                                      playPreviousVideo: Function.createDelegate(this, this._onPlayPreviousVideo),
                                      playNextVideo: Function.createDelegate(this, this._onPlayNextVideo)
                                  },
                                  null, $get(this._hostname));

        this._playlist = [];
        try {
            var playText = 'this._playlist=[';

            for (var i = 1; i < tvidply.length; i++) {
                if (i + 1 == tvidply.length) {
                    // Last line - no comma
                    playText = playText + '{"mediaSource":"' + tvidply[i][1] + '",' +
                                          '"placeholderSource":"",' +
		                                  '"chapters":' +
			                              '[' +
			                              ']}'
                }
                else {
                    playText = playText + '{"mediaSource":"' + tvidply[i][1] + '",' +
                                          '"placeholderSource":"",' +
		                                  '"chapters":' +
			                              '[' +
			                              ']},'
                }
            }
            playText = playText + '];';
            eval(playText);

        }
        catch (e) { alert(e); }

        this._galleryItems = [];
        try {
            var galleryText = 'this._galleryItems=[';

            for (var j = 1; j < tvidply.length; j++) {
                if (j + 1 == tvidply.length) {
                    // Last line - no comma
                    galleryText = galleryText + 'new ExpressionPlayer.GalleryItem("' + tvidply[j][2] + '","")';
                }
                else {
                    galleryText = galleryText + 'new ExpressionPlayer.GalleryItem("' + tvidply[j][2] + '",""),';
                }
            }

            galleryText = galleryText + ']';

            eval(galleryText);
        }
        catch (e) { alert(e); }

        this._player.set_galleryInfo(this._galleryItems, Function.createDelegate(this, this._onClickGalleryItem));

        //setTimeout('this.playByQS();', 1500);
        //this.playByQS();

        this._onPlayNextVideo(null, null);
    },

    _onClickGalleryItem: function(galleryItemIndex) {
        this._player.set_mediainfo(this._playlist[galleryItemIndex]);
        this._currentMediainfo = galleryItemIndex + 1;
    },

    _onMediaEnded: function(sender, eventArgs) {
        window.setTimeout(Function.createDelegate(this, this._onPlayNextVideo), 1000);
    },

    _onMediaFailed: function(sender, eventArgs) {
        alert(String.format(Sys.UI.Silverlight.MediaPlayer.Res.mediaFailed, this._player.get_mediaSource()));
    },


    _onPlayPreviousVideo: function(sender, eventArgs) {
        if (this._playlist != null) {
            if (this._currentMediainfo > 0) {
                this._player.set_mediainfo(this._playlist[--this._currentMediainfo]);
            }
        }
    },

    _onPlayNextVideo: function(sender, eventArgs) {
        if (this._playlist != null) {
            if (this._currentMediainfo < this._playlist.length - 1) {
                this._player.set_mediainfo(this._playlist[++this._currentMediainfo]);
            }
        }
    },

    autoLoadParam: function() {
        var autoLoad = true;
        try {
            eval("autoLoad=('True'!=='False')");
        } catch (e) { }
        return autoLoad;
    },

    autoPlayParam: function() {
        var autoPlay = false;
        try {
            eval("autoPlay=('True'!=='False')");
        } catch (e) { }
        return autoPlay;
    },

    scaleModeParam: function() {
        var scaleMode = 1/*Normal*/;
        try {
            eval("scaleMode=1;");
        } catch (e) { }
        return scaleMode;
    },

    enableCaptionsParam: function() {
        var enableCaptions = true;
        try {
            eval("enableCaptions=('True'!=='False');");
        } catch (e) { }
        return enableCaptions;
    },

    mutedParam: function() {
        var muted = false;
        try {
            eval("muted=('False'!=='False');");
        } catch (e) { }
        return muted;
    },

    playByQS: function(posn) {
        var argVal = $getArgument("v", 1);
        if (posn != null) {
            argVal = posn;
        }

        this._player.set_mediainfo(this._playlist[argVal - 1]);
        this._currentMediainfo = this.getVideoPos(argVal - 1);

        ///////////////////////// Custom code ///////////////////////////////////////////////////
        expressionObj.set_autoPlay($getArgument(ExpressionPlayer.UrlParam.autoplay, customAutoPlay));

        // After the first load all videos should autoplay
        customAutoPlay = true;
        ///////////////////////// End of Custom code ///////////////////////////////////////////////////
    },

    getVideoPos: function(argVal) {
        var pos = 0;    // Default position
        for (var i = 1; i < vidply.length; i++) {
            if (vidply[i][0] == argVal) {
                pos = i - 1;
                break;
            }
        }
        return pos;
    },

    playByChoice: function(selectedVideo) {
        var argVal = selectedVideo;

        if ((argVal > this._playlist.length - 1) || (argVal < 0))
            argVal = 0;

        this._player.set_mediainfo(this._playlist[argVal]);
        this._currentMediainfo = argVal + 1;
    }

}



function StartWithParent(parentId, appId) {
    new StartPlayer_0(parentId);
}
