/*************************************************************************************
JPlayer functionality
Requirement(s):
- /js/lib/jquery-1.3.2.min.js
- /js/lib/jquery.jplayer.js
- /js/lib/Jplayer.swf
************************************************************************************/

$(document).ready( function(){			
	$("#jquery_jplayer").jPlayer({
		swfPath: "/js/lib",
		ready: function() {						
			mediaplayer.playListInit(false); // Parameter is a boolean for autoplay.
		},
		bgcolor: "black",
		volume: 50
	})	
	.jPlayer("cssId", "play", "player_play")
	.jPlayer("cssId", "pause", "player_pause")
	.jPlayer("cssId", "stop", "stop")
	.jPlayer("cssId", "volumeBarValue", "player_volume_bar_value")
	.jPlayer("cssId", "volumeBar", "player_volume_bar")	
	.jPlayer("onSoundComplete", function() {										
		mediaplayer.playListNext();
	});		
	
	$("#ctrl_prev").click( function() {
		mediaplayer.playListPrev();
		return false;
	});

	$("#ctrl_next").click( function() {
		mediaplayer.playListNext();
		return false;
	});
	
	// this should be disabled if autoplay is on
	$("#player_play").click( function() {
		// if this is the first play of preloaded song
		if (mediaplayer.playedFirstSong == 0) {
			if($.browser.mozilla || $.browser.msie){
				// this is to initialize the player in mozilla and IE for some reason, need to look more into this
				mediaplayer.playListChange( 0 );	
			}
			mediaplayer.playedFirstSong = 1;
			
			// do ajax call to track song plays
			$.ajax({
				type : 'POST',
				url : '/track_plays.php',
				dataType : 'json',
				data: {
					name : mediaplayer.myPlayList[mediaplayer.playItem].name
				},
				success : function(data){
				},
				error : function(XMLHttpRequest, textStatus, errorThrown) {
				}
			});
		}
		
	});
});

var mediaplayer = {
	//handles the music player
	playItem: 0,
	playedFirstSong: 0, 
	
	myPlayList: [
		{name:"Mirage (A Portrait of 4am)",mp3:"/music/Portrait_of_4am-01-Mirage.mp3"},
		{name:"Erosion  (A Portrait of 4am)",mp3:"/music/Portrait_of_4am-02-Erosion.mp3"},
		{name:"Down the Road  (A Portrait of 4am)",mp3:"/music/Portrait_of_4am-09-Down_the_Road.mp3"},
		{name:"Life by the Mile (Step Out of Line (EP))",mp3:"/music/Step_Out_of_Line-02-Life_By_The_Mile.mp3"},
		{name:"Stone Cold (Step Out of Line (EP))",mp3:"/music/Step_Out_of_Line-03-Stone_Cold.mp3"},
		{name:"Distraction of You (Live)",mp3:"/music/Live-Distraction_Of_You.mp3"}
	],	

	playListInit: function(autoplay) {
		if(autoplay) {
			mediaplayer.playListChange( mediaplayer.playItem );
		} else {			
			mediaplayer.playListConfig( mediaplayer.playItem );
		}
	},

	playListConfig: function( index ) {		
		mediaplayer.playItem = index;
		$("#song_title").text(mediaplayer.myPlayList[mediaplayer.playItem].name);
		$("#jquery_jplayer").jPlayer("setFile", mediaplayer.myPlayList[mediaplayer.playItem].mp3);
	},

	playListChange: function( index ) {
		mediaplayer.playListConfig( index );
		$("#jquery_jplayer").jPlayer("play");		
		if(contenthandler.currentpage == 'media'){				
			media.updateNowPlaying();
		}
		// do ajax call to track song plays
		$.ajax({
			type : 'POST',
			url : '/track_plays.php',
			dataType : 'json',
			data: {
				name : mediaplayer.myPlayList[index].name
			},
			success : function(data){
			},
			error : function(XMLHttpRequest, textStatus, errorThrown) {
			}
		});
	},

	playListNext: function() {
		var index = (mediaplayer.playItem + 1 < mediaplayer.myPlayList.length) ? mediaplayer.playItem + 1 : 0;
		mediaplayer.playListChange( index );
	},

	playListPrev: function() {
		var index = (mediaplayer.playItem-1 >= 0) ? mediaplayer.playItem-1 : mediaplayer.myPlayList.length-1;
		mediaplayer.playListChange( index );
	}			
}
