<?php

function theme_shortcode_message($atts, $content = null, $code) {
	extract(shortcode_atts(array(
		'align' => false,
	), $atts));
	$align = $align?' '.$align:'';
	return '<p class="' . $code . $align . '">' . do_shortcode($content) . '</p>';
}

add_shortcode('success','theme_shortcode_message');
add_shortcode('errors','theme_shortcode_message');
add_shortcode('notice','theme_shortcode_message');

function theme_shortcode_framed_box($atts, $content = null, $code) {
	extract(shortcode_atts(array(
		'width' => '',
		'height' => '',
		'bgcolor' => '',
		'textcolor' => '',
		'bordercolor'=>'',
	), $atts));
	
	if($width){
		if(is_numeric($width)){
			$width = $width.'px';
		}
		$width = 'width:'.$width.';';
	}else{
		$width = '';
	}
	if($height){
		if(is_numeric($height)){
			$height = $height.'px';
		}
		$height = 'height:'.$height.';';
	}else{
		$height = '';
	}
	

	$bordercolor = $bordercolor?'border-color:'.$bordercolor.';':'';
	$bgcolor = $bgcolor?'background-color:'.$bgcolor.';':'';
	$textcolor = $textcolor?'color:'.$textcolor.';':'';

	if( !empty($height) || !empty($bgcolor) || !empty($textcolor)){
		$content_style = ' style="'.$height.$textcolor.'"';
	}else{
		$content_style = '';
	}
	$content = do_shortcode(trim(shortcode_unautop( $content )));
	$content = preg_replace('#^<\/p>|^<br \/>|<br \/>$|<p>$#', '', trim($content));
	return '<div class="' . $code. '" style="'.$width.$bgcolor.$bordercolor.'"><div class="framed_box_content"'.$content_style.'>' . $content . '<div class="clear"></div></div></div>';
}
add_shortcode('framed_box','theme_shortcode_framed_box');

function theme_shortcode_note($atts, $content = null) {
	extract(shortcode_atts(array(
		'align' => false,
		'title' => '',
		'width' => false,
	), $atts));
	$align = $align?' align'.$align:'';
	$width = $width?' style="width:'.(int)$width.'px"':'';
	if(!empty($title)){
		$title = '<h3 class="note_title">'.$title.'</h3>';
	}
	$content = do_shortcode(trim(shortcode_unautop( $content )));
	$content = preg_replace('#^<\/p>|^<br \/>|<br \/>$|<p>$#', '', trim($content));
	return '<div class="note' . $align . '"'.$width.'>'.$title.'<div class="note_content">' . $content . '</div></div>';
}
add_shortcode('note','theme_shortcode_note');

function theme_shortcode_fade($atts, $content = null) {
	extract(shortcode_atts(array(
		'bgcolor' => '',
		'bordercolor'=>'',
		'align' => false,
		'width' => false,
	), $atts));
	$align = $align?' align'.$align:'';
	$width = $width?'width:'.(int)$width.'px;':'';
	$bordercolor = empty($bordercolor) ? 'border-width:0px;':'border-color:'.$bordercolor.';';
	$colorcss = '';
	if (! empty($bgcolor)) {
		$bgcolor = 'background:'.$bgcolor.';
		background: -moz-linear-gradient(top,'.$bgcolor.'   0%, '.change_rgba($bgcolor,0).' 100%); 
		background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,'.$bgcolor.'), color-stop(100%,'.change_rgba($bgcolor,0).')); 
		background: -webkit-linear-gradient(top,  '.$bgcolor.' 0%,'.change_rgba($bgcolor,0).' 100%); 
		background: -o-linear-gradient(top,  '.$bgcolor.' 0%,'.change_rgba($bgcolor,0).' 100%); 
		background: -ms-linear-gradient(top,  '.$bgcolor.' 0%,'.change_rgba($bgcolor,0).' 100%); 
		background: linear-gradient(top,  '.$bgcolor.' 0%,'.change_rgba($bgcolor,0).' 100%);';
		/*$bgcolor = 'background-color:'.$bgcolor.';background:-moz-linear-gradient(top,'.$bgcolor.', white);background:-webkit-gradient(linear, 0 0, 0 100%, from('.$bgcolor.'), to(white));background: -o-linear-gradient('.$bgcolor.',#ffffff);';*/
	}
	else 
		$bgcolor = '';
	$content = do_shortcode(trim(shortcode_unautop( $content )));
	$content = preg_replace('#^<\/p>|^<br \/>|<br \/>$|<p>$#', '', trim($content));
	return '<div class="fadebox' . $align .'" style="'.$width.$bgcolor.$bordercolor.'"><div class="fade_content">' . $content . '</div></div>';
}
add_shortcode('fade','theme_shortcode_fade');


function theme_shortcode_info($atts, $content = null) {
	extract(shortcode_atts(array(
		'bgcolor' => '',
		'align' => false,
		'size' => 'small',
		'color' => '',
		'overlay' => 'light'
	), $atts));
	$align = $align?' align'.$align:'';
	if (! empty($bgcolor)) {
	$bgcolor = 'background-color:'.$bgcolor;
	}else 
	$bgcolor = '';
	if (! empty($color)) {
	$color = 'color:'.$color;
	}else 
	$color = '';
	$content = trim(shortcode_unautop( $content ));
	$content = preg_replace('#^<\/p>|^<br \/>|<br \/>$|<p>$#', '', trim($content));
	return '<div class="info-box '.$align.' '.$size.'-'.$overlay.'" style="'.$bgcolor.$color.'"><p>'.$content.'</p></div>'; 
}
add_shortcode('info_box','theme_shortcode_info');
function theme_shortcode_cycle($atts, $content = null) {
	extract(shortcode_atts(array(
		'title' => '',
		'animate'=>'fade',
		'easing'=>'swing',
		'speed'=>200,
		'delay'=>3000,
		'bgcolor'=>'#E6E6E6',
		'bordercolor'=>'',
		'width'=>false,
		'align'=>false,
	), $atts));
	if (!preg_match_all("/(.?)\[(cycle)\b(.*?)(?:(\/))?\](?:(.+?)\[\/cycle\])?(.?)/s", $content, $matches)) {
		return do_shortcode($content);
	} else {
		if(empty($title))
			$title = '';
		$align = $align?' align'.$align:'';
		$width = $width?'width:'.(int)$width.'px;':'';
		$bordercolor = empty($bordercolor) ? 'border-width:0px;':'border-color:'.$bordercolor.';';
		$bgcolor = 'background:'.$bgcolor.';
		background: -moz-linear-gradient(top,'.$bgcolor.'   0%, '.change_rgba($bgcolor,0).' 100%); 
		background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,'.$bgcolor.'), color-stop(100%,'.change_rgba($bgcolor,0).')); 
		background: -webkit-linear-gradient(top,  '.$bgcolor.' 0%,'.change_rgba($bgcolor,0).' 100%); 
		background: -o-linear-gradient(top,  '.$bgcolor.' 0%,'.change_rgba($bgcolor,0).' 100%); 
		background: -ms-linear-gradient(top,  '.$bgcolor.' 0%,'.change_rgba($bgcolor,0).' 100%); 
		background: linear-gradient(top,  '.$bgcolor.' 0%,'.change_rgba($bgcolor,0).' 100%); ';
		/*$bgcolor = 'background-color:'.$bgcolor.';background:-moz-linear-gradient(top,'.$bgcolor.', white);background:-webkit-gradient(linear, 0 0, 0 100%, from('.$bgcolor.'), to(white));background: -o-linear-gradient('.$bgcolor.',#ffffff);';*/
		$output = '<div class="cycle-box '.$align.'" style="'.$width.$bgcolor.$bordercolor.'" data-speed="'.$speed.'" data-easing="'.$easing.'" data-animate="'.$animate.'" data-delay="'.$delay.'">';
		$output .= "\n".'<div class="cycle-panel">';
		$output .= "\n".'<div class="cycle-title alignleft"><h3>'.$title.'</h3></div>';
		$output .= "\n".'<div class="cycle-control alignright">';
		$output .= "\n".'<div class="cycleforward">&gt;</div><div class="cycledivider"></div><div class="cycleback">&lt;</div>';
		$output .= "\n".'</div>';
		$output .= "\n".'<div class="clear"></div>';
		$output .= "\n".'</div>';
		$output .= "\n".'<div class="cycle-wrap">';
		for($i = 0; $i < count($matches[0]); $i++) {
			$output .= "\n".'<div class="cycle-content">'."\n";
			$output .= do_shortcode(trim($matches[5][$i]));
			$output .= "\n".'</div>';
		}
		$output .= "\n".'</div>';
		$output .= "\n".'</div>';
		return $output;
	}
}
add_shortcode('cycle_box','theme_shortcode_cycle');

