<?php
define('USERNAME_REQUIRED', TRUE);
define('ACCOUNT_REQUIRED', TRUE);
include("../global.php");
$username = $core->EscapeString($_SESSION['username']);
if(!$users->UserPermission('hk_rooms', $username))
{
header("Location: nopermission.php");
die;
}
?>

<script language="javascript">
	$('.tooltip').tooltip({ 
	    track: true, 
	    delay: 0, 
	    showURL: false, 
	    showBody: " - ", 
	    fade: 250 
	});
	
	$('.modify').click(function() {
		LoadContent('page_modify_room', $(this).attr('id'));
	    $('.overlay').fadeIn();
	    $('.page').fadeIn("slow");
	});

	$('.delete').click(function() { 
		DeleteStory($(this).attr('id'));
	});

	$('.exitbutton').click(function() { 
	    $('.page').css('display', 'none');
	    $('.overlay').css('display', 'none');
	});

	function LoadContent(PageName, ID) {
		$.ajax({
		   type: "POST",
		   url: "functions/" + PageName + ".php?id=" + ID,
		   success: function(msg){
		     $('.content').html(msg);
		   }
		 });
	}
	
		function Search() {
		    $('.page').css('display', 'none');
		    $('.overlay').css('display', 'none');
			$.ajax({
			   type: "POST",
			   url: "rooms.php",
			   data: "roomtype=private&method=name&value=" + $('#room').val(),
			   success: function(msg){
			     $('.conColumn').html(msg);
			   }
			 });
		}
		
		function Search2(roomtype) {
		    $('.page').css('display', 'none');
		    $('.overlay').css('display', 'none');
			$.ajax({
			   type: "POST",
			   url: "rooms.php",
			   data: "roomtype="+roomtype,
			   success: function(msg){
			     $('.conColumn').html(msg);
			   }
			 });
		}
		
		function Search3(username) {
		    $('.page').css('display', 'none');
		    $('.overlay').css('display', 'none');
			$.ajax({
			   type: "POST",
			   url: "users.php",
			   data: "method=username&value="+username ,
			   success: function(msg){
			     $('.conColumn').html(msg);
			     $('li#' + OldPage).removeClass('selected');
			     $('li#users').addClass('selected');
			     OldPage = 'users';
			   }
			 });
		}
		
</script>

<div>
	<h1>Rooms</h1>
	<div class="overlay hidden">
		<div class="page hidden">
			<div class="exitbutton"></div>
			<div class="content">
			</div>
		</div>
	</div>
	
	<div class="formPiece">
		<div>
			<h3>Find Room</h3>
		</div>

		<div>
        	<img src="img/info_16.png" class="tooltip" title="Search room" />
			<label for="room">Search Room: </label><input type="text" value="" name="room" id="room" /><br/>
            <button id="General" onClick="Search();">Search</button>
		</div>
	</div>
    
    <div class="formPiece">
    <?php
	if(@$_POST['roomtype'] == 'public')
	{
	?>
    <button id="General" class="left" onClick="Search2('private');">Show Private Rooms</button>
    <?php
    }
	else
	{?>
	<button id="General" class="left" onClick="Search2('public');">Show Public Rooms</button>
    <?php
	}
	?>
    </div>

	<div class="formPiece">
		<h3>Search Result</h3>

		<div>
			<?php
			switch(@$_POST['roomtype'])
			{
				case 'private':
				switch(@$_POST['method'])
				{
					case 'userid':
					$rooms = mysql_query("SELECT * FROM rooms WHERE owner = '".$core->EscapeString($_POST['value'])."'");
					break;
					case 'name':
					$rooms = mysql_query("SELECT * FROM rooms WHERE caption LIKE '".$core->EscapeString($_POST['value'])."'");
					break;
					default:
					$rooms = mysql_query("SELECT * FROM rooms WHERE roomtype = 'private' ORDER BY users_now DESC LIMIT 20");
					break;
				}
				break;
				case 'public':
				$rooms = mysql_query("SELECT * FROM rooms WHERE roomtype = 'public' ORDER BY users_now DESC");
				break;
				case 'id':
				$rooms = mysql_query("SELECT * FROM rooms WHERE id = '".$core->EscapeString($_POST['value'])."'");
				break;
				default:
				$rooms = mysql_query("SELECT * FROM rooms WHERE roomtype = 'private' ORDER BY users_now DESC LIMIT 20");
				break;
			}
			
			if(mysql_num_rows($rooms) == 0)
			{
				echo '<h3>No Data Available</h3>';
			}
			
			while($room = mysql_fetch_array($rooms))
			{
			?>
				<div class="RoomSearchResults" id="<?php echo $room['id']; ?>">
                	<div class="RoomSearchResultsIcon" style="background-image:url(img/rooms/<?php if($room['roomtype'] == 'private')echo $room['model_name']; else echo 'public'; ?>.gif)"></div>
					<p>Room name: <strong><?php echo $room['caption']; ?></strong><br />
                    Room type: <strong><?php echo $room['roomtype']; ?></strong><br />
                    Owner: <strong onClick="Search3('<?php echo $room['owner']; ?>');"><?php echo $room['owner']; ?></strong><br />
                    Password: <strong><?php echo $room['password']; ?></strong><br />
                    State: <strong><?php echo $room['state']; ?></strong><br />
                    Users now: <strong><?php echo $room['users_now']; ?></strong><br />
                    Users max: <strong><?php echo $room['users_max']; ?></strong><br />
                    </p>
                    <?php if($users->UserPermission('hk_edit', $username))
					{ ?><button id="<?php echo $room['id']; ?>" class="modify">Edit Room</button><?php } ?>
				</div>
            <?php
			}
			?>
			
		</div>
	</div>
</div>