// MP3 OnClick Replacer ------------------------------------------------------//
// By Emma Stott, for Sarah Helene

// License: http://creativecommons.org/licenses/by-sa/2.0/uk/

// Configuration -------------------------------------------------------------//

soundManager.url               = '/swf/'; // Set this to the path where you store the SoundManager swf files.
soundManager.waitForWindowLoad = true;    // Have SoundManager wait for everything to finish loading before running.

// OnLoad Handler ------------------------------------------------------------//

// This function runs when SoundManager has finished loading, and goes through
// <a> tags to find all the properly marked mp3 links. When it finds one, it
// will add an OnClick and OnMouseOver to replace the default behaviour.

// Technical notes:
// - Only <a> tags will be checked.
// - The anchors need to have the rel attribute set to 'soundmanager'.
// - The href MUST contain a link to a file ending in '.mp3'.
// - For SoundManager to reliably play the file, it should be 128kbps, 22/44KHz.
// - A custom attribute is assigned to the anchor containing the ID - not all
//   browsers may support this, but it is much faster and cleaner.
soundManager.onload = function()
{
	// Variables for later use.
	var HRef;
	var MP3;
	var Sound;
	// Collect all the anchors in the document.
	var Anchors = window.document.getElementsByTagName('a');
	// Loop through them.
	for (var Index = 0; Index < Anchors.length; Index++)
	{
		// Test for the 'rel' attribute being set to 'soundmanager'.
		if (Anchors[Index].getAttribute('rel') === 'soundmanager')
		{
			// Get the HRef location from the anchor.
			HRef = Anchors[Index].href;
			// Use regular expressions to extract the filename. RegExp returns
			// an array, of which the first entry is the matching text in full,
			// and in this case the second entry is the captured filename.
			MP3 = /^.+\/([^\/]+\.mp3)$/i.exec(HRef)[1];
			// Clear the sound object to ensure it's free, then assign it a new
			// SoundManager instance. Options used are:
			// - Auto loading enabled.
			// - Auto playing disabled.
			// - ID set to the mp3 name
			// - Multiple playing disabled
			// - URL set to the original link location
			Sound = null;
			Sound = soundManager.createSound({autoLoad: true,
			                                  autoPlay: false,
			                                  id: MP3,
			                                  multiShot: false,
			                                  url: HRef})
			// If the sound object was created successfully...
			if (Sound !== null)
			{
				// Assign a custom attribute to the anchor for performance. May
				// not be supported by all browsers, so test it before assigning
				// the OnClick event.
				Anchors[Index].setAttribute('soundmanager_id', MP3);
				if (Anchors[Index].getAttribute('soundmanager_id') === MP3)
				{
					Anchors[Index].onclick = function() {soundManager.play(this.getAttribute('soundmanager_id'));
					                                     return false;};
					//Anchors[Index].onmouseover = Sound.play;
				}
			}
		}
	}
	return true;
};
