var Player = 
{
	tracks			: null,
	playBtn			: null,
	player			: null,
	progress		: null,
	loadhead		: null,
	playhead		: null,
	file			: null,
	track			: null,
	progressId		: null,
	isPlaying		: false,
	currentTrack	: null,
	
	// you may override these, just mind the return value (or lack thereof)
	prepareFile			: function(href) { return href; },
	onPlay 				: function()
	{
		this.loadhead.style.width 	= 0;
		this.playhead.style.width 	= 0;
		return;
	},
	onPause				: function() { return; },
	onProgressUpdate 	: function(ph, lh)
	{
		this.loadhead.style.width = lh + '%';
		this.playhead.style.width = ph + '%';
		return;
	},
	
	prepare : function(pathToPlayerSWF)
	{
		if (swfobject)
		{
			swfobject.embedSWF(pathToPlayerSWF, 'player', '0', '0', '6.0.0', false, {}, {}, {'id' : 'player'});
			swfobject.addDomLoadEvent(function(){ Player.init(); });
		};
	},
	
	init : function()
	{
		this.player		= document.getElementById('player');
		if (this.player.nodeName == 'DIV')
		{
			document.body.className += ' no-player';
			return;
		};
		this.playBtn 	= document.getElementById('play-pause');
		this.progress 	= document.getElementById('progress');
		this.loadhead 	= document.getElementById('loadhead');
		this.playhead 	= document.getElementById('playhead');
		this.tracks 	= document.getElementById('tracks').getElementsByTagName('a');

		this.file		= this.tracks[0].href;
		this.track		= -1;

		document.onkeydown = function(e)
		{
			if (e.metaKey || e.ctrlKey) return;
			
			var acted = false;
			
			switch(e.keyCode)
			{
				case 32: // space
					Player.playPause();
					acted = true;
				break;
				
				case 37: // left
				case 38: // up
					Player.playPrevious();
					acted = true;
				break;
				
				case 39: // right
				case 40: // down
					Player.playNext();
					acted = true;
				break;
			};
			if (acted && e.preventDefault)
			{
				e.preventDefault();
			};
		};

		this.playBtn.onclick = function()
		{
			Player.playPause();
			return false;
		};

		this.playBtn.ondblclick = function()
		{
			Player.playNext();
			return false;
		};

		for (var i=0; i<this.tracks.length; i++)
		{
			var a = this.tracks[i];
			a.onclick = this.makePlayTrackHandler(i);
			a.target = '_blank';
			var span = a.getElementsByTagName('span')[0];
			span.onclick = function(event){ Player.downloadTrack(event); };
		};
	},
	
	play : function()
	{
		this.isPlaying = true;
		this.playBtn.className = this.playBtn.className.replace(/\s*\bpaused\b/g,'');
		this.playBtn.className += ' playing';
		if (this.currentTrack)
		{
			var li = this.currentTrack.parentNode;
			li.className = li.className.replace(/\s*\bpaused\b/g,'');
		};
		this.onPlay();
		this.player.TCallLabel('/','play');
	},
	
	pause : function()
	{
		this.isPlaying = false;
		this.playBtn.className = this.playBtn.className.replace(/\s*\bplaying\b/g,'');
		this.playBtn.className += 'paused';
		this.currentTrack.parentNode.className += ' paused';
		this.onPause();
		this.player.TCallLabel('/','pause');
	},
	
	playPause : function()
	{
		if (this.isPlaying) // pause
		{
			this.pause();
		}
		else // play
		{
			if (this.track == -1)
			{
				this.playNext();
			}
			else
			{
				this.play();
			};
		};
	},
	
	playTrack : function(i)
	{
		if (this.track != i) // changing tracks
		{
			// update class of containing list-item
			if (this.currentTrack)
			{
				var li = this.currentTrack.parentNode;
				li.className = li.className.replace(/\s*\b(currently\-playing|paused)\b/g,'');
			};
			this.currentTrack = this.tracks[i];
			this.track = i;
			this.currentTrack.parentNode.className += ' currently-playing';
			
			this.player.SetVariable('file', this.prepareFile(this.currentTrack.href));
			this.player.TCallLabel('/','new');
		};
		this.play();
	},
	
	playNext : function()
	{
		var n = this.track + 1;
		if (n >= this.tracks.length)
		{
			n = 0;
		};
		this.playTrack(n);
	},
	
	playPrevious : function()
	{
		var n = this.track - 1;
		if (n < 0)
		{
			n = this.tracks.length - 1;
		};
		this.playTrack(n);
	},
	
	updateProgress : function(ph, lh)
	{
		this.onProgressUpdate(ph, lh);
	},
	
	makePlayTrackHandler : function(i)
	{
		return function()
		{
			Player.playTrack(i);
			return false;
		};
	},
	
	downloadTrack : function(event)
	{
		event.stopPropagation();
		return true;
	}
};
