<?php

function theme_shortcode_tabs($atts, $content = null, $code) {
	extract(shortcode_atts(array(
        'style' => false,
        'history' => false,
		'initialtab' => 1
	), $atts));
	
	if (!preg_match_all("/(.?)\[(tab)\b(.*?)(?:(\/))?\](?:(.+?)\[\/tab\])?(.?)/s", $content, $matches)) {
		return do_shortcode($content);
    } else {
        for($i = 0; $i < count($matches[0]); $i++) {
            $matches[3][$i] = shortcode_parse_atts($matches[3][$i]);
        }
        $output = '<ul class="'.$code.'">';

        for($i = 0; $i < count($matches[0]); $i++) {
            if($history=='true'){
				$href= '#tab'.($i+1);
			}else{
				$href = '#';
			}
			$output .= '<li><a href="'.$href.'">' . $matches[3][$i]['title'] . '</a></li>';
        }
        $output .= '</ul>';
        $output .= '<div class="panes">';
        for($i = 0; $i < count($matches[0]); $i++) {
            $output .= '<div class="pane">' . do_shortcode(trim($matches[5][$i])) . '</div>';
        }
        $output .= '</div>';
        
        if($history=='true'){
			$data_history = ' data-history="true"';
		}else{
			$data_history = '';
		}
		if($initialtab!="1"){
			$initialIndex = $initialtab -1;
			$data_initialIndex = ' data-initialIndex="'.$initialIndex.'"';
		}else{
			$data_initialIndex = '';
        }

       return '<div class="'.$code.'_container"'.$data_history.$data_initialIndex.'>' . $output . '</div>';
    }
}
add_shortcode('tabs', 'theme_shortcode_tabs');
add_shortcode('mini_tabs', 'theme_shortcode_tabs');

function theme_shortcode_accordions($atts, $content = null, $code) {
	extract(shortcode_atts(array(
        'style' => false,
        'initialnumber' => 1
	), $atts));
	
	if (!preg_match_all("/(.?)\[(accordion)\b(.*?)(?:(\/))?\](?:(.+?)\[\/accordion\])?(.?)/s", $content, $matches)) {
		return do_shortcode($content);
	} else {
		for($i = 0; $i < count($matches[0]); $i++) {
			$matches[3][$i] = shortcode_parse_atts($matches[3][$i]);
		}
		$output = '';
		$initialIndex=0;
		if($initialnumber!="1"){
			$initialIndex = $initialnumber -1;
		}
		for($i = 0; $i < count($matches[0]); $i++) {
			$current = '';
			$display = 'style="display:none;"';
			if($i == $initialIndex){
				$current = 'current';
				$display = 'style="display:block;"';
			}
				
			$output .= '<div class="tab '.$current.'"> <span class="icon"></span>' . $matches[3][$i]['title'] . '</div>';
			$output .= '<div class="pane" '.$display.'>' . do_shortcode(trim($matches[5][$i])) . '</div>';
		}
		return '<div class="accordion">' . $output . '</div>';
	}
}
add_shortcode('accordions', 'theme_shortcode_accordions');

function theme_shortcode_toggle($atts, $content = null, $code) {
	extract(shortcode_atts(array(
        'title' => false,
        'size' => 'normal',
		'align'=>'left',
		'opening'=>false,
		'opentext'=>'OPEN',
		'closetext'=>'CLOSE',
	), $atts));
	$toggleCss = $opening?'accTogglerOpen':'accToggler';
	$opening = $opening ? '' : 'close';
    //return '<div class="toggler '.$size.' align'.$align.'" data-open='.$opening.'><div class="togglebar '.$toggleCss.'"><div class="alignright opentext">'.$opentext.'</div><div class="alignright closetext">'.$closetext.'</div>'.$title.'</div><div class="accContent">'.do_shortcode(trim($content)).'</div></div>';
	return '<div class="toggler '.$size.' align'.$align.' '.$opening.'"><div class="togglebar '.$toggleCss.'"><div class="alignright opentext">'.$opentext.'</div><div class="alignright closetext">'.$closetext.'</div>'.$title.'</div><div class="accContent">'.do_shortcode(trim($content)).'</div></div>';
}
add_shortcode('toggle', 'theme_shortcode_toggle');
