<?php
get_header();
the_post();
$thread = CMA_AnswerThread::getInstance($post->ID);
?>
<div id="primary" class="site-content cma-content">
    <div class="cma-main">
        <div class="cma-wrapper">
            <div id="content" role="main" class="entry-content">
                <header class="entry-header" style="margin-top:-10px">
                <div class="" style="font-size:12px; text-align:right"><a class="cma-backlink" href="<?php echo get_post_type_archive_link(CMA_AnswerThread::POST_TYPE); ?>"> <?php _e('שאלות הגולשים', 'cm-answers'); ?></a> &nbsp; &raquo; &nbsp;<?=$thread->getTitle();?></div>
                    <h1 class="entry-title"><?php echo $thread->getTitle(); ?></h1>

                </header>
                <?php
                do_action('CMA_show_messages');
                ?>
                <?php if(CMA_AnswerThread::isRatingAllowed()): ?>
                    <script type="text/javascript">
                        function thumbsUp(comment_id){
                            thumbs(comment_id, 'up');
                        }
    <?php if(CMA_AnswerThread::isNegativeRatingAllowed()): ?>function thumbsDown(comment_id) {
                                thumbs(comment_id, 'down');
                            }<?php endif; ?>
                        function thumbs(comment_id, upDown){
                            jQuery('#comment-' + comment_id + ' .cma-answer-rating-loading').show();
                            jQuery.post(self.location, {'cma-action': 'vote', 'cma-comment': comment_id, 'cma-value': upDown}, function(data){
                                if(data.success == 1){
                                    jQuery('#comment-' + comment_id + ' .cma-answer-rating-count').text(data.message);
                                    jQuery().toastmessage('showSuccessToast', "<?php _e('Thank you for voting!', 'cm-answers'); ?>");
                                }else {
                                    jQuery().toastmessage('showErrorToast', data.message);
                                }
                                jQuery('#comment-' + comment_id + ' .cma-answer-rating-loading').hide();
                            });
                        }
                    </script>
                <?php endif; ?>
                <table class="cma-answers-list cma-question-table">
                    <tr>
                        <td class="cma-question-content">
                            <div class="cma-question-body"><?php echo $thread->getContent(); ?></div>
                            <div class="cma-question-meta">
                                <div class="cma-question-author"><?php printf(__('נשאל ע"י %s', 'cm-answers'), $thread->getAuthor()->display_name); ?></div>
                                <div class="cma-question-date"><?php printf(__('%s', 'cm-answers'), $thread->getCreationDate()); ?></div>
                            </div>
                        </td>
                    </tr>
                </table>

                <?php
                $currentSort = !empty($_GET['sort']) ? $_GET['sort'] : 'newest';
                $answers     = $thread->getAnswers($currentSort);
                if(!empty($answers)):
                    ?>
                    <?php if(CMA_AnswerThread::isRatingAllowed()): ?><ul class="cma-thread-orderby cma-answers-orderby">
                            <li<?php if($currentSort == 'newest'): ?> class="cma-current-sort"<?php endif; ?>><a href="?sort=newest"><?php _e('Newest', 'cm-answers'); ?></a></li>
                            <li<?php if($currentSort == 'votes'): ?> class="cma-current-sort"<?php endif; ?>><a href="?sort=votes"><?php _e('Highest Rating', 'cm-answers'); ?></a></li>
                        </ul><?php endif; ?>
                    <?php
                    foreach($answers as $answer):
                        ?>
                        <table class="cma-answers-list">
                            <tr id="comment-<?php echo $answer['id']; ?>">
                                <?php if(CMA_AnswerThread::isRatingAllowed()): ?><td class="cma-answer-rating" >
                                        <div class="cma-answer-rating-loading"></div>
                                        <a href="javascript:void(0)" onclick="thumbsUp(<?php echo $answer['id']; ?>)" class="cma-thumbs-up" title="<?php _e('Thumbs Up!', 'cm-answers'); ?>">▲</a>
                                        <div class="cma-answer-rating-count"><?php echo $answer['rating']; ?></div>
                                        <?php if(CMA_AnswerThread::isNegativeRatingAllowed()): ?><a href="javascript:void(0)" onclick="thumbsDown(<?php echo $answer['id']; ?>)" class="cma-thumbs-down" title="<?php _e('Thumbs Down!', 'cm-answers'); ?>">▼</a><?php endif; ?>

                                    </td>
                                <?php else: ?>
                                    <td class="cma-answer-norating"></td>
                                <?php endif; ?>

                                <td class="cma-answer-content<?php if(CMA_AnswerThread::isRatingAllowed()): ?> cma-answer-norating-content<?php endif; ?>">
                                    <div class="cma-answer-body"><?php echo $answer['content']; ?></div>
                                    <div class="cma-answer-meta">
                                        <div class="cma-answer-author"><?php printf(__('נכתב על ידי %s', 'cm-answers'), $answer['author']); ?></div>
                                        <div class="cma-answer-date"><?php printf(__('נענה ב%s', 'cm-answers'), $answer['date']); ?></div>
                                    </div>
                                </td>
                            </tr>
                            <?php
                        endforeach;
                    endif;
                    ?>
                </table>
                <div class="cma-form-container">
                    <?php if(is_user_logged_in()): ?>

                        <?php if(!$thread->isResolved()) : ?>
                            <div style="clear:both;height:25px;"></div>
                            <form method="post">
                                <input type="hidden" name="cma-action" value="add" />
                                <h3><?php _e('תשובה:', 'cm-answers'); ?></h3>
                                <div style="clear:both;height:10px;"></div>
								<!--<ul class="cma-form-notes">
                                    <li><?php _e('Allow markup', 'cm-answers'); ?>: &lt;strong&gt;, &lt;em&gt;, &lt;a&gt;</li>
                                    <li><?php _e('Wrap your code using', 'cm-answers'); ?> &lt;pre&gt;&lt;/pre&gt;</li>
                                </ul>-->
                                <input type="hidden" name="cma-thread" value="<?php echo $post->ID; ?>"/>
                                <textarea name="content" placeholder="<?php _e('תוכן תשובה', 'cm-answers'); ?>"></textarea><br />
                                <label>
                                    <div style="clear:both;height:5px;"></div>
                                    <input name="thread_notify" type="checkbox" value="1"/>
                                    <?php _e('שלחו עדכונים', 'cm-answers'); ?></label><br />
                                <?php if(get_current_user_id() == $thread->getAuthorId()): ?><label>
                                        <div style="clear:both;height:5px;"></div>
                                        <input name="thread_resolved" type="checkbox" value="1"/>
                                        <?php _e('סמן כשאלה פתורה', 'cm-answers'); ?></label><br /><?php endif; ?>
                                <div style="clear:both;height:10px;"></div>
                                <input type="submit" value="<?php _e('שליחת תשובה', 'cm-answers'); ?>" />
                            </form>
                        <?php endif; ?>

                    <?php else: ?>
                        <?php
                        //do_action('CMA_login_form');
                        ?>
                    <?php endif; ?>
                </div>
            </div>
            <span class="cma_poweredby">
                <a href="http://plugins.cminds.com/" target="_blank" class="cma_poweredbylink">CreativeMinds WordPress Plugins</a> <a href="http://answers.cminds.com/" target="_blank" class="cma_poweredbylink">CM Answers</a>
            </span>
        </div>
        <?php if(CMA_AnswerThread::isSidebarEnabled() && is_active_sidebar('cm-answers-sidebar')): ?>
            <div id="secondary" role="complementary" class="widget-area cma-sidebar" <?php
                 $maxWidth = CMA_AnswerThread::getSidebarMaxWidth();
                 if($maxWidth > 0) echo 'style="max-width:' . $maxWidth . 'px"';
                 ?>>
                     <?php dynamic_sidebar('cm-answers-sidebar'); ?>
            </div>
        <?php endif; ?>
    </div>
</div>
<?php
get_footer();
