<?php
include("includes/site_info.php");
include('session_handler.inc');

$WUserID = $_SESSION['Sess_UserId'];
$WUsername = $_SESSION['Sess_UserName'];

if ($_REQUEST['pid']) { $pid = $_REQUEST['pid']; }
else { $pid = 0; }

$photoitem_query=mysql_query("SELECT *, DATE_FORMAT(pic_date, '%M %d, %Y') as pic_date, DATE_FORMAT(mod_date, '%M %d, %Y') as mod_date FROM pictures WHERE pic_userid = '$WUserID' and pic_id = '$pid' order by pic_default asc, pic_id desc") or die(mysql_error());
$photoitem_TOTAL = mysql_num_rows($photoitem_query);
$photo_TOTAL = mysql_num_rows(mysql_query("SELECT * FROM pictures WHERE pic_userid = '$WUserID'"));

?>
<div id="top_panel" class="grey_row_back"><?php include ("panel_top.php"); ?></div>
<?php 
	foreach($_GET as $field=>$data) {
	if ($field=="done") {
		echo'<div id="msg_content_done">Wow, Your request was done!</div>';
	}
	elseif ($field=="error") {
		echo'<div id="msg_content_error">Ops! Database Error, please try again!</div>';
	}
}
?>
			<table width="100%" border="0" cellpadding="0" cellspacing="0">
  <tr>
    <td valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="5" class="white_back_t_xb">
            <tr>
              <td><div class="light_orang_h">
                  <div style="background-color:#FF0000; width:10px; height:10px; float:left; margin-top:3px; margin-right:5px;"><img src="images/spacer.gif" width="10" height="10" /></div>
                Profile Picture </div>
                  <div class="dot_line"><img src="images/spacer.gif" width="1" height="1" /></div>
                <div class="solid_line"><img src="images/spacer.gif" width="1" height="1" /></div></td>
            </tr>
<?php if ($photoitem_TOTAL > 0){ 
		while ($photo = mysql_fetch_object($photoitem_query) ) {
		//$prev_id = mysql_fetch_object(mysql_query("SELECT * FROM pictures WHERE pic_userid = '$WUserID' and pic_id < '$photo->pic_id' order by pic_default asc, pic_id desc LIMIT 1"));
		//$next_id = mysql_fetch_object(mysql_query("SELECT * FROM pictures WHERE pic_userid = '$WUserID' and pic_id > '$photo->pic_id' order by pic_default asc, pic_id desc LIMIT 1"));
		
		$all_string = mysql_query("SELECT group_concat(pic_id order by pic_default asc, pic_id desc) as newid FROM pictures WHERE pic_userid = '$WUserID'") or die(mysql_error());
		$all_string_TOTAL = mysql_num_rows($all_string);
		if ($all_string_TOTAL>0) {
			$find_string = mysql_result($all_string,0);
			$pg = mysql_result(mysql_query("SELECT FIND_IN_SET('$pid','$find_string')"),0);
			$next_id = mysql_result(mysql_query("SELECT ELT($pg+1, $find_string)"),0);
			$prev_id = mysql_result(mysql_query("SELECT ELT(1, $find_string)"),0);
		} else {
			$find_string=0;
			$pg = 0;
		}

?>
            <tr class="lightgrey_10">
              <td align="center">Photo <?=$pg?> of <?=$photo_TOTAL?> | <a href="#cphoto?tab=media" onclick="javascript:callpage(this.href);" rel="subpage_link">Back to Photo</a> | <a href="#ceditpropic?tab=media&pid=<?=$pid?>" onclick="javascript:callpage(this.href);" rel="subpage_link">Edit</a></td>
            </tr>
            <tr class="solid_line_separator">
              <td>
          <div align="center" class="padding_10 grey_10 style_left">
                <div>
				<?php if ($next_id) { 
				$next_query=mysql_query("SELECT pic_picture, ext FROM pictures WHERE pic_userid = '$WUserID' and pic_id = '$next_id'") or die(mysql_error());
				$next_TOTAL = mysql_num_rows($next_query);
				if ($next_TOTAL >0) {
				$next_photo = mysql_fetch_object($next_query); ?>
				<img src="<?=$SITE_MAIN_WWW_ROOT?>/members/<?=$WUsername?>/picture/<?=$next_photo->pic_picture?>.<?=$next_photo->ext?>" width="0" border="0">
				<?php
				}
				?>
				<a href="#cpropic?tab=media&pid=<?=$next_id?>" onclick="javascript: callpage(this.href);" rel="subpage_link"><img src="<?=$SITE_MAIN_WWW_ROOT?>/members/<?=$WUsername?>/picture/<?=$photo->pic_picture?>.<?=$photo->ext?>" class="img_b_p3" border="0"></a>
				<?php } elseif ($prev_id) { 
				$prev_query=mysql_query("SELECT pic_picture, ext FROM pictures WHERE pic_userid = '$WUserID' and pic_id = '$prev_id'") or die(mysql_error());
				$prev_TOTAL = mysql_num_rows($prev_query);
				if ($prev_TOTAL >0) {
				$prev_photo = mysql_fetch_object($prev_query); ?>
				<img src="<?=$SITE_MAIN_WWW_ROOT?>/members/<?=$WUsername?>/picture/<?=$prev_photo->pic_picture?>.<?=$prev_photo->ext?>" width="0" border="0">
				<?php
				}
				?>
				<a href="#cpropic?tab=media&pid=<?=$prev_id?>" onclick="javascript: callpage(this.href);" rel="subpage_link"><img src="<?=$SITE_MAIN_WWW_ROOT?>/members/<?=$WUsername?>/picture/<?=$photo->pic_picture?>.<?=$photo->ext?>" class="img_b_p3" border="0"></a>
				<?php } else { 
				?>
				<img src="<?=$SITE_MAIN_WWW_ROOT?>/members/<?=$WUsername?>/picture/<?=$photo->pic_picture?>.<?=$photo->ext?>" class="img_b_p3" border="0">
				<?php } ?>
				<br />
			  <div class="left padding_lr_5" align="left"> <span class="lightgrey_10">
                    <?php if($photo->pic_private=='Y') echo "Private"; elseif($photo->pic_private=='N') echo"Public";?>
                    <br />
                    </span>						  			  <?php 
			  if ($photo->description) { 
			  echo "<span class=\"line_height20\">";
			  echo nl2br($photo->description);
			  echo "<br />";
			  echo "</span>";
              } 
			  ?>

                    <span class="lightgrey_10"> Updated
                    <?=$photo->mod_date?>
                    <br />
                    Created
                    <?=$photo->pic_date?>
                    </span> </div>
			</div>
            </div>
             </td>
                </div>
            </tr>
		  <?php }
		  } else {
?>
            <tr class="lightgrey_10">
              <td align="center"><a title="Add Photo" href="#caddpphoto?tab=media" onclick="javascript: callpage(this.href);">Add Photo</a> | <a href="#cmedia" onclick="javascript: callpage(this.href);">Back to Media</a></td>
            </tr>
        <tr class="solid_line_separator">
          <td align="center"><div class="padding_10">Can't find a photo to view.</div></td>
        </tr>
<?php
	die;
}
?>
            <tr>
              <td>&nbsp;</td>
            </tr>
          </table></td>
  </tr>
</table>
