var AudioPlayer = function(pBridgeIdentifier) {
	
	this.bridgeID = pBridgeIdentifier;
	this.bridge = "FABridge."+pBridgeIdentifier;
	this.swfPath = "";
	this.songId = "";
	this.playing = false;
	this.streamUrl = "";
	
	this.cashedIn = true;
		
	this.loadSong = function(pFilename)
	{
		eval(this.bridge).root().loadSong(pFilename);
	}
	
	this.play = function()
	{
		eval(this.bridge).root().playSong();
		this.playing = true;
	}
	
	this.pause = function()
	{
		eval(this.bridge).root().pauseSong();
		this.cashedIn = false;
		this.playing = false;
	}
	
	this.setPlayerTitle = function(pTitle)
	{
		eval(this.bridge).root().setTitle(pTitle);
	}
	
	this.writeFlashObject = function(pTuneId) {
		var ap = "<div id='AudioPlayer_"+this.bridgeID+"' style='visibility:hidden;position:absolute'><object id='AudioPlayer_"+this.bridgeID+"_o' classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0' height='0' width='0'>";
		ap += "<param name='flashvars' value='bridgeName="+this.bridgeID+"'/>";
		ap += "<param name='src' value='"+this.swfPath+"AudioPlayer.swf?bridgeName="+this.bridgeID+"'/>";
		ap += "<embed name='AudioPlayer_"+this.bridgeID+"' pluginspage='http://www.macromedia.com/go/getflashplayer' src='"+this.swfPath+"AudioPlayer.swf?bridgeName="+this.bridgeID+"' height='0' width='0' flashvars='bridgeName="+this.bridgeID+"'/>";
		ap += "</object></div>";
		
		jQuery("#tr_ap_"+pTuneId).append(ap);
	}
	
	this.callback = function() {
		// replace this function in the instance to recieve callback calls
	}
}

AudioPlayer.createPlayer = function(tuneId, streamUrl)
{
	var ap = new AudioPlayer("audioplayer"+tuneId);
	ap.swfPath = "/swf/";
	ap.writeFlashObject(tuneId);
	ap.songId = tuneId;
	ap.streamUrl = streamUrl;
	
	return ap;
}

AudioPlayer.playBtnAction = function(e)
{
	var trPlayer = jQuery.data(this, 'trplayer')
	
	trPlayer.callback = function(erg) {alert("test " +erg);}
	
	trPlayer.loadSong(trPlayer.streamUrl);
	
	jQuery('.tr_play_btn').removeClass('pause');
	jQuery('.tr_play_btn').addClass('play');
	jQuery('.tr_play_btn').each(function(idx, elm) {
		if(jQuery.data(elm, 'trplayer').playing && jQuery.data(elm, 'trplayer') != trPlayer)
			jQuery.data(elm, 'trplayer').pause();
	});
	
	if(trPlayer.playing) {
		trPlayer.pause();
		jQuery(this).removeClass('pause');
		jQuery(this).addClass('play');
	} else {
		trPlayer.play();
		jQuery(this).removeClass('play');
		jQuery(this).addClass('pause');
	}
	
	this.focus();
}
