<?php
/**
 * Shortcode For Carousel Post Type
 * @package Symple Carousel
 * @since 1.0
 * @author AJ Clarke : http://sympleplorer.com
 * @copyright Copyright (c) 2012, AJ Clarke
 * @link http://sympleplorer.com
 * @License: GNU General Public License version 3.0
 * @License URI: http://www.gnu.org/licenses/gpl-3.0.html
 */

if(! function_exists( 'symple_carousel_shortcode' ) ) :

	function symple_carousel_shortcode($atts, $content = null) {
		
		// Define shortcode params
		extract(shortcode_atts(array(
				'post_type' => 'portfolio',
				'img_width' => '220',
				'img_height' => '170',
				'prev_icon' => 'icon-arrow-left',
				'next_icon' => 'icon-arrow-right',
				'adaptive_height' => 'true',
				'speed' => '1000',
				'ticker' => 'false',
				'margin' => '20',
				'count' => '-1',
				'min_slides' => '1',
				'max_slides' => '4',
				'pager' => 'false',
				'controls' => 'true',
				'auto' => 'true',
				'mode' => 'horizontal',
				'order' => 'DESC',
				'orderby' => 'date',
				'no_links' => 'false',
				'lightbox_only' => 'false',
				'title' => 'true',
				'excerpt' => 'true',
				'excerpt_length' => '15',
				'read_more' => 'false',
				'read_more_text' => __('read more','wpex'),
				'taxonomy_name' => '',
				'taxonomy_id' => '',
				'filter_content' => 'false',
				'offset' => 0,
				'filter' => '',
			), $atts));
		
		// Give carousel a unique name
		$unique_classname = 'carousel-'. rand(1, 100);
		
		// Start Tax Query
		$tax_query = NULL;
		if( $taxonomy_name !=='' && $taxonomy_id !== '' ) {
			$tax_query = array(
				array(
					'taxonomy' => $taxonomy_name,
					'field' => 'id',
					'terms' => $taxonomy_id
				),
			);
		}
		
		// Set Default query
		global $wpex_carousel_query;
		
		// The Query
		$wpex_carousel_query = new WP_Query(
			array(
				'post_type' => $post_type,
				'posts_per_page' => $count,
				'offset' => $offset,
				'order' => $order,
				'orderby' => $orderby,
				'tax_query' => $tax_query,
				'filter_content' => $filter_content,
				'no_found_rows' => true
			)
		);

		$output = '';

		//Output posts
		if( $wpex_carousel_query->posts ) :
		
		// Give carousel a unique name
		$unique_classname = 'carousel-'. rand(1, 100);
		
		// Load BX Slider
		wp_enqueue_script('bxslider');
		
		// Output filter JS into the footer like a WP Jedi Master
		$output .='
			<script type="text/javascript">
				jQuery(function($){
					$(document).ready(function(){
						$(".'. $unique_classname .'").bxSlider({
							mode: "'. $mode .'",
							minSlides: '. $min_slides .',
							maxSlides: '. $max_slides .',
							slideWidth: '. $img_width .',
							slideMargin: '. $margin .',
							controls: '. $controls .',
							pager: '. $pager .',
							auto: '. $auto .',
							prevText: "<span class=\''. $prev_icon .'\'>",
							nextText: "<span class=\''. $next_icon .'\'>",
							adaptiveHeight: ' . $adaptive_height .',
							speed: '. $speed .',
							ticker: '. $ticker .'
						});
					});
				});
			</script>';
		
			// Main wrapper div
			$output .= '<div class="symple-carousel"><ul class="clearfix '. $unique_classname .'">';
		
			// Loop through posts
			foreach ( $wpex_carousel_query->posts as $post ) :
			
				// Post vars/meta
				$postid = $post->ID;
				$full_featured_img = wp_get_attachment_url( get_post_thumbnail_id($postid), 'full' );
				$featured_img = $full_featured_img;
				$post_title = get_the_title($postid);
				
				// Define Excerpt Length Default
				$excerpt_length = ( $excerpt_length ) ? $excerpt_length : '10';
				
				// Define URL target
				$url = get_permalink($postid);
				
				// Crop Images if wpex_img_heights exists
				if( function_exists( 'aq_resize' ) ) {
					$featured_img =  aq_resize( $full_featured_img, $img_width-10, $img_height, true );
				}
		
				// Carousel Post Output
				$output .= '<li class="symple-carousel-entry">';
				
					// Image Wrap
					$output .= '<div class="symple-carousel-entry-media">';
						
						// Lighbox Style
						if( $lightbox_only == 'true' ) {
							
							$lightbox_group = ( $lightbox_only == 'true' ) ? 'data-fancybox-group="'. $unique_classname .'"' : NULL;
							
							if( has_post_thumbnail($postid) ) {
								
								
								$output .= '<a href="'. $full_featured_img .'" title="'. $post_title .'" class="carousel-entry-img fancybox" '. $lightbox_group .'>';
								$output .= '<img src="'. $featured_img .'" alt="'. $post_title .'" />';
								$output .= '</a><!-- .symple-carousel-entry-img -->';
								
							} // End thumbnail check
							
						}
						
						// Plain Img Style
						else {
							
							if( has_post_thumbnail($postid) ) {
								$output .= '<a href="'. $url .'" title="'. $post_title .'" class="carousel-entry-img">';
								$output .= '<img src="'. $featured_img .'" alt="'. $post_title .'" />';
								$output .= '</a><!-- .carousel-entry-img -->';
							} // End thumbnail check
							
						} // End else			
					
					$output .= '</div><!-- .symple-carousel-entry-media -->';
				
					// Open details div
					if( $title == 'true' ||  $excerpt == 'true' ) {
						$output .='<div class="symple-carousel-entry-details">';
					}
					
						//Title
						if( $title == 'true' ) {
							$output .= '<header class="symple-carousel-entry-title">';
								$output .= '<h3><a href="'. $url .'" title="'. $post_title .'">'. $post_title .'</a></h3>';
							$output .= '</header><!-- .symple-carousel-entry-title -->';
							
						}
						
						//Excerpt + Read More
						if( $excerpt == 'true' ) {
							$output .='<div class="symple-carousel-entry-excerpt">';
								
								// Excerpt
								!empty($post->post_excerpt) ? $output .= $post->post_excerpt : $output .= wp_trim_words( strip_shortcodes( $post->post_content ), $excerpt_length);
								//Read More
								if( $read_more == 'true' ) { 
									if( $post_style == 'Redirect' && $redirect_url ) {
										$output .= '<a href="'. $url .'" title="'. __('Visit Site','wpex') .'" class="carousel-entry-readmore">'. __('visit site','wpex') .' &rarr;</a>';
									} else {
										$output .= '<a href="'. $url .'" title="'. $read_more_text .'" class="carousel-entry-readmore">'. $read_more_text .' &rarr;</a>';
									}
								}
							$output .=' </div><!-- .symple-carousel-entry-excerpt -->';
						}
					
					// Close details div
					if( $post_title == 'true' || $excerpt == 'true' ) {
						$output .='</div><!-- .symple-carousel-entry-details -->';
					}
					
				// Close main wrap	
				$output .= '</li>';
			
			// End foreach loop
			endforeach;
			
			// Close main wrap
			$output .= '</ul></div><!-- END Symple Carousel -->';
			
		endif; // End $wpex_carousel_query->posts check
		
		
		// Set things back to normal
		$wpex_carousel_query = NULL;
		wp_reset_postdata();


		// Return data
		return $output; 
		
	}
	add_shortcode("symple_carousel", "symple_carousel_shortcode");
	
	
	
	/*
	 * Add Editor Button
	 * @since v1.1
	*/
	
	//Creating TinyMCE buttons
	function symple_carousel_editor_button() {
	   if ( current_user_can('edit_posts') &&  current_user_can('edit_pages') ) {
		 if (get_user_option('rich_editing')) {
			add_filter('mce_external_plugins', 'symple_carousel_add_custom');
			add_filter('mce_buttons', 'symple_carousel_register_button');
		 }
	   }
	} 
	add_action('init', 'symple_carousel_editor_button');
	
	function symple_carousel_add_custom($plugin_array) {
		   $plugin_array['symple_carousel'] = get_template_directory_uri() .'/functions/shortcodes/mce/carousel-mce-button.js';
		   return $plugin_array;
	}
	
	function symple_carousel_register_button($buttons) {
	   array_push($buttons, "symple_carousel");
	   return $buttons;
	} 
	

	
endif; // end symple_carousel_shortcode check
