/* Cool Javascript MP3 Playback in Web Page with Custom Controls
Version 1.2b
By Jeff Baker 
Created October 12, 2007
Copyright 2007 by Jeff Baker
www.seabreezecomputers.com
modified by Marc Haumann February 2009
www.marchaumann.net
requires jQuery - http://jquery.com/

DON'T STEAL MUSIC! If you like the songs here, please purchase them. These versions are low-quality anyway!
*/
var currentsong = 0; // start at song 0 in playlist
var ispaused = 'off'; //state of play/pause
var t; // for song time display timer

var yearningriver = [
	['12-26-09','The Books, DM Stith, The Swell Season, Blonde Redhead, Thom Yorke…','YEARNING RIVER THE SKY THE LIGHT'],
	['/mixtape/yearningriver/Cello Song.mov','Cello Song (feat. Jos&eacute; Gonz&aacute;lez)','The Books'],
	['/mixtape/yearningriver/Master and a Hound.mov','Master & a Hound','Gregory Alan Isakov'],
	['/mixtape/yearningriver/A Soft Seduction.mov','A Soft Seduction','DM Stith'],
	['/mixtape/yearningriver/Drown Out.mov','Drown Out','The Swell Season'],
	['/mixtape/yearningriver/Anticipation.mov','Anticipation','Blonde Redhead'],
	['/mixtape/yearningriver/You Are the Blood.mov','You Are the Blood','Castanets'],
	['/mixtape/yearningriver/All for the Best.mov','All for the Best','Thom Yorke'],
	['/mixtape/yearningriver/River.mov','River','Akron/Family'],
	['/mixtape/yearningriver/Boy Lilikoi.mov','Boy Lilikoi','J&oacute;nsi']	
];

var nervousmemories = [
	['10-7-09','Fleet Foxes, Radiohead, Grizzly Bear, Fanfarlo, Mew, Beirut…','NERVOUS MEMORIES IN BLACK &amp; WHITE'],
	['/mixtape/nervousmemories/White Winter Hymnal.mov','White Winter Hymnal','Fleet Foxes'],
	['/mixtape/nervousmemories/Harry Patch (In Memory Of).mov','Harry Patch (In Memory Of)','Radiohead'],
	['/mixtape/nervousmemories/Two Weeks.mov','Two Weeks','Grizzly Bear'],
	['/mixtape/nervousmemories/Only Straight Girls Wear Dresses.mov','Only Straight Girls Wear Dresses','CWA'],
	['/mixtape/nervousmemories/Finish Line.mov','Finish Line','Fanfarlo'],
	['/mixtape/nervousmemories/This Blackest Purse.mov','This Blackest Purse','Why?'],
	['/mixtape/nervousmemories/Nervous.mov','Nervous','Mew'],
	['/mixtape/nervousmemories/Scenic World.mov','Scenic World','Beirut']
];

var ghostfolk = [
	['8-2-09','Nancy Sinatra, Portishead, Sufjan Stevens, Smashing Pumpkins, Pink Floyd…','GHOST FOLK'],
	['/mixtape/ghostfolk/I Will.mov','I Will','Radiohead'],
	['/mixtape/ghostfolk/Bang Bang (My Baby Shot Me Down).mov','Bang Bang (My Baby Shot Me Down)','Nancy Sinatra'],
	['/mixtape/ghostfolk/Pity Dance.mov','Pity Dance','DM Stith'],
	['/mixtape/ghostfolk/Hunter.mov','Hunter','Portishead'],
	['/mixtape/ghostfolk/Marla.mov','Marla','Grizzly Bear'],
	['/mixtape/ghostfolk/Sodom South Georgia.mov','Sodom, South Georgia','Iron &amp; Wine'],
	['/mixtape/ghostfolk/Romulus.mov','Romulus','Sufjan Stevens'],
	['/mixtape/ghostfolk/Landslide.mov','Landslide','Smashing Pumpkins'],
	['/mixtape/ghostfolk/Is There Anybody Out There.mov','Is There Anybody Out There?','Pink Floyd']
];

var boygirl = [
	['6-30-09','Stars, Massive Attack, M83, Snow Patrol…','BOY + GIRL'],
	['/mixtape/boygirl/My Clouds.mov','My Clouds','Cutfeeling'],
	['/mixtape/boygirl/Ive Seen It All.mov','I\'ve Seen It All (featuring Thom Yorke)','Bj&ouml;rk'],
	['/mixtape/boygirl/Group Four.mov','Group Four','Massive Attack'],
	['/mixtape/boygirl/Set the Fire to the Third Bar.mov','Set the Fire to the Third Bar (featuring Martha Wainwright)','Snow Patrol'],
	['/mixtape/boygirl/Nothing Better.mov','Nothing Better','The Postal Service'],
	['/mixtape/boygirl/Graveyard Girl.mov','Graveyard Girl','M83'],
	['/mixtape/boygirl/A Thread Cut with a Carving Knife.mov','A Thread Cut with a Carving Knife','Stars']
];

var lastsongs = yearningriver;
var lastmix = 0;
var media;
var songs;

// Make a div to hold the player and place it off the screen
// so that we don't see it
document.write('<div id="player"'
+ 'style="position:absolute;left:-1000px;top:-1000px"'
+ '></div>');

function detect_browser(){
	var browser_name = navigator.userAgent;
	// We have to check for Opera first because
	// at the beginning of the userAgent variable
	// Opera claims it is MSIE. 
	
	if (browser_name.indexOf("Opera")!= -1)
		browser_name = "Opera";
	else if (browser_name.indexOf("Firefox")!= -1)
		browser_name = "Firefox";
	else if (browser_name.indexOf("MSIE")!= -1)
		browser_name = "MSIE";
	else if (browser_name.indexOf("Netscape")!= -1)
		browser_name = "Netscape";
	else if (browser_name.indexOf("Safari")!= -1)
		browser_name = "Safari";
	
	return browser_name;
} // end function detect_browser()


function load(media){
	var player = document.getElementById('player'); 
	
	if (detect_browser() == "MSIE" || detect_browser() == "Netscape"){ 
		player.innerHTML = '<object id="sound"' 
		+ 'classid="clsid:6BF52A52-394A-11d3-B153-00C04F79FAA6"'
		+ 'codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701"'
		+ 'standby="Loading Microsoft® Windows® Media Player components..."'  
		+ 'type="application/x-oleobject" width="160" height="144">'                
		+ '<param name="url" value="'+media+'">'      
		+ '<param name="volume" value="100">'            
		+ '<embed id="sound" type="application/x-mplayer2" src="'+media+'"' 
		+ 'classid="clsid:6BF52A52-394A-11d3-B153-00C04F79FAA6"'
		+ 'pluginspage="http://www.microsoft.com/Windows/MediaPlayer/"'
		+ 'type="application/x-mplayer2"'
		+ 'url="'+media+'"' 
		+ 'volume="100"' 
		+ 'width="160" height="144">'               
		+ '<\/embed>'
		+ '<\/object>';
	}
	else{ // if Safari or Firefox, then load Quicktime controls
		player.innerHTML = '<object '
		+ 'classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" '
		+ 'width="160" height="144" id="sound"'
		+ 'style="position:absolute;left:-1000px;top:-1000px"'
		+ 'codebase="http://www.apple.com/qtactivex/qtplugin.cab">'
		+ '<param name="src" value="'+media+'">'
		+ '<param name="autoplay" value="true">'
		+ '<param name="controller" value="false">'
		+ '<param name="volume" value="100">'
		+ '<param name="enablejavascript" value="true">'
		+ '<param name="type" value="video/quicktime">'
		+ '<embed classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B"' 
		+ 'name="sound"'
		+ 'id="sound"' 
		+ 'src="'+media+'"' 
		+ 'pluginspage="http://www.apple.com/quicktime/download/"'
		+ 'volume="100"' 
		+ 'enablejavascript="true" '
		+ 'type="video/quicktime" '
		+ 'height="16" '
		+ 'width="200"'
		+ 'style="position:absolute;left:-1000px;top:-1000px"'
		+ 'autostart="true"'
		+ '> </embed>'
		+ '</object>';
	}
} // end function load(media)


function next_song(dir,songs,currentmix){
	
	if (lastmix != currentmix) {
		currentsong = 0;
		$("div.songtime")[lastmix].innerHTML = '';
		display_song(lastsongs,lastmix);
	}
	//alert(lastsongs);
	playlist_length = songs.length - 1;
	
	// stop the current song if playing
	if (document.getElementById('player').innerHTML != ''){
		stop_song();
		$("div.songtime")[currentmix].innerHTML = '';
	}
	if (dir == 'bk') // previous
	{
		currentsong--;
		if (currentsong < 0) 
			currentsong = playlist_length; // loop to last song
			$("div.songtime")[currentmix].innerHTML = '';
	}
	if (dir == 'fw') // next
	{
		currentsong++;
		if (currentsong > playlist_length)
			currentsong = 0; // loop to first song
			$("div.songtime")[currentmix].innerHTML = '';
	}
	media = songs[currentsong][0];
	ispaused = 'off';
	play_song(songs,currentmix);
	lastmix = currentmix;
	lastsongs = songs;
} // end function next_song(dir)


function play_song(songs,currentmix)
{
	// Check to see if current song has stopped
	if (document.getElementById('player').innerHTML == ''){
		load(media);
		setTimeout('display_time('+currentmix+');', 500);
		display_song(songs,currentmix);
	}
	else // otherwise wait until it has 
		setTimeout('play_song('+songs+','+currentmix+')', 500);
}  // end function play


function display_song(songs,currentmix) {
	sartist = songs[currentsong][2];
	sname = songs[currentsong][1];

	$("div.songartist")[currentmix].innerHTML = sartist;
	
	$("div.songname")[currentmix].innerHTML = sname;
}  // end function display_song()


function stop_song() {
	var player = document.getElementById('player'); 
	
	// Call stop function if available in quicktime player
	//document.getElementById('message').innerHTML = (typeof document.sound.Stop);
	if (document.sound)
	if (typeof document.sound.Stop == 'function'){
		document.sound.Stop();
	}
	// Call stop function if available in Media player
	//document.getElementById('message').innerHTML = (typeof document.sound.controls);
	if (document.sound)
	if (typeof document.sound.controls == 'object'){
		document.sound.controls.Stop();
	}
	
	
	// Stop time display timer
	clearTimeout(t);
	
	// Wipe out contents of player div
	player.innerHTML = '';
	
	// set speed display to 1X
	if (document.getElementById('speed'))
	document.getElementById('speed').innerHTML = '1X';
} // end function stop()

function pause_song(){

	// if a song is not playing then just return
	if (document.getElementById('player').innerHTML == '')
		return;

	
// if IE or Netscape then Media Player Pause Controls
	if (detect_browser() == "MSIE" || 
		detect_browser() == "Netscape"){ 	
	if (!document.sound.controls)
	if (ispaused == 'off'){
		document.sound.controls.pause();
		ispaused = 'on';
	}
	else // if unpaused
	{
		document.sound.controls.play();
		ispaused = 'off';
	}
}
else // If Firefox or Safari then use Quicktime Stop (Pause)	
{
	// if unpaused
	if (ispaused == 'off')
	{
		document.embeds['sound'].Stop();
		ispaused = 'on';
	}
	else // if paused
	{
		document.embeds['sound'].Play();
		ispaused = 'off';
	}
}

}  // end function pause_song()


function display_time(currentmix){
	// This function not only displays the current position
	// of time in the file, but it also gets the end time
	// so that if a song reaches the end it will play the
	// next song
	var time;
	var duration;
	var song_status;
	var buffer_status; // used for media player
	var mins; // used for quicktime
	var secs; // used for quicktime
	var scale; // used for quicktime
	
	// if IE or Netscape then Media Player 
	if (detect_browser() == "MSIE" || 
		detect_browser() == "Netscape")
	{
		//document.getElementById('message').innerHTML = typeof document.sound.settings;
		if (!document.sound.settings)
	{
		if (document.getElementById('message'))
			document.getElementById('message').innerHTML = 'This'
			+ ' browser does not support autoplay of next song in playlist.';
		return; // done because no fade support
	}		
		//song_status = document.sound.status;
		song_status = document.sound.playState;
		buffer_status = document.sound.network.bufferingProgress;
		
		
	if (buffer_status < 100) // 3 = playing; 1 = stopped
	{	
		t = setTimeout('display_time('+currentmix+');', 20); // recall this function in 20ms
		return;
	}
	else 
	{
		if ($("div.songtime")[currentmix])
			$("div.songtime")[currentmix].innerHTML = '&#151;'; 
	}			
		//time = document.sound.controls.currentPosition;
		time = document.sound.controls.currentPositionString;
		//duration = document.sound.currentMedia.duration;
		duration = document.sound.currentMedia.durationString;
		
	}	
	else  // Firefox or Safari
	{
	
	song_status = document.embeds['sound'].GetPluginStatus();
	

	if (song_status.toLowerCase() != 'playable' &&
		song_status.toLowerCase() != 'complete')
	{	
		t = setTimeout('display_time('+currentmix+');', 250); // recall this function every 250 ms
		return;
	}
	else 
	{
		if ($("div.songtime")[currentmix])
		$("div.songtime")[currentmix].innerHTML = '&#151;'; //old is 'song_status' without quotes
	} 
	
	time = document.embeds['sound'].GetTime();
	duration = document.embeds['sound'].GetDuration();
	scale = document.embeds['sound'].GetTimeScale();
	time = Math.floor(time*(1/scale));  // convert to seconds
	duration = Math.floor(duration*(1/scale));  // converts to seconds
	// convert seconds into mm:ss
	mins = Math.floor(time / 60);
	secs = time - (mins * 60);
	//new stuff
	mins_formatted = mins ? mins+':' : '';
	secs_formatted = mins ? (secs < 10 ? '0'+secs : secs) : secs;
	time = mins_formatted + secs_formatted;	
	//old stuff
	//time = mins + ':' + secs;
	mins = Math.floor(duration / 60);
	secs = duration - (mins * 60);
	mins_formatted = mins ? mins+':' : '';
	secs_formatted = mins ? (secs < 10 ? '0'+secs : secs) : secs;
	duration = mins_formatted + secs_formatted;	
	}
	
	if ($("div.songtime")[currentmix])
		$("div.songtime")[currentmix].innerHTML = time; //add " + duration" here if you want out of total time
	

	// This is for Firefox
	if (time == duration) // if at end of song
	{	
		next_song('fw',lastsongs,currentmix); // then play next song in playlist
		return;
	}
	// This is for Ie
	if (song_status == 1) // song stopped
	{	
		next_song('bk',lastsongs,currentmix);
		return;
	}
	t = setTimeout('display_time('+currentmix+');', 250); // recall this function every 250ms
	
} // end function display_time()


function shuffle(songs)
{
	songs.sort(function() 
	{
	return 0.5 - Math.random()
	}) //Array elements now scrambled
	
	media = songs[currentsong][0]; // media becomes the random song at current position
	display_song();
}

window.onunload = stop_song;