var $j = jQuery.noConflict();

function initMediaplayer() {
	
	var count = 0;
	
	$j("a[href$='.mp3'][href*='http']").each( function(i, el) {
		
                
	});
	
	$j("a[href$='.mp3'][href*='editorial']").each( function(i, el) {
				
		$j(el).replaceWith('<a href="javascript: void(0);" onclick="createMediaplayer( ' + count + ', \'' + $j(this).attr("href") + '\',\''+$j(this).html()+'\');" id="file_' + count + '">' + $j(this).html() + '</a>');
					
		count++;
                
	});
	
	
}

function createMediaplayer( id, file , text ) {

	var width = 400;
	var height = 20;
	
	if( file && width && height ) {
		
		var text = $j('#file_' + id).html();
		
		$j('#file_' + id).before('<div id="mediaplayer' + id + '"></div>');
		
		$j('#file_' + id).replaceWith('<div class="mediaplayer_' + id + '_a" style="padding: 10px 0 10px 0;"><a href="javascript: void(0);" onclick="javascript: removeMediaplayer(\'' + id + '\', \'' + file + '\', \'' + text + '\');">stop</a></div>');

		//
		
		var flashvars = false;
		
		var params = {
			menu: 'false',
			allowfullscreen: 'true',
			flashvars: 'autostart=true&file='+file
		};
		
		var attributes = {
			id: 'mediaplayer_' + id,
			name: 'mediaplayer_' + id
		};

		swfobject.embedSWF('./js/mediaplayer.swf', 'mediaplayer' + id, ( width - 30 ), height, '9.0.0','expressInstall.swf', flashvars, params, attributes);
		
		//
		
		$j('#mediaplayer_' + id).css("margin-top", "10px");
	}
	
}

function removeMediaplayer( id, file, text ) {
	
	$j('#mediaplayer_' + id).remove();
	
	$j('.mediaplayer_' + id + '_a').replaceWith('<a href="javascript: void(0);" onclick="createMediaplayer(' + id + ', \'' + file + '\');" id=file_' + id + '>' + text + '</a>');

}

$j(document).ready( function() {
	initMediaplayer();
});
