/***************************************
 replace specified images with SVG images
Based on the script written by Alexis "Fyrd" Deveria, 11/28/2007
****************************************

License: http://creativecommons.org/licenses/LGPL/2.1/

*/

var svgRE = /.*\.svg/i;

function testCapabilitiesAndReplaceSVG() {
	var testImg = 'data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIyNzUiIGhlaWdodD0iMjc1Ij48L3N2Zz4%3D';
	var img = document.createElement('img')
	img.setAttribute('src',testImg);

	if(!img.complete) {
		replaceSVGImages();
	}
}

function replaceSVGImages(){
	var imgs = document.getElementsByTagName("img");
	for(var i=0,l=imgs.length;i<l;i++){
		var img = imgs[i];
		if(img.src.match(svgRE)){
			var o = document.createElement("object");
			var imgWidth = img.getAttribute("width");
			var imgHeight = img.getAttribute("height");
			if(imgWidth) o.width = imgWidth;
			if(imgHeight) o.height = imgHeight;

			if(!window.ActiveXObject){
				o.type = "image/svg+xml"
				o.data = img.src;
			}else{
				//o.setAttribute("src",img.src);
                                //fascinating bug: it looks like o.setAttribute("src",whatever); o.getAttribute("src") will always return null
                                //but setting the attribute using object properties as below works correctly!
                                //classid attribute works either way
				o.src=img.src;
				o.classid="image/svg+xml";
			}

			img.parentNode.replaceChild(o,img);
		}
	}
}

//this should be run before svg-web

    if (document.addEventListener) {
      // DOMContentLoaded natively supported on Opera 9/Mozilla/Safari 3
      document.addEventListener('DOMContentLoaded', function() {
          testCapabilitiesAndReplaceSVG();
      }, false);
    } else { // Internet Explorer
      // id is set to be __ie__svg__onload rather than __ie_onload so
      // we don't have name collisions with other scripts using this
      // code as well
      document.write('<script id="__ie__svg__onload1" defer '
                      + 'src=//0><\/script>');
      var script = document.getElementById('__ie__svg__onload1');
      script.onreadystatechange = function() {
        if (this.readyState == 'complete') {
          // all the DOM content is finished loading -- continue our internal
          // execution now
          testCapabilitiesAndReplaceSVG();
        }
      }
    }

