// Where to display song information
var spectacleSelector = "#songspectacleSelector > p";
var titleSelector = "#songtitleSelector > p";




var playlist = new Array();
var playerStatus = 'IDLE';
var playerPreviousStatus = '';
var playerStatusContainer = null;
var playerTimer = null;
var currentMusic = 0;
var playerWhichBar = null;
var mp3Position = 0;


//////////SETTINGS
var playerInitialLeftPosition = 70;
var playerProgressBarLength = 116;
var intervalID;


	// ------- Private vars -------
	var jsReady = false;
	var swfReady = false;
	//var backgroundState = 'STOP';

	var swfMovie = null;

	// ------- functions called by ActionScript -------
	// called to check if the page has initialized and JavaScript is available
	function isReady() {
		return jsReady;
	}

	// called to notify the page that the SWF has set it's callbacks
	function setSWFIsReady() {
		// record that the SWF has registered it's functions (i.e. that JavaScript
		// can safely call the ActionScript functions)
		swfReady = true;

		swfMovie = getSWF("myFlashMovie");
		//playSong('play_list.php');
		//backgroundState = 'PLAYING';
		//updateStatus();

		setTimeout('randomBackground();', 500);
	}
	function songDone() {
		//alert('SWF says song is done '+playerStatus);
		if ((playerStatus == 'BACKGROUND') || (playerStatus=='RANDOM-BACKGROUND')) {
			if (playerStatus == 'BACKGROUND') {
				currentMusic++;
				if (currentMusic >= playlist.length) {
					currentMusic = 0;
				}
				startBackground();
			}
			if (playerStatus == 'RANDOM-BACKGROUND') {
				randomBackground();
			}
		} else {
			playerStatus = 'IDLE';
			mp3Position = 0;
			playerStop();
		}
	}
	
	// called to notify the page of a new message
	function newMessage(value) {
		// append the message text to the end of the transcript
		//document.forms["imForm"].transcript.value += "The Other Person: " + value + "\n";
	}

	// called to notify the page that the SWF user's availability (status) has changed
	function statusChange() {
		updateStatus();
	}


	// ------- utility functions -------
	// if the SWF has indicated it's ready for communication, calls the ActionScript
	// function to get the current "availability" status and writes it into the text field.
	function updateStatus() {
		if (swfReady) {
			//var currentStatus = getSWF("myFlashMovie").getStatus();
			//document.forms["imForm"].status.value = currentStatus;
		}
	}

	// Gets a reference to the specified SWF file by checking which browser is
	// being used and using the appropriate JavaScript.
	// Unfortunately, newer approaches such as using getElementByID() don't
	// work well with Flash Player/ExternalInterface.
	function getSWF(movieName) {
		if (navigator.appName.indexOf("Microsoft") != -1) {
			return window[movieName];
		} else {
			return document[movieName];
		}
	}
		


	// ------- event handling -------
	// called by the onload event of the <body> tag
	function pageInit() {
		// record that JavaScript is ready to go.
		jsReady = true;
	}

	// called when the "Send" button is pressed; the value in the messageText text field
	// is passed in as a parameter.
	function playSong(songname, status) {
		if (swfReady) {
			swfMovie.playSong(songname);
			playerStatus = status;
		}
	}
	function doStop() {
		if (swfReady) {
			swfMovie.pauseSong();
			mp3Position = 0;
			$(spectacleSelector).html("&nbsp");
			$(titleSelector).html("&nbsp");
		}
	}
	function pauseSong() {
		if (swfReady) {
			playerStatus = 'PAUSE';
			mp3Position = swfMovie.pauseSong();
			$(spectacleSelector).html("&nbsp");
			$(titleSelector).html("&nbsp");
			playerPreviousStatus = playerStatus;
		}
	}
	function resumeSong() {
		if (swfReady) {
			//playerStatus = 'PLAYING';
			playerStatus = playerPreviousStatus;
			swfMovie.resumeSong(mp3Position);
			$(titleSelector).html(playlist[currentMusic].title);
			$(spectacleSelector).html(playlist[currentMusic].artist);
		}
	}
	




function doPlayer() {

	playerStatusContainer.html( playerStatus );
	//$(titleSelector).html(playlist[currentMusic].title);
	//$(spectacleSelector).html(playlist[currentMusic].artist);

}


function startBackground() {
	playSong(playlist[currentMusic].filename, 'BACKGROUND');
	$(spectacleSelector).html(playlist[currentMusic].artist);
	$(titleSelector).html(playlist[currentMusic].title);

	$(titleSelector).html(playlist[currentMusic].title);
	$(spectacleSelector).html(playlist[currentMusic].artist);
}

function randomBackground() {
	
	var found = false;
	while (! found) {
		newSong = Math.floor(Math.random()*(playlist.length-1));
		if (newSong != currentMusic) {
			found = true;
		}
	};
	currentMusic = newSong;

	playSong(playlist[currentMusic].filename, 'RANDOM-BACKGROUND');
	$(spectacleSelector).html(playlist[currentMusic].artist);
	$(titleSelector).html(playlist[currentMusic].title);
}






function playerPlayClick(This) {
	//var objFrame = parent.top.frames["framePlayer"];
	doStop();//backgroundStop();
	//playerStop();

	playerWhichBar = 0;
	$('.playerPosition').removeClass('playerPositionOn').removeClass('playerPositionOff').addClass('playerPositionOff').css('left', playerInitialLeftPosition);
	$('.playerImagePause').removeClass('playerImagePauseOn').addClass('playerImagePauseOff');
	$('.playerImageStop').removeClass('playerImageStopOn').addClass('playerImageStopOff');
	$('.playerImagePlay').removeClass('playerImagePlayOff').addClass('playerImagePlayOn');
	
	songname = $(This).attr('rel');

	playSong(songname, 'PLAY');
	id = $(This).attr('id'); //get the id "play???"
	playerWhichBar = id.substring(16); //get the number
	

	$(This).removeClass('playerImagePlayOn').addClass("playerImagePlayOff");
	$(This).parent().children('img.playerPosition').removeClass('playerPositionOff').addClass('playerPositionOn');
	$(This).parent().children('img.playerImagePause').removeClass('playerImagePauseOff').addClass('playerImagePauseOn');
	$(This).parent().children('img.playerImageStop').removeClass('playerImageStopOff').addClass('playerImageStopOn');
	intervalID = setInterval('playerCheckStatus()', 200);
	setButtons();

}

function playerPauseClick(This) {
	//var objFrame = parent.top.frames["framePlayer"];
	pauseSong();
	
	$(This).parent().children('img.playerImagePause').removeClass('playerImagePauseOn').addClass('playerImagePauseOff');
	$(This).parent().children('img.playerImagePlay').removeClass('playerImagePlayOff').addClass('playerImagePlayOn');
	$(This).parent().children('img.playerImageStop').removeClass('playerImageStopOn').addClass('playerImageStopOff');
	
}

function playerStop() {
	clearInterval(intervalID);
	//var objFrame = parent.top.frames["framePlayer"];
	doStop();
	
	$('.playerPosition').removeClass('playerPositionOn').addClass('playerPositionOff').css('left', playerInitialLeftPosition);
	$('.playerImagePause').removeClass('playerImagePauseOn').addClass('playerImagePauseOff');
	$('.playerImageStop').removeClass('playerImageStopOn').addClass('playerImageStopOff');
	$('.playerImagePlay').removeClass('playerImagePlayOff').addClass('playerImagePlayOn');
	
//	if (objFrame.backgroundState == 'PLAYING') {
//		playerBackgroundMusic();
//	}
}


function setButtons() {
	$('.playerImageStop').unbind('click').click(
		function() {
			if ($(this).hasClass('playerImageStopOn')) {
				playerStop();
			}
		}
	);

	$('.playerImagePause').unbind('click').click(
		function() {
			if ($(this).hasClass('playerImagePauseOn')) {
				playerStatus = 'PAUSE';
				This = this;
				playerPauseClick(This);
			}
		}
	);
}




$('.playerImagePlay').unbind('click').click(
	function () {
		if ($(this).hasClass('playerImagePlayOn')) {
			This = this;
			playerPlayClick(This);
		}
	}
);





function playerCheckStatus() {
	if ((playerStatus == 'RANDOM-BACKGROUND')) {
		$(".playerVuMeter").removeClass('playerVuMeterOff').addClass('playerVuMeterOn');
	} else {
		$(".playerVuMeter").removeClass('playerVuMeterOn').addClass('playerVuMeterOff');
	}
}


$(document).ready(function () {

	$.get('play_list_json.php', function (data) {
		playlist = data;
		playerStatusContainer = $("#statusBox");
		playerTimer = setInterval('doPlayer()', 500);
	}, "json");

	intervalID = setInterval('playerCheckStatus()', 200);

	$('.playerVuMeter').click(
		function() {
			//clearInterval(intervalID);
			//playerStop();
			if ($(this).hasClass('playerVuMeterOff')) {
				playerStop();
				if (mp3Position == 0) {
					randomBackground();
				} else {
					playerStatus = 'RANDOM-BACKGROUND';
					This = this;
					$(this).removeClass('playerVuMeterOff').addClass('playerVuMeterOn');
					resumeSong();
					$(titleSelector).html(playlist[currentMusic].title);
					$(spectacleSelector).html(playlist[currentMusic].artist);
					playerStatus = 'RANDOM-BACKGROUND';
				}
			} else {
				//alert('pause');
				//var objFrame = parent.top.frames["framePlayer"];
				playerStatus = 'PAUSE';
				This = this;
				$(this).removeClass('playerVuMeterOn').addClass('playerVuMeterOff');
				pauseSong();
			}
			intervalID = setInterval('playerCheckStatus()', 200);
		}
	);

});


