
/* - stx_flowplayer.js - */
function stx_init_flowplayer() {
  var links = jQuery('a[href$="flv"], a[href$="mp4"]');
  links.each(function() {
    var item = jQuery(this);
    var elmId = item.attr('id');
    if(!elmId) {
      elmId = Math.round(Math.random() * 1000000000);
      item.attr('id', elmId);
    }
    var splash = item.find('img');
    var splashUrl = splash.attr('src');
    splash.attr('src', '');
    splash.load(function() {
      stx_embed_flowplayer(elmId);
    });
    splash.attr('src', splashUrl);
  });
}

function stx_embed_flowplayer(elmId) {
  var basePath = window.location.pathname;
  if(basePath.substring(basePath.length-1, 1) != '/') {
    basePath += '/';
  }
  var links = jQuery('#' + elmId);
  links.each(function() {
    var item = jQuery(this);
    var elmId = item.attr('id');
    var movieUrl = basePath + item.attr('href');
    var title = item.attr('title');
    title = title ? title : 'Film: ' + movieUrl;
    var splash = item.find('img');
    var splashUrl = basePath + splash.attr('src');
    if(splashUrl.lastIndexOf('/') > splashUrl.lastIndexOf('.')) {
      splashUrl = splashUrl.substring(0, splashUrl.lastIndexOf('.') + 4);
    }
    var width = splash.width();
    var height = splash.height();
    // var flashvars = { config: '{ \'playlist\': [ { \'url\': \'' + splashUrl + '\', \'scaling\': \'orig\', \'title\': \'' + title + '\' }, { \'url\': \'' + movieUrl + '\', \'autoPlay\': true, \'title\': \'' + title + '\', \'autoBuffering\': true } ],  \'plugins\': {   \'controls\': {     \'url\': \'flowplayer.controls-3.2.3.swf\'   } } }' };
    var flashvars = { config: '{ \'buffering\': false, \'playlist\': [ { \'url\': \'' + movieUrl + '\', \'autoPlay\': true, \'title\': \'' + title + '\', \'autoBuffering\': true, \'playlist\': [ { \'url\': \'' + splashUrl + '\', \'scaling\': \'orig\', \'title\': \'' + title + '\', \'position\': \'0\' }, { \'url\': \'' + splashUrl + '\', \'scaling\': \'orig\', \'title\': \'' + title + '\', \'position\': \'-1\', \'autoBuffering\': false } ] } ],  \'plugins\': {   \'controls\': {     \'url\': \'flowplayer.controls-3.2.3.swf\'   } } }' };
    var params = { allowfullscreen:'true', allowscriptaccess:'always' };
    var attributes = { id:elmId, name:elmId };
    swfobject.embedSWF('flowplayer-3.2.5.swf',elmId, width, height,'9.0.115','false', flashvars, params, attributes);
  });
}

function stx_debug(msg) {
  jQuery('body').append('<span style="color: #fff; font-size: 9px;">' + msg + '</span>' + "\n");
}

jQuery(document).ready(function() {
  stx_init_flowplayer();
});



