/*
 * This code searches for all the <script type="application/processing" target="canvasid">
 * in your page and loads each script in the target canvas with the proper id.
 * It is useful to smooth the process of adding Processing code in your page and starting
 * the Processing.js engine.
 */

$(document).ready(function (){
	$('replace').replaceWith(function(index, html) {
		if($(this).attr('type') == 'audio'){
			//<![CDATA[
			return "<audio src='"+$(this).attr('src')+"' controls='controls'>"+$(this).html()+"</audio>";
			//]]>
		}
	});
	if ( window.addEventListener ) {
		window.addEventListener("load", function() {
			var scripts = document.getElementsByTagName("script");
			
			for ( var i = 0; i < scripts.length; i++ ) {
				if ( scripts[i].type == "application/processing" ) {
					var src = scripts[i].src, canvas = scripts[i].nextSibling;
		
					if ( src && src.indexOf("#") ) {
						canvas = document.getElementById( src.substr( src.indexOf("#") + 1 ) );
					} else {
						while ( canvas && canvas.nodeName.toUpperCase() != "CANVAS" )
							canvas = canvas.nextSibling;
					}
	
					if ( canvas ) {
						new Processing(canvas, scripts[i].text);
					}
				}
			}
		}, false);
	}

});
