//allSongs contains all song information
var allSongs = [
	{"name": "Bright Day", "location": "../musicplayer/Evan_Hammer_-_Bright_Day.mp3","tag": "brightday"},
	{"name": "Harold Called This Morning", "location": "../musicplayer/Evan_Hammer_-_Harold_Called_This_Morning.mp3","tag": "haroldcalled"},
	{"name": "Martin Kane's Magic Kandy Kanes", "location": "../musicplayer/Evan_Hammer_-_Martin_Kane's_Magic_Kandy_Kanes.mp3","tag": "martinkanes"},
	{"name": "Hello Love", "location": "../musicplayer/Evan_Hammer_-_Hello_Love.mp3","tag": "hellolove"},
	{"name": "Rascal and a Bum", "location": "../musicplayer/Evan_Hammer_-_Rascal_and_a_Bum.mp3","tag": "rascalandabum"},
	{"name": "Little Girl", "location": "../musicplayer/Evan_Hammer_-_Little_Girl.mp3","tag": "littlegirl"},
	{"name": "What She Wants With Me", "location": "../musicplayer/Evan_Hammer_-_What_She_Wants_With_Me.mp3","tag": "whatshewants"},
	{"name": "Lunatics Down", "location": "../musicplayer/Evan_Hammer_-_Lunatics_Down.mp3","tag": "lunaticsdown"},
	{"name": "Honeypot Delinquents", "location": "../musicplayer/Evan_Hammer_-_Honeypot_Delinquents.mp3","tag": "honeypotdelinquents"},
	{"name": "A Barren Blue", "location": "../musicplayer/Evan_Hammer_-_A_Barren_Blue.mp3","tag": "abarrenblue"}
];

//songs contains active song information
var songs = [
	{"name": "Harold Called This Morning", "location": "../musicplayer/Evan_Hammer_-_Harold_Called_This_Morning.mp3","tag": "haroldcalled","id": "0"},
	{"name": "Hello Love", "location": "../musicplayer/Evan_Hammer_-_Hello_Love.mp3","tag": "hellolove","id": "1"},
	{"name": "Bright Day", "location": "../musicplayer/Evan_Hammer_-_Bright_Day.mp3","tag": "brightday","id": "2"}
];

//for clearInterval
var timerId; 

//to store current song that is playing
var currentSongId="";
var firstSong = "haroldcalled";
var maxId="2";

//loads initial song, but doesn't play it
function initialLoadSong(songToUse) {
	this.obj = FlashHelper.getMovie(name);
	if (!FlashHelper.movieIsLoaded(this.obj)) {
		//alert('here');
		clearInterval(timerId);
		currentSongId = songToUse;
		$(currentSongId).addClass('loaded');
		loadSongByTag(currentSongId.replace("song-",""), false);
	}
	
	//loads the google tracking for the player
	//category: Music
	//action: play, stop, finished
	//label: song name
	//SONG OVER is below
	niftyplayer('musicplayer').registerEvent('onPlay', 'googleEventLoader(\'Music\',\'Play\',tagToSong(currentSongId.replace(\'song-\',\'\')).name);');
	niftyplayer('musicplayer').registerEvent('onPause', 'googleEventLoader(\'Music\',\'Stop\',tagToSong(currentSongId.replace(\'song-\',\'\')).name);');
	niftyplayer('musicplayer').registerEvent('onStop', 'googleEventLoader(\'Music\',\'Stop\',tagToSong(currentSongId.replace(\'song-\',\'\')).name);');
}

//loadPlayer loads a swf music player
function loadPlayer() {
	var params = {
		bgcolor: "#353f6a",
		quality: "high",
		wmode: "transparent"
		
	};
	swfobject.embedSWF("/musicplayer/niftyplayer.swf", "musicplayer", "168", "38", "6,0,0,0", false, false, params);
	
	//load first song after a short wait to give the swf time to load
	var timerId = setInterval("initialLoadSong(firstSong)", 800);
	setTimerId(timerId);

	loadAllSongs();
}

//makes sure all song links work
function loadAllSongs() {
	$('.song').click(function() {
		loadSongByTag(this.id.replace("song-",""), true);
	});
	
}

//loads and potentially plays a song by song tag
function loadSongByTag(tag, play) {
	var songToPlay = tagToSong(tag);
	
	// if the song DOES NOT exist in the queue then add it
	if (songToPlay == "") {
		songToPlay = createNewSongEntry(tag);
	}
	
	//then load/play song
	loadSongBySong(songToPlay, play);
}

//converts a tag into a song
function tagToSong(tag) {
	var songToReturn = "";
	for(var i = 0; i < songs.length; i++) {
		if (songs[i].tag == tag) {
			songToReturn = songs[i]; 
			break;
		}
	}
	return songToReturn;

}
//loads and potentially plays a song by song location
function loadSongBySong(songToLoad, play) {
	niftyplayer('musicplayer').load(songToLoad.location);
	var oldSongId = currentSongId;
	$('#' + oldSongId).removeClass('loaded');
	currentSongId = 'song-' + songToLoad.tag;
	$('#' + currentSongId).addClass('loaded');
	//load next song and create te finished song handler for google tracking
	niftyplayer('musicplayer').registerEvent('onSongOver', 'nextSong(' + songToLoad.id + '); googleEventLoader(\'Music\',\'Finished\',\'' + songToLoad.name + '\');');
	if (play) {
		niftyplayer('musicplayer').play();
	}
}

//manages switching to next song
function nextSong(currSongId) {
	if (currSongId >= songs.length - 1) {
		currSongId = 0;
	} else {
		currSongId++;
	}
	loadSongBySong(songs[currSongId], true);
}

function createNewSongEntry(songToLoadTag) {
	//get info from allSongs
	var newSong = "";
	for (var i = 0; i < allSongs.length; i++) {
		if (allSongs[i].tag == songToLoadTag) {
			newSong = allSongs[i];
			break;
		}
	} //assume it was found
	
	//add it to visible list
	$('#songList').append($('<li></li>').addClass('song musicPlayerSong').attr('id','song-' + songToLoadTag).append('<a>' + newSong.name + '</a>'));
	
	//add it to javascript list
	var newId = songs.length;
	songs.push({"name" : newSong.name, "location" : newSong.location, "tag" : newSong.tag, "id": newId});
	
	//makes sure this link now works
	loadAllSongs();
	
	return songs[newId];
}
//makes sure that the clearInterval can function
function setTimerId(theId) {
	timerId = theId;
}