$(document).ready(function() {
	
	$('#content .wp-table-reloaded .column-2 a').each(function(){
		$(this).attr('alt', $(this).attr('href'));
		$(this).attr('href', 'javascript:void()');
	});
	
	$('#content .wp-table-reloaded .column-1').each(function(index){
		$(this).prepend('<span>' + $(this).html() + '</span');
	});
	
	$('#content .wp-table-reloaded .column-1 span').each(function(index){
		var title = $(this).find('a').html();
		if (title != null) {
			$(this).html(title);
		}
	});
	
	$('#content .wp-table-reloaded .column-1 a, #content .wp-table-reloaded .column-7 a').attr('target', '_blank');
	
	$('#content .wp-table-reloaded .column-2 a').bind('click', processClick);
	
});


function playerReady(obj) {
	//_debug('the media player ['+obj['id']+'] has been instantiated');
	if (obj['id'] == "forrest_player") {
		//obj['id'] != undefined
		player = (window.document[obj['id']]) ? window.document[obj['id']] : document.getElementById(obj['id']);
		player.addModelListener("state","stateTracker");
	}
}


function processClick()
{
	var playlist = player.getPlaylist();

	var currentFile = playlist[0].file
	var thisFile = $(this).attr('alt');
	
	// alert("current: " + currentFile + ", this: " + thisFile);
	
	if (currentFile == thisFile) {
		if (player.getConfig().state == 'PLAYING') {
			$(this).removeClass('playing').addClass('paused');
			player.sendEvent("PLAY", "false");
		} else if (player.getConfig().state == 'PAUSED') {
			$(this).removeClass('paused').addClass('playing');
			player.sendEvent("PLAY", "true");
		}
	} else {
		$('#content .wp-table-reloaded .column-2 a').removeClass('paused').removeClass('playing');
		$(this).addClass('buffering');
		player.sendEvent("LOAD", { file: thisFile });
		player.sendEvent("PLAY", "true");
	}
	
	return false;
}

function stateTracker(obj) {
	// alert(obj.newstate);
	switch(obj.newstate) {
		case "PLAYING":
			$('#content .wp-table-reloaded .column-2 a.buffering').removeClass('buffering').addClass('playing');
		break;
		case "COMPLETED":
			$('#content .wp-table-reloaded .column-2 a').removeClass('playing');
			player.sendEvent("LOAD", { file: "preview_holder.mp3" });
			// player.sendEvent("STOP");
		break;
	}
};
