
//----------------------------------------------------------
// Copyright (C) Microsoft Corporation. All rights reserved.
//----------------------------------------------------------
// StartPlayer.js


//Global Variable
var mediaObj;

//Local Variable
var mediaUrl;
var slideParent ;
var videoParent;

//Stub for handling Media Element
function stub(sender)
{
    mediaObj = sender;
}

function StartPlayer_0(parentId,videoUrl,src,divId,width,height,slideDivId,altVideo)
 {      
    slideParent = document.getElementById(slideDivId);
	videoParent = document.getElementById(divId);	
	this._altVideo = altVideo;
    this._hostname = EePlayer.Player._getUniqueName("xamlHost");
    Silverlight.createObjectEx( {   source: src, 
                                        parentElement: $get(parentId ||divId), 
                                        id:this._hostname, 
                                        properties:{ width: width, height: height, version:'1.0', background:document.body.style.backgroundColor, isWindowless:'true' }, 
                                        events:{ onLoad:Function.createDelegate(this, this._handleLoad),onError:Silverlight.default_error_handler }                                    
                                         } );
    this._currentMediainfo = 0;          
    mediaUrl = videoUrl  ;
}



StartPlayer_0.prototype=
 {
    _handleLoad: function(control) {    
        this._player = $create(   ExtendedPlayer.Player, 
                                  { // properties
                                    autoPlay    : true, 
                                    volume      : 1.0,
                                    muted       : false
                                  }, 
                                    { // event handlers
                                    mediaEnded: Function.createDelegate(this, this._onMediaEnd),
                                    mediaFailed: Function.createDelegate(this, this._onMediaFailed),
                                    mediaOpened: Function.createDelegate(this, this._onMediaOpened)
                                  },
                                  null, $get(this._hostname)  ); 
        stub(this);                                  
        this._playNextVideo();             
    },    
    _onMediaEnd: function(sender, eventArgs) {          
     if(slideParent.innerHTML!='')
     {  
        clearTimeout(t);
        t = setTimeout('ShowSlide()',1000);
     }     
    },
    _onMediaFailed: function(sender, eventArgs) {     
    var iframeString = this._altVideo;     
    if(videoParent)
    {
        videoParent.innerHTML = addHttp(iframeString);
    }
    },
    _playNextVideo: function() 
    {
        var cVideos = 1;
        if (this._currentMediainfo<cVideos)
            this._player.set_mediainfo( get_mediainfo( this._currentMediainfo ) );  
    }, 
      _onMediaOpened: function(sender, eventArgs){
        var videoPlayer = document.getElementById(this._hostname);       
        var mediaElement = videoPlayer.content.FindName("VideoWindow");
        
        var nDuration = mediaElement.NaturalDuration.Seconds;
        
        var bufferTime = nDuration * 0.75;
        if(nDuration > 25)
            nDuration = "0:0:25";
        else
            nDuration = convertTimeSpan(bufferTime);
        
        mediaElement.BufferingTime = nDuration;
        mediaElement.play();
    } 
}


function get_mediainfo(mediainfoIndex) {
    switch (mediainfoIndex) {        

        case 0:
            return  { "mediaUrl": mediaUrl,
                      "placeholderImage": "",
                      "chapters": [               
                                  ] };                                                                
                          
        default:
             throw Error.invalidOperation("No such mediainfo");
     }
}
function convertTimeSpan(sec)
{
   return (Math.floor(sec / 3600) + ":" + Math.floor((sec % 3600) / 60) + ":" + Math.floor(sec % 60) ); 
}


//For replacing dummy string with HTTP header
function addHttp(altVideo)
{
    if(altVideo!=null)
    {
        return altVideo.replace(/&h&/g,"http://");
    }    
}
