<?php

require_once('../core.php');
require_once('../includes/session.php');

$key = FilterText($_GET['key']);

switch($key){
	case "friends_all": $mode = 1; break;
	case "groups": $mode = 2; break;
	case "rooms": $mode = 3; break;
}

if(!isset($mode) || !isset($key)){ $mode = 1; }




switch($mode){
	case 1:
		$str = "friends";
		$get_em = mysql_query("SELECT * FROM messenger_friendships WHERE user_one_id = '".$my_id."'") or die(mysql_error());
	//$get_em2 = mysql_query("SELECT * FROM messenger_friendships WHERE user_one_id  = '".$my_id."' OR user_two_id  = '".$my_id."'") or die(mysql_error());
		break;
	case 2:
		$str = "groups";
		$get_em = mysql_query("SELECT * FROM group_memberships WHERE userid = '".$my_id."'") or die(mysql_error());
		break;
	case 3:
		$str = "rooms";
		$get_em = mysql_query("SELECT * FROM rooms WHERE owner = '".$name."' ORDER BY caption ASC LIMIT 50") or die(mysql_error());
		break;
}

$results = mysql_num_rows($get_em);
$oddeven = 0;

	if($results > 0){
		if($mode == 1){
		echo "<ul id=\"offline-friends\">\n";
			while ($row = mysql_fetch_assoc($get_em)){
			if($row['user_one_id'] == $my_id){
				$userdatasql = mysql_query("SELECT username FROM users WHERE id = '".$row['user_two_id']."' LIMIT 1") or die(mysql_error());
			} else {
				$userdatasql = mysql_query("SELECT username FROM users WHERE id = '".$row['user_one_id']."' LIMIT 1") or die(mysql_error());
			}
			$user_exists = mysql_num_rows($userdatasql);
				if($user_exists > 0){
					$userrow = mysql_fetch_assoc($userdatasql);
					$oddeven++;
					if(IsEven($oddeven)){ $even = "odd"; } else { $even = "even"; }
					printf("        <li class=\"%s\"><a href=\"user_profile.php?name=%s\">%s</a></li>\n",$even,$userrow['username'],$userrow['username']);
				}
			}
		echo "\n</ul>";
		} elseif($mode == 2){
		echo "<ul id=\"quickmenu-groups\">\n";

		$num = 0;

			while($row = mysql_fetch_assoc($get_em)){

				$num++;

				$group_id = $row['groupid'];

				$check = mysql_query("SELECT id,name,ownerid FROM groups WHERE id = '".$group_id."' LIMIT 1") or die(mysql_error());
				$groupdata = mysql_fetch_assoc($check);

				echo "<li class=\"";
				if(IsEven($num)){ echo "odd"; } else { echo "even"; }
				echo "\">";
				$fav = mysql_fetch_array(mysql_query("SELECT groupid FROM user_stats WHERE id = ".$my_id));
				if($groupdata['id'] == $fav[0]){ echo "<div class=\"favourite-group\" title=\"Favorito\"></div>\n"; }
				if($row['member_rank'] > 1 && $groupdata['ownerid'] !== $my_id){ echo "<div class=\"admin-group\" title=\"Admin\"></div>\n"; }
				if($groupdata['ownerid'] == $my_id && $row['member_rank'] > 1){ echo "<div class=\"owned-group\" title=\"Proprietario\"></div>\n"; }

				echo "\n<a href=\"group_profile.php?id=".$group_id."\">".HoloText($groupdata['name'])."</a>\n</li>";
			}

		echo "\n</ul>";
		} elseif($mode == 3){
		echo "<ul id=\"quickmenu-rooms\">\n";
			while ($row = mysql_fetch_assoc($get_em)){
			$oddeven++;
			if(IsEven($oddeven)){ $even = "odd"; } else { $even = "even"; }
			printf("        <li class=\"%s\"><a href=\"client.php?forwardId=2&amp;roomId=%s\" onclick=\"roomForward(this, '%s', 'private'); return false;\" target=\"client\" id=\"room-navigation-link_%s\">%s</a></li>\n",$even,$row['id'],$row['id'],$row['id'],$row['caption']);
			}
		echo "\n</ul>";
		} else {
		echo "Invalito";
		}
	} else {
		echo "<ul id=\"quickmenu-" . $str . "\">\n	<li class=\"odd\">Nessun dato trovato </li>\n</ul>";
	}

	if($mode == "3"){
	echo "<p class=\"create-room\"><a href=\"client.php?shortcut=roomomatic\" onclick=\"HabboClient.openShortcut(this, 'roomomatic'); return false;\" target=\"client\">Crea Una Nuova Stanza</a></p>";
	} elseif($mode == "2"){
	echo "<p class=\"create-group\"><a href=\"#\" onclick=\"GroupPurchase.open(); return false;\">Crea un Gruppo</a></p>";
	}

?>