<?
    
function tile_is_available($id$x$y$zoom 1$noadmin 0)
    {
        
$quilts mysql_query("SELECT * FROM quilts WHERE id = '" $id "'");
        if (
$quilts_row mysql_fetch_array($quilts))
        {
            
$admin 0;
            if (
$noadmin == 0)
            {
                
$quilts_permissions mysql_query("SELECT * FROM quilts_permissions WHERE user_id = '" $GLOBALS['auth']['id'] . "' AND quilt_id = '" $id "'");
                if (
$quilts_permissions_row mysql_fetch_array($quilts_permissions))
                {
                    if (
$quilts_permissions_row['active'])
                    {
                        
$admin 1;
                    }
                }
            }
            
$display_available 0;
            
$display_hidden 0;
            
$display_none 0;
            
$display_pending 0;
            
$display_pending_approval 0;
            
$display_tile 0;
            
$display_unavailable 0;
            
//DO THE WRAP AROUND
            
$x_plus $x 1;
            
$x_minus $x 1;
            
$y_plus $y 1;
            
$y_minus $y 1;
            if (
$quilts_row['edge_wrap'] == || $quilts_row['edge_wrap'] == 3)
            {
                if (
$x_plus $quilts_row['quilt_width'])
                {
                    
$x_plus 1;
                }
                if (
$x_minus 1)
                {
                    
$x_minus $quilts_row['quilt_width'];
                }
            }
            if (
$quilts_row['edge_wrap'] == || $quilts_row['edge_wrap'] == 3)
            {
                if (
$y_plus $quilts_row['quilt_height'])
                {
                    
$y_plus 1;
                }
                if (
$y 1)
                {
                    
$y_minus $quilts_row['quilt_height'];
                }
            }
            if (
$y || $x || $x $quilts_row['quilt_width'] || $y $quilts_row['quilt_height'])
            {
                
$display_none 1;
            }
            elseif (
$quilts_row['finished'])
            {
                
$tile_middle_center mysql_fetch_assoc(mysql_query("SELECT * FROM tiles WHERE quilt_id = '" $id "' AND matrix_x = '" $x "' AND matrix_y = '" $y "' AND deleted = '0'"));
                if (
$tile_middle_center['visibility'] == -1)
                {
                    
$display_pending_approval 1;
                }
                else
                {
                    
$display_tile 1;
                }
            }
            else
            {
                
//CHECK IS THE QUILT HAS NOT BEEN STARTED YET AND LET ANY TILE BE AVAILABLE
                
$num_tile mysql_num_rows(mysql_query("SELECT * FROM tiles WHERE quilt_id = '" $id "' AND deleted = '0' LIMIT 1"));
                
$num_pending mysql_num_rows(mysql_query("SELECT * FROM tiles_pending WHERE quilt_id = '" $id "' LIMIT 1"));
                if (
$num_tile || $num_pending)
                {
                    
//LOAD SURROUNDING TILES INTO MEMORY
                    
$tile_top_center mysql_fetch_assoc(mysql_query("SELECT * FROM tiles WHERE quilt_id = '" $id "' AND matrix_x = '" $x "' AND matrix_y = '" $y_minus "' AND deleted = '0'"));
                    
$tile_middle_left mysql_fetch_assoc(mysql_query("SELECT * FROM tiles WHERE quilt_id = '" $id "' AND matrix_x = '" $x_minus "' AND matrix_y = '" $y "' AND deleted = '0'"));
                    
$tile_middle_center mysql_fetch_assoc(mysql_query("SELECT * FROM tiles WHERE quilt_id = '" $id "' AND matrix_x = '" $x "' AND matrix_y = '" $y "' AND deleted = '0'"));
                    
$tile_middle_right mysql_fetch_assoc(mysql_query("SELECT * FROM tiles WHERE quilt_id = '" $id "' AND matrix_x = '" $x_plus "' AND matrix_y = '" $y "' AND deleted = '0'"));
                    
$tile_bottom_center mysql_fetch_assoc(mysql_query("SELECT * FROM tiles WHERE quilt_id = '" $id "' AND matrix_x = '" $x "' AND matrix_y = '" $y_plus "' AND deleted = '0'"));
                    
//LOAD PENDING TILES INTO MEMORY
                    
$pending_top_center mysql_fetch_assoc(mysql_query("SELECT * FROM tiles_pending WHERE quilt_id = '" $id "' AND matrix_x = '" $x "' AND matrix_y = '" $y_minus "'"));
                    
$pending_middle_left mysql_fetch_assoc(mysql_query("SELECT * FROM tiles_pending WHERE quilt_id = '" $id "' AND matrix_x = '" $x_minus "' AND matrix_y = '" $y "'"));
                    
$pending_middle_center mysql_fetch_assoc(mysql_query("SELECT * FROM tiles_pending WHERE quilt_id = '" $id "' AND matrix_x = '" $x "' AND matrix_y = '" $y "'"));
                    
$pending_middle_right mysql_fetch_assoc(mysql_query("SELECT * FROM tiles_pending WHERE quilt_id = '" $id "' AND matrix_x = '" $x_plus "' AND matrix_y = '" $y "'"));
                    
$pending_bottom_center mysql_fetch_assoc(mysql_query("SELECT * FROM tiles_pending WHERE quilt_id = '" $id "' AND matrix_x = '" $x "' AND matrix_y = '" $y_plus "'"));
                    
//PROCESS EVERYTHING
                    
if ($tile_middle_center)
                    {
                        if (
$admin == || $tile_middle_center['visibility'] == || $quilts_row['show_all'] == || ($tile_middle_center['user_id'] == $GLOBALS['auth']['id'] && $noadmin == 0))
                        {
                            
$display_tile 1;
                        }
                        elseif (
$tile_middle_center['visibility'] == -1)
                        {
                            
$display_pending_approval 1;
                        }
                        elseif (
$quilts_row['show_all'] == 2)
                        {
                            
$display_hidden 1;
                        }
                        elseif (
$pending_top_center || $pending_middle_left || $pending_middle_right || $pending_bottom_center)
                        {
                            
$display_hidden 1;
                        }
                        elseif ((
$y_minus >= && !$tile_top_center) || ($x_minus >= && !$tile_middle_left) || ($x_plus <= $quilts_row['quilt_width'] && !$tile_middle_right) || ($y_plus <= $quilts_row['quilt_height'] && !$tile_bottom_center))
                        {
                            
$display_hidden 1;
                        }
                        else
                        {
                            
$display_tile 1;
                        }
                    }
                    elseif (
$pending_middle_center)
                    {
                        
$display_pending 1;
                    }
                    else
                    {
                        if (
$pending_top_center || $pending_middle_left || $pending_middle_right || $pending_bottom_center)
                        {
                            
$display_unavailable 1;
                        }
                        elseif (
$tile_top_center['visibility'] == -|| $tile_middle_left['visibility'] == -|| $tile_middle_right['visibility'] == -|| $tile_bottom_center['visibility'] == -1)
                        {
                            
$display_unavailable 1;
                        }
                        else
                        {
                            if (
$quilts_row['start_anywhere'] && $quilts_row['work_on_all'])
                            {
                                
$display_available 1;
                            }
                            elseif (
$quilts_row['start_anywhere'])
                            {
                                if ((
$tile_top_center && $tile_top_center['user_id'] == $GLOBALS['auth']['id']) || ($tile_middle_left && $tile_middle_left['user_id'] == $GLOBALS['auth']['id']) || ($tile_middle_right && $tile_middle_right['user_id'] == $GLOBALS['auth']['id']) || ($tile_bottom_center && $tile_bottom_center['user_id'] == $GLOBALS['auth']['id']))
                                {
                                    
$display_unavailable 1;
                                }
                                else
                                {
                                    
$display_available 1;
                                }
                            }
                            elseif (
$quilts_row['work_on_all'])
                            {
                                if (
$tile_top_center || $tile_middle_left || $tile_middle_right || $tile_bottom_center)
                                {
                                    
$display_available 1;
                                }
                                else
                                {
                                    
$display_unavailable 1;
                                }
                            }
                            else
                            {
                                if (
$tile_top_center || $tile_middle_left || $tile_middle_right || $tile_bottom_center)
                                {
                                    if ((
$tile_top_center && $tile_top_center['user_id'] == $GLOBALS['auth']['id']) || ($tile_middle_left && $tile_middle_left['user_id'] == $GLOBALS['auth']['id']) || ($tile_middle_right && $tile_middle_right['user_id'] == $GLOBALS['auth']['id']) || ($tile_bottom_center && $tile_bottom_center['user_id'] == $GLOBALS['auth']['id']))
                                    {
                                        
$display_unavailable 1;
                                    }
                                    else
                                    {
                                        
$display_available 1;
                                    }
                                }
                                else
                                {
                                    
$display_unavailable 1;
                                }                        
                            }
                        }
                    }
                    if (
$display_available)
                    {
                        
//CHECK IF YOU HAVE USED BY THE MAXIMUM TILES YOU CAN WORK ON FOR THIS QUILT
                        
$used_pending mysql_num_rows(mysql_query("SELECT * FROM tiles_pending WHERE quilt_id = '" $id "' AND user_id = '" $GLOBALS['auth']['id'] . "'"));
                        if (
$used_pending >= $quilts_row['multiple'])
                        {
                            
$display_available 0;
                            
$display_unavailable 1;
                        }
                    }
                }
                else
                {
                    
$display_available 1;
                }
            }
            
$temp $x $y;
            
$color = ($temp == 0) ? '#2E3336' '#303538';
            
$textsize round(12 * ($zoom), 0);
            
//RETURN THE RESULTS
            
if ($display_available)
            {
                if (
$GLOBALS['auth']['id'])
                {
                    return array(
10'<td width="' round($quilts_row['tile_width'] * ($zoom), 0) . '" height="' round($quilts_row['tile_height'] * ($zoom), 0) . '" style="font-size: ' $textsize 'px; text-align: center; background: ' $color ';"><a href="?s=tile_checkout&i=' $id '&x=' $x '&y=' $y '">Available</a></td>');    
                }
                else
                {
                    return array(
00'<td width="' round($quilts_row['tile_width'] * ($zoom), 0) . '" height="' round($quilts_row['tile_height'] * ($zoom), 0) . '" style="font-size: ' $textsize 'px; text-align: center; background: ' $color ';">Available</td>');
                }
            }
            elseif (
$display_hidden)
            {
                return array(
00'<td width="' round($quilts_row['tile_width'] * ($zoom), 0) . '" height="' round($quilts_row['tile_height'] * ($zoom), 0) . '" style="font-size: ' $textsize 'px; text-align: center; background: ' $color ';">Completed By<br /><a href="?s=profile&u=' get_username($tile_middle_center['user_id']) . '">' get_username($tile_middle_center['user_id']) . '</a><br />Hidden Until<br />Surrounding Tiles<br />Are Complete</td>');
            }
            elseif (
$display_none)
            {
                return array(
00'<td></td>');
            }
            elseif (
$display_pending)
            {
                return array(
00'<td width="' round($quilts_row['tile_width'] * ($zoom), 0) . '" height="' round($quilts_row['tile_height'] * ($zoom), 0) . '" style="font-size: ' $textsize 'px; text-align: center; background: ' $color ';">In Use By<br /><a href="?s=profile&u=' get_username($pending_middle_center['user_id']) . '">' get_username($pending_middle_center['user_id']) . '</a></td>');
            }
            elseif (
$display_pending_approval)
            {
                return array(
00'<td width="' round($quilts_row['tile_width'] * ($zoom), 0) . '" height="' round($quilts_row['tile_height'] * ($zoom), 0) . '" style="font-size: ' $textsize 'px; text-align: center; background: ' $color ';">Completed By<br /><a href="?s=profile&u=' get_username($tile_middle_center['user_id']) . '">' get_username($tile_middle_center['user_id']) . '</a><br />Pending<br />Approval<br /></td>');
            }
            elseif (
$display_tile)
            {
                return array(
01'<td width="' round($quilts_row['tile_width'] * ($zoom), 0) . '" height="' round($quilts_row['tile_height'] * ($zoom), 0) . '" style="font-size: ' $textsize 'px; text-align: center; background: ' $color ';"><a href="?s=tile&i=' $tile_middle_center['tile_id'] . '"><img src="?g=tile&i=' $tile_middle_center['tile_id'] . '" width="' round($quilts_row['tile_width'] * ($zoom), 0) . '" height="' round($quilts_row['tile_height'] * ($zoom), 0) . '" border="0" title="' .  htmlspecialchars(get_username($tile_middle_center['user_id'])) . ': ' .  htmlspecialchars($tile_middle_center['comment']) . '" alt="" style="border: 0px;"></a></td>');
            }
            elseif (
$display_unavailable)
            {
                return array(
00'<td width="' round($quilts_row['tile_width'] * ($zoom), 0) . '" height="' round($quilts_row['tile_height'] * ($zoom), 0) . '" style="font-size: ' $textsize 'px; text-align: center; background: ' $color ';">Unavailable</td>');
            }
            else
            {
                return array(
00'<td width="' round($quilts_row['tile_width'] * ($zoom), 0) . '" height="' round($quilts_row['tile_height'] * ($zoom), 0) . '" style="font-size: ' $textsize 'px; text-align: center; background: ' $color ';">Error</td>');
            }
        }
        else
        {
            return array(
00'<td width="100" height="100" style="text-align: center;">Error</td>');
        }
    }
?>