<?php

if(!function_exists('origami_gallery')) :
/**
 * Display a special Origami image gallery
 * 
 * @param $contents
 * @param $atts
 * @return string
 */
function origami_gallery($contents, $attr){
	if(!siteorigin_setting('display_gallery')) return $contents;

	if(!empty($attr['type']) && $attr['type'] == 'default') return '';

	if(siteorigin_panels_is_home() && empty($attr['ids'])){
		// Display the default Origami gallery
		return origami_gallery_default();
	}

	global $post;
	
	static $instance = 0;
	$instance++;

	// We're trusting author input, so let's at least make sure it looks like a valid orderby statement
	if ( isset( $attr['orderby'] ) ) {
		$attr['orderby'] = sanitize_sql_orderby( $attr['orderby'] );
		if ( !$attr['orderby'] )
			unset( $attr['orderby'] );
	}

	/**
	 * @var $order
	 * @var $orderby
	 * @var $id
	 * @var $itemtag
	 * @var $icontag
	 * @var $captiontag
	 * @var $size
	 * @var $include
	 * @var $exclude
	 * @var $wp_default
	 * @var $target_blank
	 */
	extract(shortcode_atts(array(
		'order'      => 'ASC',
		'orderby'    => 'menu_order ID',
		'id'         => $post->ID,
		'itemtag'    => 'dl',
		'icontag'    => 'dt',
		'captiontag' => 'dd',
		'columns'    => 3,
		'size'       => 'origami-slider',
		'include'    => '',
		'exclude'    => '',
		'wp_default'    => false,
		'target_blank' => false,
	), $attr));
	
	// This gallery has requested to use the WordPress default gallery
	if($wp_default) return $contents;

	$id = intval($id);
	if ( 'RAND' == $order )
		$orderby = 'none';

	if ( !empty($include) ) {
		$include = preg_replace( '/[^0-9,]+/', '', $include );
		$_attachments = get_posts( array('include' => $include, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );

		$attachments = array();
		foreach ( $_attachments as $key => $val ) {
			$attachments[$val->ID] = $_attachments[$key];
		}
	}
	elseif ( !empty($exclude) ) {
		$exclude = preg_replace( '/[^0-9,]+/', '', $exclude );
		$attachments = get_children( array('post_parent' => $id, 'exclude' => $exclude, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
	}
	else {
		$attachments = get_children( array('post_parent' => $id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
	}

	if ( empty($attachments) ) return '';

	// This is the custom stuff

	// Create the gallery content
	$return = '';
	$return .= '<div class="flexslider-wrapper">';
	$return .= '<div class="flexslider">';
	$return .= '<ul class="slides">';
	foreach($attachments as $attachment){
		$return .= '<li>';
		$return .= apply_filters('origami_slide_before', '', $attachment, $target_blank);
		$return .= wp_get_attachment_image($attachment->ID, $size, false, array('class' => 'slide-image'));
		if($attachment->post_excerpt){
			$return .= '<div class="flex-caption">' . $attachment->post_excerpt . '</div>';
		}
		$return .= apply_filters('origami_slide_after', '', $attachment, $target_blank);
		$return .= '</li>';
	}
	$return .= '</ul>';
	$return .= '</div>';
	$return .= '</div>';

	return $return;
}
endif;
add_filter('post_gallery', 'origami_gallery', 10, 2);

function origami_gallery_default(){
	$return = '';
	$return .= '<div class="flexslider-wrapper">';
	$return .= '<div class="flexslider">';
	$return .= '<ul class="slides">';
	
	$default_gallery = array(
		array(get_template_directory_uri().'/demo/slide.jpg', 903, 452, __('This is a default gallery.', 'origami')),
		array(get_template_directory_uri().'/demo/slide2.jpg', 903, 452, __('Edit it by editing the gallery widget.', 'origami')),
		array(get_template_directory_uri().'/demo/slide.jpg', 903, 452, __('You can easily add your own images and captions.', 'origami')),
		array(get_template_directory_uri().'/demo/slide2.jpg', 903, 452, __('With a beautifully simple interface.', 'origami')),
	);
	foreach($default_gallery as $img){
		$return .= '<li>';
		$return .= '<img src="' . esc_url($img[0]) . '" width="' . intval($img[1]) . '" height="' . intval($img[2]) . '" class="slide-image" />';
		$return .= '<div class="flex-caption">' . esc_html($img[3]) . '</div>';
		$return .= '</li>';
	}
	
	
	$return .= '</ul>';
	$return .= '</div>';
	$return .= '</div>';
	
	return $return;
}