/**
 * Attaches jmp3 player to an A-link.
 * Works with any structure like this:
 * 
 * <container class="playlist">
 * 	<container>
 * 		<a href="music.mp3">Song title</a>
 *  </container>
 * 	<container>
 * 		<a href="music.mp3">Song title</a>
 *  </container>
 * 	<container>
 * 		<a href="music.mp3">Song title</a>
 *  </container>
 * 	<container>
 * 		<a href="music.mp3">Song title</a>
 *  </container>
 * </container>
 * 
 * Specially for Colonnade
 */
(function($) {
	/**
	 * Configuration for jquery.jmp3.js
	 */
	var playerConfig = {
			showfilename: "false",
			backcolor: "000000",
			forecolor: "00ff00",
			width: 220,
			filepath: "/data/music/", // filepath + a.href = full file path
			autoplay: "true",
			showdownload: "false"
	};
	
	/**
	 * Counter for players
	 */
	var counter;
	
	/**
	 * Remove all players from document and restore closest hidden A
	 */
	function resetPlayers() {
		$.each($('span.player'), function() {
			$('a:hidden', $(this).parent()).show();
			$(this).remove();
		});
	}
	
	/**
	 * hide A, and create player instance
	 */
	function playMe(a) {
		resetPlayers();
		
		var a = $(a);
		var player = $('<span class="player">'+a.attr('href')+'</span>');
		
		a.hide();
		a.parent().append(player);
		
		player.jmp3(playerConfig);
	}
	
	/**
	 * Init player.
	 */
	$(document).ready(function () {
		$('a', $('.playlist')).click(function(event) {
			playMe(this);
			event.stopPropagation();
			return false;
		});
	});
})($);
