<?php

class Intense_Collapse extends Intense_Shortcode {

	function __construct() {
		parent::__construct();
		
		global $intense_visions_options;

		$this->title = __( 'Collapse', 'intense' );
		$this->category = __( 'Layout', 'intense' );
		$this->show_preview = true;
		$this->hidden = true;
		$this->parent = 'intense_collapsibles';
		$this->is_container = true;		
	}

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

		global $intense_collapsible_icon_settings,
		$intense_collapsible_title_border, $intense_collapsible_content_border;

		$output = '';		
		$title_font_color_style = '';
		$content_font_color_style = '';

		$border_size = $intense_visions_options['intense_content_box_style']['border-top'];
		$border_color = $intense_visions_options['intense_content_box_style']['border-color'];
		$border_style = $intense_visions_options['intense_content_box_style']['border-style'];
		$border = 'border-bottom:' . $border_size . ' ' . $border_style . ' ' . $border_color . ';';

		$title_border_style = '';
		$content_border_style = '';

		if ( !empty ( $intense_collapsible_title_border ) ) {
			$title_border_style = "border: " . $intense_collapsible_title_border . ' !important;';
		}

		if ( !empty ( $intense_collapsible_content_border ) ) {
			$content_border_style = "border: " . $intense_collapsible_content_border . ' !important;';
			$border = '';
		}

		$title_background_color = intense_get_plugin_color( $title_background_color );
		$content_background_color = intense_get_plugin_color( $content_background_color );
		$title_font_color = intense_get_plugin_color( $title_font_color );

		if ( empty( $title_font_color ) ) {
			$title_font_color = intense_get_contract_color( $title_background_color );
		}

		$content_font_color = intense_get_plugin_color( $content_font_color );

		if ( empty( $content_font_color ) ) {
			$content_font_color = intense_get_contract_color( $content_background_color );
		}

		if ( !empty( $title_background_color ) ) {
			$title_background_color = 'background-color: ' . $title_background_color . ';';
		}

		if ( !empty( $content_background_color ) ) {
			$content_background_color = 'background-color: ' . $content_background_color . ';';
		}		

		if ( !empty( $title_font_color ) ) $title_font_color_style = 'color: ' . $title_font_color . ' !important;';
		if ( !empty( $content_font_color ) ) $content_font_color_style = 'color: ' . $content_font_color . ' !important;';

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

		$icon_color = intense_get_plugin_color( $icon_color );

		$intense_collapsible_expand_icon = intense_run_shortcode( 'intense_icon', array( 'source' => $intense_collapsible_icon_settings['expand_icon_source'], 'type' => $intense_collapsible_icon_settings['expand_icon'], 'extra_class' => 'expand-icon hide', 'size' => $intense_collapsible_icon_settings['icon_size'], 'color' => $icon_color ) );
		$intense_collapsible_collapse_icon = intense_run_shortcode( 'intense_icon', array( 'source' => $intense_collapsible_icon_settings['collapse_icon_source'], 'type' => $intense_collapsible_icon_settings['collapse_icon'], 'extra_class' => 'collapse-icon hide', 'size' => $intense_collapsible_icon_settings['icon_size'], 'color' => $icon_color ) );

		if ( $active ) {
			$active = 'in';
			$collapse_icon = str_replace( "hide", "", $intense_collapsible_collapse_icon );
			$expand_icon = $intense_collapsible_expand_icon;
		} else {
			$expand_icon = str_replace( "hide", "", $intense_collapsible_expand_icon );
			$collapse_icon = $intense_collapsible_collapse_icon;
		}

		$output = '<div class="intense panel ' . $class . '">';
		$output .= '	<div class="intense panel-heading" style="' . $title_border_style . $title_background_color . $title_font_color_style . '">';

		if ( !empty( $external_link ) ) {
			if ( !empty( $external_link_rel ) ) $external_link_rel = ' rel="' . $external_link_rel . '"';

			if ( is_numeric( $external_link ) ) {
				$external_link = get_permalink( $external_link );
			}

			$output .= '	<a class="intense faq-title"' . $external_link_rel . ' href="' . $external_link . '" target="' . $external_link_target . '" style="text-decoration: none; ' . $title_font_color_style . '">';			
		}

		$output .= '	' . $collapse_icon . $expand_icon;

		if ( empty( $external_link ) ) {		
			$output .= '	<a class="intense panel-title accordion-toggle" data-parent="#accordion-' . $GLOBALS['collapsibles_count'] . '" href="#collapse_' . $GLOBALS['current_collapse'] . '_'. sanitize_title( $title ) .'" style="text-decoration: none; ' . $title_font_color_style . '">';			
		}

		$output .= '	' . $title . '';
		$output .= '	</a>';

		$output .= '	</div>';
		$output .= '	<div id="collapse_' . $GLOBALS['current_collapse'] . '_'. sanitize_title( $title ) .'" class="intense panel-collapse collapse ' . $active . '" style="' . $border . $content_border_style . $content_background_color . $content_font_color_style . '";">';
		$output .= '		<div class="intense panel-body">';
		$output .= '		' . do_shortcode( $content ) . '';
		$output .= '		</div>';
		$output .= '	</div>';
		$output .= '</div>';

		return $output;
	}
}