<?php

	$actions = array('new', 'top', 'dload');	
	$types = array('mid', 'wav', 'amr', 'mp3');

//	(key_exists('action', $_GET) && $_GET['action']<>'' && in_array($_GET['action'], $actions) && session_is_registered('mobile_logged_in')) ? $action = $_GET['action'] : $action = '';
//	(key_exists('interpret', $_GET) && $_GET['interpret']<>'' && session_is_registered('mobile_logged_in')) ? $interpret = $_GET['interpret'] : $interpret = '';
//	(key_exists('nazov', $_GET) && $_GET['nazov']<>'' && session_is_registered('mobile_logged_in')) ? $nazov = $_GET['nazov'] : $nazov = '';
//	(key_exists('type', $_GET) && $_GET['type']<>'' && in_array($_GET['type'], $types) && session_is_registered('mobile_logged_in')) ? $type = $_GET['type'] : $type = '';
//	(key_exists('genre', $_GET) && $_GET['genre']<>'' && key_exists($_GET['genre'], $melody_genres) && session_is_registered('mobile_logged_in')) ? $genre = $_GET['genre'] : $genre = '';
//	(key_exists('i', $_GET) && $_GET['i']<>'' && session_is_registered('mobile_logged_in')) ? $i = intval($_GET['i']) : $i = 1;

	$action = (key_exists('action', $_GET) && $_GET['action']<>'' && in_array($_GET['action'], $actions)) ? $_GET['action'] : '';
	$interpret = (key_exists('interpret', $_GET) && $_GET['interpret']<>'') ? $_GET['interpret'] : '';
	$nazov = (key_exists('nazov', $_GET) && $_GET['nazov']<>'') ? $_GET['nazov'] : '';
	$type = (key_exists('type', $_GET) && $_GET['type']<>'' && in_array($_GET['type'], $types)) ? $_GET['type'] : '';
	$genre = (key_exists('genre', $_GET) && $_GET['genre']<>'' && key_exists($_GET['genre'], $melody_genres)) ? $_GET['genre'] : '';
	$i = (key_exists('i', $_GET) && $_GET['i']<>'') ? intval($_GET['i']) : 1;
	
	if(session_is_registered('mobile_logged_in') && session_is_registered('mobile_page_melody') && $_SESSION['mobile_page_melody']!=0)
	{
		$nastranu = $_SESSION['mobile_page_melody'];
	}
	else
	{
		$nastranu = 9;
	}
	
	if($action=='top') 
	{
		$final_query = "SELECT * FROM mobile_melody WHERE hlasov>10 ORDER BY priemer DESC limit 0, $melodie_charts";
		$query = $final_query;
	}

	if($action=='dload') 
	{
		$final_query = "SELECT * FROM mobile_melody WHERE dload>0 ORDER BY dload DESC limit 0, $melodie_charts";
		$query = $final_query;
	}
	
	if($action=='new') 
	{
		$inc = '';
		if($type<>'') $inc = " WHERE RIGHT(file, 3)='$type'";
		$final_query = " SELECT * FROM mobile_melody$inc ORDER BY ID DESC limit 0, $melodie_charts";
		$query = $final_query;
	}
	
	if($action=='')
	{
		if($interpret=='cis')
		{
			$inc = '';
			if($type<>'') $inc .= " AND RIGHT(file, 3)='$type'";
			if($genre<>'') $inc .= " AND genre='$genre'";
			$query = "
				SELECT ID FROM mobile_melody WHERE (LEFT(interpret,1)='1' or LEFT(interpret,1)='2' or LEFT(interpret,1)='3' or LEFT(interpret,1)='4' or 
				LEFT(interpret,1)='5' or LEFT(interpret,1)='6' or LEFT(interpret,1)='7' or LEFT(interpret,1)='8' or LEFT(interpret,1)='9')$inc";
			$chunk = $dba->GetLimit($query);
			$lim = $chunk[0];
			$final_query = "
				SELECT * FROM mobile_melody WHERE (LEFT(interpret,1)='1' or LEFT(interpret,1)='2' or LEFT(interpret,1)='3' or LEFT(interpret,1)='4' or 
				LEFT(interpret,1)='5' or LEFT(interpret,1)='6' or LEFT(interpret,1)='7' or LEFT(interpret,1)='8' or LEFT(interpret,1)='9')$inc ORDER BY interpret, nazov ASC limit $lim, $nastranu";
		}
		
		if($interpret<>'' && $interpret<>'cis')
		{
			$inc = '';
			if($type<>'') $inc .= " AND RIGHT(file, 3)='$type'";
			if($genre<>'') $inc .= " AND genre='$genre'";
			$query = "
				SELECT ID FROM mobile_melody WHERE LEFT(interpret,1)='$interpret'$inc";
			$chunk = $dba->GetLimit($query);
			$lim = $chunk[0];
			$final_query = "
				SELECT * FROM mobile_melody WHERE LEFT(interpret,1)='$interpret'$inc ORDER BY interpret, nazov ASC limit $lim, $nastranu";
		}
		
		if($nazov=='cis')
		{
			$inc = '';
			if($type<>'') $inc .= " AND RIGHT(file, 3)='$type'";
			if($genre<>'') $inc .= " AND genre='$genre'";
			$query = "
				SELECT ID FROM mobile_melody WHERE (LEFT(nazov,1)='1' or LEFT(nazov,1)='2' or LEFT(nazov,1)='3' or LEFT(nazov,1)='4' or 
				LEFT(nazov,1)='5' or LEFT(nazov,1)='6' or LEFT(nazov,1)='7' or LEFT(nazov,1)='8' or LEFT(nazov,1)='9')$inc";
			$chunk = $dba->GetLimit($query);
			$lim = $chunk[0];
			$final_query = "
				SELECT * FROM mobile_melody WHERE (LEFT(nazov,1)='1' or LEFT(nazov,1)='2' or LEFT(nazov,1)='3' or LEFT(nazov,1)='4' or 
				LEFT(nazov,1)='5' or LEFT(nazov,1)='6' or LEFT(nazov,1)='7' or LEFT(nazov,1)='8' or LEFT(nazov,1)='9')$inc ORDER BY interpret, nazov ASC limit $lim, $nastranu";
		}
		
		if($nazov<>'' && $nazov<>'cis')
		{
			$inc = '';
			if($type<>'') $inc .= " AND RIGHT(file, 3)='$type'";
			if($genre<>'') $inc .= " AND genre='$genre'";
			$query = "
				SELECT ID FROM mobile_melody WHERE LEFT(nazov,1)='$nazov'$inc";
			$chunk = $dba->GetLimit($query);
			$lim = $chunk[0];
			$final_query = "
				SELECT * FROM mobile_melody WHERE LEFT(nazov,1)='$nazov'$inc ORDER BY interpret, nazov ASC limit $lim, $nastranu";
		}
		
		if($interpret=='' && $nazov=='')
		{
			$inc = '';
			if($type<>'') $inc .= " WHERE RIGHT(file, 3)='$type'";
			if($genre<>'') 
			{
				if($inc == '')
				{
					$inc .= ' WHERE ';
				}
				else 
				{
					$inc .= ' AND ';
				}
				$inc .= " genre='$genre'";
			}
			$query = "
				SELECT ID FROM mobile_melody$inc";
			$chunk = $dba->GetLimit($query);
			$lim = $chunk[0];
			$final_query = "
				SELECT * FROM mobile_melody$inc ORDER BY interpret, nazov ASC limit $lim, $nastranu";
		}
	}

	$nadpisy = array
	(
		'new' => 'NEW '.$melodie_charts.' melódie',
		'top' => 'TOP '.$melodie_charts.' melódie',
		'dload' => 'TOP DOWNLOAD '.$melodie_charts.' melódie',
	);
	
	
?>

	<div id="register_form01">
	  <div id="menu01_head"><h1 class='h1_header'>:: <?php if(key_exists($action, $nadpisy)) echo $nadpisy[$action]; else echo $type.' melódie'; ?></h1></div>
	  <div id="melodie_01">
	    <?php 
	    
//	    	dump($final_query);
//	    	dump('g: '.$genre);
	    
	    	if(!session_is_registered('mobile_logged_in')) echo "<br />Zaregistrovaní používatelia môžu sťahovať a počúvať všetky melódie! <br />";
	    	$chunk = $dba->GetLimit($query);
	    	$max = $chunk[1];
	    	$stranok = $chunk[2];
//	    	echo $final_query;
	    	if($max>0)
	    	{
//	    		if(($action=='') && session_is_registered('mobile_logged_in')) fnNavi($i, $stranok, "module=melodie&amp;action=$action&amp;interpret=$interpret&amp;nazov=$nazov&amp;type=$type&amp;genre=$genre");
	    		if($action=='') fnNavi($i, $stranok, "module=melodie&amp;action=$action&amp;interpret=$interpret&amp;nazov=$nazov&amp;type=$type&amp;genre=$genre");
		    	$result = $dba->GetResult($final_query);
//		    	if(fnIsAdmin())
//		    	{
		    		fnVypisMelodieNew($result);
//		    	}
//		    	else{
//		    		fnVypisMelodie($result);
//		    	}
//		    	if(($action=='') && session_is_registered('mobile_logged_in')) fnNavi($i, $stranok, "module=melodie&amp;action=$action&amp;interpret=$interpret&amp;nazov=$nazov&amp;type=$type&amp;genre=$genre");
		    	if($action=='') fnNavi($i, $stranok, "module=melodie&amp;action=$action&amp;interpret=$interpret&amp;nazov=$nazov&amp;type=$type&amp;genre=$genre");
	    	}
	    	else echo "Prepáčte, ale melódia podľa zadaných kritérií sa tu nenachádza.";
	    	?>
	  </div>
	</div>