<?php

class Intense_Collapsibles extends Intense_Shortcode {

	function __construct() {
        parent::__construct();

		global $intense_visions_options;

		$this->title = __( 'Collapsibles', 'intense' );
		$this->description = __( 'Add a collapsible box with a title using the collapsibles shortcode. Often referred to as an accordion, the collapsibles shortcode is useful for adding content that you want the user to be able to toggle. Collapsibles allow for simple layouts where the content would be otherwise overwhelming. Users can choose which items they want to see by expanding/collapsing items.
			<br><br>
			There are many different options available to control the styles of the title and content area. The collapse and expand buttons can be customized. You can also expand items by default and control if only one item can be open at once.', 'intense' );
		$this->category = __( 'Layout', 'intense' );
		$this->show_preview = true;
		$this->is_container = true;
	}

	function shortcode( $atts, $content = null ) {
		global $intense_visions_options;
		extract( shortcode_atts( $this->get_shortcode_defaults( null, $atts ), $atts ) );

		if ( isset( $GLOBALS['collapsibles_count'] ) )
			$GLOBALS['collapsibles_count']++;
		else
			$GLOBALS['collapsibles_count'] = 0;

		intense_add_script( 'intense.collapsible' );

		global $intense_collapsible_icon_settings, $intense_collapsible_title_border, $intense_collapsible_content_border;

		$intense_collapsible_icon_settings = array(
			'expand_icon_source' => $expand_icon_source, 
			'expand_icon' => $expand_icon,
			'collapse_icon_source' => $collapse_icon_source, 
			'collapse_icon' => $collapse_icon,
			'icon_size' => $icon_size,
		);
		
		$intense_collapsible_title_border = $title_border;
		$intense_collapsible_content_border = $content_border;

		// Extract the tab titles for use in the tab widget.
		preg_match_all( '/collapse *title *= *["\']([^\"\']+)"/i', $content, $matches, PREG_OFFSET_CAPTURE );

		$collapse_titles = array();
		if ( isset( $matches[1] ) ) { $collapse_titles = $matches[1]; }

		$output = '';

		if ( count( $collapse_titles ) ) {
			$output .= '<div class="intense panel-group" id="' . $id . '-accordion-' . $GLOBALS['collapsibles_count'] . '" data-single-toggle="' . $single_toggle . '">';
			$output .= do_shortcode( $content );
			$output .= '</div>';
		} else {
			$output .= do_shortcode( $content );
		}

		return $output;
	}	
}
