/**      Copyright (c) 2009, Frank Bruder [http://purl.org/net/2008,frankbruder/home]
 *
 *      Redistribution and use in source and binary forms, with or without
 *      modification, are permitted provided that the following conditions are
 *      met:
 *      
 *      * Redistributions of source code must retain the above copyright
 *        notice, this list of conditions and the following disclaimer.
 *      * Redistributions in binary form must reproduce the above
 *        copyright notice, this list of conditions and the following disclaimer
 *        in the documentation and/or other materials provided with the
 *        distribution.
 *      
 *      THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 *      "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 *      LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 *      A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 *      OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 *      SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 *      LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 *      DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 *      THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 *      (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 *      OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
/**
  IEfixObjectTags version 1.0
  
  Use the following HTML code to make object tags work in MSIE more like they
  do in other browsers. (Adapt the src attribute depending on your folder
  structure.)

  <!--[if IE]><script language="Javascript" type="text/javascript" defer="defer" src="/script/iefixobjecttags.js"></script><![endif]-->

  To make nested object tags work in IE 6, you also need to enclose them in
  conditional comments like this:

  <object type="image/svg+xml" data="example.svg" width="100%">
    <!--[if fallback 1]><!-->
    <object type="image/png" data="example.png">
    <!--<![endif]-->
      <!--[if fallback 2]><!-->
      <object type="text/html" data="example.html" width="100%" height="100%">
      <!--<![endif]-->
        object tags don't work. Textual fallback content follows.
        ...
      <!--[if fallback 2]><!-->
      </object>
      <!--<![endif]-->
    <!--[if fallback 1]><!-->
    </object>
    <!--<![endif]-->
  </object>
*/
/*@cc_on
window.attachEvent('onload', function() {
  var keepAspectRatio = function (objectNode, w_or_h, aspectRatio) {
    var flag = false;
    if (w_or_h == 'w') {
      listener = function () {
        if (!flag) {
          flag = true;
          objectNode.style.width = Math.ceil(objectNode.offsetHeight * aspectRatio).toString() + 'px';
          flag = false;
        }
      }
    } else {
      listener = function () {
        if (!flag) {
          flag = true;
          objectNode.style.height = Math.ceil(objectNode.offsetWidth / aspectRatio).toString() + 'px';
          flag = false;
        }
      }
    }
    objectNode.attachEvent('onresize', listener);
    listener();
  };
  var getLength = function (value) {
    var lengthUnit;
    if (value == 'auto') {
      return null;
    }
    if (lengthUnit = value.match(/(.*)(\%|px|pt|pc|cm|mm|in|em|ex)$/)) {
      return [parseFloat(lengthUnit[1]), lengthUnit[2]];
    } else {
      return [parseFloat(value), 'px'];
    }
  };
  var setObjectSize = function (objectNode, contentWidth, contentHeight, aspectRatio) {
    var width, height;
    if (objectNode.currentStyle.width != 'auto') {
      width = getLength(objectNode.currentStyle.width);
    }
    if (objectNode.currentStyle.height != 'auto') {
      height = getLength(objectNode.currentStyle.height);
    }
    if (width && height) {
      return;
    }
    if((!aspectRatio) && (contentWidth && contentHeight)) {
      //## not accounting for different units!
      aspectRatio = contentWidth[0] / contentHeight[0];
    }
    if (aspectRatio) {
      if (width) {
        if (width[1]=='%') {
          keepAspectRatio(objectNode, 'h', aspectRatio);
        } else {
          height = [width[0] / aspectRatio, width[1]];
          objectNode.style.height = height[0].toString()+height[1];
        }
        return;
      } else if (height) {
        if (height[1]=='%') {
          keepAspectRatio(objectNode, 'w', aspectRatio);
        } else {
          width = [height[0] * aspectRatio, height[1]];
          objectNode.style.width = width[0].toString()+width[1];
        }
        return;
      }
    }
    if (contentWidth && (!width)) {
      width = contentWidth;
      objectNode.style.width = width[0].toString()+width[1];
    }
    if (contentHeight && (!height)) {
      height = contentHeight;
      objectNode.style.height = height[0].toString()+height[1];
    }
    if (width && height) {
      return;
    }
    if (aspectRatio) {
      if (width) {
        if (width[1]=='%') {
          keepAspectRatio(objectNode, 'h', aspectRatio);
        } else {
          height = [width[0] / aspectRatio, width[1]];
          objectNode.style.height = height[0].toString()+height[1];
        }
        return;
      } else if (height) {
        if (height[1]=='%') {
          keepAspectRatio(objectNode, 'w', aspectRatio);
        } else {
          width = [height[0] * aspectRatio, height[1]];
          objectNode.style.width = width[0].toString()+width[1];
        }
        return;
      }
    }
    if (!width) {
      width = [300, 'px'];
      objectNode.style.width = '300px';
      if (height) {
        return;
      } else if (aspectRatio) {
        height = [width[0] / aspectRatio, width[1]];
        objectNode.style.height = height[0].toString()+height[1];
        return;
      }
    }
    if (!height) {
      height = [150, 'px'];
      objectNode.style.height = height[0].toString()+height[1];
    }
  };
  var idcount = (new Date()).getTime();
  var objectFallback = function(objectNode) {
    var id = objectNode.id ? objectNode.id : ('id_iefo_x'+(idcount++).toString(16));
    var revise = false;
    if(objectNode.innerHTML.match(/\<object\s/i)) {
      revise = true;
    }
    objectNode.outerHTML = '<span id="'+id+'" class="removedObjectTag">'+objectNode.innerHTML.replace(
      /\<\!(\-\-)?\[if\s+fallback(\s+\d+)?\s*\]\>/g,
      function (cc) {
        var d = cc.match(/\d+/);
        if (d) {
          d = parseInt(d[0]);
        } else {
          d = 1;
        }
        return cc.replace(/fallback(\s+\d+)?/, (d>1) ? 'fallback '+(d-1) : 'true');
      }
    )+'</span>';
    if(revise) {
      fixObjectTagsIn(document.getElementById(id));
    }
  }
  var fixObject = function (objectNode) {
    var contentWidth, contentHeight, contentAspectRatio;
    if (objectNode.type) {
      if (objectNode.type.match(RegExp('^image\\/.*', 'i'))) {
        if (objectNode.type.match(/^image\/svg\+xml/i)) {
          objectNode.wmode = 'transparent';
          var param = document.createElement("param");
          param.setAttribute("name", "src");
          param.setAttribute("value", objectNode.getAttribute("data"));
          if(objectNode.firstChild==null) {
            objectNode.appendChild(param);
          } else {
            objectNode.insertBefore(param, objectNode.firstChild);
          }
          var param = document.createElement("param");
          param.setAttribute("name", "wmode");
          param.setAttribute("value", "transparent");
          objectNode.insertBefore(param, objectNode.firstChild);

          try {
            if(! objectNode.getSVGDocument) {
              objectFallback(objectNode);
              return;
            }
          } catch (xc) {
          }
          try {
            var svgDocument = objectNode.getSVGDocument();
            var rootElement = svgDocument.documentElement;
            if(rootElement.namespaceURI+':'+rootElement.localName != 'http://www.w3.org/2000/svg:svg') {
              objectFallback(objectNode);
              return;
            }
          } catch (xc) {
            objectFallback(objectNode);
            return;
          }
          try {
            if (rootElement.hasAttributeNS(null, 'viewBox')) {
              var viewBox = rootElement.getAttributeNS(null, 'viewBox').match(/^\s*([^\s,]*)\s*[\s,]\s*([^\s,]*)\s*[\s,]\s*([^\s,]*)\s*[\s,]\s*([^\s,]*)\s*$/);
              if(viewBox) {
                if(viewBox[3] && viewBox[4]) {
                  if((isFinite(viewBox[3] = parseFloat(viewBox[3])))&&(isFinite(viewBox[4] = parseFloat(viewBox[4])))) {
                    contentAspectRatio = viewBox[3] / viewBox[4];
                  }
                }
              }
            }
            if (rootElement.hasAttributeNS(null, 'width')) {
              contentWidth = getLength(rootElement.getAttributeNS(null, 'width'));
              if ((contentWidth[1] == '%') || (!isFinite(contentWidth[0]))) {
                contentWidth = null;
              }
            }
            if (rootElement.hasAttributeNS(null, 'height')) {
              contentHeight = getLength(rootElement.getAttributeNS(null, 'height'));
              if ((contentHeight[1] == '%') || (!isFinite(contentHeight[0]))) {
                contentHeight = null;
              }
            }
          } catch(xc) {
          }
        } else {
          if(! objectNode.body) {
            self.setTimeout(function() {fixObject(objectNode);}, 500);
            return;
          }
          try {
            objectNode.body.style.margin = '0px';
            objectNode.body.style.border = 'none';
            objectNode.body.style.overflow = 'hidden';
            var imageNode = objectNode.body.getElementsByTagName('img').item(0);
            if (! imageNode) {
              imageNode = objectNode.body.document.createElement('img');
              imageNode.onerror = function () {
                objectFallback(objectNode);
              }
              imageNode.onload = function () {
                fixObject(objectNode);
              }
              objectNode.body.appendChild(imageNode);
              imageNode.src = objectNode.data;
              return;
            }
            if (imageNode.width) {
              contentWidth = [imageNode.width, 'px'];
            }
            if (imageNode.height) {
              contentHeight = [imageNode.height, 'px'];
            }
            contentAspectRatio = null;
            imageNode.style.width = '100%';
            imageNode.style.height = '100%';
          } catch (xc) {
          }
        }
      }
    }
    try {
      if (objectNode.body.document.fileSize === "") {
        objectFallback(objectNode);
        return;
      }
    } catch (xc) {
    }
    setObjectSize(objectNode, contentWidth, contentHeight, contentAspectRatio);
  };
  var fixObjectTagsIn = function(node) {
    var objecttags = node.getElementsByTagName('object');
    var objs = new Array(objecttags.length);
    for(n = 0; n < objecttags.length; n++) {
      objs[n] = objecttags.item(n);
    }
    for(n = 0; n < objs.length; n++) {
      fixObject(objs[n]);
    }
  }
  fixObjectTagsIn(document.body);
} );
@*/