/*
	Provide sounds and onmouseover utility functions in MSIE.
	Doesn't work, but gracefully degrades, elsewhere.
	
	We are not concerned about Firefox support at this point because stock
	Firefox installations will display an "Additional plugins are required
	to display all the media on this page" warning, which is not desirable.
	
	Notes:
	-	Do not execute addSound() methods from the <head>.  It relies on using
		DOM methods to add elements to document.body, which isn't there yet
		when executing JavaScript from the <head>.
	-	It should be obvious that running addSound() before setting up sound
		events is an excellent idea.  :-)

	example usage:
	
	<body>
		<script type="text/javascript">
			var sound1 = addSound("wheee.wav");
			var sound2 = addSound("cow_2.aiff");
		</script>
		...
		<p>onmouseover:
			<a href="#" onmouseover="startSound(sound1);">are we having fun</a><br />
			<a href="#" onmouseover="startSound(sound2);">how now brown cow</a>
		</p>
		...
	</body>
*/

var MSIE = (navigator.appVersion.indexOf("MSIE") != -1 && document.all);

var debugMessageElement;

var bgSoundElement;

function startSound (el) {
	if (!MSIE) {
		return;
	}
	if (el == null) {
		return;
	}
	if (bgSoundElement) {
		bgSoundElement.src = "";
		bgSoundElement.src = el.src;
	} else {
		if (el.ReadyState && el.ReadyState >= 4) {
			el.stop();
			el.play();
		}
	}
}

function stopSound (el) {
	if (!MSIE) {
		return;
	}
	if (el == null) {
		return;
	}
	if (bgSoundElement) {
		bgSoundElement.src = "";
	} else {
		if (el.ReadyState && el.ReadyState >= 4) {
			el.stop();
		}
	}
}

function addSound (soundFile) {
	if (!MSIE) {
		return;
	}
	if (!document.createElement) {
		return;
	}
	if (!document.body) {
		return;
	}

	//	if (!bgSoundElement) {
	//		document.write("<bgsound id=\"bgSoundElement\" />");
	//		bgSoundElement = document.getElementById("bgSoundElement");
	//	}

	// This also has the effect of preloading the sound.
	var embed = document.createElement("embed");
	embed.style.display = "none";
	embed.hidden = "true";
	embed.autostart = "false";
	embed.src = soundFile;
	embed.startFunction = function () {
		startSound(embed);
	};
	/* if prependChild() existed, this is what it would do. */		
	if (document.body.hasChildNodes()) {
		document.body.insertBefore(embed, document.body.childNodes[0]);
	} else {
		document.body.appendChild(embed);
	}
	return embed;
}

/*
	setupSoundRolloversOnElements() accepts the following paramenters:

	-	container - a container element within which certain elements
		will have sound rollover triggers added
	-	sound - a sound object created using addSound().
	-	tagName - an HTML tag name
	-	className (OPTIONAL) - a CSS class name

	This function will find any elements whose tag name is tagName within
	the container element, and will add onmouseover events to those elements
	to play the specified sound.  If the optional className argument is
	specified, then only those elements matching the tagName that also have
	the specified className as one of the classes in the element's class
	attribute will receive the sound rollovers.

	Example:
		<div id="container">
			<a class="hover" href="1.html">page 1</a>
			<a class="hover fred" href="2.html">page 2</a>
			<a class="fred" href="3.html">page 3</a>
			<a href="4.html">page 4</a>
		</div>
		<script type="text/javascript">
			var container = document.getElementById("container");
			var sound = addSound("moo.wav");
			setupSoundRolloversOnElements(container, sound, "a", "hover");
			// triggers will be added to the "page 1" and "page 2" links,
			// but not the "page 3" and "page 4" links.
		</script>
*/

function setupSoundRolloversOnElements (container, sound, tagName, className) {
	if (!MSIE) return;
	if (!RegExp) return;
	var classNameRegex;
	if (className != null) {
		/* We need to match the className in a string containing
		   multiple space-separated class names. */
		classNameRegex = new RegExp("(^|\\s)" + className + "(\\s|$)");
	}
	if (container && container.getElementsByTagName) {
		var els = container.getElementsByTagName(tagName);
		for (i = 0; i < els.length; ++i) {
			var el = els[i];
			if (className != null) {
				if (el.className == null) continue; // next element
				if (!classNameRegex.test(el.className)) continue; // next elem.
			}
			el.onmouseover = sound.startFunction;
		}
	}
}

/*
	setupSoundRolloversOnLinks() accepts a container element within an HTML
	document, finds any links within that container element, and triggers
	the specified sound to be played upon mouseover.
	
	Example:
		<div id="container">
			<a href="1.html">page 1</a>
			<a href="2.html">page 2</a>
			<a href="3.html">page 3</a>
			<a href="4.html">page 4</a>
		</div>
		<script type="text/javascript">
			var container = document.getElementById("container");
			var sound = addSound("moo.wav");
			setupSoundRolloversOnLinks(container, sound);
		</script>
*/

function setupSoundRolloversOnLinks (container, sound) {
	if (!MSIE) return;
	setupSoundRolloversOnElements(container, sound, "a");
}

