var PlayRelatedMovieOneMouseDown;
var PlayRelatedMovieTwoMouseDown;
var PlayRelatedMovieThreeMouseDown;


function get_mediainfo(mediainfoIndex) {
    switch (mediainfoIndex) {        

        case 0:
            return  { "mediaUrl": "/movies/testvideo.wmv",
                      "placeholderImage": "",
                      "chapters": [               
                                  ] };                                                                
                          
        default:
             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: '/msdn_player.xaml', 
                                        parentElement: $get(parentId ||"divPlayer_0"), 
                                        id:this._hostname, 
                                        properties:{ width:'100%', height:'100%', version:'1.0', background:document.body.style.backgroundColor, isWindowless:'false' }, 
                                        events:{ onLoad:Function.createDelegate(this, this._handleLoad) } } );
    this._currentMediainfo = 0;      
}
StartPlayer_0.prototype= {
    _handleLoad: function() {
        //alert('debug: _handleload!');
        this._player = $create(
            ExtendedPlayer.Player, 
            {   // properties
                autoPlay    : true, 
                volume      : 1.0,
                muted       : false
            }, 
            {   // event handlers
                mediaEnded: Function.createDelegate(this, this._onMediaEnded),
                mediaFailed: Function.createDelegate(this, this._onMediaFailed)
            },
            null,
            $get(this._hostname)
        );
        
        
    },   
     _loadVars: function() {
        if(typeof(this._currentMovie.source) != 'undefined') {
        	this._host = $get(this._hostname)                   
        	this._loadVideoByURL(this._currentMovie.source);
        	
        	if (typeof this._currentMovie.rating != 'undefined')
        		this._getObjectFromXAML("RatingText").Text = this._currentMovie.rating + "";
        	       	        	
			if (typeof MailToFriendBtn != 'undefined' && this._getObjectFromXAML("MailToButton") != null) {
				//this._costumButtons.mailTo.addEventListener("mouseLeftButtonDown", Silverlight.createDelegate(this, function));
				//this._costumButtons.mailTo.addEventListener("mouseLeftButtonDown", Silverlight.createDelegate(this, this._test));
				this._getObjectFromXAML("MailToButton").addEventListener("mouseLeftButtonDown", "MailToFriendBtn");
				this._getObjectFromXAML("MailToButton").Visibility = "Visible";
			}
			//alert((typeof RatingUpBtn != 'undefined' && typeof RatingDownBtn != 'undefined' && this._getObjectFromXAML("RatingUpButton") != null && this._getObjectFromXAML("RatingDownButton")));
			if (typeof RatingUpBtn != 'undefined' && typeof RatingDownBtn != 'undefined' && this._getObjectFromXAML("RatingUpButton") != null && this._getObjectFromXAML("RatingDownButton")) {
				this._getObjectFromXAML("RatingUpButton").addEventListener("mouseLeftButtonDown", "RatingUpBtn");
				this._getObjectFromXAML("RatingDownButton").addEventListener("mouseLeftButtonDown", "RatingDownBtn");
				this._getObjectFromXAML("RatingButtons").Visibility = "Visible";
				
			}
        }
    },
    _test: function(arg) {
    	alert("hi");
    },
    _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 cVideos = 1;
        if (this._currentMediainfo<cVideos)
            this._player.set_mediainfo( get_mediainfo( this._currentMediainfo++ ) );    
    },*/
    _loadVideoByURL: function(url)  {
        if(url)
        {
            this._player.set_mediainfo({"mediaUrl": url,"placeholderImage": "","chapters": []});
        }
        else
        {
            this._player.set_mediainfo({"mediaUrl": "","placeholderImage": "","chapters": []});
        }
    },
    _stopVideo: function() {
        this._player.set_mediainfo({"mediaUrl": "","placeholderImage": "","chapters": []});
    },
    _getObjectFromXAML: function(nameString) {
    	return this._host.content.findName(nameString);
    }
}
