// Setup Player Variables
var playIndex = 0;
var playStatus = 'stopped';
var songList = [
	{ songName: 'Meet Me On the Equinox', artistName: 'Death Cab For Cutie', duration: '3:45',
	streamingUrl: 'http://atlantic.edgeboss.net/flash/atlantic/newmoonsoundtrack/audio/dcfc_meet_me_on_the_equinox.mp3' },
	{ songName: 'Satellite Heart', artistName: 'Anya Marina', duration: '3:33', streamingUrl: 'http://atlantic.edgeboss.net/flash/atlantic/newmoonsoundtrack/audio/anya_marina_satellite_heart_soundtrack_album_version_192.mp3' }
/*	{ songLalaId: '360569453759136328' },
	{ songUrl: 'http://streamos.atlrec.com/flash/atlantic/twilight/audio/spotlight_twilight_website_twilight_mix.mp3',
		songName: 'Spotlight',
		artistName: 'Mutemath',
		duration: '3:21' 
	},
	{ songUrl: 'http://atlantic.edgeboss.net/download/atlantic/twilight/audio/go_all_the_way_into_the_twilight.mp3',
		songName: 'Go All The Way Into The Twilight',
		artistName: 'Perry Farrell',
		duration: '3:27'
	}*/
];


// Subscribe to Player Events
// 		Event Types: 
//		lala.Player.EVENT_PLAYER_LOADED,
// 		lala.Player.EVENT_PLAYER_STATUS_CHANGE,
// 		lala.Player.EVENT_PLAYBACK_COMPLETE,
// 		lala.Player.EVENT_PLAYBACK_FATAL_ERROR,
// 		lala.Player.EVENT_PLAYBACK_ERROR

lala.Player.addEventListener(lala.Player.EVENT_PLAYER_STATUS_CHANGE, onPlayerStatusChange);
lala.Player.addEventListener(lala.Player.EVENT_PLAYBACK_ERROR, onPlaybackError);
lala.Player.addEventListener(lala.Player.EVENT_PLAYBACK_FATAL_ERROR, onPlaybackFatalError);
lala.Player.addEventListener(lala.Player.EVENT_PLAYBACK_COMPLETE, onPlaybackComplete);
lala.Player.addEventListener(lala.Player.EVENT_PLAYER_LOADED, onPlayerLoaded);

// Call initialization function
//     Required Arguments:
//     divId = The id of the div in which the player should be rendered
//             Should match id of div created above (e.g. lalaWidgetPlayer)
//     Optional Arguments:
//     size = Object { width, height }
//     params = Object {
//                songLalaId: Id of the song. Pass this in or pass songName and artistName
//                songName:   Name of the song. Pass this with artistName.
//                artistName: Artist of the song. Pass this with songName.
//                adDivId:    Div id of where to show ads (custom images)
//                partnerId:  Unique partner id (e.g. billboard)
//
//				(below is if you want to customize the widget colors)
//                useCustomColors:    Boolean to use custom colors (true|false)
//                noBorder:           Option to hide border (true|false)
//                backgroundColor:    Background color of the widget (e.g. #B71E70)
//                fontColor:          Color of the song title (e.g. #FFFFFF)
//                fontSecondaryColor: Color of the artist name (e.g. #DDDDDD)
//                progressTrackColor: Color of the progress track (e.g. #FFFFFF)
//                progressLoadColor:  Color of the progress load (e.g. #7C0E52)
//                progressBarColor:   Color of the progress bar (e.g. #E8258B)
//              }

lala.Player.init(
	"lalaWidgetPlayer", 
	{ width: "298px", height: "70px" }, 
	{ partnerId: 'memberAffiliate.19323@77422', useSignupDialog: false, 
		useCustomColors: true, noBorder: false, backgroundColor: 'transparent', 
		fontColor: '#666666', fontSecondaryColor: '#333333', progressTrackColor: '#333333',
		progressLoadColor: '#666666', progressBarColor: '#000000' }
);

// Supported Player APIs
// lala.Player.togglePlayback()
//             stopPlayback()
//             playSongById(id, autoPlay)
//             playSong(title, artist, autoPlay)
//             playMP3(url, title, artist, duration, autoPlay)

// In-Page Signup
// Required Arguments:
// params:
//     Object {
//         callbackUrl: A callback url in the parent domain to be loaded when signup
//                      is complete (e.g. http://www.billboard.com/lalaauthcallback.htm)
//                      Copy the source for the callback page from our sample page at
//                      http://www.lala.com/partnerauthcallback
//         partnerId:   'memberAffiliate.34788@73477'
//     }
//    lala.SignupDialog.init( {  } );


// Setup Player Functions
function onPlayerLoaded() {

//	playSong(0,true);
    updatePlayIcon('stopped');

//    playSong(0, true);
window.setTimeout(function() {playSong(0);},100);
}

function onPlaybackFatalError() {
	// do nothing
}

function onPlaybackError() {
	setTimeout(function() {
		onNextClick();
	}, 100);
}

function onPlaybackComplete() {
	setTimeout(function() {
		onNextClick();
	}, 100);
}


function onPlayerStatusChange(status) {
	var playPauseLink = document.getElementById("playPauseButton");
	if (playPauseLink) {
		switch (status) {
			case 'stopped':
				playPauseLink.innerHTML = 'Play';
				break;
			case 'paused':
				playPauseLink.innerHTML = 'Resume';
				break;
			case 'playing':
				playPauseLink.innerHTML = 'Pause';
				break;
		}
	}
	
	updatePlayIcon(status);
}

function updatePlayIcon(status) {
	var playIcon = document.getElementById("playIcon_" + playIndex);
	if (playIcon) {
		playIcon.className = "icon " + status;
	}
}

function onNextClick() {
	if (playIndex < songList.length - 1) {
		updatePlayIcon('stopped');
	
		playIndex++;
		playSong(playIndex, true);
	}
}

function onTogglePlay() {
	lala.Player.togglePlayback();
}

function onPreviousClick() {
	if (playIndex > 0) {
		updatePlayIcon('stopped');
	
		playIndex--;
		playSong(playIndex, true);
	}
}

function onPlayButtonClick(songIndex) {
	updatePlayIcon('stopped');
	
	playSong(songIndex, true);
}

function playSong(index, autoPlay) {
	playIndex = index;
	
	var song = songList[index];
	if (song && song.songLalaId) {
		lala.Player.playSongById(song.songLalaId, autoPlay);
	} else if (song && song.songUrl) {
		lala.Player.playMP3(song.songUrl, song.songName, song.artistName, song.duration, autoPlay);
	} else if (song && song.streamingUrl) {
		lala.Player.playStream(song.streamingUrl, song.songName, song.artistName, autoPlay);
	} else {
		lala.Player.playSong(song.songName, song.artistName, autoPlay);
	}
}
