﻿$(document).ready(function() {
    addMedia();
});
function addMedia() {
    $('.mediaSLAudio').each(
            function(index) {
                //var currentId = $(this).attr('id');
                var hrefFilename = $(this).attr('href');
                var data = $(this).metadata({type:'style'});
                var width = '100%';
                var height = '100%';
                var background = 'White';
                var autoplay = false;
                if (data.width) { width = data.width; }
                if (data.height) { height = data.height; }
                if (data.background) { background = data.background; }
                if (data.autoplay) { autoplay = (data.autoplay === 'true'); }
                var title = 'titel';
                if ($(this).attr('title')) { title = $(this).attr('title'); }
                if (Silverlight.isInstalled()) {
                    var retval = CreateSLAudioPlayer(index, width, height, background, hrefFilename, title, autoplay);
                    $(this).replaceWith(retval);
                }
                else {
                    standardMediaWithOpt(this, hrefFilename, width, height, background, title, autoplay)
                }
            });
    $('.mediaSLVideo').VideoLightbox()
    $('.mediaSLVideo1').each(
            function(index) {
    //var currentId = $(this).attr('id');
    var hrefFilename = $(this).attr('href');
    var data = $(this).metadata({ type: 'style' });
                var width = '100%';
                var height = '100%';
                var background = 'White';
                var autoplay = false;
                if (data.width) { width = data.width; }
                if (data.height) { height = data.height; }
                if (data.background) { background = data.background; }
                if (data.autoplay) { autoplay = (data.autoplay === 'true'); }
                var relFilename = $(this).attr('rel');
                if (relFilename == undefined) { relFilename = 'images/videolayover.png'; } //thumb.jpg
                var altFilename = $(this).attr('alt');
                if (altFilename == undefined) { altFilename = ''; } //top/logo_klein.jpg
                var title = 'titel';
                if ($(this).attr('title')) { title = $(this).attr('title'); }

                if (Silverlight.isInstalled()) {
                    var retval;
                    if (getExtFromFilename(_filename) == "ism") {
                        retval = CreateSLVideoPlayerAdaptiveStreaming(index, width, height, background, hrefFilename, relFilename, title, autoplay);
                    }
                    else {
                        retval = CreateSLVideoPlayer(index, width, height, background, hrefFilename, relFilename, altFilename, autoplay);
                    };
                    $(this).replaceWith(retval);
                }
                else {
                    standardMediaWithOpt(this, hrefFilename, width, height, background, title, autoplay)
                }
            });
    $('.media').each(
        function(index) {
            //var currentId = $(this).attr('id');
            var hrefFilename = $(this).attr('href');
            var data = $(this).metadata({ type: 'style' });
            var width = '100%';
            var height = '100%';
            var background = 'White';
            var autoplay = false;
            if (data.width) { width = data.width; }
            if (data.height) { height = data.height; }
            if (data.background) { background = data.background; }
            if (data.autoplay) { autoplay = (data.autoplay === 'true'); }
            var title = 'titel';
            if ($(this).attr('title')) { title = $(this).attr('title'); }

            standardMediaWithOpt(this, hrefFilename, width, height, background, title, autoplay);
        });
}
function standardMediaWithOpt(obj, _filename, _width, _height, _background, _title, _autoplay) {
    if (getExtFromFilename(_filename) == "ism") {
        _filename = _filename.replace('ism', 'wmv');
        $(obj).attr('href',_filename);
    }
    $(obj).empty();
    $(obj).media({ width: _width, height: _height, autoplay: _autoplay });
}
function getExtFromFilename(filename) {
    var ext = /^.+\.([^.]+)$/.exec(filename);
    return ext == null ? "" : ext[1];
}
//function CreateSLVideoPlayer(id, _width, _height, _background, _filename, _PreviewImage, _LogoImage, _autoplay) {
//    return Silverlight.createObject(
//        'ClientBin/MinoPlayer_Ver1_2.xap',  // source
//        null,  // parent element
//        'slPluginSLVideo' + id,  // id for generated object element
//        {width: _width, height: _height, background: _background }, //, version: '3.0.40624.0'
//        {onError: onSLErrorPlayer, onLoad: onSLLoadPlayer, minRuntimeVersion: '2.0' },
//        'AutoPlay=' + _autoplay + ',EnableScrubbing=true,InitialVolume=1,MarkersTimeView=5,' +
//        'VideoSource=' + _filename + ',' +
//        'LogoImage=' + _LogoImage + ',' +
//        'PreviewImage=' + _PreviewImage,
//        'context'    // context helper for onLoad handler.
//    );
//}
function CreateSLVideoPlayer(id, _width, _height, _background, _filename, _PreviewImage, _title, _autoplay) {
    var thumb = '';
    if (_PreviewImage != '') {
        thumb = '<ThumbSource>' + _PreviewImage + '</ThumbSource>';
    }
    return Silverlight.createObject(
        'ClientBin/SmoothStreamingPlayer.xap',  // source
        null,  // parent element
        'slPluginSLVideo' + id,  // id for generated object element
        {width: _width, height: _height, background: _background, autoUpgrade: 'true', enableHtmlAccess: 'true', enableGPUAcceleration: 'true' }

    //unused valid silverlight init parameters
    //<param name="enableFrameRateCounter" value="bool" />
    //<param name="enableRedrawRegions" value="bool" />
    //<param name="maxFrameRate" value="int" />
    //<param name="allowHtmlPopupWindow" value="bool"/>
    //<param name="background" value="colorValue"/>
    //<param name="splashScreenSource" value="uri"/>
    //<param name="fullScreen" value="bool"/>
    //<param name="onFullScreenChanged" value="functionname"/>
    //<param name="onResize" value="functionname"/>
    //<param name="onSourceDownloadComplete" value="functionname"/>
    //<param name="onSourceDownloadProgressChanged" value="functionname"/>
    //<param name="windowLess" value="bool"/>
    , { onError: onSLErrorPlayer, onLoad: onSLLoadPlayer, minRuntimeVersion: '2.0'}//, version: '3.0.40624.0'
     , 'playerSettings = ' +
            '<Playlist>' +
            '<AutoLoad>true</AutoLoad>' +
            '<AutoPlay>' + _autoplay + '</AutoPlay>' +
            '<DisplayTimeCode>false</DisplayTimeCode>' +
            '<EnableCachedComposition>true</EnableCachedComposition>' +
            '<EnableCaptions>true</EnableCaptions>' +
            '<EnableOffline>false</EnableOffline>' +
            '<EnablePopOut>true</EnablePopOut>' +
            '<StartMuted>false</StartMuted>' +
            '<StretchMode>None</StretchMode>' +
            '<Items>' +
                '<PlaylistItem>' +
                    '<IsAdaptiveStreaming>false</IsAdaptiveStreaming>' +
                    '<MediaSource>' + _filename + '</MediaSource>' +
                    thumb +
                    '<Title>' + _title + '</Title>' +
                '</PlaylistItem>' +
            '</Items>' +
            '</Playlist>', //'			<FileSize>1010691350</FileSize>' +
        'context'    // context helper for onLoad handler.
    );
}
function CreateSLVideoPlayerAdaptiveStreaming(id, _width, _height, _background, _filename, _PreviewImage, _title, _autoplay) {
    var thumb = '';
    if (_PreviewImage != '') {
        thumb = '<ThumbSource>' + _PreviewImage + '</ThumbSource>';
    }
    return Silverlight.createObject(
        'ClientBin/SmoothStreamingPlayer.xap',  // source
        null,  // parent element
        'slPluginSLVideo' + id,  // id for generated object element
        {width: _width, height: _height, background: _background, autoUpgrade: 'true', enableHtmlAccess: 'true', enableGPUAcceleration: 'true' }

    //unused valid silverlight init parameters
    //<param name="enableFrameRateCounter" value="bool" />
    //<param name="enableRedrawRegions" value="bool" />
    //<param name="maxFrameRate" value="int" />
    //<param name="allowHtmlPopupWindow" value="bool"/>
    //<param name="background" value="colorValue"/>
    //<param name="splashScreenSource" value="uri"/>
    //<param name="fullScreen" value="bool"/>
    //<param name="onFullScreenChanged" value="functionname"/>
    //<param name="onResize" value="functionname"/>
    //<param name="onSourceDownloadComplete" value="functionname"/>
    //<param name="onSourceDownloadProgressChanged" value="functionname"/>
    //<param name="windowLess" value="bool"/>
    , { onError: onSLErrorPlayer, onLoad: onSLLoadPlayer, minRuntimeVersion: '2.0'}//, version: '3.0.40624.0'
     , 'playerSettings = ' +
            '<Playlist>' +
            '<AutoLoad>true</AutoLoad>' +
            '<AutoPlay>' + _autoplay + '</AutoPlay>' +
            '<DisplayTimeCode>false</DisplayTimeCode>' +
            '<EnableCachedComposition>true</EnableCachedComposition>' +
            '<EnableCaptions>true</EnableCaptions>' +
            '<EnableOffline>false</EnableOffline>' +
            '<EnablePopOut>true</EnablePopOut>' +
            '<StartMuted>false</StartMuted>' +
            '<StretchMode>None</StretchMode>' +
            '<Items>' +
                '<PlaylistItem>' +
                    '<AudioCodec>WmaProfessional</AudioCodec>' +
                    '<Description></Description>' +
                    '<FrameRate>29.97000002997</FrameRate>' +
                    '<Height>1080</Height>' +
                    '<IsAdaptiveStreaming>true</IsAdaptiveStreaming>' +
                    '<MediaSource>' + _filename + '%5CManifest</MediaSource>' +
                    thumb +
                    '<Title>' + _title + '</Title>' +
                    '<VideoCodec>VC1</VideoCodec>' +
                    '<Width>1920</Width>' +
                '</PlaylistItem>' +
            '</Items>' +
            '</Playlist>', //'			<FileSize>1010691350</FileSize>' +
        'context'    // context helper for onLoad handler.
    );
}
function CreateSLAudioPlayer(id, _width, _height, _background, _filename, _title, _autoplay) {
    return Silverlight.createObject(
        'ClientBin/SLAudioPlayer.xap',  // source
        null,  // parent element
        'slPluginSLAudioPlayer' + id,  // id for generated object element
        {width: 400, height: 40, background: _background },
        { onError: onSLErrorPlayer, onLoad: onSLLoadPlayer, minRuntimeVersion: '2.0' },
        'VideoSource=' + _filename + ',' +
        'VideoSourceTitle=' + _title + ',' +
        'AutoPlay=' + _autoplay + ',EnableScrubbing=true,InitialVolume=1,MarkersTimeView=5',
        'context'    // context helper for onLoad handler.
    );
}
function onSLLoadPlayer(sender, args) {
    //var slCtl = sender.getHost();
    //if (slCtl != null) {
    //    window.status += " loaded into " + slCtl.content.id + ". ";
    //}
}
function onSLErrorPlayer(sender, errorArgs) {
    // The error message to display.
    var errorMsg = "Silverlight Error: \n\n";
    // Error information common to all errors.
    errorMsg += "Error Type:    " + errorArgs.errorType + "\n";
    errorMsg += "Error Message: " + errorArgs.errorMessage + "\n";
    errorMsg += "Error Code:    " + errorArgs.errorCode + "\n";

    // Determine the type of error and add specific error information.
    switch (errorArgs.errorType) {
        case "RuntimeError":
            // Display properties specific to RuntimeErrorEventArgs.
            if (errorArgs.lineNumber != 0) {
                errorMsg += "Line: " + errorArgs.lineNumber + "\n";
                errorMsg += "Position: " + errorArgs.charPosition + "\n";
            }
            errorMsg += "MethodName: " + errorArgs.methodName + "\n";
            break;
        case "ImageError":
            return;
        case "MediaError":
            return;
        case "ParserError":
            // Display properties specific to ParserErrorEventArgs.
            errorMsg += "Xaml File:      " + errorArgs.xamlFile + "\n";
            errorMsg += "Xml Element:    " + errorArgs.xmlElement + "\n";
            errorMsg += "Xml Attribute:  " + errorArgs.xmlAttribute + "\n";
            errorMsg += "Line:           " + errorArgs.lineNumber + "\n";
            errorMsg += "Position:       " + errorArgs.charPosition + "\n";
            break;
        default:
            break;
    }
    // Display the error message.
    alert(errorMsg);
}