<?php

	include "library.php";

	$cxn = start_code();

	$cxn_common = db_common_login();


	$start_pos = val( $_GET, "startpos" );

	$id = val( $_SESSION, "id" );


//	if ($id == "")
//	{
//		$_SESSION['nextpage'] = url_encode( "members_online_now.php" );
//
//	    jump_to_page( val( $_SESSION, "login_page" ) );		/* must happen before print output */
//
//		exit();
//	}


	if ($id != "")
		$query = "SELECT id, handle, short_intro, sex, sex_wanted, city, country, dayofyear(profile.date_of_birth) as dob_dayofyear, year(profile.date_of_birth) as dob_year FROM profile WHERE visibility = 'all_members' AND deleted = 'no' AND locked  = 'no' AND system_user = 'no' and profile.id in (SELECT member_id FROM online_status WHERE member_id <> ".$id.") ORDER BY last_login DESC";
	else
		$query = "SELECT id, handle, short_intro, sex, sex_wanted, city, country, dayofyear(profile.date_of_birth) as dob_dayofyear, year(profile.date_of_birth) as dob_year FROM profile WHERE visibility = 'all_members' AND deleted = 'no' AND locked  = 'no' AND system_user = 'no' and profile.id in (SELECT member_id FROM online_status) ORDER BY last_login DESC";
	
	
	$heading = "Members Online Now";		
	

	$result = run_query( $query, $cxn );


	page_header( $heading, $cxn, $cxn_common );

	
	if (query_num_rows( $result ) == 0)
	{
		if ($id != "")
			echo "<br>There are no other members online at the moment.<br>";
		else
			echo "<br>There are no members online at the moment.<br>";
			
		exit();
	}
//	else
//		echo "<hr>";

		
	for ($i=0; $i < query_num_rows( $result ); $i++)
	{
		$data = get_row( $result );
			
		$age = calc_person_age( $data['dob_dayofyear'], $data['dob_year'] );			
			
			
		echo "<a name='id".$data['id']."'>";		/* bookmark to go back to search screen */


		echo "<table width='100%'><tr>";

		echo "<td width='7%'>";

		echo show_handle( $data['id'], $data['handle'], "search", $start_pos );

//		if ($data[$i]['logged_in'] != "")
//			echo "<br><br>".watermark( "online now" );

		echo "</td><td width='16%' align='center'>";

		show_photo( $data['id'], 100, -1, true, false, $cxn );

		echo "</td>";

		if (! low_res_screen())
		{
    		echo "<td width='33%'>".show_introduction( $data['id'], convert_to_html( $data['short_intro'] ), "main", "0" )."</td>";

			echo "<td width='1%'></td>";
		
			echo "<td width='5%'>".black( "Age ".$age )."</td>";		
		
			echo "<td width='1%'></td>";		
		
			echo "<td width='5%'>".black( $data['sex'] )."</td>";				

			echo "<td width='1%'></td>";		
		
			echo "<td width='12%'>".black( "Looking for ".$data['sex_wanted'] )."</td>";				

			echo "<td width='1%'></td>";		
				
			echo "<td width='8%'>".black( name_case( $data['city'] ) )."</td>";

			echo "<td width='16%'>".black( $data['country'] )."</td>";

			echo "<td width='2%'></td>";	
		}
		else
		{
			echo "<td width='10%'>".black( name_case( $data['city'] )."<br><br>".name_case( $data['country'] ) )."</td>";		
		}
		
		echo "</tr></table>";
		
		if (low_res_screen())
		{
    		echo "<table width='100%'><tr><td width='50%'>".show_introduction( $data['id'], convert_to_html( $data['short_intro'] ), "main", "0" )."</td>";

			echo "<td width='1%'></td>";
		
			echo "<td width='5%'>".black( "Age ".$age )."</td>";		
		
			echo "<td width='1%'></td>";		
		
			echo "<td width='5%'>".black( $data['sex'] )."</td>";				

			echo "<td width='1%'></td>";		
		
			echo "<td width='12%'>".black( "Looking for ".$data['sex_wanted'] )."</td>";				

			echo "<td width='1%'></td>";		
		
			
			echo "</tr></table>";
		}
		
				
		echo "<hr>";
	}


	
	if (! low_res_screen())
	{
		echo "<br><br><br><br><br><br>";
		
		show_double_ads( $cxn );
		
		    // space to prevent the ads being squashed against the bottom of the page
		echo "<br><br><br><br><br><br>";		
	}
	else
	{
		echo "<br><br><br><br>";
		
		show_ads_large_square();	

		    // space to prevent the ads being squashed against the bottom of the page
//		show_ads_narrow_banner();
		
		echo "<br><br><br><br><br><br>";		
	}

	function show_introduction( $member_id, $text, $returnpoint, $startpos )
	{
		if ($startpos == "")
			$startpos = "0";		// if missing default to start of list

		$url = url_encode( "display_profile.php", "showid", $member_id, "returnpoint", $returnpoint, "startpos", $startpos );

		return ("<a href='".$url."' style='text-decoration: none;'><span class='gblack'>".$text."</span></a>");
	}
		
?>