var lojack;
$(document).ready(function()
{	
	lojack = new player();
});
function player()
{
	this.currentSongID = -1;
	this.currentSound = null;
	this.volume = 100;//0-100
	this.playPosition = 0;//in seconds
	this.songLength = 0;//in seconds
	this.playFraction = 0;//the amount of the song that has been listened to as a fraction
	this.loadFraction = 0;//no comment needed
	this.visualizer = new visualizer('visualizer');
	this.showVisualizer = true;
	this.isSeeking = false;
	this.playMode = "shuffle";
	this.setPlayMode = function(v)
	{
		if(!$(v).hasClass("on")) {
			$(v).addClass("on");
		}
		this.playMode = v;
		setCookie("play_mode", v, 30);
	}
	this.toggleVisualizer = function()
	{
		if(!this.showVisualizer)
		{
			$("#show_visualizer").html("hide visualizer");
			$("#modal-overlay").fadeIn('normal');
			$("#visualizer-holder").fadeIn('normal');
			this.showVisualizer = true;
		}
		else
		{
			$("#show_visualizer").html("show visualizer");
			$("#visualizer-holder").fadeOut('normal');
			$("#modal-overlay").fadeOut('normal');
			this.showVisualizer = false;
		}

	}
	this.whileplaying = function()
	{
		this.songLength = this.currentSound.duration;
		this.loadFraction = 1.0;
		if(this.currentSound.readyState == 1)
		{
			this.songLength = this.currentSound.durationEstimate;
			this.loadFraction = this.currentSound.bytesLoaded/this.currentSound.bytesTotal;
		}

		this.songLength /= 1000;
		this.playPosition = this.currentSound.position/1000;//convert to seconds from millis
		this.playFraction = this.playPosition/this.songLength;
		
		
		

		if(this.showVisualizer)
		{
			this.visualizer.drawCanvas(this.currentSound.waveformData, this.currentSound.eqData);
		}
		this.updateUI();
	}
	this.minSecFormat = function(millis)
	{
		var min = Math.floor(millis/60000);
		var sec = Math.floor(millis/1000 - min*60);
		if(sec < 10) sec = "0"+sec;
		return min+":"+sec;

	}
	//updates player counter/load counter	
	this.updateUI = function() 
	{
		if(this.isSeeking)
		{
			$("#play_counter").html(this.minSecFormat(this.currentSound.duration*$("#position-slider").slider('value')/100) + "/" + this.minSecFormat(this.currentSound.duration));
		}
		else
		{
			$("#play_counter").html(this.minSecFormat(this.currentSound.position) + "/" + this.minSecFormat(this.currentSound.duration));
			$("#position-slider").slider('value', this.playFraction*100);
		}
	}
	
	this.setVolume = function(v)
	{
		if(v > 100) this.volume = 100;
		else if(v < 0) this.volume = 0;
		else this.volume = v;
		this.currentSound.setVolume(this.volume);
		setCookie('volume', v);
	}
	
	this.playFirstSong = function(sid)
	{
		lojack.playNextSong();
	}
	
	this.playSong = function(id)
	{
		var info_url = "/php/song_info?id="+id;
		$.getJSON(info_url, function(songinfo)
		{
			lojack.updateSongInfo(songinfo);
			lojack.doPlaySong(songinfo.id);
		});
	}
	
	this.playRandomSong = function() {
		var info_url = "/php/song_info?id=rand";
		$.getJSON(info_url, function(songinfo)
		{
			lojack.updateSongInfo(songinfo);
			lojack.doPlaySong(songinfo.id)
		});
	}
	
	this.playNextSong = function()
	{
		if(this.playMode == "shuffle")
		{
			lojack.playRandomSong();
		}
		else if(this.playMode == "repeat_one")
		{
			lojack.doPlaySong(this.currentSongID);
		}
		else
		{
			var currentSongRow = $("#song_"+this.currentSongID);
			var nextSongRow = currentSongRow.next();
			if(nextSongRow.length == 0)
			{
				nextSongRow = $("tr.song:first");
			}
			if(nextSongRow.length != 0)
			{
				id = nextSongRow[0].id.split("_")[1];
				lojack.playSong(id);
			}
		}
	}

	this.doPlaySong = function(sid)
	{
		if(this.currentSound != null)
		{
			$.ajax({url: "/php/addPlayHistory.php?id="+this.currentSongID+"&duration="+this.playPosition});
			this.currentSound.destruct();
		}
		$("#song_"+sid).addClass("playing");
		$("#song_"+this.currentSongID).removeClass("playing");
		this.currentSongID = sid;
	
		var pp_button = $("#play_pause");
		pp_button.removeClass('pause');
		pp_button.addClass('play');

		this.currentSound = soundManager.createSound({
						id : sid,
						url : '/php/song.php?id='+sid,
					});

		this.currentSound.setVolume(this.volume);
		this.currentSound.play();
		
		setCookie("current_song", sid, 30);
	}

	this.playPauseToggle = function()
	{
		if(this.currentSound != null)
		{
			this.currentSound.togglePause();
			var pp_button = $("#play_pause");
			if(pp_button.hasClass('pause')) {
				pp_button.removeClass('pause');
				pp_button.addClass('play');
			} else {
				pp_button.removeClass('play');
				pp_button.addClass('pause');	
			}
		}
	}

	this.stop = function()
	{
		if(this.currentSound != null)
		{
			this.currentSouind.stop();
		}
	}
	
	this.seek = function(pf)
			{
				if(this.currentSound != null)
				{
					this.currentSound.setPosition(pf*this.currentSound.duration);
				}
			}

	this.updateSongInfo = function(song_info)
	{
		var pic_url = "/php/album_cover.php?song_id="+song_info.id;
		$("#album-art").attr('src', pic_url);
		$(".player-title").html(song_info.title);
		$(".player-album").html(song_info.album);
		$(".player-artist").html(song_info.artist);
	}
	
	/***********************INITIALIZATION***********************/
	this.currentSongID = readCookie("current_song");
	this.playMode = readCookie("play_mode");
	if(!this.playMode) {
		this.setPlayMode('shuffle');
	}

	soundManager.debugMode = false; // disable debug output
	soundManager.url = '/soundmanager/swf/'; // path to directory containing SoundManager2 .SWF file
	soundManager.flashVersion = 9;
	soundManager.defaultOptions.useWaveformData = true;
	soundManager.defaultOptions.useEQData = true;
	soundManager.defaultOptions.autoLoad = true;
	soundManager.defaultOptions.onfinish = function()
	{
		lojack.playNextSong();
	}
	
	soundManager.defaultOptions.whileplaying = function()
	{
		lojack.whileplaying();
	}

	soundManager.onload = function()
	{
		lojack.playFirstSong();
	}
	this.toggleVisualizer();
	
	$("#position-slider").slider({	
					handle:	'.slider_handle',
					minValue:0, 
					maxValue:100, 
					range: 'max',
					start: function(event, ui)
					{
						lojack.isSeeking = true;
					},
					stop: function(event, ui)
					{
						lojack.isSeeking = false;
					},
					change: function(event, ui)
					{
						if(event.originalEvent)
						{
							lojack.seek(ui.value/100);
						}
					}});

	$("#volume-slider").slider({	
					handle:	'.slider_handle',
					minValue:0, 
					maxValue:100,
				        value:100,	
					orientation: "vertical",
					range: "min",
					slide: function(event, ui)
					{
						if(event.originalEvent)
						{
							lojack.setVolume(ui.value);
						}
					}});


	$("#song-info-container").hover(function()
					{
						$("#song-info").slideDown("normal");
					},
					function()
					{
						$("#song-info").slideUp("normal");
					});

	$(".mode_button").click(function()
				{
					lojack.setPlayMode(this.id);
					$(this).parent().find(".on").removeClass("on");
					$(this).addClass("on");
				});

}


