<?php

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
function pr($name){
     echo'<pre>';
    print_r($name);
    echo '</pre>';
}

// [xs_main_tab]   
function xs_main_tab($atts) {
    extract(shortcode_atts(array(
                    ), $atts));
    global $within_section;
    $html = "";
    $schedule = "";
    $exe = "";
    $gallery = "";
    $aKlass = ' class="active"';
    global $post;

    
    
   
    /* Schedule */
    $term_id = xs_get_option('tab_options', 'schedule_group');
    $taxonomy_name = 'xs_schedules';
    $termchildren = get_term_children($term_id, $taxonomy_name);

//   pr($termchildren)
    
    foreach ($termchildren as $child) {
        $term = get_term_by('id', $child, $taxonomy_name);

// WP_Query arguments
        $args = array(
            'post_type' => 'xs_schedule',
            'xs_schedules' => $term->slug,
            'orderby' => 'menu_order',
            'order' => 'ASC'
        );

// The Query
        $query = new WP_Query($args);

        $schedule .= '<div class="col-md-4 col-sm-12 schedule-box">
  <h6 class="section-head"><span>' . $term->name . '</span> ' . get_tax_meta($term->term_id, 'xs_text_field_id') . '</h6>
  <ul class="activities">';
// The Loop
        if ($query->have_posts()) {
            while ($query->have_posts()) {
                $query->the_post();
                $schedule .= '<li>
	  <h6><strong>' . get_post_meta($post->ID, 'xs_schedule_time', true) . '</strong> - ' . $post->post_title . ' </h6>
	  <p>' . get_the_content() . '</p>
	</li>';
            }
            $schedule .= '</ul>
</div>';
        }
// Restore original Post Data
        //wp_reset_postdata();
    }


    
    
    
    
    
    /* Gallery */
    if (xs_get_option('tab_options', 'gal_gal') != '') {
        $g_array = explode(',', xs_get_option('tab_options', 'gal_gal'));

        foreach ($g_array as $v) {
            $img = wp_get_attachment_image_src($v, 'full'); // returns an array
            $thumb = wp_get_attachment_image_src($v, 'gallery-thumb'); // returns an array
            $caption = get_post($v)->post_excerpt; // returns an array

            if (!empty($thumb) || $thumb != '') {
                $gallery .= '<li><a href="' . $img[0] . '" title="Event PhotoGallery" data-rel="prettyPhoto[gallery]">
			<img src="' . $thumb[0] . '" alt="' . get_the_title($v) . '" class="img-responsive img-edge" />
			<div class="img-caption"><span>' . $caption . '</span></div>
			</a></li>';
            }
        }
    } else {
        $gallery .= '<strong>No gallery has been selected</strong>';
    }
    /* Executives */
    $e_array = xs_get_option('tab_options', 'exe_executives', array());

    foreach ($e_array as $v) {
        $exe .= '<li><strong>' . $v['title'] . '</strong> - ' . $v['job'] . '</li>';
    }
    $iframe = 'iframe';
    /* Nav tabs */
    $html .= '<ul class="nav nav-tabs clearfix">';
    if (xs_get_option('tab_options', 'show_schedule') != 'off') {
        $html .= '<li ' . $aKlass . '>
		<a href="#event-schedule" data-toggle="tab">
			<i class="fa ' . xs_get_option('tab_options', 'schedule_icon') . '"></i>
			<h5>' . sprintf(xs_get_option('tab_options', 'schedule_title'), '', '') . '<small>' . xs_get_option('tab_options', 'schedule_sub_title') . '</small></h5>
		</a>
	</li>';
        $aKlass = '';
    }
    if (xs_get_option('tab_options', 'show_video') != 'off') {
        $html .= '<li' . $aKlass . '>
		<a href="#promo" data-toggle="tab">
			<i class="fa ' . xs_get_option('tab_options', 'tab_video_icon') . '"></i>
			<h5>' . sprintf(xs_get_option('tab_options', 'video_title'), '', '') . '<small>' . xs_get_option('tab_options', 'video_sub_title') . '</small></h5>
		</a>
	</li>';
        $aKlass = '';
    }
    if (xs_get_option('tab_options', 'show_gallery') != 'off') {
        $html .= '<li' . $aKlass . '>
		<a href="#event-gallery" data-toggle="tab">
			<i class="fa ' . xs_get_option('tab_options', 'tab_gal_icon') . '"></i>
			<h5>' . sprintf(xs_get_option('tab_options', 'gal_title'), '', '') . '<small>' . xs_get_option('tab_options', 'gal_sub_title') . '</small></h5>
		</a>
	</li>';
        $aKlass = '';
    }
    if (xs_get_option('tab_options', 'show_exe') != 'off') {
        $html .= '<li' . $aKlass . '>
		<a href="#exec-presence" data-toggle="tab">
			<i class="fa ' . xs_get_option('tab_options', 'tab_exe_icon') . '"></i>
			<h5>' . sprintf(xs_get_option('tab_options', 'exe_title'), '', '') . '<small>' . xs_get_option('tab_options', 'exe_sub_title') . '</small></h5>
		</a>
	</li>';
    }
    $html .= '</ul>';
    $aKlass = ' active';



    /* Tab panes */

    $html .= '<div class="tab-content">';
    if (xs_get_option('tab_options', 'show_schedule') != 'off') {
        $html .= '<div class="tab-pane ' . $aKlass . '" id="event-schedule">
		<h3><i class="fa fa-clock-o"></i> ' . sprintf(xs_get_option('tab_options', 'schedule_title'), '<span>', '</span>') . '</h3>
		' . xs_get_option('tab_options', 'schedule_desc') . '
		<div class="row clearfix">' . $schedule . '</div>
		<!--end-of-row--> 
	</div>
	<!--end-event-schedule-content-->';
        $aKlass = '';
    }
    if (xs_get_option('tab_options', 'show_video') != 'off') {
        if (xs_get_option('tab_options', 'video_url') != '') {
            $html .= '<div class="tab-pane' . $aKlass . '" id="promo">
		<h3><i class="fa fa-play-circle"></i> ' . sprintf(xs_get_option('tab_options', 'video_title'), '<span>', '</span>') . '</h3>
		<' . $iframe . ' width="425" height="250" src="' . xs_get_option('tab_options', 'video_url') . '" allowfullscreen></' . $iframe . '>
	</div>
	<!--end-event-schedule-content-->';
        } else {
            $html .= '<div class="tab-pane' . $aKlass . '" id="promo">
		<h3><i class="fa fa-play-circle"></i> ' . sprintf(xs_get_option('tab_options', 'video_title'), '<span>', '</span>') . '</h3>' . xs_get_option('tab_options', 'video_textarea') . '</div>
	<!--end-event-schedule-content-->';
        }
        $aKlass = '';
    }
    if (xs_get_option('tab_options', 'show_gallery') != 'off') {
        $html .= '<div class="tab-pane' . $aKlass . '" id="event-gallery">
		<h3><i class="fa fa-picture-o"></i> ' . sprintf(xs_get_option('tab_options', 'gal_title'), '<span>', '</span>') . '</h3>
		' . xs_get_option('tab_options', 'gal_desc') . '
		<div class="img-grid clearfix">
			<ul id="eg-thumbs" class="eg-thumbs clearfix">' . $gallery . '</ul>
		</div>
		<!--end-img-grid--> 
	</div>
	<!--end-event-schedule-content-->';
        $aKlass = '';
    }
    if (xs_get_option('tab_options', 'show_exe') != 'off') {
        $html .= '<div class="tab-pane' . $aKlass . '" id="exec-presence">
		<h3><i class="fa fa-star"></i> ' . sprintf(xs_get_option('tab_options', 'exe_title'), '<span>', '</span>') . '</h3>
		' . xs_get_option('tab_options', 'exe_desc') . '
		<ul class="li-blocks clearfix">' . $exe . '</ul>
	</div>
	<!--end-event-schedule-content--> ';
        $aKlass = '';
    }
    $html .= '</div>';


    if (isset($within_section) == 'y') {
        return '<div id="schedule" class="schedule_section"><div class="section no-padding clearfix">' . $html . '</div></div>';
    } else {
        return '<div id="schedule" class="no-paddingbox"><div class="clearfix">' . $html . '</div></div>';
    }
}

add_shortcode('xs_main_tab', 'xs_main_tab');
