<?php

	$actions = array('new', 'top', 'dload');	

//	(key_exists('action', $_GET) && $_GET['action']<>'' && in_array($_GET['action'], $actions) && session_is_registered('mobile_logged_in')) ? $action = $_GET['action'] : $action = '';
//	(key_exists('i', $_GET) && $_GET['i']<>'' && session_is_registered('mobile_logged_in')) ? $i = $_GET['i'] : $i = 1;
//	(key_exists('typ', $_GET) && $_GET['typ']<>'' && session_is_registered('mobile_logged_in')) ? $typ = $_GET['typ'] : $typ = 0;
//	(key_exists('genre', $_GET) && $_GET['genre']<>'' && session_is_registered('mobile_logged_in')) ? $genre = $_GET['genre'] : $genre = 0;
	
	$action = (key_exists('action', $_GET) && $_GET['action']<>'' && in_array($_GET['action'], $actions)) ? $_GET['action'] : '';
	$i = (key_exists('i', $_GET) && $_GET['i']<>'') ? intval($_GET['i']) : 1;
	$typ = (key_exists('typ', $_GET) && $_GET['typ']<>'') ? intval($_GET['typ']) : 0;
	$genre = (key_exists('genre', $_GET) && $_GET['genre']<>'') ? intval($_GET['genre']) :  0;
	
	if(session_is_registered('mobile_logged_in') && session_is_registered('mobile_page_hry') && $_SESSION['mobile_page_hry']!=0)
	{
		$nastranu = $_SESSION['mobile_page_hry'];
	}
	else
	{
		$nastranu = 9;
	}
	
	if($action=='top') 
	{
		$final_query = "SELECT * FROM mobile_games WHERE hlasov>10 ORDER BY priemer DESC limit 0, $hry_charts";
	}

	if($action=='dload') 
	{
		$final_query = "SELECT * FROM mobile_games WHERE dload>0 ORDER BY dload DESC limit 0, $hry_charts";
	}
	
	if($action=='new') 
	{
		$final_query = "SELECT * FROM mobile_games GROUP BY nazov ORDER BY ID DESC limit 0, $hry_charts";
		if($typ != 0)
		{
			$final_query = "SELECT * FROM mobile_games WHERE typ='$typ' ORDER BY ID DESC limit 0, $hry_charts";
		}
	}
	
	if($_SERVER['REQUEST_METHOD'] == 'POST')
	{
		$stranok = 1;
//		if(strlen($_POST['search']) >= 3)
		{
			if($_POST['zaner'] == 0)
			{
				$final_query = "SELECT * FROM mobile_games WHERE nazov LIKE '%".$_POST['search']."%' ORDER BY nazov ASC limit 0, 10";
			}
			else 
			{
				$final_query = "SELECT * FROM mobile_games WHERE nazov LIKE '%".$_POST['search']."%' AND typ='".$_POST['zaner']."' ORDER BY nazov ASC limit 0, 10";
			}
		}
	}
	elseif($action=='') 
	{
//		if(session_is_registered('mobile_logged_in'))
		{
			$inc = '';
			if($typ != 0)
			{
				$inc .= " WHERE typ='$typ'";
			}
			if($genre != 0)
			{
				if($inc == '')
				{
					$inc .= ' WHERE ';
				}
				else 
				{
					$inc .= ' AND ';
				}
				$inc .= " genre='$genre'";
			}
			$query = "SELECT * FROM mobile_games $inc"; 
			$chunk = $dba->GetLimit($query);
			$lim = $chunk[0];
	    	$max = $chunk[1];
	    	$stranok = $chunk[2];
			$final_query = "SELECT * FROM mobile_games $inc ORDER BY ID DESC limit $lim, $nastranu";
		}
//		else 
//		{
//			$final_query = "SELECT * FROM mobile_games  ORDER BY nazov ASC limit $lim, 5";
//		}
		
//		if($typ!=0 && session_is_registered('mobile_logged_in'))
//		{
//			$query = "SELECT * FROM mobile_games WHERE typ=$typ"; 
//			$chunk = $dba->GetLimit($query);
//			$lim = $chunk[0];
//	    	$max = $chunk[1];
//	    	$stranok = $chunk[2];
//			$final_query = "SELECT * FROM mobile_games WHERE typ=$typ ORDER BY nazov ASC limit $lim, $nastranu";
//		}
//		else 
//		{
//			$query = "SELECT * FROM mobile_games"; 
//			$chunk = $dba->GetLimit($query);
//			$lim = $chunk[0];
//	    	$max = $chunk[1];
//	    	$stranok = $chunk[2];
//			$final_query = "SELECT * FROM mobile_games ORDER BY nazov ASC limit $lim, $nastranu";
//			if(!session_is_registered('mobile_logged_in')) $final_query = "SELECT * FROM mobile_games  ORDER BY nazov ASC limit $lim, 5";
//		}
	}

	$nadpisy = array
	(
		'new' => 'NEW '.$hry_charts.' hry',
		'top' => 'TOP '.$hry_charts.' hry',
		'dload' => 'TOP DOWNLOAD '.$hry_charts.' hry',
	);
	
		function  fnVypisHry($result)
	{
		global $game_types, $game_genres, $blocked_dloads;
		
		if($result)
		{
			InitialzeReklama();
			if(fnIsAdmin() && session_is_registered('hry_edit'))
			{
				echo "<form name='frm' action='index.php?module_top=hry_sort&action=sort' method='POST'>";
			}
			while($row = mysql_fetch_array($result))
			{
				ShowReklama();
				$size=round(filesize('games/'.$row["file"])/1024,2);		
				
				echo "<div id=\"melodia_div\">";
				echo "<table width=\"100%\" border=\"0\" cellspacing=\"2\"><tr><td valign=\"top\" width=\"128\">";
				echo "<img src=\"images/games/".$row['image']."\" alt=\"\" />";
				echo "</td><td valign=\"top\" style='padding-left:5px;'>";
				echo"<b>".$row['nazov']."</b> - <span id=\"red_b\">ID: ".$row["ID"]."</span> (".$size." kB)<br />";
				echo "http://4mobile.sk/hra/".$row["ID"]."/<br />";
				if($row["poznamka"]<>'') echo $row["poznamka"]."<br />";
				echo "<span id=\"red\"><b>Pre telefóny:</b></span> ".$game_types[$row['typ']]['popis'].'<br />';
				$priemer = round($row["priemer"],2);
				fnShowStars($priemer);
				echo '&nbsp;'.$row["hlasov"]." hlasov, priemer: ".$priemer.", ".$row["dload"]." stiahnutí <br /> <span id=\"red_b\">Hlasuj!</span> <img src=\"images/smile/17.gif\" alt=\"\" /> ";
				for($k=1;$k<=5;$k++) {echo"<a href='hlasuj.php?action=game&amp;id=".$row['ID']."&amp;hlas=".$k."' id=\"hlasuj\">".$k."</a> ";}
				echo "<img src=\"images/smile/5.gif\" alt=\"\" />";
				if(!$blocked_dloads[2] && session_is_registered('mobile_logged_in'))
				{
					echo " | <a href='http://4mobile.sk/hra/".$row["ID"]."/' class='tahaj'>STIAHNI</a>";
				}
				echo "</td></tr></table>";
				if(fnIsAdmin() && session_is_registered('hry_edit'))
				{
					echo 'Žáner: <select name="sort['.$row["ID"].']" class="field">';
					foreach($game_genres as $kg => $g_name)
					{
						echo '<option value="'.$kg.'"';
						if($kg == $row['genre'])
						{
							echo ' selected';
						}
						echo '>'.$g_name.'</option>';
					}
					echo "</select><br />";
				}
				echo "</div>";
			}
			if(fnIsAdmin() && session_is_registered('hry_edit'))
			{
				echo '<div style="width:100%: text-align: center;"><input type="submit" name="submit" value="Utrieď" class="field" /></div>';
				echo '</form>';
			}
		}
	}
	
?>

	<div id="register_form01">
	  <div id="menu01_head"><h1 class='h1_header'>:: 
	  	<?php 
	  		if(key_exists($action, $nadpisy))
	  		{ 
	  			echo $nadpisy[$action];
	  		} 
	  		else 
	  		{
	  			if(array_key_exists($genre, $game_genres))
	  			{
	  				echo $game_genres[$genre].' ';
	  			}
	  			echo 'hry na mobil';
	  		}		
	  	?></h1></div>
	  <div id="melodie_01">
	    <?php 
	    
	    	if(!session_is_registered('mobile_logged_in')) echo "<br />Zaregistrovaní používatelia získajú prístup ku všetkým hrám! <br />";
    		if(($action=='')) fnNavi($i, $stranok, "module=hry&amp;action=$action&amp;typ=$typ&amp;genre=$genre");
	    	$result = $dba->GetResult($final_query);
	    	fnVypisHry($result);
	    	if(($action=='')) fnNavi($i, $stranok, "module=hry&amp;action=$action&amp;typ=$typ&amp;genre=$genre");
    	?>
	  </div>
	</div>