/* 
    Image to Flash swf v2.1 - 26/05/2010
    mike foskett - http://websemantics.co.uk/resources/embed_flash/

    Replaces multiple images of class="img2swf" with Flash swf files in an accessible manner.

    Usage:
      <div>
        <img class="img2swf" src="example.jpg" width-"160" height="160" title="" alt="alt text here" />
      </div>
    
    Requires:
      image and swf in the same directory.
      image and swf to have the same name and dimensions.

    History:
      v2.1: Ability to use flashVars added.
      v2.0: Removed the requirement for image to have an id.
            Replaced Flash embed method.
            Parent width & height set to minimise layout disturbance.
            Function inside a closure.
            wmode transparent added as default (to allow html overlays)

*/
var img2swf=function(){

  var minFlash=7; // the minimum version of Flash player required to run
  var imgClass="img2swf";

  /* author: Simon Willisons - http://simonwillison.net/2004/May/26/addLoadEvent/ */
  function addLoadEvent(f){var o=window.onload;if(typeof window.onload!='function'){window.onload=f;}else{window.onload=function(){if(o){o();}f();};}}

  function isFlash(v){
  // tests which, if any, Flash player version is installed
    var testTo=20, installed=0, x;
    if (navigator.plugins && navigator.plugins.length){
      for (x=0;x<navigator.plugins.length;x++){
        if (navigator.plugins[x].name.indexOf('Shockwave Flash')!=-1){
          installed=parseInt(navigator.plugins[x].description.split('Shockwave Flash ')[1],10);
          break;
    } } }
    else if(window.ActiveXObject){
      for (x=2;x<=testTo;x++){
        try {if (eval("new ActiveXObject('ShockwaveFlash.ShockwaveFlash."+x+"');")){installed=x;}}
        catch(e){}
    } }
    return((installed>=v)?installed:0);
 }

  function flashInset(id){
  // reads image data and creates a Flash object

    if (isFlash(minFlash) && document.getElementById(id)){
      var obj=document.getElementById(id);
      if (obj.src){

        // Replace the image src extension with swf
        var src=obj.src.substring(0,obj.src.lastIndexOf("."))+".swf",

            // Get the image properties
            width=obj.width,
            height=obj.height,
            altText=obj.alt,
            parent=obj.parentNode;

        // If img in a link then go up a parent level
        if (parent.href){parent=parent.parentNode;}

        // Check required properties are available
        if (src && parent && width && height){

        // Build the Flash object
        var str='<!--[if !IE]> -->';
        str+='<object data="'+src+'" width="'+width+'" height="'+height+'" type="application/x-shockwave-flash">';
        str+='<!-- <![endif]-->';
        str+='<!--[if IE]>';
        str+='<object id="'+id+'" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version='+minFlash+',0,0,0" width="'+width+'" height="'+height+'">';
        str+='<param name="movie" value="'+src+'" />';
        str+='<!--><!-- -->';
        if (!(typeof flashVars==="undefined")){
          str+='<param name="flashvars" value="'+flashVars+'" />';
        }
        str+='<param name="quality" value="high" />';
        str+='<param name="wmode" value="transparent" />';
        str+='<p>'+altText+'</p>';          
        str+='</object>';
        str+='<!-- <![endif]-->';

        // set container dimesions to minimise layout disturbance 
        parent.style.width=width+"px";
        parent.style.height=height+"px";
        parent.style.overflow="hidden";

        // Replace the images parent with the built object
        parent.innerHTML=str;
	} } } }


  function init(){

    // Only run the script when the page has fully loaded
    addLoadEvent(function(){

      // Get a list of all images on the page
      var imgs=document.getElementsByTagName('img'),
          imgIDs=[];

      // work through each image details
      for (var i=0;i<imgs.length;i++){

        // If image class is "img2swf" add id to a new list
        if ((imgs[i].className==imgClass)){
          imgs[i].id=(imgs[i].id)?imgs[i].id:imgClass+i;
          imgIDs[i]=imgs[i].id;
      } }

      // Replace each image with Flash swf
      for (i=0;i<imgIDs.length;i++){
        flashInset(imgIDs[i]);
      }
    });
  }

  return{
    init:init
  };

}();

// To use flashvars declare a variable before the img2swf call.
//  Example
//    <script type="text/javascript">/*<![CDATA[*/
//      var flashVars="VARS IN HERE";
//    /*]]>*/</script>
//

img2swf.init();

