﻿function get_mediainfo(mediainfoIndex) {

  var a_info = [
		{ "mediaUrl": "http://go.microsoft.com/?linkid=8999295",
          "placeholderImage": "",
          "chapters": [
            ],
            "title": "Präsentieren Sie Ihr Unternehmen",
            "subtitle": "",
            "metainfo": "Dauer: 00:41"
        }
	];
    if(mediainfoIndex=="element_length") return a_info.length;
    
	if(mediainfoIndex<a_info.length){
		return a_info[mediainfoIndex];
	}
	else {
		throw Error.invalidOperation("No such mediainfo");
	}
}

function StartWithParent(parentId, appId) {
    new StartPlayer_0(parentId);
}

function StartPlayer_0(parentId) {

    this._hostname = EePlayer.Player._getUniqueName("xamlHost");
    Silverlight.createObjectEx( {   source: '/germany/windows/components/silverlight/mediaplayer/player.xaml',
									parentElement: $get(parentId ||"mplayer_0"),
                                    id:this._hostname,
                                    properties:{ width:'520', height:'277', version:'1.0', background:'transparent', isWindowless:'true' },
                                    events:{ onLoad:Function.createDelegate(this, this._handleLoad) } } );
    this._currentMediainfo = 0;
}
StartPlayer_0.prototype= {
    _handleLoad: function() {
        
        this._player = $create(   ExtendedPlayer.Player,
                                  { // properties
                                    autoPlay    : false,
                                    volume      : 1.0,
                                    muted       : false
                                  },
                                  { // event handlers
                                    mediaEnded: Function.createDelegate(this, this._onMediaEnded),
                                    mediaFailed: Function.createDelegate(this, this._onMediaFailed)
                                  },
                                  null, $get(this._hostname)  );
        this._playNextVideo();
    },
    _onMediaEnded: function(sender, eventArgs) {
        window.setTimeout( Function.createDelegate(this, this._playNextVideo), 1000);
    },
    _onMediaFailed: function(sender, eventArgs) {
        alert(String.format( Ee.UI.Xaml.Media.Res.mediaFailed, this._player.get_mediaUrl() ) );
    },
    _playNextVideo: function() {
        var id = this._currentMediainfo++;
        var b_add = true;
        for(var i=0; i<a_watched.length;i++){
            if(a_watched[i]==id) b_add = false;
        }
        if(b_add) a_watched.push(id);
  
if(this._currentMediainfo>=get_mediainfo("element_length")){
            this._currentMediainfo = 0;
        }

      
		this._player.set_mediainfo( get_mediainfo( id ) );
    },
    _playVideo: function(id) {
        var b_add = true;
        for(var i=0; i<a_watched.length;i++){
            if(a_watched[i]==id) b_add = false;
        }
        if(b_add) a_watched.push(id);
        this._currentMediainfo = Number(id)+1;
        this._player.set_mediainfo( get_mediainfo( id ) );

        this._player.set_autoPlay(true);
		      this._player.play();
    }
}