//load script function (for soundmanager)
/*
function loadScript(sURL, onLoad)
{
	function loadScriptHandler()
	{
		var rs = this.readyState;
		if (rs == 'loaded' || rs == 'complete')
		{
			this.onreadystatechange = null;
			this.onload = null;
			if (onLoad)
			{
				onLoad();
			}
		}
	}

	function scriptOnload()
	{
		this.onreadystatechange = null;
		this.onload = null;
		window.setTimeout(onLoad,20);
	}

	var oS = document.createElement("script");
	oS.type = 'text/javascript';

	if (onLoad)
	{
		oS.onreadystatechange = loadScriptHandler;
		oS.onload = scriptOnload;
	}
	oS.src = sURL;
	document.getElementsByTagName('head')[0].appendChild(oS);
}
*/

jQuery.fn.outerHTML = function(s)
{
	return (s) ? this.before(s).remove() : jQuery("<p>").append(this.eq(0).clone()).html();
}



/*
var sound;
var soundManager;
var playCount = 0;
var repeat = 1;
var sounds = [];
*/

$(function()
{
	$(document).ready(function()
	{

		var trackContainer = $("#content"),
			menu = $("ol li"),
			first = $("ol li:first"),
			dest_tracklisting = $("#content #tracklisting"),
			dest_header = $("#content #head"),
			dest_control = $("#content #controls a"),
			tracks = menu.find("a[href*='.mp3']"),
			baseURL;

		//fill content div on load
		dest_header.html(first.find("div:eq(1) h3").outerHTML());
		dest_tracklisting.html(first.find("div:eq(1) ul").outerHTML());
		dest_control.attr("href",first.find("a:first").attr("href"));
		
		//highlight menu item
		first.addClass("selected");
		
		//bind menu to change content
		menu.find("a").click(function(e)
		{
			e.preventDefault();
			
			var incoming = $(this).parents("li");
			
			//remove currently selected menu item
			menu.filter(".selected").removeClass("selected");
			
			//select new menu item
			incoming.addClass("selected");
			
			//change content pane to new content
			dest_header.html(incoming.find("div:eq(1) h3").outerHTML());
			dest_tracklisting.html(incoming.find("div:eq(1) ul").outerHTML());
			dest_control.attr("href",incoming.find("a:first").attr("href"));
		});




/*
		loadScript('./js/soundmanager.debug.js', function()
		{
			soundManager.url = './soundmanager/swf/';
			soundManager.flashVersion = 9;
			soundManager.useHTML5Audio = true;
			soundManager.useMovieStar = true;
			soundManager.waitForWindowLoad = true;
			
			var trackContainer = $("#content"),
				menu = $("ol li"),
				first = $("ol li:first"),
				dest_tracklisting = $("#content #tracklisting"),
				dest_header = $("#content #head"),
				dest_control = $("#content #controls a"),
				tracks = menu.find("a[href*='.mp3']"),
				dummy = "&#160;&#160;&#160;&#160;<span>play audio</span>",
				baseURL;

			//fill content div on load
			dest_header.html(first.find("div:eq(1) h3").outerHTML());
			dest_tracklisting.html(first.find("div:eq(1) ul").outerHTML());
			dest_control.attr("href",first.find("a:first").attr("href"));
			
			//highlight menu item
			first.addClass("selected");
			
			//bind menu to change content
			menu.find("a").click(function(e)
			{
				e.preventDefault();
				
				var incoming = $(this).parents("li");
				
				//remove currently selected menu item
				menu.filter(".selected").removeClass("selected");
				
				//select new menu item
				incoming.addClass("selected");

				//toggleSound(incoming.find("a:first").attr("href"));
				
				//change content pane to new content
				dest_header.html(incoming.find("div:eq(1) h3").outerHTML());
				dest_tracklisting.html(incoming.find("div:eq(1) ul").outerHTML());
				dest_control.attr("href",incoming.find("a:first").attr("href"));
			});
			
			//onload soundmanager
			soundManager.onload = function()
			{
				var setupSounds = function()
				{
					if ((tracks.length) && (trackContainer.hasClass("done") == false))
					{
						tracks.each(function()
						{
							var track = $(this),
								soundID = track.attr("href"),
								control = $("#controls a"),
								url = soundID;
								
							//console.log(soundID);
							
							//track.html(dummy);
							//track.addClass("sound");
				
							//loop through to create each sound on page load
							sounds[soundID] = soundManager.createSound({
								id: soundID,
								url: url
							});
							
							//click a href and do what...
							control.click(function(e)
							{
								//soundManager.stopAll();
								//tracks.filter(".playing").removeClass("playing");

								toggleSound(control);

								e.preventDefault();
								return false;
							});

						});
						
						trackContainer.addClass("done");
					}
				}
		
				var toggleSound = function(track)
				{
					var soundID = track.attr("href"),
						control = $("#controls a");
					//var img = track.find("img").attr("src");
					
					if (track.hasClass("playing"))
					//stop sound
					{
						tracks.removeClass("playing");
						track.removeClass("playing");
						control.text("stop");
						sounds[soundID].setVolume(100);
						sounds[soundID].stop(soundID);
					}
					else
					//play sound
					{
						tracks.removeClass("playing");
						track.addClass("playing");
						control.text("play");
						sounds[soundID].setVolume(100);
						sounds[soundID].play(soundID);
					}
				}

				setupSounds();
			}
	
			//ensure start-up in case document.readyState and/or DOMContentLoaded are unavailable
			soundManager.beginDelayedInit(); 
		});
*/
	});
});








