<?php
// Post & Page Options
add_action("admin_init", "serpentsoft_post_meta");

function serpentsoft_post_meta() {
    $types = array('post', 'page');

    foreach ($types as $type) {
//	add_meta_box("post_review_options", sprintf( __( '%s - Post Reviews Options.', 'serpentsoft' ) , theme_name ), "serpentsoft_post_review_options_meta_box", $type, "normal", "high");

        add_meta_box("post_options", sprintf(__('%s - Post Options.', 'serpentsoft'), theme_name), "serpentsoft_post_options_meta_box", $type, "normal", "high");
    }
}

function serpentsoft_post_options_meta_box() {
    global $post;
    $get_meta = get_post_custom($post->ID);

    $serpentsoft_page_layout = 'default';
    $serpentsoft_review_item = '';
    
    if( isset($get_meta) ):
        if( isset($get_meta["serpentsoft_page_layout"][0]) ){
            $serpentsoft_page_layout = ($get_meta["serpentsoft_page_layout"][0]);
        }
        
        if( isset($get_meta["serpentsoft_review_item"][0]) ){
            $serpentsoft_review_item = unserialize($get_meta["serpentsoft_review_item"][0]);
        }
    endif; ?>

    <div class="serpentsoft_post_options">
        <h3>Review Options</h3>

        <?php serpentsoft_post_options(
                array("name" => __("Review", 'serpentsoft'),
                    "id" => "serpentsoft_post_review_types",
                    "type" => "select",
                    "options" => array(
                        'disabled' => 'Disabled',
                        'percent' => 'Percentage',
                        'stars' => 'Stars',
                        'points' => 'Points',
        ))); ?>

        <div id="reviews-options">

            <?php serpentsoft_post_options(
                    array("name" => __("Review Position", 'serpentsoft'),
                        "id" => "serpentsoft_post_review_position",
                        "type" => "select",
                        "options" => array(
                            'top' => 'Top Of The Post',
                            'bottom' => 'Bottom Of The Post',
                            /* 'topbottom' => 'Top and Bottom Of The Post' */ /* Cancelled: Because of Rich Snippet */
            )));

            // Summary On The Top
            serpentsoft_post_options(
                    array("name" => __("Review Title", 'serpentsoft'),
                        "id" => "serpentsoft_post_review_title",
                        "type" => "text"));

            serpentsoft_post_options(
                    array("name" => __("Review Description", 'serpentsoft'),
                        "id" => "serpentsoft_post_review_desc",
                        "type" => "textarea"));

            // Summary On The Bottom
            serpentsoft_post_options(
                    array("name" => __("Review Summary Title (Default: Summary)", 'serpentsoft'),
                        "id" => "serpentsoft_post_review_summary_title",
                        "type" => "text"));

            serpentsoft_post_options(
                    array("name" => __("Review Summary Description", 'serpentsoft'),
                        "id" => "serpentsoft_post_review_summary_desc",
                        "type" => "textarea"));

            // Create Review Criteria
            serpentsoft_post_options(
                    array("name" => __("Review Criterias", 'serpentsoft'),
                        "id" => "serpentsoft_post_review",
                        "type" => "review",
            )); ?>
        </div>
    </div>

    <div class="serpentsoft_post_options">
        <h3>Header Options</h3>

        <?php serpentsoft_post_options(
                array("name" => __("Post Format", 'serpentsoft'),
                    "id" => "serpentsoft_post_format",
                    "type" => "select",
                    "options" => array(
                        'default' => 'Default',
                        'image' => 'Featured Image',
                        'video' => 'Video',
                        'soundcloud' => 'SoundCloud',
                        'map' => 'Google Map',
                        'quotation' => 'Quotation',
                        'selfaudio' => 'Self Hosted Audio',
                        'selfvideo' => 'Self Hosted Video',
                        'url' => 'URL',
                        'none' => 'None'
        ))); ?>

        <div id="serpentsoft_post_formats_container">

            <div id="post_video">
                <?php serpentsoft_post_options(
                        array("name" => __("Select Video Provider", 'serpentsoft'),
                            "id" => "serpentsoft_video_provider",
                            "type" => "select",
                            "options" => array(
                                'youtube' => 'Youtube',
                                'vimeo' => 'Vimeo',
                                'dailymotion' => 'Dailymotion',
                                'embedcode' => 'Embed Code'
                ))); ?>
                <div id="post_video_provider_code">

                <?php serpentsoft_post_options(
                        array("name" => __("Enter Video Code Only", 'serpentsoft'),
                            "id" => "serpentsoft_video_url_code",
                            "type" => "text"
                )); ?>

                </div>

                <div id="post_video_provider_embed_code">                

                    <?php serpentsoft_post_options(
                        array("name" => __("Embed Code", 'serpentsoft'),
                            "id" => "serpentsoft_video_embed_code",
                            "type" => "textarea")); ?>

                </div>

            </div>

            <div id="post_soundcloud">
                <?php serpentsoft_post_options(
                        array("name" => __("SoundCloud URL", 'serpentsoft'),
                            "id" => "serpentsoft_soundcloud_url",
                            "type" => "text"));

                serpentsoft_post_options(
                        array("name" => __("Auto Play", 'serpentsoft'),
                            "id" => "serpentsoft_soundcloud_autoplay",
                            "type" => "select",
                            "options" => array(
                                'false' => 'False',
                                'true' => 'True'
                )));

                serpentsoft_post_options(
                        array("name" => __("Art Work", 'serpentsoft'),
                            "id" => "serpentsoft_soundcloud_artwork",
                            "type" => "select",
                            "options" => array(
                                'true' => 'True',
                                'false' => 'False'
                ))); ?>

            </div>


            <div id="post_googlemap">
                <?php serpentsoft_post_options(
                        array("name" => __("Google Map URL", 'serpentsoft'),
                            "id" => "serpentsoft_googlemap_url",
                            "type" => "text"
                )); ?>
            </div>

            <div id="post_quotation">
                <?php serpentsoft_post_options(
                        array("name" => __("Quotation Text", 'serpentsoft'),
                            "id" => "serpentsoft_quote_text",
                            "type" => "textarea"));

                serpentsoft_post_options(
                        array("name" => __("Quotation Author", 'serpentsoft'),
                            "id" => "serpentsoft_quote_author",
                            "type" => "text")); ?>
            </div>


            <div id="post_self_audio">
                <?php serpentsoft_post_options(
                        array("name" => __("MP3 File URL", 'serpentsoft'),
                            "id" => "serpentsoft_self_audio_mp3_url",
                            "type" => "text"
                ));
                serpentsoft_post_options(
                        array("name" => __("OGA File URL", 'serpentsoft'),
                            "id" => "serpentsoft_self_audio_oga_url",
                            "type" => "text"
                ));
                serpentsoft_post_options(
                        array("name" => __("M4A File URL", 'serpentsoft'),
                            "id" => "serpentsoft_self_audio_m4a_url",
                            "type" => "text"
                )); ?>
            </div>

            <div id="post_self_video">
                <?php serpentsoft_post_options(
                        array("name" => __("M4V File URL", 'serpentsoft'),
                            "id" => "serpentsoft_self_video_m4v_url",
                            "type" => "text"
                ));
                serpentsoft_post_options(
                        array("name" => __("OGV File URL", 'serpentsoft'),
                            "id" => "serpentsoft_self_video_ogv_url",
                            "type" => "text"
                ));
                serpentsoft_post_options(
                        array("name" => __("WEBMV File URL", 'serpentsoft'),
                            "id" => "serpentsoft_self_video_webmv_url",
                            "type" => "text"
                ));
                serpentsoft_post_options(
                        array("name" => __("Poster File URL", 'serpentsoft'),
                            "id" => "serpentsoft_self_video_poster_url",
                            "type" => "text"
                )); ?>
            </div>

            <div id="post_url">
                <?php serpentsoft_post_options(
                    array("name" => __("URL Text", 'serpentsoft'),
                        "id" => "serpentsoft_url_text",
                        "type" => "text"
                ));

                serpentsoft_post_options(
                    array("name" => __("URL", 'serpentsoft'),
                        "id" => "serpentsoft_url_destination",
                        "type" => "text")); ?>
            </div>

            <div id="post_bannerCaption">
            <?php serpentsoft_post_options(
                    array("name" => __("Post Banner Caption (Optional)", 'serpentsoft'),
                        "id" => "serpentsoft_post_banner_caption",
                        "type" => "text"
            )); ?>
            </div>

            <div id="post_banner_lightbox">
            <?php serpentsoft_post_options(
                    array("name" => __("Open in Popup Lightbox?", 'serpentsoft'),
                        "id" => "serpentsoft_lightbox_check",
                        "type" => "checkbox"
            )); ?>
            </div>
            
        </div> <!-- End Of The Post Formats Whole Container -->

    </div> <!-- End Of The Post Head Options Whole Container -->


    <div class="serpentsoft_post_options">
        <h3>Page Layout</h3>

        <?php $page_layout_options_panel = serpentsoft_get_data('serpentsoft_pagelayout'); ?>
        
        <div class="option-item">
            <?php $checked = 'checked="checked"'; ?>
            <ul id="page-layout-options" class="serpentsoft-pagelayout-options">
                
                <li>
                    <input id="serpentsoft_page_layout_default"  name="serpentsoft_page_layout" type="radio" value="default" <?php if ($serpentsoft_page_layout == 'default' || !$serpentsoft_page_layout) echo $checked; ?> />
                    <a class="checkbox-select" href="#"><img src="<?php echo get_template_directory_uri(); ?>/admin/backend//images/layout-default.png" /></a>
                </li>

                <?php if( $page_layout_options_panel == 'left' || $page_layout_options_panel == 'middle' || $page_layout_options_panel == 'right' ){ ?>
                
                <li>
                    <input id="serpentsoft_page_layout_left"  name="serpentsoft_page_layout" type="radio" value="left" <?php if ($serpentsoft_page_layout == 'left') echo $checked; ?> />
                    <a class="checkbox-select" href="#"><img src="<?php echo get_template_directory_uri(); ?>/admin/backend//images/layout-left.png" /></a>
                </li>

                <li>
                    <input id="serpentsoft_page_layout_middle"  name="serpentsoft_page_layout" type="radio" value="middle" <?php if ($serpentsoft_page_layout == 'middle') echo $checked; ?> />
                    <a class="checkbox-select" href="#"><img src="<?php echo get_template_directory_uri(); ?>/admin/backend//images/layout-middle.png" /></a>
                </li>

                <li>
                    <input id="serpentsoft_page_layout_right" name="serpentsoft_page_layout" type="radio" value="right" <?php if ($serpentsoft_page_layout == 'right') echo $checked; ?> />
                    <a class="checkbox-select" href="#"><img src="<?php echo get_template_directory_uri(); ?>/admin/backend//images/layout-right.png" /></a>
                </li>

                <?php } elseif( $page_layout_options_panel == '2cols-left' || $page_layout_options_panel == '2cols-right' ){ ?>
                
                <li>
                    <input id="serpentsoft_page_layout_2cols_left" name="serpentsoft_page_layout" type="radio" value="2cols-left" <?php if ($serpentsoft_page_layout == '2cols-left') echo $checked; ?> />
                    <a class="checkbox-select" href="#"><img src="<?php echo get_template_directory_uri(); ?>/admin/backend//images/layout-2cols-left.png" /></a>
                </li>
                
                <li>
                    <input id="serpentsoft_page_layout_2cols-right" name="serpentsoft_page_layout" type="radio" value="2cols-right" <?php if ($serpentsoft_page_layout == '2cols-right') echo $checked; ?> />
                    <a class="checkbox-select" href="#"><img src="<?php echo get_template_directory_uri(); ?>/admin/backend//images/layout-2cols-right.png" /></a>
                </li>
                
                <?php } ?>
                
            </ul>
        </div>

<!--serpentsoft_post_options(				
        array(	"name" => __("Layout", 'serpentsoft'),
                        "id" => "serpentsoft_layout",
                        "type" => "select",
                        "options" => array(
                                'middle' => 'Middle Content',
                                'left' => 'Left Content',
                                'right' => 'Right Content'
                        )));
                        -->
    </div>


    <div class="serpentsoft_post_options">
        <h3>General Options</h3>

        <?php
        serpentsoft_post_options(
                array("name" => __("Breadcrumb", 'serpentsoft'),
                    "id" => "serpentsoft_post_breadcrumb",
                    "type" => "select",
                    "options" => array(
                        'show' => 'Show',
                        'hide' => 'Hide'
        )));

        serpentsoft_post_options(
                array("name" => __("Meta Info", 'serpentsoft'),
                    "id" => "serpentsoft_post_meta_info",
                    "type" => "select",
                    "options" => array(
                        'show' => 'Show',
                        'hide' => 'Hide'
        )));

        serpentsoft_post_options(
                array("name" => __("Share Icons", 'serpentsoft'),
                    "id" => "serpentsoft_post_share_box",
                    "type" => "select",
                    "options" => array(
                        'show' => 'Show',
                        'hide' => 'Hide'
        )));

        serpentsoft_post_options(
                array("name" => __("Tags Box", 'serpentsoft'),
                    "id" => "serpentsoft_post_tags",
                    "type" => "select",
                    "options" => array(
                        'show' => 'Show',
                        'hide' => 'Hide'
        )));

        serpentsoft_post_options(
                array("name" => __("Author Box", 'serpentsoft'),
                    "id" => "serpentsoft_post_author_box",
                    "type" => "select",
                    "options" => array(
                        'show' => 'Show',
                        'hide' => 'Hide'
        )));

        serpentsoft_post_options(
                array("name" => __("Related Posts", 'serpentsoft'),
                    "id" => "serpentsoft_related_posts",
                    "type" => "select",
                    "options" => array(
                        'show' => 'Show',
                        'hide' => 'Hide'
        )));

        serpentsoft_post_options(
                array("name" => __("Posts Navigation", 'serpentsoft'),
                    "id" => "serpentsoft_posts_navigation",
                    "type" => "select",
                    "options" => array(
                        'show' => 'Show',
                        'hide' => 'Hide'
        )));
        ?>
    </div>

    <?php
}

function serpentsoft_post_options($value) {
    global $post;
    $get_meta = get_post_custom($post->ID);

    $current_value = '';
    $serpentsoft_review_item = '';
    
    if( isset($get_meta) && isset($get_meta["serpentsoft_review_item"][0]) ):
        $serpentsoft_review_item = unserialize($get_meta["serpentsoft_review_item"][0]);
    endif; ?>
    
    <div class="option-item" id="<?php echo $value['id']; ?>-item">
        <style type="text/css">.checkbox-label { display: inline !important; }</style>
        
        <?php if($value['type'] != 'checkbox'){ ?><span class="label"><?php echo $value['name']; ?></span><?php } ?>
        
        <?php $id = $value['id'];

        if( isset( $get_meta[$id][0] ) ): $current_value = $get_meta[$id][0]; endif;
        
//        try{ $current_value = $get_meta[$id][0]; }
//        catch(Exception $ex){
//            switch ($value['type']) {
//                case 'text':
//                    $current_value = '';
//                break;
//            
//                case 'select':
//                    $current_value = $value['options'][0];
//                    break;
//                
//                case 'textarea':
//                    $current_value = '';
//                    break;
//                
//                case 'review':
//                    $current_value = '';
//                    break;
//                
//                default: $current_value = '';
//                    break;
//            }
                
        switch ($value['type']) {

            case 'text': ?>
                <input  name="<?php echo $value['id']; ?>" id="<?php echo $value['id']; ?>" type="text" value="<?php echo $current_value; ?>" />
            <?php break;

            case 'select': ?>
                <select name="<?php echo $value['id']; ?>" id="<?php echo $value['id']; ?>">
                    <?php foreach ($value['options'] as $key => $option) { ?>
                        <option value="<?php echo $key; ?>" <?php
                        if ($current_value == $key) {
                            echo ' selected="selected"';
                        }
                        ?>><?php echo $option; ?></option>
                <?php } ?>
                </select>
            <?php break;

            case 'textarea': ?>
                <textarea name="<?php echo $value['id']; ?>" id="<?php echo $value['id']; ?>" type="<?php echo $value['type']; ?>" cols="" rows="2"><?php echo $current_value ?></textarea>
            <?php break;

            case 'checkbox': ?>
                <input type="<?php echo $value['type']; ?>" class="" name="<?php echo $value['id']; ?>" id="<?php echo $value['id']; ?>"
                       <?php echo $current_value == 'on' ? 'checked="checked"' : ''; ?> />
                <label class="label checkbox-label" for="<?php echo $value['id']; ?>"><?php echo $value['name']; ?></label>
            <?php break;
            
            case 'review':
                for ($i = 1; $i <= 10; $i++) { ?>

                <div class="option-item review-item">				
                    <span class="label">Criteria <?php echo $i; ?> Title</span>
                    <input name="serpentsoft_review_item[<?php echo $i; ?>][name]" type="text" value="<?php if( isset($serpentsoft_review_item[$i]['name']) ): echo $serpentsoft_review_item[$i]['name']; else: echo ''; endif; ?>" />
                    <div class="clear"></div><br />
                    <span class="label">Score <?php echo $i; ?></span>

                    <div id="criteria<?php echo $i; ?>-slider" class="criteria_slider"></div>
                    <input id="criteria<?php echo $i; ?>" type="text" class="criteria_slider_value" readonly="true" 
                           value="<?php if (isset($serpentsoft_review_item[$i]['score']) && $serpentsoft_review_item[$i]['score']): echo $serpentsoft_review_item[$i]['score']; else: echo 0; endif; ?>" name="serpentsoft_review_item[<?php echo $i ?>][score]" />
                    <script type="text/javascript">
                        //<![CDATA[
                        jQuery(document).ready(function() {
                            "use strict";
                            jQuery("#criteria<?php echo $i; ?>-slider").slider({
                                range: "min",
                                min: 0,
                                max: 100,
                                value: <?php if (isset($serpentsoft_review_item[$i]['score']) && $serpentsoft_review_item[$i]['score']): echo $serpentsoft_review_item[$i]['score']; else: echo 0; endif; ?>,
                                slide: function(event, ui) {
                                    jQuery('#criteria<?php echo $i ?>').attr('value', ui.value);
                                }
                            });
                        });
                        //]]>
                    </script>
                </div>
            <?php } ?>

            <?php
            break;
    }
    ?>
    </div>
    <?php
}

// Save Posts Using Options IDs
add_action('save_post', 'serpentsoft_save_post_options');

function serpentsoft_save_post_options() {
    global $post;

    try{
        $get_meta = get_post_custom($post->ID);

        if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { return $post->ID; }

        $custom_meta_fields = array(
            'serpentsoft_post_review_title',
            'serpentsoft_post_review_desc',
            'serpentsoft_post_review_summary_title',
            'serpentsoft_post_review_summary_desc',
            'serpentsoft_post_review_types',
            'serpentsoft_post_review_position',
            'serpentsoft_post_format',
            // Video Providers
            'serpentsoft_video_provider',
            'serpentsoft_video_url_code',
            'serpentsoft_video_embed_code',
            'serpentsoft_soundcloud_url',
            'serpentsoft_soundcloud_autoplay',
            'serpentsoft_soundcloud_artwork',
            'serpentsoft_googlemap_url',
            'serpentsoft_quote_text',
            'serpentsoft_quote_author',
            'serpentsoft_self_audio_mp3_url',
            'serpentsoft_self_audio_oga_url',
            'serpentsoft_self_audio_m4a_url',
            'serpentsoft_self_video_m4v_url',
            'serpentsoft_self_video_ogv_url',
            'serpentsoft_self_video_webmv_url',
            'serpentsoft_self_video_poster_url',
            'serpentsoft_url_text',
            'serpentsoft_url_destination',
            'serpentsoft_lightbox_check',
            'serpentsoft_post_banner_caption',
            'serpentsoft_page_layout',
            //'serpentsoft_layout',
            'serpentsoft_post_meta_info',
            'serpentsoft_post_breadcrumb',
            'serpentsoft_post_share_box',
            'serpentsoft_post_tags',
            'serpentsoft_post_author_box',
            'serpentsoft_related_posts',
            'serpentsoft_posts_navigation'
        );

        foreach ($custom_meta_fields as $custom_meta_field) {
            if (isset($_POST[$custom_meta_field])):
                update_post_meta($post->ID, $custom_meta_field, htmlspecialchars(stripslashes($_POST[$custom_meta_field])));
            else:
                if (isset($post->ID) && isset($custom_meta_field) && $custom_meta_field != '') {
                    delete_post_meta($post->ID, $custom_meta_field);
                }
            endif;
        }

        /* Check Review Type */
        if( isset($get_meta['serpentsoft_post_review_types'][0]) && $get_meta['serpentsoft_post_review_types'][0] != 'disabled' ){
            
            update_post_meta($post->ID, 'serpentsoft_review_item', $_POST['serpentsoft_review_item']);

            if (isset($get_meta["serpentsoft_review_item"][0]) && $get_meta["serpentsoft_review_item"][0] != '') {

                $serpentsoft_review_item = unserialize($get_meta["serpentsoft_review_item"][0]);
                // Reviews Score Summation
                $reviews_sum = 0;
                $reviews_count = 0;

                foreach ($serpentsoft_review_item as $criteria) {
                    if ( $criteria['name'] && !empty($criteria['name']) ) {
                        $reviews_sum += $criteria['score'];
                        $reviews_count++;
                    }
                }

                if ($reviews_count > 0) {
                    update_post_meta($post->ID, 'serpentsoft_post_reviews_summation', ($reviews_sum / $reviews_count));
                } else {
                    update_post_meta($post->ID, 'serpentsoft_post_reviews_summation', '0');
                }
            }
        }
    
    }
    catch (Exception $exGetMeta){
        $get_meta = NULL;
    }
} ?>