function pew_chart_metabox_content()
{
    global $post;
    wp_nonce_field(plugin_basename(__FILE__), 'pew_chart_metabox_content');
    //$post_meta = get_post_meta( $post->ID );
    $post_meta = get_pew_chart_meta($post->ID);
    ?>

    <div class="custom_metabox">

    	<table class="form-table">
    	<tr><td>
		<label for="charttype" class="prfx-row-title">Chart Type</label>
		</td><td>
	    <select name="charttype" id="charttype">
	    	<option value="line" <?php 
    selected('line', $post_meta['charttype']);
    ?>
>Line</option>
	        <option value="bar" <?php 
    selected('bar', $post_meta['charttype']);
    ?>
>Bar</option>
	        <option value="column" <?php 
    selected('column', $post_meta['charttype']);
    ?>
>Column</option>
	        <option value="area" <?php 
    selected('area', $post_meta['charttype']);
    ?>
>Area</option>
	        <option value="pie" <?php 
    selected('pie', $post_meta['charttype']);
    ?>
>Pie</option>
	        <option value="scatter" <?php 
    selected('scatter', $post_meta['charttype']);
    ?>
>Scatter</option>
	    </select>
		</td></tr>
		<tr><td>
		<label for="chartheight" class="prfx-row-title">Chart Height</label>
		</td><td>
		<input type="text" name="chartheight" id="chartheight" value="<?php 
    echo $post_meta['chartheight'];
    ?>
" placeholder="Default: 400px" />
		</td></tr>
		<tr><td>
		<label for="chartsubtitle" class="prfx-row-title">Chart Subtitle</label>
		</td><td>
		<input type="text" name="chartsubtitle" id="chartsubtitle" value="<?php 
    echo $post_meta['chartsubtitle'];
    ?>
" />
		</td></tr>
		<tr><td>
		<label for="xaxistype">x-Axis Type</label>
		</td><td>
		<select name="xaxistype" id="xaxistype">
			<option value="linear" <?php 
    selected('linear', $post_meta['xaxistype']);
    ?>
>Linear</option>
	    	<option value="datetime" <?php 
    selected('datetime', $post_meta['xaxistype']);
    ?>
>Date/Time</option>
		</select>
		</td></tr>
		<tr><td>
	    <label for="xaxislabel">x-Axis Label</label>
		</td><td>
		<input type="text" name="xaxislabel" id="xaxislabel" value="<?php 
    echo $post_meta['xaxislabel'];
    ?>
" />
		</td></tr>
		<tr><td>
	    <label for="yaxislabel">y-Axis Label</label>
		</td><td>
		<input type="text" name="yaxislabel" id="yaxislabel" value="<?php 
    echo $post_meta['yaxislabel'];
    ?>
" />
	    </td></tr>
		<tr><td>
	    <label for="yaxismax">y-Axis Max Value</label>
		</td><td>
		<input type="text" name="yaxismax" id="yaxismax" value="<?php 
    echo $post_meta['yaxismax'];
    ?>
" />
	    </td></tr>
		<tr><td>
		<label for="zoomtype">Chart Zooming</label>
	    </td><td>
	    <select name="zoomtype" id="zoomtype">
			<option value="none" <?php 
    selected('none', $post_meta['zoomtype']);
    ?>
>None</option>
	    	<option value="x" <?php 
    selected('x', $post_meta['zoomtype']);
    ?>
>x-Axis Only</option>
			<option value="y" <?php 
    selected('y', $post_meta['zoomtype']);
    ?>
>y-Axis Only</option>
			<option value="xy" <?php 
    selected('xy', $post_meta['zoomtype']);
    ?>
>x &amp; y-Axis</option>
	    </select>
		</td></tr>
		<tr><td>
	    <label for="credits">Source Credits</label>
		</td><td>
		<input type="text" name="credits" id="credits" value="<?php 
    echo $post_meta['credits'];
    ?>
" />
		</td></tr>
		<tr><td>
	    <label for="credits_link">Source Link</label>
		</td><td>
		<input type="text" name="credits_link" id="credits_link" value="<?php 
    echo $post_meta['credits_link'];
    ?>
" />
		</td></tr>
		<tr><td colspan="2">
	    <label for="inverted"><input type="checkbox" name="inverted" id="inverted" value="true" style="width:auto;" <?php 
    checked('true', $post_meta['inverted']);
    ?>
 /> Inverted</label>
	    <p>Whether to invert the axes so that the x axis is horizontal and y axis is vertical.</p>
	    </td></tr>
		<tr><td colspan="2">
	    <label for="seriesstacking"><input type="checkbox" name="seriesstacking" id="seriesstacking" value="true" style="width:auto;" <?php 
    checked('true', $post_meta['seriesstacking']);
    ?>
 /> Stack multiple series?</label>
	    <p>If not stacking, bar and column charts will show series next to each other; area charts will overlap.</p>
	    </td></tr>
		<tr><td colspan="2">
		<label for="hidemarkers"><input type="checkbox" name="hidemarkers" id="hidemarkers" value="true" style="width:auto;" <?php 
    checked('true', $post_meta['hidemarkers']);
    ?>
 /> Hide Markers</label>
	    <p>When checked, the circular points on a line graph will be hidden. Reccomended if there are a lot of data points.</p>
	    </td></tr>
		<tr><td colspan="2">
		<label for="iframe"><input type="checkbox" name="iframe" id="iframe" value="true" style="width:auto;" <?php 
    checked('true', $post_meta['iframe']);
    ?>
 /> Allow iframe?</label>
	    <p>This will display an extra tab above the chart displaying embed code; it can be set my default in the <a href="<?php 
    echo admin_url('options-general.php?page=pew-charts');
    ?>
">plugin settings</a>.</p>
	    </td></tr>
	    <?php 
    // If is_admin
    ?>
		<tr><td colspan="2">
		<p>
		<label for="args">Custom Args</label>
		</p>
		<p>
		<?php 
    $json = '';
    if ($post_meta['args'] != '') {
        if (version_compare(phpversion(), '5.3.0', '>=')) {
            $array = json_decode($post_meta['args'], true, 10);
        } else {
            $array = json_decode($post_meta['args'], true);
        }
        if (is_array($array)) {
            if (version_compare(phpversion(), '5.4.0', '>=')) {
                $json = json_encode($array, JSON_PRETTY_PRINT);
            } else {
                $json = json_encode($array);
            }
        }
    }
    ?>
	    <textarea name="args" id="args" style="width:100%; height:100px;" <?php 
    if (!current_user_can('edit_theme_options')) {
        echo 'readonly';
    }
    ?>
><?php 
    echo $json;
    ?>
</textarea>
    	<?php 
    if ($post_meta['args'] && !is_array($array)) {
        echo '<div class="error"><p>The custom JSON in this chart is not properly formatted. Please make sure all keys and values are in double quotes.</p></div>';
    } else {
    }
    ?>
	    </p>
	    </td></tr>
	    </table>
    </div>
    <?php 
}
function pew_chart_prep_chart_options($chart = FALSE)
{
    global $pew_chart_options;
    //Get site wide defaults
    $site_options = get_option('pew_charts');
    if (isset($site_options['defaults']) && !empty($site_options['defaults'])) {
        $default_options = json_decode($site_options['defaults'], true);
    }
    if (!$default_options) {
        $default_options = array();
    }
    //$chart is a $post object
    if (!$chart) {
        $site_options['waypoints'] = false;
        $chart = get_post();
    }
    //Get chart options
    $options = get_pew_chart_meta($chart->ID);
    if (isset($options['args']) && !empty($options['args'])) {
        if (version_compare(phpversion(), '5.3.0', '>=')) {
            $custom_chart_options = json_decode($options['args'], true, 10);
        } else {
            $custom_chart_options = json_decode($options['args'], true);
        }
    }
    if (!$custom_chart_options) {
        $custom_chart_options = array();
    }
    $chart_options = array('chart' => array(), 'subtitle' => array(), 'xAxis' => array('title' => array()), 'yAxis' => array('title' => array()), 'plotOptions' => array('line' => array('marker' => array('enabled' => true)), 'area' => array('marker' => array('enabled' => true)), 'bar' => array('stacking' => null), 'column' => array('stacking' => null), 'pie' => array('visible' => true, 'dataLabels' => array('enabled' => true))));
    if ($options['charttype']) {
        $chart_options['chart']['type'] = $options['charttype'];
    }
    if ($options['zoomtype'] && $options['zoomtype'] != 'none') {
        $chart_options['chart']['zoomType'] = $options['zoomtype'];
    }
    if ($options['chartsubtitle'] && $options['chartsubtitle'] != 'none') {
        $chart_options['subtitle']['text'] = $options['chartsubtitle'];
    }
    if ($options['seriesstacking'] == true) {
        $chart_options['plotOptions']['area']['stacking'] = 'normal';
        $chart_options['plotOptions']['bar']['stacking'] = 'normal';
        $chart_options['plotOptions']['column']['stacking'] = 'normal';
        $chart_options['plotOptions']['line']['stacking'] = 'normal';
    }
    if ($options['inverted'] && $options['inverted'] != 'none') {
        $chart_options['chart']['inverted'] = true;
    }
    if ($options['xaxislabel'] && $options['xaxislabel'] != 'none') {
        $chart_options['xAxis']['title']['text'] = $options['xaxislabel'];
    }
    if ($options['yaxislabel'] && $options['yaxislabel'] != 'none') {
        $chart_options['yAxis']['title']['text'] = $options['yaxislabel'];
    }
    if ($options['yaxismax'] && $options['yaxismax'] != 'none') {
        $chart_options['yAxis']['max'] = $options['yaxismax'];
    }
    if ($options['xaxistype'] && $options['xaxistype'] != 'none') {
        $chart_options['xAxis']['type'] = $options['xaxistype'];
    }
    if ($options['hidemarkers']) {
        $chart_options['plotOptions']['line']['marker']['enabled'] = false;
        $chart_options['plotOptions']['area']['marker']['enabled'] = false;
    }
    $chart_options['html'] = array('waypoints' => $site_options['waypoints'], 'data_tab' => _('Data'), 'chart_tab' => _('Chart'), 'height' => $options['chartheight'] && preg_match('/[0-9]+([px]{2}|)$/i', $options['chartheight']) ? str_replace(array('px', 'PX'), '', $options['chartheight']) . 'px' : '400px', 'iframe' => $options['iframe'], 'iframe_tab' => _('Embed'), 'iframe_text' => _('Copy and paste the below iframe code into your own website to embed this chart.'), 'URL' => get_permalink($chart->ID), 'id' => $chart->ID, 'domain' => get_site_url(), 'credits' => $options['credits'] ? $options['credits'] : $site_options['credits'], 'creditsURL' => $options['credits_link'] ? $options['credits_link'] : $site_options['credits_link'], 'creditText' => _('Source: '));
    $js_options = array_replace_recursive((array) $default_options, $chart_options, (array) $custom_chart_options);
    $js_options = apply_filters('pew_chart_options', $js_options, $chart);
    $pew_chart_options[] = json_encode($js_options);
}