<?
    $GLOBALS
['highlight'] = 'members';

    if (
$_user)
    {
        
$members mysql_query("SELECT * FROM members WHERE username = '" $_user "'");
        if (
$members_row mysql_fetch_array($members))
        {
            
$_user_id $members_row['id'];
            if (!
$_id)
            {
                
$_id $members_row['main_image_id'];
            }
        }
        else
        {
            echo(
'Sorry, that user does not exist');    
            die;
        }
    }

    
$image mysql_query("SELECT height, width FROM images WHERE id = '" $_id "'");
    if (!
$image mysql_fetch_array($image))
    {
        echo(
'Sorry, that image could not be found.');
        die();
    }
    
    if (!
mysql_num_rows(mysql_query("SELECT user_id FROM gallery_images WHERE image_id = '" $_id.  "'")))
    {
        
$_user_id null;
    }
    
    if (
$_user_id)
    {
        
$username get_username($_user_id);
        
$user_info mysql_fetch_array(mysql_query("SELECT gender FROM members_extras WHERE user_id = '" $_user_id "'"));
        
$user_gender $user_info['gender'] == 'his' 'her';
        
$user_images_prev mysql_query("SELECT image_id AS id FROM gallery_images WHERE user_id = '" $_user_id "' AND image_id > '" $_id "' ORDER BY posted_on LIMIT 1");
        
$user_images_next mysql_query("SELECT image_id AS id FROM gallery_images WHERE user_id = '" $_user_id "' AND image_id < '" $_id "' ORDER BY posted_on DESC LIMIT 1");        
    }
    else
    {
        
$user_images_prev mysql_query("SELECT id FROM images WHERE id > '" $_id "' ORDER BY posted_on LIMIT 1");
        
$user_images_next mysql_query("SELECT id FROM images WHERE id < '" $_id "' ORDER BY posted_on DESC LIMIT 1");        
    }

    include(
'include/parts/header.php');
?>
    
    <script type="text/javascript">
        var user_id = <?= $GLOBALS['auth']['id'] > $GLOBALS['auth']['id'] : 'null' ?>;
    </script>
    
<?
    
if ($user_images_prev mysql_fetch_array($user_images_prev))
    {
        
$user_images_prev $user_images_prev['id'];
    }
    if (
$user_images_next mysql_fetch_array($user_images_next))
    {
        
$user_images_next $user_images_next['id'];
    }

    echo 
'<div class="major_fill">';
    
    if (
$_user_id)
    {
        echo 
'<div class="sub_fill" style="font-size: 12px; font-weight: bold; padding: 4px 10px 4px 10px;">';
        echo 
'<a href="?s=u&u=' $username '">&#171; Back to ' $user_gender ' profile</a>';
        echo 
'</div>';
        echo 
'<span style="font-size: 4px;"><br /></span>';
    }        
    echo 
'<div class="minor_fill" style="text-align: center;">';

    echo 
'<table width="100%" border="0" cellpadding="0" cellspacing="0"><tr><td class="patch_fill_on" rowspan="3" valign="top">';
    
    require_once(
'include/functions/image_overall_type.php');
    require_once(
'include/functions/image_overall_rating.php');

    echo 
'<table width="100%" border="0" cellpadding="0" cellspacing="0" style="font-size: 14px;"><tr>';
    echo 
'<td align="left" style="font-weight: bold; padding-left: 10px;">Image<div style="font-size: 28px;">[<b>' $_id '</b>]</div></td>';
    echo 
'<td width="300" align="right" style="padding-right: 10px;">';
    echo 
'<div style="font-size: 11px;">Overall Rating: </div><b id="v_rating">' image_overall_rating($_id) . '</b><br /><div style="font-size: 11px;">Overall Voted Type: </div><b id="v_type">'.image_overall_type($_id).'</b>';
    echo 
'</td></tr></table>';
    
    echo 
'</td><td width="4" nowrap></td><td width="250" id="voting_area" class="patch_fill_off" style="text-align: center; font-size: 14px;">';
    
    
$vote mysql_query('SELECT vote FROM images_rating WHERE user_id="'.$GLOBALS['auth']['id'].'" AND image_id="'.$_id.'"');
    if (
mysql_num_rows($vote))
    {
        
$vote mysql_fetch_array($vote);
        echo 
'You voted: ';
        if (
$vote['vote'] == 1)
        {
            echo 
'<span class="good" style="font-weight: bold;">Good</span>';
        }
        elseif (
$vote['vote'] == -1)
        {
                echo 
'<span class="error" style="font-weight: bold;">Bad</span>';
        }
    }
    else
    {
?>
        <script type="text/javascript">
        function vote_good()
        {
            if (user_id > 0)
            {
                get_data('a=ajax_image_rate&i=<?= $_id ?>&w=good', new_vote);
            }
            else
            {
                alert("You must be logged-in in order to vote.");
            }
        }
        function vote_bad()
        {
            if (user_id > 0)
            {
                get_data('a=ajax_image_rate&i=<?= $_id ?>&w=bad', new_vote);
            }
            else
            {
                alert("You must be logged-in in order to vote.");
            }
        }
        function new_vote(resp)
        {
            eval(resp.responseText);
        }
        </script>
<?
        
echo '<span id="vote_holder">';
        echo 
'Rate this Image: ';
        echo 
'<span style="font-weight: bold;" class="good"><a href="javascript: vote_good();">Good</a></span> / ';
        echo 
'<span style="font-weight: bold;" class="error"><a href="javascript: vote_bad();">Bad</a></span>';
        echo 
'</span>';
    }
    
    echo 
'</td></tr><tr><td height="4" nowrap></td></tr><tr><td width="4" nowrap></td><td width="250" class="patch_fill_off" style="text-align: center; font-size: 14px;">';

    
$your_type mysql_query("SELECT category_id FROM images_categories_rating WHERE image_id = '" $_id "' AND user_id = '" $GLOBALS['auth']['id'] . "'");
    
$type 0;
    if (
mysql_num_rows($your_type))
    {
        
$your_type mysql_fetch_array($your_type);
        
$type $your_type['category_id'];
    }
    echo 
'Vote Type: <form style="display: inline; margin: 0;"><select name="section" onChange="i_type_changed($F(this));">';
    echo 
'<option value="0"'.($type == 'selected' '').'></option>';
    
$gallery_sections mysql_query("SELECT name, id, worksafe FROM images_categories ORDER BY name ASC");
    while (
$gallery_section mysql_fetch_array($gallery_sections))
    {
        echo 
'<option value="' $gallery_section['id'] . '" ' . ($type == $gallery_section['id'] ? 'selected ' '') . 'class="' . ($gallery_section['worksafe'] == 'error' 'good') . '">' $gallery_section['name'] . ($gallery_section['worksafe'] == '' ' - Not Worksafe') . '</option>';
    }
    echo 
'</select></form>';
?>
    <script type="text/javascript">
        function i_type_changed(value)
        {
            if (user_id > 0)
            {
                get_data('a=ajax_image_typed&i=<?= $_id ?>&v=' + value, new_typed);
            }
            else
            {
                alert("You must be logged-in in order to vote.");
            }
        }
        function new_typed(resp)
        {
            eval(resp.responseText);
        }
    </script>
<?    
    
    
echo '</td></tr></table>';

    echo 
'</div>';
    echo 
'<span style="font-size: 4px;"><br /></span>';
    echo 
'<div class="minor_fill" style="text-align: center;">';

    echo 
'<table width="100%" cellpadding="0" cellspacing="0" border="0"><tr>';
    echo 
'<td valign="top" align="right" width="40%">'.($user_images_prev '<div class="button" style="width: 100px; margin-right: 10px; padding: 4px; text-align: center;"><a href="?s=image'.($_user_id '&u='.$username '').'&i='.$user_images_prev.'">&#171;&#171; Last</a></div>' '').'</td>';
    if (
$_user_id)
    {
        echo 
'<td width="20%"><div class="button" style="font-weight: bold; padding: 4px; text-align: center;"><a href="?s=images&u='.$username.'">'.$username.'</a></div></td>';
    }
    else
    {
        echo 
'<td width="20%"><div class="button" style="font-weight: bold; padding: 4px; text-align: center;"><a href="?s=images">Images</a></div></td>';    
    }
    echo 
'<td valign="top" align="left" width="40%">'.($user_images_next '<div class="button" style="width: 100px; margin-left: 10px; padding: 4px; text-align: center;"><a href="?s=image'.($_user_id '&u='.$username '').'&i='.$user_images_next.'">Next &#187;&#187;</a></div>' '').'</td>';
    echo 
'</tr></table><span style="font-size: 4px;"><br /></span>';

    echo 
'<img src="?g=full&i=' $_id '" width="' $image['width'] . '" height="' $image['height'] . '" alt="" border="1">';

    echo 
'<br /><span style="font-size: 4px;"><br /></span><table width="100%" cellpadding="0" cellspacing="0" border="0"><tr>';
    echo 
'<td valign="top" align="right" width="40%">'.($user_images_prev '<div class="button" style="width: 100px; margin-right: 10px; padding: 4px; text-align: center;"><a href="?s=image'.($_user_id '&u='.$username '').'&i='.$user_images_prev.'">&#171;&#171; Last</a></div>' '').'</td>';
    if (
$_user_id)
    {
        echo 
'<td width="20%"><div class="button" style="font-weight: bold; padding: 4px; text-align: center;"><a href="?s=images&u='.$username.'">'.$username.'</a></div></td>';
    }
    else
    {
        echo 
'<td width="20%"><div class="button" style="font-weight: bold; padding: 4px; text-align: center;"><a href="?s=images">Images</a></div></td>';    
    }
    echo 
'<td valign="top" align="left" width="40%">'.($user_images_next '<div class="button" style="width: 100px; margin-left: 10px; padding: 4px; text-align: center;"><a href="?s=image'.($_user_id '&u='.$username '').'&i='.$user_images_next.'">Next &#187;&#187;</a></div>' '').'</td>';
    echo 
'</tr></table>';

    echo 
'</div>';
    
    echo 
'<span style="font-size: 4px;"><br /></span>';
    
comments($_id'images');
    
    if (
$_user_id)
    {
        echo 
'<span style="font-size: 4px;"><br /></span>';
        echo 
'<div class="sub_fill" style="font-size: 12px; font-weight: bold; padding: 4px 10px 4px 10px;">';
        echo 
'<a href="?s=u&u='.$username.'">&#171; Back to '.$user_gender.' profile</a>';
        echo 
'</div>';            
    }
    
    echo 
'</div>';

    include(
'include/parts/footer.php');
?>