/*
 * @author hanssen, seeger
 * 
 * Processes all flash placeholders in the document and replaces them.
 *
 * REQUIREMENTS
 * - jQuery 1.2.6 (jQuery.js in root/javascripts/)
 * - SWFObject v2.0 (swfobject.js in root/javascripts/)
 *
* Let preview images be replaced by flash content. 
* Class needs a "flashobject" to be processed.
* If width and height are set, these values will be used to set up the video player accordingly.
* Source expects to have a flash_preview in path and the original filename extended by an ".jpg".
* Flash path is expected to be in path "original" instead of "flash_preview" and 
* named like the preview without the ".jpg".
* Example:
 * <img id="pool_file_18_uniq_id_e3f63" class="flashobject" src="/files/18/flash_preview/kipi.flv.jpg" width="360" height="270" alt="" />
 *
 * Processed flash placeholder must have the class "flashobject" and
 * an style attribute defining width and height and an src attribute defining the swf path, like:
 * <div style="background: #c0c0c0; width: 320px; height: 200px;" class="flashobject" src="files/16/original/colorBoxes.swf" id="16"></div>
 *
 * The resulting object will have the css class "embedded_swf" 
 * or whatever you define in attributes.styleclass.
 * 
 */

/* include SWFObject */
document.write( '<script type="text/javascript" src="/javascripts/file_pool/swfobject.js"></script>' );
 
/* initalizer */
$(document).ready(function(){
  loadFlashObjects();
});
 
/* default overrideable parameters */
var flashvars = false;

/* set bufferlength in seconds */
var buffertime = 10;

var flvPlayerVars = {
  autoplay: "0",
  autoload: "0"
};
 
var params = {
  allowfullscreen: 'true',
  loop:    "true",
  quality: "high",
/*  scale:   "showall",*/ /* this is responsible for strange scaling behaviour! */
  wmode:   "transparent"
};

/* resulting html attributes 
 * warning: mirror this array in replaceByFlashObject()
 */
var attributes = {
  styleclass: "embedded_swf",  /* remark: dont use "class" attribute, this is ECMA4 reserved */  
  name:       ""
};

/* default hardcoded parameters */
var updateURL = "/swf/expressInstall.swf";
var minFlashVersion = "8.0.0";
var flvPlayerPath = "/swf/player.swf"; 
var swfUrl;

function loadFlashObjects() {
  $('img.flashobject').each(function() {
    var img = $(this);
    var id = this.id;
    var width = img.attr('width');
    var height = img.attr('height');
    var autoplay = (img.attr('autoplay') == '1') ? '1' : flvPlayerVars.autoplay;
    var img_src = img.attr('src');
    var flv = img_src.replace('/flash_preview/', '/original/');
        flv = flv.replace('.flv.jpg', '.flv');
    //alert(id + ' ' + width + ' x ' + height + ' - ' + img_src + ' -> ' + flv + ' autoplay: ' + autoplay);

    if (flv.indexOf(".flv") != -1) {
      flashvars = {
        flv: '../' + flv,
        image: img_src,
        autoload: flvPlayerVars.autoload,
        autoplay: autoplay
      }
      var src = flvPlayerPath;
    }  
    
    replaceByFlashObject(id, src, width, height);
  });
  
  $('div.flashobject').each(function() {
    var div = $(this);
    var id = this.id;
    var style = div.attr('style').toLowerCase();   
    var tokens = style.split(';');
    while (tokens.length > 0) {
      token = tokens.pop();
      if (token.indexOf('width:') != -1)
        var width = token.replace(/\D/g, '');
      if (token.indexOf('height:') != -1)
        var height = token.replace(/\D/g, '');      
    }
    var src = div.attr('src');
    //alert(id + ' ' + width + ' x ' + height + ' - ' + src);
    replaceByFlashObject(id, src, width, height);
  });
  
}

function replaceByFlashObject(objId, src, width, height) {
  attributes.id = objId;

  if (src == flvPlayerPath) {
    var element = $('#' + objId);
    if (element)
  		display_style = element.attr("display");

    // rebuild flashvars for new flash player
    flv = flashvars.flv;
    if (flv.indexOf('?') != -1)
      flv = flv.substr(0, flv.indexOf('?')); // get rid off trailing timestamp
    
    // set autostart to true or false
    autostart = flashvars.autoplay == 1 ? true : false;
    
    // turning off preview image if autostart is enabled
    // required, because FF3 was showing the preview image even when the video was running
    preview_image = autostart ? null : flashvars.image;
  
    flashvars = { file:      flv,
                  image:     preview_image,
                  autostart: autostart,
                  bufferlength: buffertime };
  
    /*alert(src + "\n" +
          objId + "\n" + 
          width + "\n" +
          height + "\n" +
          minFlashVersion + "\n" +
          updateURL + "\n" +
          flashvars.file + "\n" +
          flashvars.image + "\n" +
          flashvars.autostart + "\n" +
          flashvars.bufferlength + "\n" +
          attributes.id + "\n" +
          attributes.styleclass + "\n" +
          attributes.name);*/
  }

  swfobject.embedSWF(src, objId, width, height, minFlashVersion, 
                     updateURL, flashvars, params, attributes);

  if (src == flvPlayerPath) {
    element = $('#' + objId);
    if (element && (display_style == "none")) {
      element.css({visibility: hidden, display: none});
      element.addClass('hidden');
    }
  }
}
