function sgPopupMediaButtonThickboxs()
{
    global $pagenow, $typenow;
    $pages = array('post.php', 'page.php', 'post-new.php', 'post-edit.php');
    $checkPage = in_array($pagenow, $pages);
    if ($checkPage && $typenow != 'download') {
        ?>
		<script type="text/javascript">
			jQuery(document).ready(function ($) {
				$('#sg-ptp-popup-insert').on('click', function () {
					var id = $('#sg-insert-popup-id').val();
					if ('' === id) {
						alert('Select your popup');
						return;
					}
					selectionText = '';
					if (typeof(tinyMCE.editors.content) != "undefined") {
						selectionText = (tinyMCE.activeEditor.selection.getContent()) ? tinyMCE.activeEditor.selection.getContent() : '';
					}
					window.send_to_editor('[sg_popup id="' + id + '"]'+selectionText+"[/sg_popup]");
				});
			});
		</script>

		<div id="sg-popup-thickbox" style="display: none;">
			<div class="wrap">
				<p>Insert the shortcode for showing a Popup.</p>
				<div>
					<select id="sg-insert-popup-id">
						<option value="">Please select...</option>
						<?php 
        global $wpdb;
        $proposedTypes = array();
        $orderBy = 'id DESC';
        $allPopups = SGPopup::findAll($orderBy);
        foreach ($allPopups as $allPopup) {
            ?>
								<option value="<?php 
            echo $allPopup->getId();
            ?>
"><?php 
            echo $allPopup->getTitle();
            echo " - " . $allPopup->getType();
            ?>
</option>;
							<?php 
        }
        ?>
					</select>
				</div>
				<p class="submit">
					<input type="button" id="sg-ptp-popup-insert" class="button-primary dashicons-welcome-widgets-menus" value="Insert"/>
					<a id="sg_popup_cancel" class="button-secondary" onclick="tb_remove();" title="Cancel">Cancel</a>
				</p>
			</div>
		</div>
	<?php 
    }
}
function sgPopupCallback($post)
{
    wp_nonce_field(basename(__FILE__), 'prfx_nonce');
    $prfx_stored_meta = get_post_meta($post->ID);
    ?>
	<p class="preview-paragaraph">
<?php 
    global $wpdb;
    $proposedTypes = array();
    $orderBy = 'id DESC';
    $proposedTypes = SGPopup::findAll($orderBy);
    function sgCreateSelect($options, $name, $selecteOption)
    {
        $selected = '';
        $str = "";
        $str .= "<select class=\"choose-popup-type\" name=\"{$name}\">";
        $str .= "<option value=''>Not selected</potion>";
        foreach ($options as $option) {
            if ($option) {
                $title = $option->getTitle();
                $type = $option->getType();
                $id = $option->getId();
                if ($selecteOption == $id) {
                    $selected = "selected";
                } else {
                    $selected = '';
                }
                $str .= "<option value='" . $id . "' disable='" . $id . "' " . $selected . " >{$title} - {$type}</potion>";
            }
        }
        $str .= "</select>";
        return $str;
    }
    global $post;
    $page = (int) $post->ID;
    $popup = "sg_promotional_popup";
    $popupId = SGPopup::getPagePopupId($page, $popup);
    echo sgCreateSelect($proposedTypes, 'sg_promotional_popup', $popupId);
    $SG_APP_POPUP_URL = SG_APP_POPUP_URL;
    ?>
	</p>
	<input type="hidden" value="<?php 
    echo $SG_APP_POPUP_URL;
    ?>
" id="SG_APP_POPUP_URL">
<?php 
}
<?php

$pagenum = isset($_GET['pn']) ? (int) $_GET['pn'] : 1;
$limit = SG_APP_POPUP_TABLE_LIMIT;
$offset = ($pagenum - 1) * $limit;
$total = SGPopup::getTotalRowCount();
$num_of_pages = ceil(esc_html($total) / $limit);
if ($pagenum > $num_of_pages || $pagenum < 1) {
    $offset = 0;
    $pagenum = 1;
}
$orderBy = 'id DESC';
$entries = SGPopup::findAll($orderBy, $limit, $offset);
?>
<div class="wrap">
	<div class="headers-wrapper">
	<h2>Popups <a href="<?php 
echo admin_url();
?>
admin.php?page=create-popup" class="add-new-h2">Add New</a></h2>
		<?php 
if (!SG_POPUP_PRO) {
    ?>
				<input type="button" class="main-update-to-pro" value="Upgrade to PRO version" onclick="window.open('<?php 
    echo SG_POPUP_PRO_URL;
    ?>
')">
		<?php 
}
?>
	</div>