var player;

Drupal.behaviors.abraPlaylist = function(context) {
  // Initialize Player
  if (Drupal.settings.featuredVideo > 0) {
    $('div#video-section ul#playlist').scrollTo($('li#video-'+ Drupal.settings.featuredVideo));
    $('div#video-section ul#playlist li#video-'+ Drupal.settings.featuredVideo).each(function() {
      $(this).addClass('active');
      embedVideo($('a:first', this).attr('href'), 'false');
    });
    $('a:first', 'div#video-section ul#playlist li#video-'+ Drupal.settings.featuredVideo).each(function() {
  	  embedVideo($(this).attr('href'), 'false');
  	});
  }
  else if (Drupal.settings.streamIsLive == 1) {
  	$('div#video-section a#streaming-link').each(function() {
  	  embedVideo($(this).attr('href'), 'true');
  	});
  }
  else {
  	$('div#video-section ul#playlist li.item:first').each(function() {
  	  embedVideo($('a:first', this).attr('href'), 'false');
  	  $(this).addClass('active');
  	});
  }

  // Enable streaming link
  $('div#video-section a#streaming-link').click(function() {
        $('div#video-section ul#playlist li').removeClass('active');
        embedVideo($(this).attr('href'), 'true');
        return false;
  });
  // Enable video links
  $('div#video-section ul#playlist li.item a').click(function() {
        var target = 'li#'+ $(this).attr('rel');
        $('div#video-section ul#playlist li').removeClass('active');
        $(target, 'div#video-section ul#playlist').addClass('active');
        embedVideo($(this).attr('href'), 'true');
        return false;
  });
};

function embedVideo(filename, start) {
  $('div#video-section div#player-wrapper').removeClass('stream-enabled').addClass('stream-disabled');
  
  var cfg = {
    autostart: start,
    backcolor: '#0d0d0d',
    backgroundcolor: '#0d0d0d',
    frontcolor: '#cccccc',
    lightcolor: '#ffffff',
    file: filename,
    id: 'player', // Set ID here to make it play in Linux
    image: Drupal.settings.abraHelpPath +'preview.jpg',
    height: '288',
    screencolor: '#0d0d0d',
    start: '0',
    width: '512'
  };
  var elm = document.getElementById("player");
  var src = Drupal.settings.abraHelpPath +'wmvplayer.xaml';
  player = new jeroenwijering.Player(elm,src,cfg);
};

function embedVideo2(filename) {
    output = '<div id="silverlightControlHost">';
    output += '<object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="512" height="288">';
    output += '<param name="source" value="'+ Drupal.settings.basePath +'sites/all/modules/abra_help/VideoPlayer.xap"/>';
    output += '<param name="background" value="black" />';
    output += '<param name="initParams" value="autostart=true,m='+ filename +'" />';
    output += '<param name="minruntimeversion" value="2.0.31005.0" />';
    output += '<a href="http://go.microsoft.com/fwlink/?LinkId=124807" style="text-decoration: none;">';
    output += '<img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight" style="border-style: none"/>';
    output += '</a>';
    output += '</object>';
    output += '</div>';

    $('#player').html(output);
};
