/** Gets a string suitable for inserting into our document in order to setup our Flash object.
    original: http://code.google.com/p/doctype/wiki/ArticleFixingFlashExternalInterface

    @param width The width of the Flash object, such as "600".
    @param height The height of the Flash object, such as "300".
    @param id The ID that will be added to the Flash object.
    @param backgroundColor An optional color that will be used for the background, such
     as 'red'. If you want the Flash to be transparent, set this to null and set
     backgroundTransparent to true. If you provide a backgroundColor the background will _not_
     be transparent.
    @param backgroundTransparent Whether you want the background of the Flash object to
     show through. Set to true if you want this and make sure backgroundColor is null.
    @param style An optional style string to copy into the Flash object.
    @param className An optional className to copy into the Flash object.
    @param libraryPath A path to the SWF file; useful so you can grab your SWF file if
    it is relative to where your library is installed. For example, this could be
    '../../'. Make sure that this relative path ends in a slash.
    @param swfFileName The name of your swf file, such as 'svg.swf'.

    @returns HTML string suitable for inserting somewhere into the page. */
function getFlashString(width, height, id, backgroundColor, backgroundTransparent, style, className, libraryPath, swfFileName) {
    var src = libraryPath + swfFileName;
    var protocol = window.location.protocol;
    if (protocol.charAt(protocol.length - 1) == ':') {
      protocol = protocol.substring(0, protocol.length - 1);
    }

    // some example FlashVars, showing how to properly encode them
    // FIXME: delete this or add in your own in order to pass flags into your Flash object
    var flashVars =
          'uniqueId=' + encodeURIComponent(id)
        + '&sourceType=string'
        + '&scaleMode=showAll'
        + '&debug=true'
        + '&svgId=' + encodeURIComponent(id);

    var flash =
          '<object\n '
            + 'classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"\n '
            + 'codebase="'
            + protocol
            + '://fpdownload.macromedia.com/pub/shockwave/cabs/flash/'
            + 'swflash.cab#version=9,0,0,0"\n '
            + 'width="' + width + '"\n '
            + 'height="' + height + '"\n '
            + 'id="' + id + '"\n '
            + 'name="' + id + '"\n '
            + 'style="' + style + '"\n '
            + 'class="' + className + '"\n '
            + '>\n '
            + '<param name="allowScriptAccess" value="always"></param>\n '
            + '<param name="movie" value="' + src + '"></param>\n '
            + '<param name="quality" value="high"></param>\n '
            + '<param name="FlashVars" value="' + flashVars + '"></param>\n '
            + (backgroundColor ? '<param name="bgcolor" value="'
                                    + backgroundColor + '"></param>\n ' : '')
            + (backgroundTransparent ?
                                    '<param name="wmode" value="transparent">'
                                    + '</param>\n ' : '')
            + '<embed '
              + 'src="' + src + '" '
              + 'quality="high" '
              + (backgroundColor ? 'bgcolor="' + backgroundColor
                                     + '" \n' : '')
              + (backgroundTransparent ? 'wmode="transparent" \n' : '')
              + 'width="' + width + '" '
              + 'height="' + height + '" '
              + 'id="' + id + '" '
              + 'name="' + id + '" '
              + 'swLiveConnect="true" '
              + 'allowScriptAccess="always" '
              + 'type="application/x-shockwave-flash" '
              + 'FlashVars="' + flashVars + '" '
              + 'pluginspage="'
              + protocol
              + '://www.macromedia.com/go/getflashplayer" '
              + 'style="' + style + '"\n '
              + 'class="' + className + '"\n '
              + ' />'
          + '</object>';

    return flash;
}

/** Inserts a Flash object into the page.

    @param flash Flash HTML string.
    @param replaceMe Some DOM node already in the document to replace with the Flash object, such as a DIV.

    @returns The Flash DOM object. */
function insertFlash(flashStr, replaceMe) {
   var flashObj;
   if (document.all && !navigator.userAgent.indexOf('Opera') >= 0) { // IE
      // Note: as _soon_ as we make this call the Flash will load, even
      // before the rest of this method has finished. The Flash can
      // therefore finish loading before anything after the next statement
      // has run, so be careful of timing bugs.
      replaceMe.outerHTML = flashStr;
   } else { // other browsers
      // do a trick to turn the Flash HTML string into an actual DOM object
      // unfortunately this doesn't work on IE; on IE the Flash is immediately
      // loaded when we do div.innerHTML even though we aren't attached
      // to the document!
      var div = document.createElement('div');
      div.innerHTML = flashStr;
      flashObj = div.childNodes[0];
      div.removeChild(flashObj);

      // at this point we have the OBJECT tag; ExternalInterface communication
      // won't work on Firefox unless we get the EMBED tag itself
      for (var i = 0; i < flashObj.childNodes.length; i++) {
         var check = flashObj.childNodes[i];
         if (check.nodeName.toUpperCase() == 'EMBED') {
            flashObj = check;
            break;
         }
      }

      // now insert the EMBED tag into the document
      replaceMe.parentNode.replaceChild(flashObj, replaceMe);
   }

   return flashObj;
}
