;(function($){
	ep.MusicPlayer = new function() {
		this.playing = false;
		this.playlistItem,
		this.playlist = [
		                 	{author:"Juviley",name:"The Way That We Bend And Dent",time:"3:32",action:"theway",mp3:"http://dl.dropbox.com/u/3796749/EP/mp3%20%28192%29/01%20The%20Way%20That%20We%20Bend%20and%20Dent.mp3",ogg:"http://dl.dropbox.com/u/3796749/EP/ogg/theway.ogg"},
		                 	{author:"Juviley",name:"Young Mother",time:"3:43",action:"youngmother",mp3:"http://dl.dropbox.com/u/3796749/EP/mp3%20%28192%29/02%20Young%20Mother.mp3",ogg:"http://dl.dropbox.com/u/3796749/EP/ogg/youngmother.ogg"},                        
		                 	{author:"Juviley",name:"My Blood",time:"3:50",action:"myblood",mp3:"http://dl.dropbox.com/u/3796749/EP/mp3%20%28192%29/03%20My%20Blood.mp3",ogg:"http://dl.dropbox.com/u/3796749/EP/ogg/myblood.ogg"},
		                 	{author:"Juviley",name:"Lookalikes",time:"2:41",action:"lookalikes",mp3:"http://dl.dropbox.com/u/3796749/EP/mp3%20%28192%29/04%20Lookaliks.mp3",ogg:"http://dl.dropbox.com/u/3796749/EP/ogg/lookalikes.ogg"},
		                 	{author:"Juviley",name:"Our Choices Rhyme",time:"3:44",action:"ourchoices",mp3:"http://dl.dropbox.com/u/3796749/EP/mp3%20%28192%29/05%20Our%20Choices%20Rhyme.mp3",ogg:"http://dl.dropbox.com/u/3796749/EP/ogg/ourchoices.ogg"}
		                 ],
		this.init = function(index) {
			var self = this;
			self.playlistItem = index;
			self.clicks();
			$("#jplayer").jPlayer({
			    ready: function() {
					self.playListConfig(self.playlistItem);
			    },
			    swfPath: baseUrl + '/js/site',
			    oggSupport: true
		    })
		    .jPlayer("onProgressChange", function(loadPercent, playedPercentRelative, playedPercentAbsolute, playedTime, totalTime) {
				var timeLeft = totalTime-playedTime;
		    	var playTime = $.jPlayer.convertTime(playedTime);
				var totalTime = $.jPlayer.convertTime(totalTime);
				timeLeft = $.jPlayer.convertTime(timeLeft);
				
			    var myPlayedTime = new Date(playedTime);
			    var ptMs = myPlayedTime.getUTCMilliseconds();
			    var ptMsFraction = Math.floor(ptMs/100);	    
			    var timeDisplay = playTime+":"+ptMsFraction;
			    
				$("#time").text(timeLeft);
			    ep.readEvents(playedTime);
			})
			.jPlayer("onSoundComplete", function() {
				ep.theway.stop();
				ep.game.gameover();
				// self.playListNext();
			});
		},
		this.getSongTitle= function(index) {
		    return this.playlist[index].name;
		},
		this.getSongTime = function(index) {
		    return this.playlist[index].time;
		},
		this.getSongAction= function(index) {
		    return this.playlist[index].action;
		},
		this.displayCurrent= function() {
			var self = this;
		    $('#title').html(self.getSongTitle(self.playlistItem));
		    $('#totalTime').html(self.getSongTime(self.playlistItem));
		},
		this.playListConfig= function(index) {
			var self = this;
		    $("#jplayer").jPlayer("setFile", self.playlist[index].mp3, self.playlist[index].ogg);
		    this.displayCurrent();
		},
		this.playListChange= function(index) {
			this.playListConfig(index);
		    $("#jplayer").jPlayer("play");
		},
		this.playListNext= function() {
			this.playlistItem = (this.playlistItem+1 < this.playlist.length) ? this.playlistItem+1 : 0;
		    this.playListChange(this.playlistItem);
		},
		this.clicks = function() {
			$('#jplayer_play').click(function() {
				this.playing = true;
			});
			
			$('#jplayer_pause').click(function() {
				this.playing = false;
			});
		},
		this.nextButton = function() {
			var self = this;
			$('#next').click(function() {
				self.playListNext();
			});
		},
		this.stop = function() {
			$("#jplayer").jPlayer("stop");
		},
		this.play = function() {
			$("#jplayer").jPlayer("play");
		};
	};
})(jQuery);
