/* Audio
 * Version: 1.1
 * Author: Christian Reed (staggerwing@gmail.com)
 * This code takes an audio file link, as those generated by WordPress, and turns it into a Flash audio player.
 * It's for client work and not intended for a wide audience. Feel free to crib and learn from the code.
 * Ideally this would detect HTML5 audio player capability and insert that instead of Flash, but at the moment
 * HTML5 browser players don't seem to be fully baked enough to rely upon. At a later date it may make sense
 * to open up some of the commented capabilities and swap the check order.
 * Relies on player.swf and Modernizr.
*/

var flash_audio_count = 0;

jQuery(document).ready(function($) {
	
	// Supposed to make download links default to download dialog, but doesn't seem to work
	$('a.audio_download').live('click',function(event) {
		event.preventDefault();
		window.open($(this).attr('href'),'Download');
	});

});

(function($) {

	// Turns audio links into HTML5 players or replaces them with Flash players
	$.fn.audio_prep = function() {
		$(this).find('a[href*=".mp3"]').each(function() {
			var file_url = $(this).attr('href');
			var title_text = $(this).html();
			var download_link = $(this).clone().html('download').addClass('audio_download');

			if ( navigator.mimeTypes["application/x-shockwave-flash"] ) {
				$(this)
					.before(
						'<div class="audio_file">'
							+ '<span class="audio_title">' + title_text + '</span>'
							//+ '<a href="' + file_url + '" class="audio_download">download</a>'
							+ '<object type="application/x-shockwave-flash" data="' + template_url + '/js/player.swf" '
							+ 'id="audioplayer_' + flash_audio_count + '" class="audio_player" height="24" width="310">'
								+ '<param name="movie" value="' + template_url + '/js/player.swf">'
								+ '<param name="FlashVars" value="'
									+ 'playerID=' + flash_audio_count + '&amp;'
									+ 'bg=D9D9D9&amp;'
									+ 'leftbg=BBBBBB&amp;'
									+ 'lefticon=333333&amp;'
									+ 'voltrack=F2F2F2&amp;'
									+ 'volslider=666666&amp;'
									+ 'rightbg=A4A4A4&amp;'
									+ 'rightbghover=999999&amp;'
									+ 'righticon=333333&amp;'
									+ 'righticonhover=FFFFFF&amp;'
									+ 'loader=BBBBBB&amp;'
									+ 'track=FFFFFF&amp;'
									+ 'tracker=FF8800&amp;'
									+ 'border=B4B4B4&amp;'
									+ 'skip=666666&amp;'
									+ 'text=333333&amp;'
									+ 'titles=' + title_text.replace('&','and') + '&amp;'
									+ 'soundFile=' + file_url + '">'
								+ '<param name="quality" value="high">'
								+ '<param name="menu" value="false">'
								+ '<param name="wmode" value="transparent">'
							+ '</object>'
						+ '</div>')
					.remove();
				flash_audio_count ++;
			}

			/*else if ( Modernizr.audio && Modernizr.audio.mp3 && )
				$(this)
					.before(
						'<div class="audio_file">'
							+ '<span class="audio_title">' + title_text + '</span>'
							//+ '<a href="' + file_url + '" class="audio_download">download</a>'
							+ '<audio src="' + file_url + '" controls preload="none"></audio>'
						+ '</div>')
					.remove();*/
		});
		return this;
	}
	
	
})(jQuery);



