//unique id, passed into the player itself AND the proxy
var uid = new Date().getTime();
var flashProxy = new FlashProxy(uid, '/flash/JavaScriptFlashGateway.swf');

//the track click event
function trackClick(e) {
	//call the actionscript function via the proxy
	flashProxy.call('jsPlayTrack', e);
}

//the jquery ready function is unstable in IE7
//attach event to track listing
onloadsAdd( "$('span.trackplay').css( {_cursor:'hand', cursor:'pointer'} ).click( function() { trackClick( $(this).attr('id') ) } );" );

//attach event to playlist listing
onloadsAdd( "$('span.playlist-play').css( {_cursor:'hand', cursor:'pointer'} ).click( function() { trackClick( $(this).attr('id') ) } );" );
	
/*
 * Icon Management
 * In page track icon management
 */
var prevTrackId = 0;

//vars used to manage the in page playlist, separate from tracks
var playlistPrevTrackID;
var playlistTrackId;

function updateTrackIcons(id) {
	//alert("click");
	if (prevTrackId != 0) {
		//when we have 2 sections on a page that both need to pass in the same track id, we need to access them separately
		//(one using a modified trackid as accessor) as getElementById only retrieves the first element on the page w/ the 
		//given id. Whichever section that comes second on the page will need to use the modified trackid, in this instance,
		//the track listing.
		
		//playlist css management
		document.getElementById(prevTrackId).setAttribute("class", "playlist-play");
		document.getElementById(prevTrackId).setAttribute("className", "playlist-play");
		
		//track listing css management
		tracksPrevTrackID = prevTrackId + "_1";
		document.getElementById(tracksPrevTrackID).setAttribute("class", "play");
		document.getElementById(tracksPrevTrackID).setAttribute("className", "play");
	}
	
	//switch the icon to 'play'
	//playlist css management
	document.getElementById(id).setAttribute("class", "nowplaying-2");
	document.getElementById(id).setAttribute("className", "nowplaying-2");
	
	//track listing css management
	tracksTrackId = id + "_1";
	document.getElementById(tracksTrackId).setAttribute("class", "nowplaying");
	document.getElementById(tracksTrackId).setAttribute("className", "nowplaying");
	
	prevTrackId = id;
}

function asPlayTrack(trackID) {	
	//show the current track playing
	updateTrackIcons(trackID);
}	

function asPauseTrack() {
	if (prevTrackId != 0) {
		//playlist css management
		document.getElementById(prevTrackId).setAttribute("class", "playlist-play");
		document.getElementById(prevTrackId).setAttribute("className", "playlist-play");
		
		//track listing css management
		tracksPrevTrackID = prevTrackId + "_1";
		document.getElementById(tracksPrevTrackID).setAttribute("class", "play");
		document.getElementById(tracksPrevTrackID).setAttribute("className", "play");
	}
}

