<?

/***************************************************************************
*                            Dolphin Smart Community Builder
*                              -----------------
*     begin                : Mon Mar 23 2006
*     copyright            : (C) 2006 BoonEx Group
*     website              : http://www.boonex.com/
* This file is part of Dolphin - Smart Community Builder
*
* Dolphin is free software. This work is licensed under a Creative Commons Attribution 3.0 License. 
* http://creativecommons.org/licenses/by/3.0/
*
* Dolphin is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the Creative Commons Attribution 3.0 License for more details. 
* You should have received a copy of the Creative Commons Attribution 3.0 License along with Dolphin, 
* see license.txt file; if not, write to marketing@boonex.com
***************************************************************************/


require_once( 'inc/header.inc.php' );
require_once( BX_DIRECTORY_PATH_INC . 'design.inc.php' );
require_once( BX_DIRECTORY_PATH_INC . 'profiles.inc.php' );

$_page['name_index'] = 82;
$_page['css_name'] = 'browse.css';

check_logged();

if ( isset($_GET['iUser']) )
{
	$iID = (int)$_GET['iUser'];
	$_page['header'] = getNickName( $iID)."'s "._t("_Friends");
	$_page['header_text'] = getNickName( $iID)."'s "._t("_Friends");
}

$_ni = $_page['name_index'];
$_page_cont[$_ni]['page_main_code'] = PageCompPageMainCode();

PageCode();

function PageCompPageMainCode()
{
	global $iID;
	
	$sFriendList = getFriendList( $iID );
	
	return $sFriendList;
}

function getFriendList( $id )
{
	global $site;
	global $max_thumb_width;
	global $max_thumb_height;

	$iID = (int)$id;

	////////////////////////////
	$iTotalNum = getFriendNumber($iID);
	if( !$iTotalNum ) {
		return MsgBox(_t( '_Sorry, nothing found' ));
	}

	$iPerPage = (int)$_GET['per_page'];
	if( !$iPerPage )
		$iPerPage = 28;

	$iTotalPages = ceil( $iTotalNum / $iPerPage );

	$iCurPage = (int)$_GET['page'];

	if( $iCurPage > $iTotalPages )
		$iCurPage = $iTotalPages;

	if( $iCurPage < 1 )
		$iCurPage = 1;

	$sLimitFrom = ( $iCurPage - 1 ) * $iPerPage;
	$sqlLimit = "LIMIT {$sLimitFrom}, {$iPerPage}";
	////////////////////////////

	$friend_list_query = "
		SELECT p.*
		FROM `Profiles` AS p
		LEFT JOIN `FriendList` AS f1 ON (f1.`ID` = p.`ID` AND f1.`Profile` ='{$iID}' AND `f1`.`Check` = 1)
		LEFT JOIN `FriendList` AS f2 ON (f2.`Profile` = p.`ID` AND f2.`ID` ='{$iID}' AND `f2`.`Check` = 1)
		WHERE 1
		AND (f1.`ID` IS NOT NULL OR f2.`ID` IS NOT NULL)
		ORDER BY p.`Picture` DESC
		{$sqlLimit}
	";

	$friend_list_res = db_res("$friend_list_query");
	
	$iCounter = 0;
	while ( $friend_list_arr = mysql_fetch_assoc( $friend_list_res ) )
	{
		$iCounter ++;
		$sKey = '1';
		if( $iCounter == 3 )
		{
			$sKey = '1';
		}

		$ret .= '<div class="friends_thumb_'.$sKey.'">' . get_member_thumbnail($friend_list_arr['ID'], 'none', true) . '<div class="browse_nick"><a href="' . getProfileLink($friend_list_arr['ID']) . '">' . $friend_list_arr['NickName'] . '</a></div><div class="clear_both"></div></div>';
		
		if( $iCounter == 3)
			$iCounter = 0;
	}

	if( $iTotalPages > 1)
	{
		$sRequest = $_SERVER['PHP_SELF'] . '?';
		$aFields = array('iUser','action');
		
		foreach( $aFields as $field )
			if( isset( $_GET[$field] ) )
				$sRequest .= "&{$field}=" . htmlentities( process_pass_data( $_GET[$field] ) );
		
		$pagination = '<div style="text-align: center; position: relative;">'._t("_Results per page").':
				<select name="per_page" onchange="window.location=\'' . $sRequest . '&per_page=\' + this.value;">
					<option value="10"' . ( $iPerPage == 10 ? ' selected="selected"' : '' ) . '>10</option>
					<option value="20"' . ( $iPerPage == 20 ? ' selected="selected"' : '' ) . '>20</option>
					<option value="50"' . ( $iPerPage == 50 ? ' selected="selected"' : '' ) . '>50</option>
					<option value="100"' . ( $iPerPage == 100 ? ' selected="selected"' : '' ) . '>100</option>
				</select></div>' .
			genPagination( $iTotalPages, $iCurPage, ( $sRequest . '&page={page}&per_page='.$iPerPage ) );
	}
	else
		$pagination = '';

	return $ret . $pagination;
}
