// --- USE --- 
/*
 <head>
 	<script type="text/javascript" src="soundscape.js"></script>
 </head>
 <body>
	<script type="text/javascript">attachsoundengine()</script>
 </body>
*/
// ---

// --- DEFEINE SOUNDENGINE VARIABLES ---
var soundscape = new SoundDocument();
var soundengine = "not loaded yet";
var soundenginepath = getscriptdir()+"soundengine.swf"; 
//alert("soundenginepath: "+soundenginepath);
// --- 

// -- API ---
function SoundDocument () {}
SoundDocument.prototype.createSound 	= function( filename ) { soundengine.createSound( filename ); } 
SoundDocument.prototype.preloadSound 	= function( filename ) { soundengine.preloadSound( filename ); } 
SoundDocument.prototype.playSound 	= function( filename ) { soundengine.playSound( filename ); } 
SoundDocument.prototype.stopSound 	= function( filename ) { soundengine.stopSound( filename ); }
SoundDocument.prototype.stopAllSounds 	= function() { soundengine.stopAllSounds(); }
// ---

// --- GET THE DIRECTORY THAT THIS SCRIPT IS IN ---
function getscriptdir()
{
	// this function finds the name of the current script and then
	// returns the directory it's in. it's useful if you want to load
	// several scripts or objects - you can instruct the user to 
	// put them all of them in the same directory. then, so long as 
	// they are named appropriately - you can load them dynamically.
	
	// the method commented out below is the simplest way to get the 
	// the src attribute for this script tag. unfortanuately, firefox
	// behaves a bit weirdly when you try to access the elements of a
	// list before the page has finished loading (it will work once,
	// but then will break if you try it again from another script). 
	// we are going to use another method.
	//
	//		// wonky in firefox
	//		var scripts = document.getElementsByTagName('script'); // all script tags loaded so far
	//		var scriptsrc = scripts[scripts.length-1].src; // src for last loaded script tag (this one)
	//
	// Usually, the current script tag is the last loaded element in the DOM
	// so we could get the last loaded element using the code below. Unfortunately, 
	// this code will break when for a script tag that has been added to the DOM
	// using appendChild(). So we are going to use yet another method.
	//
	//		// wonky for dynamically loaded script tags.
	//		var element = document.lastChild;
	//		while (element.childNodes.length>0) { element = element.lastChild; }
	//
	// The method we are using walks through every element that has already been 
	// attached to the DOM and stores the last SCRIPT tag that has a non-empty
	// value for its src attribute. This still won't work if the script is loaded 
	// after the page has been loaded (say, if you click a button that attaches a
	// script) but its the best we've got so far.
	
	// best we've got so far
	var lastscriptwithsrc = null;
	var checkforscriptwithsrc = function(obj)
	{
		if (typeof(obj.tagName)!="undefined") {
		if (obj.tagName=="SCRIPT") { 
		if (obj.src!="")
		{
			lastscriptwithsrc = obj;
		}}}
	}
	foreachelement(checkforscriptwithsrc,document);
	var element = lastscriptwithsrc;
	if (!element.src) { alert('script tag not found'); return ""; } // it should be a script tag

	// get the source of this script and determine its parent directory
	var scriptsrc = element.src;
	var scriptdir = (scriptsrc.lastIndexOf('/') != -1) ? scriptsrc.substr(0,scriptsrc.lastIndexOf('/')+1) : '';
	
	// return the parent directory of this script
	//alert("scriptsrc: "+scriptsrc);
	return scriptdir;	
}
function foreachelement(func, root)
{
    if (typeof(root)=="undefined") { root=document.body; }
    var walkelements = function(ele, elefunc)
    {
        elefunc(ele);
        if (ele.childNodes)
        {
            for(var index=0;index<ele.childNodes.length;index++)
            {                
                walkelements(ele.childNodes[index], elefunc);
            }
        }
    }
    walkelements(root,func);
}
// ---

// -- CALL THIS FUNCTION FROM WITHIN THE BODY TAG --
function attachsoundengine()
{
	// This function attaches a hidden flash object to the document.
	// The flash object will play sound in the background. What and when
	// to play will be controlled by javascript. This function must be 
	// called using a script tag inside the body of the document.
	var heightwidth = "0px";
	var flashtag =
		'<object id="soundengine_object" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="'+heightwidth+'" height="'+heightwidth+'" >'+
		'	<param name="movie" value="'	+soundenginepath+	'" />'+
		'	<param name="allowScriptAccess" value="sameDomain" />'+
		'	<embed src="'	+soundenginepath+	'" id="soundengine_embed" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" width="'+heightwidth+'" height="'+heightwidth+'"/>'+
		'</object>'+
		'</div>'
	;
	// the goal here is to make the flash player invisible so that it can play sound
	// in the background. All control of the sound is done through javacript. There are 
	// four methods that can be used for truly hiding something:
	// (1) style="display:none;"
	// (2) style="visibility:hidden;position:absolute;"
	// (3) style="height:0px;width:0px;overflow:hidden;"
	// (4) style="height:1px;width:1px;overflow:hidden;" (plus an inner div with style="margin:1px")
	// IEpc wont load a flash object if it hidden with methods (1) or (2) - (3) works sometimes - (4) always works.
	// FIREFOXpx and SAFARI won't truly hide a flash object when using method (3) or (4). So, we're stuck
	// with browser checking. 
	if (navigator.userAgent.toLowerCase().indexOf('msie')!=-1) // if IE
	{
		// method (4) above - and were going to use position:absolute anyway
		var hiddenflashtag = '<div style="position:absolute;top:0;left:0;height:1px;width:1px;overflow:hidden;"><div style="margin:1px">'+flashtag+'</div></div>';
	}
	else // if not IE 
	{
		// method (2) above
		var hiddenflashtag = '<div style="position:absolute;top:0;left:0;visibility:hidden;">'+flashtag+'</div>';		
	}
	// ideally, i would like to be able to attach this tag to the document
	// transparently. ie - attach a function on body onload that adds this tag
	// to document.body using appendChild. Unfortuantely, IE6 doesn't load
	// a flash object's external interface unless this tag is written to the 
	// document using document.write - so that's what were going to do. This 
	// function must be called using a script tag inside the body
	// of the document.	
 	document.write( hiddenflashtag );
}
// ---

// --- THIS FUNCITON GETS CALLED FROM FLASH ---
function onengineloaded()
{
	// this function gets called *FROM FLASH* after all ExternalInterface methods
	// have been defined. Ideally, we would call this function at body onload - 
	// but we cant because, while we can create a pointer to the flash tag, we 
	// cannot check to see whether functions have been defined on OBJECT or EMBED
	// tag. Firefox uses EMBED, IE and Safari use OBJECT. I much prefer object detection
	// to browser detection, and so we will wait for the flash interface so we can
	// detect which tag has the interface functions on it.
	soundengine = document.getElementById('soundengine_object');
	if (!soundengine.SystemCapabilities) { soundengine = document.getElementById('soundengine_embed'); }
	if (!soundengine.SystemCapabilities) { alert('could not load soundengine'); }
	//alert("soundengine: "+soundengine); 
}



