<div class="dataBox dataBoxLibrary rel">
	<?php 
	if($session->result=="ok"){
		if(isset($_GET['pid'])){
			$listID = (int) $hash->decode($_GET['pid'])[0];
			$plist = $z->select("SELECT * FROM playlists WHERE ID=? AND uid=? AND status=? LIMIT 1", array($listID, $session->ID, "yes"), "iis");
			echo '<h2 class="size30 fontb cfff areaTitle areaTitleM">View Playlist</h2>';
			if($plist->result=="ok" && $plist->count > 0){
				$ids = array();
				$list = $z->select("SELECT tid FROM playlists_tracks WHERE pid=? AND status=? GROUP BY tid ORDER BY ID DESC", array($listID,"yes"), "is");
				if($list->result=="ok" && $list->count > 0){
					$ids = array();
					for($n = 0; $n < count($list->fetch); $n++):
						array_push($ids, $list->fetch[$n]->tid);
					endfor;
					$params = implode(",", array_map('intval', $ids));
					$list = $z->select("SELECT * FROM tracks WHERE ID IN ($params) AND status=?", array("yes"), "s");					
					echo '<div class="ibl pl-big rel">';
						$s = $plist->row->tracks > 1 ? "s" : "";						
						if($plist->row->tracks > 0){
							$in = $list->fetch[rand(0, count($list->fetch)-1)]->ID;
							$_ytid = $z->select("SELECT ytid,poster FROM tracks WHERE ID=? LIMIT 1", array($in), "i")->row;
							$ytid = $_ytid->ytid;							
							$img = baseurl.'photo/track/'.$z->encode($_ytid->poster).'/default.jpg';
						}else{					
							$img = baseurl.'ui/no-image.png';
						}
						echo '<ul class="play-list ibl fontn">'
						. '<li class="rel plist'.$hash->encode($listID).'"><div class="cvr cvr'.$hash->encode($listID).' abs"></div><a href="'.baseurl.'library/'.$hash->encode($listID).'" class="bl noul initplist initplist'.$hash->encode($listID).'" data-id="'.$hash->encode($listID).'" data-title="'.stripslashes($plist->row->label).'" data-count="'.$plist->row->tracks.'">'
						. '<div class="bl mg"><img src="'.$img.'" class="bl"></div>'
						. '<h2 class="size14 fontb cfff lbl wordwrap bl">'.stripslashes($plist->row->label).'</h2>'
						. '<h2 class="size13 fontn cfff ctr wordwrap bl">'.number_format($plist->row->tracks).' Track'.$s.'</h2></a>'				
						. '<a href="javascript:;" class="bl anim initplistB initplist'.$hash->encode($listID).' size13 fontn" data-title="'.stripslashes($plist->row->label).'" data-count="'.$plist->row->tracks.'"><div class="icon-play_arrow"></div></a>'
						. '<button  data-id="'.$hash->encode($listID).'" data-title="'.stripslashes($plist->row->label).'" data-count="'.$plist->row->tracks.'" class="bl anim initplist initplist'.$hash->encode($listID).' size13 fontn">Play</button></li>'
						. '</ul>'
					. '</div>';					
					echo '<div class="ibl pl-rit"><ul class="playlisting fontn">';
					for($n = 0; $n < count($list->fetch); $n++):
						echo $z->trackItemPlaylist($list->fetch[$n], $listID);
					endfor;
					echo '</ul></div>';
				}else{
					echo '<div class="emptyTracks rel size30 cfff fontl"><div class="icon-line_style ico"></div>Playlist is Empty</div>';	
				}
			}else{
				echo '<div class="emptyTracks rel size30 cfff fontl"><div class="icon-puzzle ico"></div>Requested Content is Unavailable</div>';
			}
		}else{
			$start = 0; $limit = 50;
			if(isset($_GET['start'])){ 
				$ostart = $_GET['start'];
				$start = $limit * $ostart - $limit;	
			}					
			echo '<h2 class="size30 fontb cfff areaTitle">Your Library</h2>';
			echo '<button class="size14 fontn cfff abs anim bnp-new btnNewPlayList"><div class="ibl icon-plus ico"></div>Create Playlist</button>';
			$list = $z->select("SELECT * FROM playlists WHERE uid=? AND status=? ORDER BY ID DESC LIMIT ?,?", array($session->ID, "yes", $start, $limit), "isii");
			if($list->result == "ok" && $list->count > 0){			
				echo '<ul class="play-list ibl fontn">';
				for($n = 0; $n < count($list->fetch); $n++):
					$s = $list->fetch[$n]->tracks > 1 ? "s" : "";
					$ID = $list->fetch[$n]->ID;
					if($list->fetch[$n]->tracks > 0){
						$in = $z->select("SELECT tid FROM playlists_tracks WHERE pid=? AND status=? ORDER BY RAND() LIMIT 1", array($ID, "yes"), "is")->row->tid;
						$_ytid = $z->select("SELECT ytid,poster FROM tracks WHERE ID=? LIMIT 1", array($in), "i")->row;
						$ytid = $_ytid->ytid;
						$img = baseurl.'photo/track/'.$z->encode($_ytid->poster).'/default.jpg';
					}else{					
						$img = baseurl.'ui/no-image.png';
					}
					echo '<li class="rel plist'.$hash->encode($list->fetch[$n]->ID).'"><div class="cvr cvr'.$hash->encode($list->fetch[$n]->ID).' abs"></div><a href="'.baseurl.'library/'.$hash->encode($list->fetch[$n]->ID).'" class="bl noul viewplist" data-id="'.$hash->encode($list->fetch[$n]->ID).'" data-title="'.stripslashes($list->fetch[$n]->label).'" data-count="'.$list->fetch[$n]->tracks.'">'
					. '<div class="bl mg"><img src="'.$img.'" class="bl"></div>'
					. '<h2 class="size14 fontb cfff lbl wordwrap bl">'.stripslashes($list->fetch[$n]->label).'</h2>'
					. '<h2 class="size13 fontn cfff ctr wordwrap bl">'.number_format($list->fetch[$n]->tracks).' Track'.$s.'</h2></a>'				
					. '<button  data-id="'.$hash->encode($list->fetch[$n]->ID).'" data-title="'.stripslashes($list->fetch[$n]->label).'" data-count="'.$list->fetch[$n]->tracks.'" class="bl anim initplist initplist'.$hash->encode($list->fetch[$n]->ID).' size13 fontn">Play</button></li>';
				endfor;
				echo '</ul>';
			}else{
				echo '<div class="emptyTracks rel size40 cfff fontl"><div class="icon-puzzle ico"></div>It\'s a bit empty here...</div>';
			}
		}		
	}else{
		echo '<div class="emptyTracks rel size30 cfff fontl"><div class="icon-user ico"></div>You must sign in to access your Library</div>';
	}
	?>
</div>