
	_Playlist = function()
		{
			if(window == this)
				return new _Playlist();
			
			return false;
		}
	
	_Playlist.prototype =
		{
			List : [],
			currentSong : -1,
			
			addSongLoad : false,
			addSong : function(ID, Obj)
				{
					if(this.addSongLoad)
						return false;
				
					Obj.src = '/images/icons/loading.ajax.gif';
					Obj.className += 'Active';
					
					this.addSongLoad	= true;
					
					this.addSong.AJAX	= new AjaxLite();
					var AJAX			= this.addSong.AJAX;
					AJAX.URL			= 'data.php/playlist/song/';
					AJAX.Post['id']		= ID;
					AJAX.ORSC			= function() { $Pl.addSong.AJAX.doCheck(); };
					AJAX.doOnChange		= function()
						{
							$Pl.addSongLoad = false;

							var Img = this.xmlhandler.imgObj;
							Img.src = '/images/icons/accept.png';
							Img.onclick = '';
							Img.title = 'Added to playlist';
							
							if(!$Pl.checkReload())
								return false;
							
							if(this.Re[0] == 'Ok')
								{
									if(!parent.$Pl.List.length)
										parent.$('Ui:Playlist:List').innerHTML = '';
								
									var New = [];
									for(var $i = 1; $i < this.Re.length; $i++)
										{
											var Data = this.Re[$i].split('<><>');
									
											New[New.length] =
												{
													ID:		Data[2],
													Title:	Data[1],
													Artist:	Data[5],
													Album:	Data[3],
													Year:	Data[0]
												};
										}
									parent.$Pl.List = parent.$Pl.List.concat(New);
									parent.$Pl.parseList(New);
									
									if(parent.$Pl.currentSong < 0)
										parent.$Pl.prepareSong(0);
									
									//setTimeout(function(a) { a.src = '/images/icons/delete.png'; a.className = a.className.split(' ')[0]; }, 2000, Img);
								}
							else
								{
									alert('Song already added to playlist!');
								}
							
							return true;
						};
					AJAX.doRequest();
					AJAX.xmlhandler.imgObj = Obj;

					return false;
				},
			
			checkReload : function(Prefix)
				{
					Prefix = (typeof(Prefix) == 'string') ? Prefix+'/' : '';
					if(typeof(top.location.href) != 'string' || top.location.pathname.match(/\/playlist\/$/) == null)
						{
							top.location.href = 'http://'+document.domain + '/playlist/#'+Prefix+document.location.pathname.substr(1);
						}
					else if(typeof(top.$Pl) != 'undefined')
						{
							return true;
						}
					else
						{
							alert("Can't load Snake86.nl Playlist module, please reload the page.");
						}
					
					return false;
				},
			
			recountList : function()
				{
					var $a = $('Ui:Playlist:List').getElementsByTagName('a');
					for(var $i = 0; $i < $a.length; $i++)
						{
							$a[$i].No = $i;
						}
				},
			
			parseList : function(Arr)
				{
					Arr = (typeof(Arr) != 'undefined') ? Arr : this.List;
					
					var $a = $('Ui:Playlist:List').childNodes.length;
					for(var $i = 0; $i < Arr.length; $i++)
						{
							var Data = Arr[$i];
							
							var DelFunc = function(e)
								{
									e = (window.event) ? window.event : e;
									if(typeof(e.preventDefault) == 'function')
										e.preventDefault();
									if(typeof(e.stopPropagation) == 'function')
										e.stopPropagation();
									return $Pl.removeList(this.parentNode.No, this);
								}
							
							var Title	= $cE('span', {className: 'Title', innerHTML: Data['Title']});
							var Artist	= $cE('span', {className: 'Info', innerHTML: Data['Artist']});
							var Delete	= $cE('img', {src: '/images/icons/delete.normal.png', className: 'Delete', onclick: DelFunc});
							var Album	= $cE('span', {className: 'Info', innerHTML: Data['Album'] + ' ('+Data['Year']+')', style: {cssFloat: 'right', htmlFloat: 'right'}});
							var Clear	= $cE('span', {style: {clear: 'both'}});
							
							var a		= $cE('a', {href: '#', No: ($a+$i), onclick : function() { return $Pl.prepareSong(this.No); }});
							a.appendChild(Delete);
							a.appendChild(Title);
							a.appendChild(Album);
							a.appendChild(Artist);
							a.appendChild(Clear);
							
							$('Ui:Playlist:List').appendChild(a);
						}
				},
			
			doReloadLoad : false,
			doReload : function()
				{
					this.doReload.AJAX	= new AjaxLite();
					var AJAX			= this.doReload.AJAX;
					AJAX.URL			= 'data.php/playlist/';
					AJAX.ORSC			= function() { $Pl.doReload.AJAX.doCheck(); };
					AJAX.doOnChange		= function()
						{
							var First = this.Re[0].split('<><>');
							$('Ui:Playlist:List').innerHTML = '';
							$Pl.List = [];
							if(First[0] == 'Ok')
								{
									for(var $i = 1; $i < this.Re.length; $i++)
										{
											var Data = this.Re[$i].split('<><>');

											$Pl.List[$Pl.List.length] =
												{
													ID:		Data[2],
													Title:	Data[1],
													Artist:	Data[5],
													Album:	Data[3],
													Year:	Data[0],
													Ext:	Data
												};
										}
									
									$Pl.parseList();
									
									if($i > 1)
										{
											$Pl.toggleList(true);
											if(First.length > 1 && $Pl.currentSong < 0)
												$Pl.prepareSong(parseInt(First[1]));
										}
								}
							else
								{
									var Span = $cE('span', {style: {padding: '20px 10px', display: 'block', textAlign: 'center', color: '#C0C0C0'}, innerHTML: 'No playlist items&hellip;<br />Start adding items by pressing the <img src="/images/icons/big/add.png" alt="+" style="vertical-align: middle;" /> next to songs'});
									$('Ui:Playlist:List').appendChild(Span);
								}
						};
					AJAX.doRequest();
				},
			
			toggleList : function(Show)
				{
					var Pl = $('Ui:Playlist');
					if(typeof(Show) == 'undefined')
						Pl.className = (Pl.className == 'Show') ? '' : 'Show';
					else
						Pl.className = (Show) ? 'Show' : '';
					
					$('Ui:Playlist:Title').innerHTML = (Pl.className == '' && this.currentSong > -1) ? this.List[this.currentSong]['Title'] : 'Playlist';
				},
			
			prepareSongLoad : false,
			prepareSong : function(Nr, stopBuffering, Force)
				{
					if(Nr == this.currentSong && typeof(Force) != 'boolean')
						return false;
				
					this.prepareSongLoad = true;
					
					if($f(0).isPlaying() && (typeof(stopBuffering) != 'boolean' || stopBuffering != false))
						$f(0).stopBuffering();

					if($('Ui:Playlist:List').childNodes.length > Nr && Nr != -1)
						$('Ui:Playlist:List').childNodes[Nr].className = 'Loading';

					this.prepareSong.AJAX	= new AjaxLite();
					var AJAX			= this.prepareSong.AJAX;
					AJAX.URL			= 'data.php/playlist/prepare/';
					AJAX.Post['no']		= Nr;
					AJAX.ORSC			= function() { $Pl.prepareSong.AJAX.doCheck(); };
					AJAX.doOnChange		= function()
						{
							if(this.Re[0] == 'Ok')
								{
									var Data = this.Re[1].split('<><>');
									var Current = parseInt(Data[2]);
									
									var curData = $Pl.List[Current];
									if($('Ui:Playlist').className != 'Show' && typeof(curData) != 'undefined')
										$('Ui:Playlist:Title').innerHTML = curData['Title'];
									
									var Cn = $('Ui:Playlist:List').childNodes;
									for(var $i = 0; $i < Cn.length; $i++)
										{
											if($i < Current)
												Cn[$i].className = 'Played';
											else if($i == Current)
												Cn[$i].className = 'Playing';
											else
												Cn[$i].className = '';
										}

									$Pl.currentSong = Current;
									$f(0).play('/media/media.php/'+Data[0]+Data[1]);
									$f(0).getPlugin("play").css({opacity: 1});
								}
							else
								{
									
								}
							
							$Pl.prepareSongLoad = false;

						};
					AJAX.onError = function()
						{
							$Pl.prepareSongLoad = false;
							$('Ui:Playlist:List').childNodes[this.xmlhandler.imgObj].className = '';
						}
					AJAX.doRequest();
					AJAX.xmlhandler.Nr = Nr;
					
					return false;
				},
			
			removeListLoad : false,
			removeList : function(Nr, Obj)
				{
					Nr = (typeof(Nr) == 'number' && Nr >= 0) ? Nr : -1;
					this.removeListLoad = true;
					
					if(typeof(Obj) == 'object' && Obj.tagName.toLowerCase() == 'img')
						{
							Obj.src = '/images/icons/loading.ajax.gif';
							Obj.style.opacity = '1 !important';
							Obj.parentNode.onclick = null;
						}

					if(Nr == -1 && !confirm('Clear playlist?'))
						return false;

					if((this.List.length - 1) <= 0)
						Nr = -1;

					if(Nr >= 0)
						{
							$('Ui:Playlist:List').removeChild($('Ui:Playlist:List').childNodes[Nr]);
							this.recountList();
							$Pl.List.splice(Nr, Nr);
						}
					else
						{
							$f(0).stopBuffering();
						}
					
					this.removeList.AJAX	= new AjaxLite();
					var AJAX			= this.removeList.AJAX;
					AJAX.URL			= 'data.php/playlist/remove/';
					AJAX.Post['no']		= Nr;
					AJAX.ORSC			= function() { $Pl.removeList.AJAX.doCheck(); };
					AJAX.doOnChange		= function()
						{
							if(this.Re[0] == 'OkList')
								{
									$Pl.List = [];
									$Pl.loadNext();
									$Pl.doReload();
								}
							else if(this.Re[0] == 'OkNo')
								{
									var Nr = parseInt(this.Re[1]);
									if($Pl.currentSong == Nr)
										{
											$Pl.currentSong--;
											$Pl.loadNext();
										}
								}
							
							$Pl.removeListLoad = false;
						};
					AJAX.doRequest();
					
					return false;
				},

			playPause : function(playObj)
				{
					var Obj = $('Ui:Playlist:Title').parentNode.childNodes[0];

					if(typeof(playObj) != 'undefined' && playObj == false)
						{
							Obj.className = 'Control Play Inactive';
							$f(0).getPlugin("play").css({opacity: 0});
							return false;
						}

					if($f(0).isPlaying())
						{
							Obj.className = 'Control Pause';
							Obj.title = 'Pause';
						}
					else
						{
							Obj.className = 'Control Play';
							Obj.title = 'Play';
						}
				},

			loadNext : function()
				{
					this.currentSong++;
					if(this.currentSong >= this.List.length)
						{
							var Cn = $('Ui:Playlist:List').childNodes;
							if(this.currentSong > 0)
								if(typeof(Cn[this.currentSong-1]) == 'object')
									Cn[this.currentSong-1].className = 'Played';
							this.playPause(false);
							this.prepareSong(-1);
							$f(0).getPlugin("play").css({opacity: 0});
							this.currentSong = -1;
						}
					else
						{
							this.prepareSong(this.currentSong, false, true);
						}
				},
			
			errorHandler : function(Err)
				{
					Err = parseInt(Err);
					
					if(Err == 300)
						return false;
					
					var Pr = confirm('The following error occured:\n\n'+Err+'\n\nIf you\'ve got the same error multiple times, please contact the webmaster.\n\nWould you like to refresh the page, to restart the playlist again? (Your playlist will NOT be deleted!)');
					if(Pr)
						parent.document.location.reload();
				}
		}
