<?php

function theme_shortcode_slideshow($atts, $content = null){
	wp_print_scripts('slider-js');
	if(isset($atts['type'])){
		switch($atts['type']){
			case 'nivo':
				return theme_slideshow_nivo($atts, $content);
				break;
			case 'flow':
				return theme_slideshow_flow($atts, $content);
				break;
			case 'anything':
				return theme_slideshow_anything($atts, $content);
				break;
		}
	}
	return '';
}
add_shortcode('slideshow', 'theme_shortcode_slideshow');
function theme_slideshow_anything($atts, $content = null){
	global $wp_filter;
	$the_content_filter_backup = $wp_filter['the_content'];
	extract(shortcode_atts(array(
		'width' =>628,
		'height'=>280,
		'align' => false,
		'number' => -1,
		'source' => '',
		'category' => '',
		'animate'=>'fade',
		'easing'=>'swing',
		'animatespeed' => '1000',
		'style'=>'',
		'pausetime' => '3000',
		'autoplay'=>'true',
		'pauseonhover' => 'false',
	), $atts));

	wp_print_scripts('jquery-swfobject');
	wp_print_scripts('mediaelementjs-scripts');
	wp_print_scripts('slider-video');
	wp_print_scripts('swfobject');
	wp_print_scripts('anyInit-js');
	wp_print_styles('mediaelementjs-styles');
	$align = $align?' align'.$align:'';
	if($pauseonhover==='true'){
		$pauseonhover = true;
	}else{
		$pauseonhover = false;
	}
	if($autoplay==='true'){
		$autoplay = true;
	}else{
		$autoplay = false;
	}
	/** fix **/
	if(!empty($category)){
		$source = '{s:'.$category.'}'; 
	}
	/** end fix **/
	
	$slide_options = array(
		'textAnimate' => $animate, 
		'textEasing' => $easing, 
		'delay' => $pausetime, 
		'animationTime' => $animatespeed, 
		'navNumber'=>4,
		'type'=>'anything',
		'symplexnav'=>false,
		'anythingnav'=>true,
		'anythingtype'=>'html',
		'pauseOnHover'=>$pauseonhover,
		'readyswf'=>THEME_SWF.'/expressInstall.swf',
		'posterImage'=>THEME_IMAGES.'/video_play.png',
		'autoPlay'=>$autoplay
	);
	$content = trim($content);
	$images = !empty($content)?preg_split("/(\r?\n)/", $content):'';
	
	if(!empty($images) && is_array($images)){
		$content = '';
		foreach($images as $image){
			$image = trim(strip_tags($image));
			
			if(!empty($image)){
				$content .= '<div class="slideitem">';
				$content .= '<img src="'.strip_tags($image).' height='.$height.' width='.$width.' title="" alt="" class="carousel-image"/>';
				$content .= '</div>';
			}
		}
	}
	if(!empty($source)){
		if($number != '-1'){
			$number = $number - count($images);
		}
		$items = theme_generator('slideShow_getItem',$source,$number,'anything');
		foreach ($items as $item) {
			$content .='<div class="slideitem">';
			$content .= do_shortcode($item['content']);
			$content .= '<div class="clear"></div></div>';
		}
	}
	$wp_filter['the_content'] = $the_content_filter_backup;
	$params = 	theme_generator('slideshow_Param',$slide_options,false);
	$nav = '<ul class="anything-control" style="display: none;">
					<li><a class="slideshowprev">Prev</a></li>
					<li>
						<div class="slide-button-pause"><a href="#" class="slidepause">Pause Slideshow</a></div>
						<div class="slide-button-play"><a href="#" class="slideresume">Resume Slideshow</a></div>
					</li>
					<li><a class="slideshownext">Next</a></li>
				</ul>';
	return <<<HTML
<div class="slideshow {$align}" style="width:{$width}px;height:{$height}px;{$style}"><div class="slidetext">{$content}</div>{$nav}<div class='slideshowParam' data-params='{$params}'></div></div>
HTML;
	
}
function theme_slideshow_flow($atts, $content = null){
	global $wp_filter;
	$the_content_filter_backup = $wp_filter['the_content'];
	extract(shortcode_atts(array(
		'width' =>628,
		'height'=>280,
		'align' => false,
		'number' => -1,
		'source' => '',
		'category' => '',
		'style'=>'',
		'borderwidth' => 0,
		'bordercolor' => '#5D5D5D',
		'largew' => 400,
		'largeh' => 400,
		'smallw'=>400,
		'smallh'=>400,
		'offsetside'=>50,
		'offsettop'=>50,
		'easing'=>'swing',
		'animatespeed' => '500',
		'pausetime' => '3000',
		'pauseonhover' => 'false',
		'captions' => 'false',
		'captionposition'=>'top',
	), $atts));

	wp_print_scripts('jquery-featureCarousel');
	wp_print_scripts('flowInit-js');
	wp_print_styles('css-feature-carousel');
	$align = $align?' align'.$align:'';
	if($pauseonhover==='true'){
		$pauseonhover = true;
	}else{
		$pauseonhover = false;
	}
	
	/** fix **/
	if(!empty($category)){
		$source = '{s:'.$category.'}'; 
	}
	/** end fix **/
	
	$slide_options = array(
		'width' =>$width,
		'height'=>$height,
		'largeW' => $largew, 
		'largeH' => $largeh, 
		'smallW' => $smallw, 
		'smallH' => $smallh, 
		'offsetSide' => $offsetside,
		'offsetTop' => $offsettop,
		'animSpeed' => $animatespeed, 
		'pauseTime' => $pausetime, 
		'pauseOnHover' => $pauseonhover, 
		'easing'=>$easing, 
		'navNumber' =>4,
		'type'=>'flow',
		'symplexnav'=>false,
	);
	$content = trim($content);
	$images = !empty($content)?preg_split("/(\r?\n)/", $content):'';
	
	if(!empty($images) && is_array($images)){
		$content = '';
		foreach($images as $image){
			$image = trim(strip_tags($image));
			
			if(!empty($image)){
				$content .= '<div class="carousel-feature '.$captionposition.'" style="border-color:'.$bordercolor.';border-width:'.$borderwidth.'px">';
				$content .= '<img src="'.strip_tags($image).' height='.$largeh.' width='.$largew.' title="" alt="" class="carousel-image"/>';
				$content .= '</div>';
			}
		}
	}
	$default_image=theme_get_option('slideshow','default_image');
	$default_image=theme_parse_image($default_image,'slide-default.jpg',false);
	$default_source = $default_image['method'];
	$default_image = $default_image['media'];

	if(!empty($source)){
		if($number != '-1'){
			$number = $number - count($images);
		}
		$items = theme_generator('slideShow_getItem',$source,$number,'flow');
		$captions = $captions=='true' ? '':'visibility:hidden;';
		foreach($items as $item) {
			$media_params=$item['media'];
			$url = $default_image;
			$source_type = $default_source;
			if(!empty($media_params['type']) && $media_params['type']=='image'){
				$url = $media_params['media'];
				$source_type = $media_params['method'];
			}
			$content .= '<div class="carousel-feature '.$captionposition.'" style="border-color:'.$bordercolor.';border-width:'.$borderwidth.'px">';
			$resize = theme_resize($url,$slide_options['largeW'],$slide_options['largeH'],$source_type);
			if($item['link'] != ''){
				$content .=  "\n".'<a href="'.$item['link'].'" target="'.$item['link_target'].'"><img width="'.$resize['width'].'" height="'.$resize['height'].'" data-msg="'.$resize['msg'].'" class="carousel-image" src="'.$resize['url'].'" alt="'.$item['title'].'"/></a>';
			}else{
				$content .=  "\n".'<img width="'.$resize['width'].'" height="'.$resize['height'].'" data-msg="'.$resize['msg'].'" class="carousel-image" src="'.$resize['url'].'" alt="'.$item['title'].'"/>';
			}
			if(!empty($item['content']))
				$content .= '<div class="carousel-caption" style="'.$captions.'">'.$item['content'].'</div>';
			$content .= '</div>';
		}
	}
	$wp_filter['the_content'] = $the_content_filter_backup;
	$params = 	theme_generator('slideshow_Param',$slide_options,false);
	return <<<HTML
<div class="slideshow {$align}" style="width:{$width}px;height:{$height}px;{$style}"><div class="carousel-container" style="width:{$width}px;"><div class="carousel"style="width:{$width}px;height:{$height}px;">{$content}</div></div><div class='slideshowParam' data-params='{$params}'></div></div>
HTML;
	
}
function theme_slideshow_nivo($atts, $content = null){
	global $wp_filter;
	$the_content_filter_backup = $wp_filter['the_content'];
	extract(shortcode_atts(array(
		'align' => false,
		'number' => -1,
		'width' => '630',
		'height' => '300',
		'style'=>'',
		'source' => '',
		'category' => '',
		'effect' => 'random',
		'slices' => '10',
		'boxcols' => '8',
		'boxrows' => '4',
		'animspeed' => '500',
		'pausetime' => '3000',
		'controlnav' => 'false',
		'captionopacity'=>'0.8',
		'pauseonhover' => 'false',
		'captions' => 'false',
		'captionposition'=>'top',
		'randomstart' => 'false',
		'autoplay'=>'true',
	), $atts));
	$align = $align?' align'.$align:'';

	wp_print_scripts('jquery-nivo');
	wp_print_scripts('nivoInit-js');
	wp_print_styles('css-nivo');

	if($controlnav==='true'){
		$controlnav = true;
	}else{
		$controlnav = false;
	}
	if($autoplay==='true'){
		$autoplay = true;
	}else{
		$autoplay = false;
	}
	if($pauseonhover==='true'){
		$pauseonhover = true;
	}else{
		$pauseonhover = false;
	}
	if($randomstart==='true'){
		$randomstart = 'true';
	}else{
		$randomstart = 'false';
	}
	
	/** fix **/
	if(!empty($category)){
		$source = '{s:'.$category.'}'; 
	}
	/** end fix **/
	$size[0]=$width;
	$size[1]=$height;
	$slide_options = array(
		'effect' => $effect, 
		'slices' => $slices, 
		'animSpeed' =>$animspeed, 
		'pauseTime' => $pausetime, 
		'pauseOnHover' => $pauseonhover, 
		'manualAdvance' =>false,
		'captions' => $captions,
		'captionOpacity'=>$captionopacity,
		'stop' => false,
		'stopAtEnd' => false,
		'boxRows'=> $boxrows,
		'boxCols'=> $boxcols,
		'navNumber'=>4,
		'control'=>$controlnav,
		'symplexnav'=>false,
		'type'=>'nivo',	
		'autoPlay'=>$autoplay,
		'randomStart'=>$randomStart,
	);
	$content = trim($content);
	$images = !empty($content)?preg_split("/(\r?\n)/", $content):'';
	
	
	if(!empty($images) && is_array($images)){
		$content = '';
		foreach($images as $image){
			$image = trim(strip_tags($image));
			
			if(!empty($image)){
				$content .= '<img src="'.strip_tags($image).' height='.$height.' width='.$width.' title="" alt="" />';
			}
		}
	}
	$default_image=theme_get_option('slideshow','default_image');
	$default_image=theme_parse_image($default_image,'slide-default.jpg',false);
	$default_source = $default_image['method'];
	$default_image = $default_image['media'];
	if(!empty($source)){
		if($number != '-1'){
			if(!empty($images)){
				$number = $number - count($images);
			}
		}
		$items = theme_generator('slideShow_getItem',$source,$number,'nivo');
		foreach($items as $item) {
			$media_params=$item['media'];
			if(!empty($media_params['type'])){
				$captions_text = !$captions||$captions=='false'?'':$item['content'];
				if(empty($captions_text))$captions_text='';
				if($media_params['type'] !="image"){
					$resize = theme_resize($default_image,$width,$height,$default_source);
					if($item['link'] != ''){
						$content.= "\n".'<a href="'.$item['link'].'" target="'.$item['link_target'].'"><img width="'.$resize['width'].'" height="'.$resize['height'].'" data-msg="'.$resize['msg'].'" src="'.$resize['url'].'" alt="'.empty($items['title'])?$item['title']:''.'"/></a>';
					}else{
						$content.= "\n".'<img width="'.$resize['width'].'" height="'.$resize['height'].'" data-msg="'.$resize['msg'].'" src="'.$resize['url'].'"  alt="'.$item['title'].'"/>';
					}
				}
				else{

					$url=$default_image;
					$source_type = 'dir';
					if(!empty($media_params['media']))
						$url = $media_params['media'];
					if(!empty($media_params['method']))
						$source_type = $media_params['method'];
					$resize = theme_resize($url,$width,$height,$source_type);
					if($item['link'] != ''){
						$content.= "\n".'<a href="'.$item['link'].'" target="'.$item['link_target'].'"><img width="'.$resize['width'].'" height="'.$resize['height'].'" data-msg="'.$resize['msg'].'" src="'.$resize['url'].'"  title="'.$captions_text.'" alt="'.$item['title'].'"/></a>';
					}
					else{
						$content.= "\n".'<img width="'.$resize['width'].'" height="'.$resize['height'].'" data-msg="'.$resize['msg'].'" src="'.$resize['url'].'" title="'.$captions_text.'" alt="'.$item['title'].'"/>';
					}
				}
			}else{
				$resize = theme_resize($default_image,$width,$height,$default_source);
				$content.= "\n".'<img width="'.$resize['width'].'" height="'.$resize['height'].'" data-msg="'.$resize['msg'].'" src="'.$resize['url'].'" alt="default"/>';
			}
		}
	}
	$wp_filter['the_content'] = $the_content_filter_backup;

	$params = 	theme_generator('slideshow_Param',$slide_options,false);
	return <<<HTML
<div class="slideshow {$align}"  style="width:{$width}px;height:{$height}px;{$style}"><div class="nivoSlider {$captionposition}">{$content}</div><span class="overlay preload"></span><div class='slideshowParam' data-params='{$params}'></div></div>
HTML;
}


function theme_shortcode_anything_box($atts, $content = null){
	extract(shortcode_atts(array(
		'animatein' => 'left',
		'animateout' => 'left',
		'easingin'=>'swing',
		'easingout'=>'swing',
		'style'=>'',
	),$atts));
	
	return '<div class="anything-box" style="'.$style.'" data-in="'.$animatein.'" data-out="'.$animateout.'" data-easingin="'.$easingin.'"  data-easingout="'.$easingout.'"><div class="anything-wrap">'.do_shortcode($content).'</div></div>';
}
add_shortcode('anything_slider_box', 'theme_shortcode_anything_box');


