
Brightcove = {

    getUrl: function(param) {
        var url = 'http://api.brightcove.com/services/library?token=qM8RqIsM5YdUoD-9ZdU9ifsoOuYcSDgCPSKEsF5UJ4WybQkPgwIuZQ..' + param;
        if ( document.location.host == 'stream.amostv.com' ) {
			url = 'http://proxy.amostv.accedo.tv/proxy.php?url=' + encodeURIComponent(url);
        } else {
			url = 'proxy.php?url=' + encodeURIComponent(url);
		}
        return url;
    },
    
    getPlaylistsByPlayer: function(id, callback) {
        var params = '&command=find_playlists_for_player_id&player_id=' + id;
        params += '&playlist_fields=id,name,videoIds';
        AMOSTV.ajax(this.getUrl(params), callback)
    },
    
    getVideoById: function(id, callback) {
        var params = '&command=find_video_by_id&video_id=' + id;
        params += '&media_delivery=http';
        params += '&video_fields=id,name,videoStillURL,renditions';
        AMOSTV.ajax(this.getUrl(params), callback)
    },
    
    getPlaylistById: function(id, callback) {
        var params = '&command=find_playlist_by_id&playlist_id=' + id;
        params += '&media_delivery=http';
        params += '&video_fields=id,name,videoStillURL,renditions';
        AMOSTV.ajax(this.getUrl(params), callback)
    },
    
    chooseRendition: function(video, max_mbps) {
        
        var max_bitrate = max_mbps*1000000; // megabits -> bits
        
        var rendition = null;

        //// choose rendition based on encoding rate
        var memory = 0;
        for ( var i = 0; i < video.renditions.length; i++ ) {
            var rend = video.renditions[i];
            
            // if rend has a lower bitrate than the maximum allowed,
            // and is larger than the one that has already been found
            if ( rend.encodingRate < max_bitrate && rend.encodingRate > memory ) {
                memory = rend.encodingRate;
                rendition = rend;
            }
        }
        
        return rendition.url;
    }
};

