<?php
include("includes/site_info.php");
include_once __INCLUDE_CLASS_PATH."/class.Picture.php";
$picture = new Picture();

$WUserID = $_SESSION['Sess_UserId'];
$WUserType = $_SESSION['Sess_UserType'];
$WUserName = $_SESSION['Sess_UserName'];

if(!empty($_REQUEST['RUserID'])){
$RUserID = $_REQUEST['RUserID'];
} else {
$RUserID = 0;
}

if(!empty($_REQUEST['oView'])){
$oView = $_REQUEST['oView'];
$pageTotal = 21;
} else {
$oView = "public";
$pageTotal = 6;
}

		$i = 2;
		$j = 0;

	if(isset($_REQUEST['start'])) {
		$start=($_REQUEST['start']-1)*$pageTotal; 
		$i = $_REQUEST['start']+1;
		$j = $_REQUEST['start']-1;
	} else {	
		$start=0;
	}
	
if($oView=="contact") { 
$page = 0;

$myhahaper_query=mysql_query("SELECT mem_userid, mem_profileid, mem_username, mem_fullname FROM my_friends LEFT JOIN members ON (friend_uid=mem_userid) WHERE uid='$RUserID' and approved='1' order by mem_username",$link) or die(mysql_error());
$myhahaper_TOTAL = mysql_num_rows($myhahaper_query);
} else {
$all_hahaper_query=mysql_query("SELECT mem_userid, mem_profileid, mem_username, mem_fullname FROM my_friends LEFT JOIN members ON (friend_uid=mem_userid) WHERE uid='$RUserID' and approved='1' order by mem_username",$link) or die(mysql_error());
$all_hahaper_TOTAL = mysql_num_rows($all_hahaper_query);
$page = ceil($all_hahaper_TOTAL/$pageTotal);

$myhahaper_query=mysql_query("SELECT mem_userid, mem_profileid, mem_username, mem_fullname FROM my_friends LEFT JOIN members ON (friend_uid=mem_userid) WHERE uid='$RUserID' and approved='1' order by mem_username limit $start, $pageTotal",$link) or die(mysql_error());
$myhahaper_TOTAL = mysql_num_rows($myhahaper_query);
}

if ($myhahaper_TOTAL >0) {
?>
<?php if($oView=="hahaper") {?>

<div class="pop_box width_600">
<?php } elseif($oView=="contact") { ?>
<div class="pop_box p5">
<div class="grey_12 orange_row_back solid_line_separator p5"><div style="background-color:#0000FF; width:10px; height:10px; float:left; margin-top:3px; margin-right:5px;"><img src="images/spacer.gif" width="10" height="10" /></div> Contacts</div>
<?php } else { ?>
<div class="pop_box">
<?php } ?>

<?php if (($page >= $i) || ($j>0)) { ?>
<div class="pagingBox">
<?php 
  if ($j>0) {
  	if (($page >= $i) && ($i!=2)) {
	echo "<a href=\"javascript: void(null)\" title=\"All Connections\" onclick=\"jQuery.facebox({ajax:'".$CONST_LINK_ROOT."/connect_all.php?RUserID=".$RUserID."&start=".$j."&mode=previous&oView=".$oView."'}, this.title, 'connectAll'); return false;\" rel=\"facebox\" ><div class=\"paging left\" align=\"center\">PREVIOUS</div></a>";
	}
	if ($page < $i) {
	echo "<a href=\"javascript: void(null)\" title=\"All Connections\" onclick=\"jQuery.facebox({ajax:'".$CONST_LINK_ROOT."/connect_all.php?RUserID=".$RUserID."&start=".$j."&mode=previous&oView=".$oView."'}, this.title, 'connectAll'); return false;\" rel=\"facebox\" ><div class=\"paging left\" align=\"center\">PREVIOUS</div></a>";
	}
	}

	if (($page >= $i) && ($i!=2)) {
	echo "<a href=\"javascript: void(null)\" title=\"All Connections\" onclick=\"jQuery.facebox({ajax:'".$CONST_LINK_ROOT."/connect_all.php?RUserID=".$RUserID."&start=".$i."&mode=next&oView=".$oView."'}, this.title, 'connectAll'); return false;\" rel=\"facebox\" ><div class=\"paging right\" align=\"center\">NEXT</div></a>";
	}
	if (($page >= $i) && ($i==2)) {
	echo "<a href=\"javascript: void(null)\" title=\"All Connections\" onclick=\"jQuery.facebox({ajax:'".$CONST_LINK_ROOT."/connect_all.php?RUserID=".$RUserID."&start=".$i."&mode=next&oView=".$oView."'}, this.title, 'connectAll'); return false;\" rel=\"facebox\" ><div class=\"paging right\" align=\"center\">NEXT</div></a>";
	}
 ?>
</div>
<div class="clear"><img src="images/spacer.gif" /></div>
<?php
} 

$k = 0;
while ($ret_pic2 = mysql_fetch_object($myhahaper_query)) {
$k++;
if ($WUserID==$ret_pic2->mem_userid) {
$photo_query=mysql_query("SELECT * FROM pictures WHERE pic_userid = '$ret_pic2->mem_userid' and pic_default='Y'") or die(mysql_error()); 
} else {
$photo_query=mysql_query("SELECT * FROM pictures WHERE pic_userid = '$ret_pic2->mem_userid' and pic_default='Y' and pic_private='N'") or die(mysql_error()); 
}
$pics_count = mysql_num_rows($photo_query);
$aPicture = mysql_fetch_object($photo_query);
if ($pics_count > 0) {
	$medium = $aPicture->pic_picture."_ps.".$aPicture->ext;
	$male1_picture1 = "members/$ret_pic2->mem_username/picture/$medium";
             }else{
$male1_picture1=$picture->GetProfilePicture($ret_pic2->mem_userid);
}

if ($ret_pic2->mem_profileid != null) { $male1_name1= $ret_pic2->mem_profileid; }
else { $male1_name1= $ret_pic2->mem_username; }

$fullname = $ret_pic2->mem_username;

$time=time();
$timeout = time()-900;
$query="SELECT COUNT(userid) FROM active_users where timestamp > $timeout AND userid=$ret_pic2->mem_userid";
$reton=mysql_query($query,$link) or die(mysql_error());
$totalon=mysql_result($reton,0,0);

if($totalon>0)
{$status="<span style=\"color:green\">Online!</span>";}
else{$status="Offline!";}

	if($ret_pic2->mem_userid==$WUserID) {
	$request=0;
	} else {
	$retval=mysql_query("SELECT * FROM my_friends WHERE (uid='$WUserID' AND friend_uid='$ret_pic2->mem_userid') or (uid='$ret_pic2->mem_userid' AND friend_uid='$WUserID')",$link);
	$sql_array = mysql_fetch_object($retval);
	$result=mysql_num_rows($retval);

	if ($result == 0) {
	$request=1;
	} elseif ($sql_array->approved == 0) {
	$request=2;
	$retval=mysql_query("SELECT * FROM my_friends WHERE uid='$ret_pic2->mem_userid' AND friend_uid='$WUserID'",$link);
	$sql_array = mysql_fetch_object($retval);
	$result=mysql_num_rows($retval);
	if ($result == 0) {
	$request=2;
	} elseif ($sql_array->approved == 0) {
	$request=4;
	}else {
	$request=2;
	}
	} else {
	$request=0;
	}
	}

?>
<?php if($oView=="hahaper") {?>
<div class="left m10">
<?php } elseif($oView=="contact") {?>
<div class="left m8">
	<?php } else { ?>
	<div class="content">
		<?php } ?>
		<div class="image"><a href="<?=$CONST_LINK_ROOT?>/<?=$male1_name1?>"><img src="<?=$CONST_LINK_ROOT?>/<?=$male1_picture1?>" width="50" title="Name:&nbsp;<?=ucwords($fullname);?>"/></a></div>
		<?php if($oView=="hahaper" || $oView=="contact") {?>
		<div class="Xfloatname"> <a href="<?=$CONST_LINK_ROOT?>/<?=$male1_name1?>"><span class="blue_11_b">
			<?=cutword($fullname);?>
			</span></a><br />
			<?=$status?>
		</div>
		<?php } else { ?>
		<div class="name"> <a href="<?=$CONST_LINK_ROOT?>/<?=$male1_name1?>"><span class="blue_12">
			<?=cutword($fullname);?>
			</span> </a><br />
			<?=$status?>
		</div>
		<div class="rightContent">
			<?php if ($request==1 || $request==3) { ?>
			<span id="send_request" class=""> <a href="javascript:void(null);" title="Request to Connect" onclick="javascript: jQuery.facebox({ajax:'<?=$CONST_LINK_ROOT?>/label-me.php?RUserID=<?=$ret_pic2->mem_userid?>&amp;RUserName=<?=$ret_pic2->mem_username?>&amp;mode=request&amp;backmode=connectAll&amp;oView=<?=$oView?>&amp;FUserID=<?=$RUserID?>'}, this.title, 'popLabel'); return false;" class="a_button" rel="facebox"><img src="images/icon-label.gif" width="18" height="15" border="0" align="absbottom"/> Request to Connect</a></span> <span id="confirm_request" class="none"> <img src="images/icon-label.gif" width="18" height="15" border="0" align="absbottom"/> Pending Confirmation</span> <span id="accept_request" class="none"> <a href="javascript:void(null);" title="Respond to Request" onclick="javascript: jQuery.facebox({ajax:'<?=$CONST_LINK_ROOT?>/label-me.php?AUserID=<?=$ret_pic2->mem_userid?>&amp;AUserName=<?=$ret_pic2->mem_username?>&amp;mode=respond&amp;backmode=connectAll&amp;oView=<?=$oView?>&amp;FUserID=<?=$RUserID?>'}, this.title, 'popLabel'); return false;" class="a_button" rel="facebox"><img src="images/icon-label.gif" width="18" height="15" border="0" align="absbottom"/> Accept Request</a></span>
			<?php } ?>
			<?php if ($request==2) { ?>
			<img src="images/icon-label.gif" width="18" height="15" border="0" align="absbottom"/> Pending Confirmation
			<?php } ?>
			<?php if ($request==4) { ?>
			<span id="accept_request" class=""> <a href="javascript:void(null);" title="Respond to Request" onclick="javascript: jQuery.facebox({ajax:'<?=$CONST_LINK_ROOT?>/label-me.php?AUserID=<?=$ret_pic2->mem_userid?>&amp;AUserName=<?=$ret_pic2->mem_username?>&amp;mode=respond&amp;backmode=connectAll&amp;oView=<?=$oView?>&amp;FUserID=<?=$RUserID?>'}, this.title, 'popLabel'); return false;" class="a_button" rel="facebox"><img src="images/icon-label.gif" width="18" height="15" border="0" align="absbottom"/> Respond to Request</a></span> <span id="send_request" class="none"> <a href="javascript:void(null);" title="Request to Connect" onclick="javascript: jQuery.facebox({ajax:'<?=$CONST_LINK_ROOT?>/label-me.php?RUserID=<?=$ret_pic2->mem_userid?>&amp;RUserName=<?=$ret_pic2->mem_username?>&amp;mode=request&amp;backmode=connectAll&amp;oView=<?=$oView?>&amp;FUserID=<?=$RUserID?>'}, this.title, 'popLabel'); return false;" class="a_button" rel="facebox"><img src="images/icon-label.gif" width="18" height="15" border="0" align="absbottom"/> Request to Connect</a></span>
			<?php } ?>
		</div>
		<div class="clear"><img src="images/spacer.gif" /></div>
		<?php } ?>
	</div>
	<?php if($k%7 == 0) echo "<div class=\"clear\"><img src=\"images/spacer.gif\" /></div>";?>
	<?php
	} 
	?>
</div>
<?php
}

function cutword($cutstring) {
	 $_data = explode(" ", ucwords($cutstring));
    foreach($_data as $value) {
        if (strlen($value)>9) {
				echo wordwrap($value, 7, "<br>", true);
        } else {
		  		echo $value;
		  }
    }
}										 

?>
