var audioPlayers = [];

$(document).ready( function() 
{	
	
	if(parseInt(swfobject.ua.pv[0]) >= 9)
	{
		console.info("start")
		var audioCounter = 0;
		
		$("a[href$=mp3]").each( function(i) 
		{
			var audioUrl = "http://fireflyband.co.uk/" + $(this).attr("href");
			
			if($(this).parent().get(0).tagName == "P")
			{
				$(this).parent().addClass("collapseParagraph")
				
				var $swapElement = $(this).parent();
				var $newElement = $("<div class='collapseParagraph' />");
					$newElement.append( $swapElement.html() )
					
				$swapElement.replaceWith( $newElement )
				
			}
			
		})
		
		$("a[href$=mp3]").each( function(i) 
		{
			var audioUrl = "http://fireflyband.co.uk/" + $(this).attr("href");
			// $(this).removeAttr("href");
			$(this).addClass("audioPlayerLink");
			$(this).wrap("<div class='audioItem'>")
			
			var audioId = "audio-component-"+i;
			
			$(this).parent().prepend("<div id='--" + audioId + "'class='audioPlayerIcon'><div class='flashAudioPlayer' id='" + audioId + "'></div></div>")
			var $item = $(this);
				$item.attr("audioId", audioId);
				$item.attr("audioIdNumber", i);
				$item.addClass("open");
				$item.attr("title", $(this).text());
				$item.html("Play <strong>" + $(this).attr("title") + "</strong>")
				$item.attr("target", "_blank");
				$item.click(function() 
				{
					var p = "#--" + $(this).attr("audioId");
					
					if($(this).hasClass("active"))
					{
						$(this).removeClass("active")
						$(this).addClass("in-active")
						$(p).removeClass("active")
						$(p).addClass("in-active")
						// $(p).hide()
						// $(this).html("Play <strong>" + $(this).attr("title") + "</strong>")
						
					} else 
					{
						$(this).removeClass("in-active")
						$(this).addClass("active")
						
						$(p).removeClass("in-active")
						$(p).addClass("active")
						// $(p).show()
						
						// $(this).html("Stop <strong>" + $(this).attr("title") + "</strong>")
					}
					
				});
				
				var p = "#" + $item.attr("audioId");
				$(p).addClass("open");
			
			audioPlayers.push({htmlId:audioId, url:audioUrl })
			
		})
		
		var l = audioPlayers.length;
		
		for(var i = 0; i<l; i++)
		{
			var minFlashVersion = "9.0.0";
			var expressInstallSwf = false;
			var contentSwf	= "http://fireflyband.co.uk/wp-content/themes/firefly/swf/audio-player.swf";
			// var contentSwf	= "swf/audio-player.swf";
			var contentContainer = audioPlayers[i].htmlId;
			var contentWidth = "30px";
			var contentHeight = "30px";
			var contentFlashvars = { isAutoPlay:false, soundFile:audioPlayers[i].url, connectionName:"audioplayers", baseUrl:"" };
			var contentParams = { domain: "*", allowFullScreen: "true", allowscriptaccess: "always"};
			var contentAttributes = { id: "audio-website"+i };
			
			
			swfobject.embedSWF( 
				contentSwf, 
				contentContainer, 
				contentWidth, 
				contentHeight, 
				minFlashVersion, 
				expressInstallSwf, 
				contentFlashvars, 
				contentParams, 
				contentAttributes );
			
		}
	} else 
	{
		$("a[href$=mp3]").each( function(i) 
		{
			var audioUrl = "http://fireflyband.co.uk/" + $(this).attr("href");
			var $item = $(this);
				$item.attr("title", $(this).text());
				$item.html("Play <strong>" + $(this).attr("title") + "</strong>")
				$item.attr("target", "_blank");
				
		})
	}
	
})
