var athl_thumbs = new Array();
var athl_previews = new Array();
var nr_athl_displayed = 0;
var athl_thumbs_init = false;

function init_athlete_preview(ids_string, exts_string, nr_displayed)
{
  if(!athl_thumbs_init)
  {
    nr_athl_displayed = parseInt(nr_displayed);

    // convert athlete ids and extension string into an array
    var athl_ids = new Array();
    var athl_exts = new Array();
    athl_ids = ids_string.split(' ');
    athl_exts = exts_string.split(' ');

    // pre-load images
    for(var i=0; i<athl_ids.length; i++)
    {
       athl_thumbs[i]= new Image();
       athl_thumbs[i].src = '../images/archive/thumbs/thumb'+athl_ids[i]+'.'+athl_exts[i];
    }

    // get pointers to all athlete thumbs
    athl_previews = document.getElementsByClassName('athl_prev');

  }

  athl_thumbs_init = true;
}


function athlete_preview()
{
  if(athl_thumbs_init)
  {

  // randomly change one of the first displayed athlete thumbs with one that is currently not displayed
  var a = Math.floor(Math.random()*nr_athl_displayed);
  var c = athl_thumbs.length-nr_athl_displayed;
  var b = Math.floor(Math.random()*c)+nr_athl_displayed;

  var h = athl_thumbs[b].height;
  var w = athl_thumbs[b].width;

  // scale image to fit within 30x30pixels
  var scale_x = 30.0/w;
  var scale_y = 30.0/h;
  if(scale_x < scale_y)
    scale = scale_x;
  else
    scale = scale_y;

  if(scale > 1)
    scale = 1;

  w *= scale;
  h *= scale;

  // flip images
  athl_previews[a].src = athl_thumbs[b].src;
  athl_previews[a].style.height = h+'px';
  athl_previews[a].style.width = w+'px';

  // flip images in array
  var tmp = athl_thumbs[a];
  athl_thumbs[a] = athl_thumbs[b];
  athl_thumbs[b] = tmp;

  }

  // invoke function again after timeout
  setTimeout('athlete_preview()', 2000);
  
}

function init_athlete_video(vid_id,vid_ext)
{
  // depending on video extension set minimal flash version
  var flash_version = [0, 9];
  if(vid_ext == 'mp4')
    flash_version = [9, 115];
  
  // install flowplayer in container
  flowplayer('video'+vid_id,
    // SECOND FLOWPLAYER PARAMETER
    { 
 
    // our Flash component 
    src: "./scripts/flowplayer-3.1.5.swf", 

    // set needed Flash version 
    version: flash_version, 
 
    // older versions will see a custom message 
    onFail: function()  { 
        document.getElementById("flowplayer_info").innerHTML = 
            "You need the latest Flash version to see this video. " + 
            "Your version is " + this.getVersion();
      }
    },
    
    // THIRD FLOWPLAYER PARAMETER (COULD IN PART BE MOVED TO A EXTERNAL CONFIGURATION FILE)
    { // set flowplayer properties
      
      // use a splash background on the player canvas     
      canvas: 
      { 
        backgroundColor:'#000000',
        backgroundImage: 'url(./videos/archive/stills/video'+vid_id+'.jpg)', 
        backgroundGradient: 'none' 
      }, 
 
      // set controls properties
      plugins:
      { 
                          
        // the default controlbar is called "controls". by tweaking this, you can modify the default controlbar 
        controls: 
        { 
          // location of the plugin 
          url: 'flowplayer.controls-3.1.5.swf', 
          autoHide: 'always',
          hideDelay: 2000,
 
          // styling properties (will be applied to all plugins) 
          backgroundColor: '#000000', 
          buttonColor: '#333333', 
          opacity: 0.80,   
          backgroundGradient: 'low', 
     
          // which buttons are visible and which are not? 
          play:true, 
          volume:true, 
          mute:true, 
          time:true, 
          stop:false, 
          playlist:false, 
          fullscreen:true, 
 
          // scrubber is a well-known nickname for the timeline/playhead combination 
          scrubber: true, 

          tooltips: 
          { 
            buttons: true, 
            fullscreen: 'Fullscreen' 
          } 
        }
      }, 

      // set a clip properties 
      clip:
      {
        url: './videos/archive/video'+vid_id+'.'+vid_ext,
        autoBuffering : false,
        autoPlay: false
      }        
    } 
  ); // end of flowplayer() call

}


