<div class="dataBox rel">

	<div class="searchSP rel">
		<h2 class="size30 fontb cfff areaTitle">Search <?php echo SITE_NAME; ?></h2>
		<div class="rel">
			<input type="text" value="<?php echo isset($_GET['q']) ? $_GET['q'] : ''; ?>" placeholder="Track name or artist" class="size30 fontl cfff searchq">
			<button class="size50 srchbtn abs"><div class="icon-zoom"></div></button>
		</div>
	</div>

	<?php 
	$start = 0; $limit = 5;
	if(isset($_GET['start'])){ 
		$ostart = $_GET['start'];
		$start = $limit * $ostart - $limit;	
	}		
	if(isset($_GET['q']) && !empty($_GET['q'])){
		$keyword = str_replace(" ", "+", urldecode($_GET['q']));
		$q = "SELECT * FROM tracks WHERE status=? AND (";						
		$okeyword = $keyword = $z->clean(str_replace("+"," ", $_GET['q']));
		$x = array_reverse(explode(" ", $keyword));	
		$keywords_set = array();
		foreach($x as $word):
			array_push($keywords_set, $word);		
		endforeach;
		$combinations = $z->getCombinations($keywords_set);
		$c = 0;
		foreach($combinations as $newWord):
			if(!empty($newWord)){	
				if(substr($newWord, -1)==' '){ $newWord = substr($newWord, 0, -1); }
				if($c>0){	$q .= " OR "; }
				$newWord = $z->realescape($newWord);
				$q .= " title LIKE '%$newWord%' ";
				$c++;
			}		
		endforeach;			
		$q .= " ) ";
		$total = $z->select($q, array("yes"), "s")->count;	
		$q .= " ORDER BY ";
		foreach($combinations as $newWord):
			if(!empty($newWord)){	
				if(substr($newWord, -1)==' '){ $newWord = substr($newWord, 0, -1); }
				$newWord = $z->realescape($newWord);
				$q .= "CASE WHEN instr(title, '$newWord') = 0 THEN 1 ELSE 0 END,";
			}
		endforeach;			
		$q .= "instr(title, '$keyword') DESC";			
		$q .= " LIMIT ?, ?";	
		$list = $z->select($q, array("yes",$start,$limit), "sii");
		if($list->result=="ok" && $list->count > 0){
			$rs = $total > 1 ? "s" : "";
			echo '<div class="searchSP searchSPP rel"><h2 class="size13 fontn areaTitle">About <span class="fontb">'.number_format($total).'</span> Result'.$rs.' for <span class="fontb">'.$_GET['q'].'</span></h2></div>';
			echo '<ul class="tracks ibl fontn">';
			for($n = 0; $n < count($list->fetch); $n++):
				echo $z->trackItem($list->fetch[$n]);
			endfor;
			echo '</ul>';
			
			$qry = str_replace(" ", "+", $_GET['q']);
			$pagin = '<ol class="pages pagesSearch ibl">';
			$fresh_start = isset($_GET['start']) ? $_GET['start'] : 1;	
			$next = $fresh_start+1;
			$prev = $fresh_start-1;
			$prevlink = baseurl.'search?q='.$qry.'&start='.$prev;
			$link_first = baseurl.'search?q='.$qry;
			$nextlink = baseurl.'search?q='.$qry.'&start='.$next;
			$link_last = baseurl.'search?q='.$qry.'&start='.ceil($total/$limit); 
			$link_i = baseurl.'search?q='.$qry.'&start=';
			$pagin .= $z->pagination($total, $limit, $start, $fresh_start, $link_first, $prevlink, $prev, $next, $nextlink, $link_last, $link_i, 'Search: '.$_GET['q']);		
			$pagin .= '</ol>';
			echo $pagin;	
				
		}else{
			echo '<div class="emptyTracks rel size40 cfff fontl"><div class="icon-puzzle ico"></div>Nothing found for '.$_GET['q'].'</div>';
		}
	}else{
		echo '<div class="emptyTracks rel size40 cfff fontl"><div class="icon-puzzle ico"></div>Enter Search Query...</div>';
	}
	?>
	
</div>