// SVGIR1.21 :: svgIR (SVG Image Replacement) 
// *****************************************************
// DOM scripting by brothercake -- http://www.brothercake.com/
// GNU Lesser General Public License -- http://www.gnu.org/licenses/lgpl.html
//******************************************************




//global variable for whether the Adobe SVG Viewer is supported
//we need it global because for IE it gets passed through VBScript 
var adobe = false;

//but here we're going to check for it using javascript
//so - if navigator.mimeTypes and the specific mime type is supported
if(typeof navigator.mimeTypes != 'undefined' && typeof navigator.mimeTypes['image/svg-xml'] != 'undefined')
{
	//check for the plugin 
	if(navigator.mimeTypes['image/svg-xml'].enabledPlugin != null) { adobe = true; }
}






//SVG image replacement object
function svgIR(eid, url, plugin)
{
	//the element to convert
	this.ele = document.getElementById(eid);
	
	//if it doesn't exist don't continue
	if(this.ele == null) { return; }
	
	
	//if native SVG is supported
	//which we can determine by testing for window.SVGElement in moz
	//but Opera doesn't implement the SVG DOM
	//so we'll use window.opera and spoof-resistent navigator information
	if(typeof window.SVGElement != 'undefined' || (typeof window.opera != 'undefined' && /opera[ \/]8/i.test(navigator.userAgent)))
	{
		//if XMLHttpRequest is supported
		if(typeof window.XMLHttpRequest != 'undefined')
		{
			//instantiate a new request object
			this.request = new XMLHttpRequest();
		}
		
		//if we've established a request object
		if(typeof this.request != 'undefined')
		{
			//reference to this
			var self = this;

			//bind readystate handler 
			this.request.onreadystatechange = function()
			{
				//if document has loaded and the status is okay or not-modified
				//(or there is no status number, which is opera viewing a page locally)
				if(self.request.readyState == 4 && /^(0|200|304)$/.test(self.request.status)) 
				{
					//pass the response DOM to import SVG
					self.importSVG(self.request.responseXML);
				}
			};

			//if overrideMimeType is supported, use it to set 
			//the response document's mime type to "text/xml"
			//this is because moz won't have a response DOM unless the XML is served with that mime
			if(typeof this.request.overrideMimeType != 'undefined')
			{
				this.request.overrideMimeType('text/xml');
			}

			//make the request
			this.request.open('GET', url, true);
			this.request.send(null);
		}
	}


	//else if we're using the secondary plugin method and the adobe viewer is detected
	else if(typeof plugin != 'undefined' && adobe)
	{
		//pass SVG document url, plus object width and height, to embed SVG method
		this.embedSVG(url, plugin[0], plugin[1]); 
	}

};


//import SVG data as XML
svgIR.prototype.importSVG = function(dom)
{
	//import the documentElement (SVG) node and its children
	var svg = document.importNode(dom.documentElement, true);

	//append it to the replacement element
	this.ele.appendChild(svg); 
};


//embed SVG for the adobe plugin 
svgIR.prototype.embedSVG = function(url, w, h)
{
	//if namespaced element creation is supported and this is not Safari (Mozilla + Viewer 6)
	if(typeof document.createElementNS != 'undefined' && navigator.vendor != 'Apple Computer, Inc.')
	{
		//embed the SVG document using that method 
		var svg = document.createElementNS('http://www.w3.org/1999/xhtml', 'object');
		svg.setAttribute('type', 'image/svg+xml');
		svg.setAttribute('data', url);
		svg.setAttribute('width', w);
		svg.setAttribute('height', h);

		//append it to the replacement element
		this.ele.appendChild(svg); 
	}
	
	//for any other environment (Internet Explorer and Safari)
	else
	{
		//embed the SVG by writing it in with innerHTML
		//because creating it with DOM methods doesn't work in Safari or Mac/IE5
		//and only works using DOM methods in Win/IE if the object is an <embed>
		this.ele.innerHTML += '<object type="image/svg+xml" data="' + url + '" width="' + w + '" height="' + h + '"></object>';
	}
};



