<?php
    $GLOBALS['highlight'] = 'quilts';

    require_once('include/functions/comments.php');
    require_once('include/functions/community_thread.php');
    require_once('include/functions/tile_is_available.php');
    
    $_tile_zoom = isset($_POST['tile_zoom']) ? $_POST['tile_zoom'] : '';
    
    if ($_tile_zoom && $_id)
    {
        make_cookie('tile_zoom', $_tile_zoom);
        header('Location: ./?s=tile&i=' . $_id);
        die;
    }

    $tiles = mysqli_query_logged("SELECT * FROM tiles WHERE tile_id = '" . $_id . "'");
    if ($tiles_row = mysqli_fetch_array($tiles))
    {
        list($available, $display, $output) = tile_is_available($tiles_row['quilt_id'], $tiles_row['matrix_x'], $tiles_row['matrix_y']);
        if ($display)
        {
            $quilts = mysqli_query_logged("SELECT * FROM quilts WHERE id = '" . $tiles_row['quilt_id'] . "'");
            $quilts_row = mysqli_fetch_array($quilts);
            if ($quilts_row['finished'])
            {
                $GLOBALS['highlight'] = 'finished';
            }
        }
        else
        {
            header('Location: ./?s=quilt&i=' . $tiles_row['quilt_id']);
            die;
        }
    }
    else
    {
        header('Location: ./?s=finished');
        die;
    }

    include('include/parts/header.php');

    echo '<div class="header">';
    echo '<table width="100%" border="0" cellpadding="0" cellspacing="0"><tr>';
    echo '<td><a href="?s=' . ($quilts_row['finished'] ? 'finished' : 'quilts') . '">Quilts</a> - <a href="?s=quilt&i=' . $quilts_row['id'] . '">' . $quilts_row['name'] . '</a> - View Tile</td>';
    echo '<td align="right">Tile Zoom: <form name="tile_zoom" action="?s=tile&i=' . $_id . '" method="post" style="margin: 0px; display: inline;"><select name="tile_zoom" style="font-size: 11px; border: 1px solid black;" onChange="document.tile_zoom.submit();">';
    echo '<option value="1.2"' . (isset($_COOKIE['tile_zoom']) && $_COOKIE['tile_zoom'] == 1.2 ? ' selected' : '') . '>120%</option>';
    echo '<option value="1.1"' . (isset($_COOKIE['tile_zoom']) && $_COOKIE['tile_zoom'] == 1.1 ? ' selected' : '') . '>110%</option>';
    echo '<option value="1"' . (!isset($_COOKIE['tile_zoom']) || (isset($_COOKIE['tile_zoom']) && $_COOKIE['tile_zoom'] == 1) ? ' selected' : '') . '>100%</option>';
    echo '<option value="0.90"' . (isset($_COOKIE['tile_zoom']) && $_COOKIE['tile_zoom'] == 0.9 ? ' selected' : '') . '>90%</option>';
    echo '<option value="0.80"' . (isset($_COOKIE['tile_zoom']) && $_COOKIE['tile_zoom'] == 0.8 ? ' selected' : '') . '>80%</option>';
    echo '<option value="0.70"' . (isset($_COOKIE['tile_zoom']) && $_COOKIE['tile_zoom'] == 0.7 ? ' selected' : '') . '>70%</option>';
    echo '<option value="0.60"' . (isset($_COOKIE['tile_zoom']) && $_COOKIE['tile_zoom'] == 0.6 ? ' selected' : '') . '>60%</option>';
    echo '<option value="0.50"' . (isset($_COOKIE['tile_zoom']) && $_COOKIE['tile_zoom'] == 0.5 ? ' selected' : '') . '>50%</option>';
    echo '<option value="0.40"' . (isset($_COOKIE['tile_zoom']) && $_COOKIE['tile_zoom'] == 0.4 ? ' selected' : '') . '>40%</option>';
    echo '</select></form></td>';
    echo '</tr></table>';
    echo '</div>';

    echo '<div class="content">';
    $quilts_permissions = mysqli_query_logged("SELECT * FROM quilts_permissions WHERE user_id = '" . $GLOBALS['auth']['id'] . "' AND quilt_id = '" . $tiles_row['quilt_id'] . "'");
    if (mysqli_num_rows($quilts_permissions) || $tiles_row['user_id'] == $GLOBALS['auth']['id'])
    {
        echo '<div class="header" style="color: #cccccc;">';
        if (mysqli_num_rows($quilts_permissions))
        {
            if ($tiles_row['visibility'] == -1)
            {
                echo '[ <span style="color: green;">Pending Approval</span> ] [ Normal Mode ] [ Always Visible ]';
            }
            elseif ($tiles_row['visibility'] == 0)
            {
                echo '[ Pending Approval ] [ <span style="color: green;">Normal Mode</span> ] [ Always Visible ]';
            }
            elseif ($tiles_row['visibility'] == 1)
            {
                echo '[ Pending Approval ] [ Normal Mode ] [ <span style="color: green;">Always Visible</span> ]';
            }
            echo ' [ <a href="?a=tile_visibility&i=' . $_id . '">Change</a> ]' . "\r\n";
        }
        if (!$quilts_row['finished'])
        {
            echo '[ <a href="?a=tile_delete&i=' . $_id . '" onclick="return confirmLink(this, \'Delete This Tile\')">Delete Tile</a> ]<br />' . "\r\n";
        }
        echo '</div>';
        echo '<div style="padding: 5px 0px 0px 0px;"></div>' . "\r\n";
    }

    echo '<table width="100%" border="0" cellpadding="0" cellspacing="0"><tr>';
    
    echo '<td valign="top" width="400">';
    echo '<img src="?g=tile&i=' . $_id . '" width="' . $quilts_row['tile_width'] . '" height="' . $quilts_row['tile_height'] . '" border="0"><br />';
    echo '<div style="padding: 10px 0px 0px 0px;"></div>';
    if ($tiles_row['deleted'])
    {
        echo '<span style="color: red;">This tile has been deleted.</span><br />';
        echo '<div style="padding: 10px 0px 0px 0px;"></div>';
    }
    if ($GLOBALS['auth']['id'] == $tiles_row['user_id'])
    {
        echo 'Comment:<br />';
        echo '<div style="padding: 5px 0px 0px 0px;"></div>';
        echo '<form action="?a=tile_comment&i=' . $_id . '&b=' . str_replace('&', '@', $_SERVER['QUERY_STRING']) . '" method="post" style="margin: 0px; display: inline;">';
        echo '<input type="text" name="comment" value="' . htmlspecialchars($tiles_row['comment']) . '" style="width: 200px; border: solid 2px black; margin: 0px; padding: 3px;"> ';
        echo '<input type="submit" value="Save" style="border: solid 1px green; background: black; color: green;">';
        echo '</form>';
        echo '<div style="padding: 10px 0px 0px 0px;"></div>';
    }
    elseif ($tiles_row['comment'])
    {
        echo 'Comment: ' . $tiles_row['comment'] . '<br />';
        echo '<div style="padding: 10px 0px 0px 0px;"></div>';
    }
    echo 'By: <a href="?s=profile&u=' . get_username($tiles_row['user_id']) . '">' . get_username($tiles_row['user_id']) . '</a><br />';
    echo 'Checked Out At: ' . nice_date($tiles_row['started_on']) . '<br />';
    echo 'Checked In At: ' . nice_date($tiles_row['posted_on']) . '<br />';
    echo '<div style="padding: 10px 0px 0px 0px;"></div>';
    echo 'Checkout Tile:<br /><br />';
    echo '<img src="?g=tile_sides&i=' . $tiles_row['quilt_id'] . '&x=' . $tiles_row['matrix_x'] . '&y=' . $tiles_row['matrix_y'] . '&borders=' . $tiles_row['borders'] . '" width="' . ($quilts_row['tile_width'] + $quilts_row['side_pixels'] * 2) . '" height="' . ($quilts_row['tile_height'] + $quilts_row['side_pixels'] * 2) . '" border="0">' . "\r\n";
    echo '</td>';
    
    $tilezoom = isset($_COOKIE['tile_zoom']) ? $_COOKIE['tile_zoom'] : 1;
    
    echo '<td align="right" valign="top" style="padding-left: 10px;">';
    echo '<table border="0" cellpadding="0" cellspacing="0">' . "\r\n";
    echo '<tr>' . "\r\n";
    list($available, $display, $output) = tile_is_available($tiles_row['quilt_id'], $tiles_row['matrix_x'] - 1, $tiles_row['matrix_y'] - 1, $tilezoom);
    echo $output . "\r\n";
    list($available, $display, $output) = tile_is_available($tiles_row['quilt_id'], $tiles_row['matrix_x'], $tiles_row['matrix_y'] - 1, $tilezoom);
    echo $output . "\r\n";
    list($available, $display, $output) = tile_is_available($tiles_row['quilt_id'], $tiles_row['matrix_x'] + 1, $tiles_row['matrix_y'] - 1, $tilezoom );
    echo $output . "\r\n";
    echo '</tr>' . "\r\n";
    echo '<tr>' . "\r\n";
    list($available, $display, $output) = tile_is_available($tiles_row['quilt_id'], $tiles_row['matrix_x'] - 1, $tiles_row['matrix_y'], $tilezoom );
    echo $output . "\r\n";
    list($available, $display, $output) = tile_is_available($tiles_row['quilt_id'], $tiles_row['matrix_x'], $tiles_row['matrix_y'], $tilezoom );
    echo $output . "\r\n";
    list($available, $display, $output) = tile_is_available($tiles_row['quilt_id'], $tiles_row['matrix_x'] + 1, $tiles_row['matrix_y'], $tilezoom );
    echo $output . "\r\n";
    echo '</tr>' . "\r\n";
    echo '<tr>' . "\r\n";
    list($available, $display, $output) = tile_is_available($tiles_row['quilt_id'], $tiles_row['matrix_x'] - 1, $tiles_row['matrix_y'] + 1, $tilezoom );
    echo $output . "\r\n";
    list($available, $display, $output) = tile_is_available($tiles_row['quilt_id'], $tiles_row['matrix_x'], $tiles_row['matrix_y'] + 1, $tilezoom );
    echo $output . "\r\n";
    list($available, $display, $output) = tile_is_available($tiles_row['quilt_id'], $tiles_row['matrix_x'] + 1, $tiles_row['matrix_y'] + 1, $tilezoom );
    echo $output . "\r\n";
    echo '</tr>' . "\r\n";
    echo '</table>' . "\r\n";
    echo '</td>';
    
    echo '</tr></table>' . "\r\n";

    echo '</div>';

    community_thread('tiles');

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