// look for action key in the calling document
var action = (action ? action : 'none');
var fadeout = (fadeout ? fadeout : 'fadeout');

var svgDiv;

function onLoadInit() {
  svgDiv = document.createElement('div');
  initSVG();
}
function initSVG() {
	// functions that pull in an SVG on load.
	try {
		if (new XMLHttpRequest()) {
			asyncInsertDoc(url=svgURL,type='svg');
		}
	}
	catch (e) {
		//stick in the fallback image instead
		img = document.createElement('img');
		img.src = svgURL.fallback;
		img.style.position = 'absolute';
		img.style.left = '10em';
		img.style.top = '2em';
		document.body.insertBefore(img,document.getElementsByTagName('div')[0]);
	}
}
function asyncInsertDoc(url,type) {
	// insert SVG: wrapper
	var arequest = new XMLHttpRequest();
	arequest.open('GET', url[type], true);
	arequest.onreadystatechange = function () {insertSVG(arequest,action,fadeout)};
	arequest.send(null);
}
function insertSVG(arequest,action,fadeout) {
	var iter = 0;
	// insert the SVG as a child of another node, adding it to the document
	if (arequest.readyState == 4 && arequest.status == 200 && iter < 4) {
		var svgDoc = arequest.responseXML;
		
		var svg = document.importNode(svgDoc.childNodes[0],1);
		
		var paths = svg.getElementsByTagName('path');
		// dim out the svg by reducing its opacity
		for (var p = 0; p < paths.length; p++) {
			paths[p].setAttribute('opacity','0.2');
		}
		svgDiv.setAttribute('id','svg');
		document.body.insertBefore(svgDiv,document.getElementsByTagName('div')[0]);
		svgDiv.appendChild(svg);
		iter += 1;
		if (action != 'none'){
			// do post-load stuff that requires the SVG to be in the DOM
			whenSVGLoadFunc(action,fadeout);
		}
	}
}
function whenSVGLoadFunc(action,fadeout) {
	// after the SVG has been loaded in.
        // a timer to control the desired effect
	var timer1 = setInterval(function () {eval(action+'()')}, 1);
        // a timer to control the ending of the animation
	var timer2 = setTimeout(function () {eval(fadeout+'()')}, 100);
}
var svgcounter = 0;

function svgToTransform() {
  svg = new Object();
  svg.element = document.getElementById('svg').childNodes[0];
	svg.counter0 = 0;
	svg.rotate = 0;
	svg.scale = 1;
	svg.translate = 1;
	svg.opacity = 1;
	return svg;
}
// transitions (one works...)
function doNothing() {
  // do nothing
}
function fadeOut() {
  var svg = svgToTransform();
	timer0 = setInterval(function () {
		svg.counter0 += 1; 
		if (svg.opacity >= 0.001) {
			svg.opacity = svg.opacity * 0.95;
			svg.element.parentNode.setAttribute('style','opacity:' + svg.opacity);
		};
		if (opacity < 0.001) {
			clearInterval(timer0);
		}
	}, 10);
}
function transformFadeOut() {
  var svg = svgToTransform();
  var svg = svg.element;
  var scale = svg.scale;
  var translate = svg.translate;
  var opacity = svg.opacity;
	timer0 = setInterval(function () {
		svg.counter0 += 1;
		if (opacity >= 0.001) {
			scale = scale *= 1.02;
			translate = translate *= ( svg.scale / 1.1);
			opacity = opacity * 0.95;
			svg.setAttribute('transform','translate(' + translate + ',' + -translate + ',), scale(' + scale + ')'); 
		};
		if (opacity < 0.001) {
			clearInterval(timer0);
		}
	}, 10);
}
function opacitise() {
	// twiddle with SVG
  var svg = document.getElementById('svg');
	var paths = svg.getElementsByTagName('path');
	var stopValue = paths.length * 5;
	if (svgcounter < stopValue) {
		try {
			var random = Math.round(paths.length*Math.random());
			opacity = Number(paths[random].getAttribute('opacity'));
			if (opacity < 1) {
				paths[random].setAttribute('opacity',(opacity + 0.2).toString());
				svgcounter = svgcounter + 1;
			}
		}
		catch (e) {
		}
	}
	if (svgcounter == stopValue) {
		clearInterval(timer1);
	}
	return svgcounter;
}
