// counter keeps track of which featured video has the focus
var f = 0;

// do not rotate videos in the widget if the user is watching one of them
var do_rotate = 1;

function featured_widget() {
  document.getElementById('ft_'+0).className = 'focus';
  document.getElementById('ft_'+1).className = 'unfocus';
  document.getElementById('ft_'+2).className = 'unfocus';
  setTimeout(rotate, 11000);

  // set initial opacity values for the objects
  document.getElementById('fv_0').style.opacity = 1;
  document.getElementById('fv_1').style.opacity = 0;
  document.getElementById('fv_2').style.opacity = 0;

  // initialize the background position of each text node
  document.getElementById('ft_0').style.backgroundPosition = '400px 0px';
  document.getElementById('ft_1').style.backgroundPosition = '400px 0px';
  document.getElementById('ft_2').style.backgroundPosition = '400px 0px';
}

function rotate() {
  if (do_rotate != 1)
    return;

  // fade out the current video
//  var el_out = document.getElementById('fv_'+f);
  setTimeout('fade_out("fv_'+f+'")', 33);

  // slide out the current text
  setTimeout('slide_out("ft_'+f+'")', 33);

  // increment the counter
  f++;
  if (f == 3) // 3 featured videos displayed in the widget, 0-2
    f = 0;

  // fade in the next video
//  var el_in = document.getElementById('fv_'+f);
  setTimeout('fade_in("fv_'+f+'")', 33);

  // display the new featured video
  document.getElementById('ft_'+f).className = 'focus';
  setTimeout('slide_in("ft_'+f+'")', 33);

  // recursive
  setTimeout(rotate, 11000);
}

function stop_rotation() {
  do_rotate = 0;
}

function fade_out(el_id)
{
  var el = document.getElementById(el_id);
//  el.style.display = 'none';

  if (el.style.opacity <= 0)
    return;

try {
  el.style.opacity -= .1;
  el.filters.alpha.opacity -= 10;
} catch (e) {
}
  setTimeout('fade_out("'+el_id+'")', 33);
}

function fade_in(el_id)
{
  var el = document.getElementById(el_id);
//  el.style.display = 'block';

  if (el.style.opacity >= 1)
    return;

try {
  // force integer interpretation
  el.style.opacity = (+el.style.opacity)+.1;
  el.filters.alpha.opacity = (+el.filters.alpha.opacity)+20;
} catch (e) {
}
  setTimeout('fade_in("'+el_id+'")', 33);
}

function slide_out(el_id)
{
  var el = document.getElementById(el_id);

  var pos = el.style.backgroundPosition;
  var len = pos.indexOf('px');
  var value = pos.substring(0,len);
      value = (+value)+20;

  if (value >= 400)
    return;

  // update the position
  el.style.backgroundPosition = value+'px 0px';

//  el.style.backgroundImage = 'url()';
  el.className = 'unfocus';

  // recursive call
  setTimeout('slide_out("'+el_id+'")', 3);
}

function slide_in(el_id)
{
  var el = document.getElementById(el_id);

  var pos = el.style.backgroundPosition;
  var len = pos.indexOf('px');
  var value = pos.substring(0,len);
      value -= 20;

  if (value <= 0)
    return;

  // update the position
  el.style.backgroundPosition = value+'px 0px';

  el.style.backgroundImage = 'url(/images/feature_text_bg.png)';
//  el.style.backgroundImage = 'url(/images/ft_background.png)';
  el.className = 'focus';

  // recursive call
  setTimeout('slide_in("'+el_id+'")', 3);
}
