<?php
    require_once('include/functions/image_overall_rating.php');
    require_once('include/functions/image_overall_type.php');

    function image_voting($id)
    {
?>

<script type="text/javascript">
var user_id = <?php echo $GLOBALS['auth']['id'] > 0 ? $GLOBALS['auth']['id'] : 'null' ?>;
function ajax_eval(script, value)
{
    if (user_id > 0)
    {
        doAjax('index.php?j=' + script + '&i=<?php echo $id ?>&v=' + value, function(AJAX) { eval(AJAX.responseText); });
    }
    else
    {
        alert("You must be logged-in in order to vote.");
    }
}
</script>

<?php
        echo '<table width="100%" border="0" cellpadding="0" cellspacing="0"><tr><td>';
        if ($GLOBALS['auth']['id'])
        {
            echo 'Rate Image: ';
            echo '<a href="javascript:ajax_eval(\'image_rate\', 1);"><span class="notice_good">Good</span></a> / ';
            echo '<a href="javascript:ajax_eval(\'image_rate\', 0);"><span class="notice_error">Bad</span></a>' . "\r\n";
            echo ' - <span id="vote_holder">';
            $vote = mysqli_query_logged("SELECT vote FROM images_rating WHERE user_id = '" . $GLOBALS['auth']['id'] . "' AND image_id = '" . $id . "'");
            if ($vote = mysqli_fetch_array($vote))
            {
                echo 'You Voted: ';
                if ($vote['vote'] == 1)
                {
                    echo '<span class="notice_good">Good</span>';
                }
                elseif ($vote['vote'] == -1)
                {
                    echo '<span class="notice_error">Bad</span>';
                }
            }
            echo '</span> ';
        }    
        echo 'Overall: <span id="v_rating">' . image_overall_rating($id) . '</span>';
        echo '</td><td align="right">';
        if ($GLOBALS['auth']['id'])
        {
            $your_type = mysqli_query_logged("SELECT category_id FROM images_categories_rating WHERE image_id = '" . $id . "' AND user_id = '" . $GLOBALS['auth']['id'] . "'");
            $type = 0;
            if ($your_type = mysqli_fetch_array($your_type))
            {
                $type = $your_type['category_id'];
            }
            echo 'Vote Type: <form style="margin: 0px; display: inline;">';
            echo '<select id="section" name="section" class="input_select" style="font-size: 12px;" onChange="ajax_eval(\'image_typed\', document.getElementById(\'section\').value);">';
            echo '<option value="0"' . ($type == 0 ? 'selected' : '') . '></option>';
            $gallery_sections = mysqli_query_logged("SELECT name, id, worksafe FROM images_categories ORDER BY name");
            while ($gallery_section = mysqli_fetch_array($gallery_sections))
            {
                echo '<option value="' . $gallery_section['id'] . '" ' . ($type == $gallery_section['id'] ? 'selected ' : '') . 'class="' . ($gallery_section['worksafe'] == 0 ? 'notice_error' : 'notice_good') . '">' . $gallery_section['name'] . ($gallery_section['worksafe'] == 1 ? '' : ' - Not Worksafe') . '</option>';
            }
            echo '</select>';
            echo '</form>&nbsp;';
        }
        echo 'Overall: <span id="v_type">' . image_overall_type($id) . '</span>' . "\r\n";
        echo '</td></tr></table>';
    }
?>