<?php 

	//povolene akcie pre pozadia
	$actions = array('new', 'top', 'dload', 'moje_pozadia');	
	
	if(!isset($_SESSION['zorad']))
	{
		$_SESSION['zorad'] = 0; //podla pridania
	}
	
	if(isset($_GET['zorad']) && in_array($_GET['zorad'], array(0, 1, 2))){
		$_SESSION['zorad'] = $_GET['zorad'];
	}
	
	$_SESSION['pass_file'] = true;

//	(array_key_exists('action', $_GET) && $_GET['action']<>'' && in_array($_GET['action'], $actions) && session_is_registered('mobile_logged_in')) ? $action = $_GET['action'] : $action = '';
	$action = (array_key_exists('action', $_GET) && $_GET['action']<>'' && in_array($_GET['action'], $actions)) ? $_GET['action'] : '';
//	$res = (array_key_exists('res', $_GET) && $_GET['res']<>'' && session_is_registered('mobile_logged_in')) ? $_GET['res'] : '';
	$res = (array_key_exists('res', $_GET)) ? $_GET['res'] : 0;
	$cat = (array_key_exists('cat', $_GET) && $_GET['cat']<>'') ? intval($_GET['cat']) : 1;
//	$i = (array_key_exists('i', $_GET) && $_GET['i']<>'' && session_is_registered('mobile_logged_in')) ? intval($_GET['i']) : 1;
	$i = (array_key_exists('i', $_GET) && $_GET['i']<>'') ? intval($_GET['i']) : 1;
	
	if(session_is_registered('mobile_logged_in') && session_is_registered('mobile_page_pozadia') && $_SESSION['mobile_page_pozadia']!=0)
	{
		$nastranu = $_SESSION['mobile_page_pozadia'];
	}
	else
	{
		$nastranu = 9;
//		$res = 1;
	}
	
	$resolution = array
	(
		0 => array ( 'width' => 200, 'height' => 200 ),
		1 => array ( 'width' => 128, 'height' => 128 ),
		2 => array ( 'width' => 174, 'height' => 130 ),
		3 => array ( 'width' => 128, 'height' => 160 ),
		4 => array ( 'width' => 128, 'height' => 80 ),
		5 => array ( 'width' => 176, 'height' => 220 ),
		6 => array ( 'width' => 132, 'height' => 176 ),
		7 => array ( 'width' => 208, 'height' => 208 ),
		8 => array ( 'width' => 240, 'height' => 320 )
	);
	
	// zvysenie statistiky poctu zobrazenych kategorii a rozliseni
	if(!session_is_registered("mobile_cat_".$cat) && $cat<>'')
	{
		$dba->Increment('mobile_img_cat', 'status', $cat, 1);
		session_register("mobile_cat_".$cat);
	}
	if(!session_is_registered("mobile_res_".$res) && $res<>'')
	{
		$dba->Increment('mobile_img_res', 'status', $res, 1);
		session_register("mobile_res_".$res);
	}
	
	if($action=='new')
	{
		$inc = '';
		if($res != 0) $inc = "mobile_pozadia.res=$res AND";

		$final_query = "
		SELECT mobile_pozadia.*, mobile_users.login as pridal_user 
		FROM mobile_pozadia, mobile_users 
		WHERE 
			$inc mobile_pozadia.pridal=mobile_users.id AND 
			mobile_pozadia.visible = 1 AND
			mobile_pozadia.confirmed = 'a' AND
			mobile_pozadia.cat <> 2
		ORDER by ID DESC limit 0, $pozadia_charts";
		$stranok = 0;
	}
	
	if($action=='top')
	{
		$final_query = "
		SELECT mobile_pozadia.*, mobile_users.login as pridal_user 
		FROM mobile_pozadia, mobile_users 
		WHERE 
			hlasov>10 AND 
			mobile_pozadia.pridal=mobile_users.id AND 
			mobile_pozadia.visible=1 AND
			mobile_pozadia.confirmed = 'a' AND
			mobile_pozadia.cat != 2
		ORDER by priemer DESC limit 0, $pozadia_charts";
		$stranok = 0;
	}
	
	if($action=='dload')
	{
		$final_query = "
		SELECT mobile_pozadia.*, mobile_users.login as pridal_user 
		FROM mobile_pozadia, mobile_users 
		WHERE 
			mobile_pozadia.dload>0 AND 
			mobile_pozadia.ID<>'-1' AND 
			mobile_pozadia.pridal=mobile_users.id AND 
			mobile_pozadia.visible=1 AND
			mobile_pozadia.confirmed = 'a'
		ORDER by dload DESC limit 0, $pozadia_charts";
		$stranok = 0;
	}
	
	if($action=='')
	{
		$inc = '';
		if($res<>'') $inc = " AND res=$res";
		$query = "SELECT ID FROM mobile_pozadia WHERE visible=1 AND cat=$cat".$inc;
		
//		dump($query);
		
		$chunk = $dba->GetLimit($query);
		$lim = $chunk[0];
		$stranok = $chunk[2];
		
		$inc = '';
		if($res<>'') $inc = "AND mobile_pozadia.res=$res ";
		
		$order = "mobile_pozadia.ID DESC";
		if($_SESSION['zorad'] == 1)
		{
			$order = "mobile_pozadia.dload DESC";
		}
		if($_SESSION['zorad'] == 2)
		{
			$order = "mobile_pozadia.priemer DESC";
		}
		
		$final_query = "
		SELECT mobile_pozadia.*, mobile_users.login as pridal_user  
		FROM mobile_pozadia, mobile_users 
		WHERE 
			mobile_pozadia.cat=$cat ".$inc."AND 
			mobile_pozadia.pridal=mobile_users.ID AND 
			mobile_pozadia.visible=1 AND
			mobile_pozadia.confirmed = 'a'
		ORDER by $order limit $lim, $nastranu";
//		dump($final_query);
	}
	
	if($action=='moje_pozadia')
	{
		$query = "SELECT ID FROM mobile_pozadia WHERE pridal=".$_SESSION['my_id'];
		$chunk = $dba->GetLimit($query);
		$lim = $chunk[0];
		$stranok = $chunk[2];
		
		$final_query = "
		SELECT mobile_pozadia.*, mobile_users.login as pridal_user  
		FROM mobile_pozadia, mobile_users 
		WHERE pridal=".$_SESSION['my_id']." AND mobile_pozadia.pridal=mobile_users.id 
		ORDER by mobile_pozadia.ID DESC limit $lim, $nastranu";
	}
	
	$nadpisy = array
	(
		'new' => 'NEW '.$pozadia_charts.' pozadia',
		'top' => 'TOP '.$pozadia_charts.' pozadia',
		'dload' => 'TOP DOWNLOAD '.$pozadia_charts.' pozadia',
		'moje_pozadia' => 'moje pozadia'
	);
//	dump(round(fnExec_time() - $start_time, 5));

	//vypis tabulky s pozadiami
	
	function fnVypisPozadia($result)
	{	
		global $nastranu, $res, $blocked_dloads, $admins, $categories;
		$times = 0;
		$num = mysql_num_rows($result);
		$j = 0;
		if(fnIsAdmin() && session_is_registered('pozadia_edit'))
		{
			echo "<form name='frm' action='index.php?module_top=pozadia_sort&action=sort' method='POST'>";
		}
		echo"<table border=\"0\" cellspacing=\"10\" cellpadding=\"0\" align=\"center\">";
		$ij = 0;
		$adsense_shown = false;
		while($row=mysql_fetch_array($result))
		{
			$ij++;
			if($row["ID"]>4922){$path='pozadia2';}
			if($row["ID"]<4922){$path='pozadia';}
			$break = ($res == 8) ? 2 : 3;
			$j++;
			if($j%$break==1) echo"  <tr>\n";
			echo"<td id=\"back\" width=\"".fnResWidth($res)."\" height=\"".fnResHeight($res)."\" valign=\"middle\" style=\"background-image:url('".$path."/".$row['file']."');\">\n<img src='images/image.gif' alt='Pozadia na mobil' width=\"".fnResWidth($res)."\" height=\"".fnResHeight($res)."\">\n</td>\n";
			if($row['res'] == 0)
			{
				list($obr[$j]['width'], $obr[$j]['height']) = getimagesize($path.'/'.$row["file"]); 
			}
			else 
			{
				$obr[$j]['width'] = fnResWidth($row['res']);
				$obr[$j]['height'] = fnResHeight($row['res']);
			}
			
			$obr[$j]['ID'] = $row["ID"];
			$obr[$j]['cat'] = $row["cat"];
			$obr[$j]['size'] = round(filesize($path.'/'.$row["file"])/1024,2);
			$obr[$j]['priemer'] = round($row["priemer"],2);
			$obr[$j]['hlasov'] = $row['hlasov'];
			$obr[$j]['pridal'] = $row['pridal_user'];
			$obr[$j]['stiahnuti'] = $row['dload'];
			if($j%$break==0 || ($num < $nastranu && $num==$j) || $ij == $nastranu) 
			{
				echo"  </tr>\n";
				echo"<tr>\n";
				$x = $times*$break + 1;
				for($i=$x; $i<=$j; $i++)
				{
					echo"<td align=\"center\"  style='background-color: #171717; padding: 6px;'>";
					
					if(!$blocked_dloads[0] && session_is_registered('mobile_logged_in'))
					{
						echo "<a href='http://4mobile.sk/pozadie/".$obr[$i]['ID']."/' class='tahaj'>STIAHNI</a><br />";
//						echo "<a href='http://4mobile.sk/pozadie.php?".SID."&amp;id=".$obr[$i]['ID']."' class='tahaj'>STIAHNI</a><br />";
					}
					
					echo "ID: ".$obr[$i]['ID']."<br />".$obr[$i]['width']."x".$obr[$i]['height']."px | ".$obr[$i]['size']." kB<br />";
					fnShowStars($obr[$i]['priemer']);
					echo '<br/>'.$obr[$i]['hlasov']." hlasov, priemer: ".$obr[$i]['priemer']."<br /><img src=\"images/smile/17.gif\" alt=\"\" /> ";
					for($k=1;$k<=5;$k++) {echo"<a href='hlasuj.php?action=image&amp;id=".$obr[$i]['ID']."&amp;hlas=".$k."' id=\"hlasuj\">".$k."</a> ";}
					echo"<img src=\"images/smile/5.gif\" alt=\"\" /><br />".$obr[$i]['stiahnuti']." stiahnutí<br />
					Pridal: <span id=\"pipe\"><b>".$obr[$i]['pridal']."</b></span><br />";
					if(fnIsAdmin() && session_is_registered('pozadia_edit'))
					{
						echo 'Kategória: <select name="sort['.$obr[$i]['ID'].']" class="field">';
						foreach($categories as $cat_id => $cat_name)
						{
							echo '<option value="'.$cat_id.'"';
							if($cat_id == $obr[$i]['cat'])
							{
								echo ' selected';
							}
							echo '>'.$cat_name.'</option>';
						}
						echo "</select><br />";
						echo "<a href='index.php?module_top=pozadie_del&amp;id=".$obr[$i]['ID']."' id='hlasuj'>zmaž</a>";
					}
					echo "</td>\n";
					unset($obr[$i]);
				}
				echo"</tr>\n";
				if(!$adsense_shown && rand(0, 1) == 1)
				{
					echo "<tr><td colspan='3' align='center'>";
					echo '<div class="reklama_div">
						<script type="text/javascript"><!--
						google_ad_client = "pub-4917301309934013";
						//468x60, bola vytvorená 14.1.2008
						google_ad_slot = "2353614394";
						google_ad_width = 468;
						google_ad_height = 60;
						//--></script>
						<script type="text/javascript"
						src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
						</script></div>
						';
					echo "</td></tr>";
					$adsense_shown = true;
				}
			$times+=1;
			}
		};
		echo"</table>\n";
		if(fnIsAdmin() && session_is_registered('pozadia_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(array_key_exists($action, $nadpisy)) echo $nadpisy[$action]; else echo 'pozadia na mobil :: '.$popis_res[$res].' :: '.$categories[$cat]; ?></h1></div>
	  <div id="pozadia_01">
	  <div id="pozadia_info_div">http://4mobile.sk/pozadie/<i>ID pozadia</i>/<br/><br/>Dôležité je aj posledné / !!!</div>
	  	
	  	
	    <?php 
	    
	    	if(fnIsAdmin())
	    	{
	    		echo "<strong>Zoraď podľa:</strong>&nbsp;&nbsp; ";
	    		if($_SESSION['zorad'] == 0)
	    		{
	    			echo "<span class='red'>Pridania</span>";
	    		}	
	    		else 
	    		{
	    			echo "<a href='index.php?module=pozadia&amp;cat=$cat&amp;res=$res&amp;zorad=0'>Pridania</a>";
	    		}
	    		echo " | ";
	    		if($_SESSION['zorad'] == 1)
	    		{
	    			echo "<span class='red'>Stiahnutí</span>";
	    		}	
	    		else 
	    		{
	    			echo "<a href='index.php?module=pozadia&amp;cat=$cat&amp;res=$res&amp;zorad=1'>Stiahnutí</a>";
	    		}
	    		echo " | ";
	    		if($_SESSION['zorad'] == 2)
	    		{
	    			echo "<span class='red'>Hodnotenia</span>";
	    		}	
	    		else 
	    		{
	    			echo "<a href='index.php?module=pozadia&amp;cat=$cat&amp;res=$res&amp;zorad=2'>Hodnotenia</a>";
	    		}
	    		echo "<br/><br/>";
//	    		Pridania | Stiahnutí | Hodnotenia<br/><br/>";
	    	}
	    
	    	if(!session_is_registered('mobile_logged_in')) echo "<br />Zaregistrovaní používatelia môžu sťahovať všetky pozadia! <br/>";
	    	$result = $dba->GetResult($final_query);
	    	$max = mysql_num_rows($result);
	    	if($max>0)
	    	{
//	    		if(($action == '' || $action == 'moje_pozadia') && session_is_registered('mobile_logged_in')) fnNavi($i, $stranok, "module=pozadia&amp;action=$action&amp;cat=$cat&amp;res=$res");
	    		if(($action == '' || $action == 'moje_pozadia')) fnNavi($i, $stranok, "module=pozadia&amp;action=$action&amp;cat=$cat&amp;res=$res");
//		    	$result = $dba->GetResult($final_query);
		    	fnVypisPozadia($result);
//		    	if(($action == '' || $action == 'moje_pozadia') && session_is_registered('mobile_logged_in')) fnNavi($i, $stranok, "module=pozadia&amp;action=$action&amp;cat=$cat&amp;res=$res");
		    	if(($action == '' || $action == 'moje_pozadia')) fnNavi($i, $stranok, "module=pozadia&amp;action=$action&amp;cat=$cat&amp;res=$res");
	    	}
	    	elseif($action != 'moje_pozadia') 
	    	{
	    		echo "V tejto kategórii nie sú pozadia s rozlíšením ".$resolution[$res]['width']."x".$resolution[$res]['height']."px pre Váš mobil, vyskúšajte inú kategóriu alebo rozlíšenie.";
	    	}
	    	else 
	    	{
	    		echo "Ešte ste nepridali žiadne pozadie.";
	    	}
	    	?>
	  </div>
	</div>
	
<?php
//
//	if(in_array($_SESSION['my_id'], $admins))
//	{
//		if($_SESSION['pass_file'])
//		{
//			echo "<img src='file.php?id=2'/>";
//		}
//		else echo 'false';
//	}
	
//	dump(round(fnExec_time() - $start_time, 5));
?>
