<?php

$EVENTERRA_THUMBNAIL_SIZES=array(

	'unlimit' => array(
		'width' => false,
		'height' => false,
		'crop' => false
	),
	'media-one-fourth' => array(
		'width' => 292,
		'height' => false,
		'crop' => false,
	),
	'media-one-fourth-square' => array(
		'width' => 292,
		'height' => 292,
		'crop' => true,
	),
	'media-one-third' => array(
		'width' => 390,
		'height' => false,
		'crop' => false,
	),
	'media-one-third-square' => array(
		'width' => 390,
		'height' => 390,
		'crop' => true,
	),
	'media-one-third-3-2' => array(
		'width' => 390,
		'height' => 260,
		'crop' => true,
	),
	'media-one-half' => array(
		'width' => 584,
		'height' => false,
		'crop' => false,
	),
	'media-one-half-square' => array(
		'width' => 584,
		'height' => 584,
		'crop' => true,
	),
	'media-one-half-3-2' => array(
		'width' => 584,
		'height' => 389,
		'crop' => true,
	),
	'media-mobile' => array(
		'width' => 728,
		'height' => false,
		'crop' => false,
	),
	'media-mobile-square' => array(
		'width' => 728,
		'height' => 728,
		'crop' => true,
	),
	'media-mobile-3-2' => array(
		'width' => 728,
		'height' => 485,
		'crop' => true,
	),

	'post-media-small' => array(
		'width' => 292,
		'height' => 292,
		'crop' => true
	),
	'post-media-small-mobile' => array(
		'width' => 728,
		'height' => 728,
		'crop' => true
	),
	'post-media-large' => array(
		'width' => 1168,
		'height' => false,
		'crop' => false
	),
	'post-media-large-mobile' => array(
		'width' => 728,
		'height' => false,
		'crop' => false
	),
	
	'sliced-gallery-1-1' => array(
		'width'=>1400,
		'height'=>0,
		'crop'=>false,
	),
	
	'sliced-gallery-2-1' => array(
		'width'=>467,
		'height'=>622,
		'crop'=>true,
	),
	'sliced-gallery-2-2' => array(
		'width'=>934,
		'height'=>622,
		'crop'=>true,
	),
	
	'sliced-gallery-3-1' => array(
		'width'=>934,
		'height'=>608,
		'crop'=>true,
	),
	'sliced-gallery-3-2' => array(
		'width'=>467,
		'height'=>304,
		'crop'=>true,
	),
	'sliced-gallery-3-3' => array(
		'width'=>467,
		'height'=>304,
		'crop'=>true,
	),
	
	'sliced-gallery-4-1' => array(
		'width'=>766,
		'height'=>789,
		'crop'=>true,
	),
	'sliced-gallery-4-2' => array(
		'width'=>467,
		'height'=>263,
		'crop'=>true,
	),
	'sliced-gallery-4-3' => array(
		'width'=>467,
		'height'=>263,
		'crop'=>true,
	),
	'sliced-gallery-4-4' => array(
		'width'=>467,
		'height'=>263,
		'crop'=>true,
	),
	
	'sliced-gallery-5-1' => array(
		'width'=>467,
		'height'=>310,
		'crop'=>true,
	),
	'sliced-gallery-5-2' => array(
		'width'=>467,
		'height'=>310,
		'crop'=>true,
	),
	'sliced-gallery-5-3' => array(
		'width'=>467,
		'height'=>310,
		'crop'=>true,
	),
	'sliced-gallery-5-4' => array(
		'width'=>700,
		'height'=>465,
		'crop'=>true,
	),
	'sliced-gallery-5-5' => array(
		'width'=>700,
		'height'=>465,
		'crop'=>true,
	),
	
	'sliced-gallery-1-1-hires' => array(
		'width'=>1920,
		'height'=>0,
		'crop'=>false,
	),
	
	'sliced-gallery-2-1-hires' => array(
		'width'=>640,
		'height'=>852,
		'crop'=>true,
	),
	'sliced-gallery-2-2-hires' => array(
		'width'=>1280,
		'height'=>852,
		'crop'=>true,
	),
	
	'sliced-gallery-3-1-hires' => array(
		'width'=>1280,
		'height'=>834,
		'crop'=>true,
	),
	'sliced-gallery-3-2-hires' => array(
		'width'=>640,
		'height'=>417,
		'crop'=>true,
	),
	'sliced-gallery-3-3-hires' => array(
		'width'=>640,
		'height'=>417,
		'crop'=>true,
	),
	
	'sliced-gallery-4-1-hires' => array(
		'width'=>1280,
		'height'=>1080,
		'crop'=>true,
	),
	'sliced-gallery-4-2-hires' => array(
		'width'=>640,
		'height'=>360,
		'crop'=>true,
	),
	'sliced-gallery-4-3-hires' => array(
		'width'=>640,
		'height'=>360,
		'crop'=>true,
	),
	'sliced-gallery-4-4-hires' => array(
		'width'=>640,
		'height'=>360,
		'crop'=>true,
	),
	
	'sliced-gallery-5-1-hires' => array(
		'width'=>640,
		'height'=>425,
		'crop'=>true,
	),
	'sliced-gallery-5-2-hires' => array(
		'width'=>640,
		'height'=>425,
		'crop'=>true,
	),
	'sliced-gallery-5-3-hires' => array(
		'width'=>640,
		'height'=>425,
		'crop'=>true,
	),
	'sliced-gallery-5-4-hires' => array(
		'width'=>960,
		'height'=>638,
		'crop'=>true,
	),
	'sliced-gallery-5-5-hires' => array(
		'width'=>960,
		'height'=>638,
		'crop'=>true,
	),
	
	'masonry-gallery-1' => array(
		'width'=>1400,
		'height'=>false,
		'crop'=>false,
	),
	
	'masonry-gallery-2' => array(
		'width'=>700,
		'height'=>false,
		'crop'=>false,
	),

	'masonry-gallery-3' => array(
		'width'=>467,
		'height'=>false,
		'crop'=>false,
	),
	
	'masonry-gallery-4' => array(
		'width'=>350,
		'height'=>false,
		'crop'=>false,
	),	
	
	'masonry-gallery-5' => array(
		'width'=>280,
		'height'=>false,
		'crop'=>false,
	),
	
	'masonry-gallery-6' => array(
		'width'=>234,
		'height'=>false,
		'crop'=>false,
	),
	
	'masonry-gallery-7' => array(
		'width'=>200,
		'height'=>false,
		'crop'=>false,
	),
	
	'masonry-gallery-8' => array(
		'width'=>175,
		'height'=>false,
		'crop'=>false,
	),
	
	'masonry-gallery-9' => array(
		'width'=>156,
		'height'=>false,
		'crop'=>false,
	),
	
	'masonry-gallery-1-hires' => array(
		'width'=>1920,
		'height'=>false,
		'crop'=>false,
	),
	
	'masonry-gallery-2-hires' => array(
		'width'=>960,
		'height'=>false,
		'crop'=>false,
	),
	
	'masonry-gallery-3-hires' => array(
		'width'=>640,
		'height'=>false,
		'crop'=>false,
	),

	'masonry-gallery-4-hires' => array(
		'width'=>480,
		'height'=>false,
		'crop'=>false,
	),
	
	'masonry-gallery-5-hires' => array(
		'width'=>384,
		'height'=>false,
		'crop'=>false,
	),
	
	'masonry-gallery-6-hires' => array(
		'width'=>320,
		'height'=>false,
		'crop'=>false,
	),
	
	'masonry-gallery-7-hires' => array(
		'width'=>275,
		'height'=>false,
		'crop'=>false,
	),
	
	'masonry-gallery-8-hires' => array(
		'width'=>240,
		'height'=>false,
		'crop'=>false,
	),
	
	'masonry-gallery-9-hires' => array(
		'width'=>214,
		'height'=>false,
		'crop'=>false,
	),

);

/*************************************************************************************
 * Post gallery images
 *************************************************************************************/
 
if ( !function_exists( 'eventerra_get_post_gallery_images' ) ) {
	function eventerra_get_post_gallery_images($post_id, $params=array()) {

		$attachments=array();
		
		$gallery_field_name='eventerra_gallery';
		if(isset($params['custom_gallery_field_name']) && $params['custom_gallery_field_name'])
			$gallery_field_name=$params['custom_gallery_field_name'];
					
		$custom_gallery=get_post_meta($post_id, $gallery_field_name, true);
		if(isset($custom_gallery['type']) && $custom_gallery['type'] == 'custom') {
			
			$ids=explode(',',$custom_gallery['images']);
			if(!empty($ids)) {
				
				if(isset($params['only_first']) && $params['only_first'])
					$ids=array_slice($ids,0,1);

				$attachments = get_posts(array(
					'post_type' => 'attachment',
					'orderby' => 'post__in',
					'post__in' => $ids,
					'post_mime_type' => 'image',
					'post_status' => null,
					'numberposts' => -1
				));				
				
			}
			
		} elseif(
				!isset($params['strict_attached']) ||
				!$params['strict_attached'] ||
				( $params['strict_attached'] && isset($custom_gallery['type']) && $custom_gallery['type'] == 'attached' )
			) {
		
			$args=array(
				'orderby' => 'menu_order',
				'order' => 'ASC',
				'post_type' => 'attachment',
				'post_parent' => $post_id,
				'post_mime_type' => 'image',
				'post_status' => null,
				'numberposts' => -1,
			);
			if(isset($params['only_first']) && $params['only_first'])
				$args['numberposts']=1;

			if(get_option('eventerra_exclude_featured_image') == 'true') {
				if( has_post_thumbnail($post_id) ) {
					$thumbid = get_post_thumbnail_id($post_id);
		
					$args['post__not_in']=array($thumbid);
				}
			}

			$attachments = get_posts($args);
			
		}

		return $attachments;
		
	}
}

/*************************************************************************************
 * Custom Gallery
 *************************************************************************************/


if ( !function_exists( 'eventerra_custom_gallery' ) ) {
	function eventerra_custom_gallery($attachments, $args=array()) {
		echo eventerra_get_custom_gallery($attachments, $args);
	}
}

if ( !function_exists( 'eventerra_get_custom_gallery' ) ) {
	function eventerra_get_custom_gallery($attachments, $args=array()) {
		
		if(!isset($args['mode'])) {
			$args['mode']='slider';
		}
		
		if($args['mode'] == 'sliced') {
			return eventerra_get_gallery_sliced($attachments, $args);
		} elseif($args['mode'] == 'masonry') {
			return eventerra_get_gallery_masonry($attachments, $args);
		} elseif($args['mode'] == 'grid') {
			return eventerra_get_gallery_grid($attachments, $args);
		} else {
			return eventerra_get_gallery_slider($attachments, $args);
		}
		
	}
}


/*************************************************************************************
 * Slides Gallery
 *************************************************************************************/

if ( !function_exists( 'eventerra_gallery_slider' ) ) {
	function eventerra_gallery_slider($attachments, $args=array()) { 
		
		echo eventerra_get_gallery_slider($attachments, $args);
		
	}
}

if ( !function_exists( 'eventerra_get_gallery_slider' ) ) {
	function eventerra_get_gallery_slider($attachments, $args=array()) { 
		
		$args = wp_parse_args( $args, array(
			'image_size' => 'full',
			'show_captions' => false,
			'url_link' => false,
			'custom_gallery_field_name' => false,
			'link_to' => 'file',
			'links' => false,
			'url_link_target' => false,
			'strict_attached' => false,
		) );
	
		if(empty($attachments))
			return '';
			
		if(!is_array($attachments)) { // that means that passed not attachments array but post id
			$attachments=intval($attachments);
			if(!$attachments)
				return '';
			$attachments = eventerra_get_post_gallery_images($attachments, array(
				'custom_gallery_field_name' => $args['custom_gallery_field_name'],
				'strict_attached' => $args['strict_attached'],
			));
		}

		if($args['url_link_target'] == '_blank')
			$args['url_link_target'] = ' target="_blank"';
		else
			$args['url_link_target']='';
					
		$out = '';
		
		if( !empty($attachments) ) {
			
			if($args['link_to'] == 'none' || $args['link_to'] == 'post' || $args['link_to'] == 'custom_link') {
				$use_link=false;
			} else {
				$use_link = !(get_option('eventerra_prettyphoto_lightbox') == 'disabled_no_action');
			}
			$gallery_unique_id=rand(0,getrandmax());
			
			$out .= '<div class="custom-gallery gallery-slider'.($args['show_captions']?' gallery-with-captions':' gallery-without-captions').'"><div class="items">';
			$i=0;
			foreach( $attachments as $attachment ) {
		    $src_full = wp_get_attachment_image_src( $attachment->ID, 'full' );
		    
				$alt=trim(strip_tags( get_post_meta($attachment->ID, '_wp_attachment_image_alt', true) ));
				if ( empty($alt) )
					$alt = trim(strip_tags( $attachment->post_excerpt )); // If not, Use the Caption
				if ( empty($alt) )
					$alt = trim(strip_tags( $attachment->post_title )); // Finally, use the title
				
				$caption='';
				if($args['show_captions']) {
					$caption = trim(strip_tags( $attachment->post_excerpt ));
					if ( empty($caption) )
						$caption = trim(strip_tags( $attachment->post_title ));
				}
				
		    $src=eventerra_img_resize($src_full[0], $args['image_size'], false);
		    if(!$src)
		    	$src=$src_full;
		    
		    //if( get_option('eventerra_lazyload') == 'true' && $i > 0) {
		    //	$img='<img height="'.$src[2].'" width="'.$src[1].'" src="data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==" class="lazyload lazyload-hidden" data-src="'.$src[0].'" alt="'.esc_attr($alt).'" />';
		  	//} else {
		  		$img='<img height="'.$src[2].'" width="'.$src[1].'" src="'.$src[0].'" alt="'.esc_attr($alt).'" />';
		  	//}
		  	
		  	if($args['link_to'] == 'post') {
		  		$args['url_link']=get_permalink($attachment->ID);
		  	} elseif($args['link_to'] == 'custom_link') {
  				if(isset($args['links'][$i]))
  					$args['url_link']=$args['links'][$i];
  				else
  					$args['url_link']=false;	
  			}
		  	
		    $img_html=eventerra_hover_extras(
		    	$img.($caption!=''?'<span class="item-caption">'.$caption.'</span>':''),
		    	($use_link ? $src_full[0] : false),
		    	$args['url_link'],
		    	'rel="prettyPhoto[postgal_'.$gallery_unique_id.']"',
				  $args['url_link_target']
		    );
		    
	    	$out .= 
	    		'<div class="item" rel="slide-'.$attachment->ID.'">'.
	    			$img_html.
	    		'</div>
	    	';
	    	
	    	$i++;
			}
			$out .= '</div></div>';
		}
		
		return $out;
	}
}


/*************************************************************************************
 * Slides Gallery Sliced
 *************************************************************************************/

if ( !function_exists( 'eventerra_gallery_sliced' ) ) {
	function eventerra_gallery_sliced($attachments, $args=array()) {
		
		echo eventerra_get_gallery_sliced($attachments, $args);
		
	}
}

if ( !function_exists( 'eventerra_get_gallery_sliced' ) ) {
	function eventerra_get_gallery_sliced($attachments, $args=array()) { 

		$args = wp_parse_args( $args, array(
			'only_1_row' => false,
			'show_captions' => false,
			'custom_gallery_field_name' => false,
			'link_to' => 'file',
			'links' => false,
			'url_link' => false,
			'url_link_target' => false,
			'hires' => false,
		) );	

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

		if(!is_array($attachments)) { // that means that passed not attachments array but post id
			$attachments=intval($attachments);
			if(!$attachments)
				return '';
			$attachments = eventerra_get_post_gallery_images($attachments, array('custom_gallery_field_name' => $args['custom_gallery_field_name']) );
		}
		
		if($args['url_link_target'] == '_blank')
			$args['url_link_target'] = ' target="_blank"';
		else
			$args['url_link_target']='';
		
		$out = '';
		
		if( !empty($attachments) ) {
			$images_left=count($attachments);

			if($args['link_to'] == 'none' || $args['link_to'] == 'post' || $args['link_to'] == 'custom_link') {
				$use_link=false;
			} else {
				$use_link = !(get_option('eventerra_prettyphoto_lightbox') == 'disabled_no_action');
			}
			$gallery_unique_id=rand(0,getrandmax());
			
			$out .= '<div class="gallery-sliced'.($args['show_captions']?' gallery-with-captions':' gallery-without-captions').'">';

			$row=1;
			$image_n=0;
			while($images_left) {
				$n=$images_left;
				if($n > 5)
					$n=5;
				
				if($args['only_1_row'] && $row > 1) {
					
					// just output links for lightbox
					for($i=1;$i<=$n;$i++) {
						$attachment=current($attachments);
						$src_full = wp_get_attachment_image_src( $attachment->ID, 'full' );

						if($use_link) {
				    	$out .= '<a href="'.esc_url($src_full[0]).'" rel="prettyPhoto[postgal_'.$gallery_unique_id.']" class="dn"></a>';
				    }
				    	
						next($attachments);
						$images_left--;
					}
					
				} else {
					$out.= '<div class="gallery-sliced-box-row gallery-sliced-box-'.$n.'">';
					for($i=1;$i<=$n;$i++) {
						$attachment=current($attachments);
						$src_full = wp_get_attachment_image_src( $attachment->ID, 'full' );
						
						$alt=trim(strip_tags( get_post_meta($attachment->ID, '_wp_attachment_image_alt', true) ));
						if ( empty($alt) )
							$alt = trim(strip_tags( $attachment->post_excerpt )); // If not, Use the Caption
						if ( empty($alt) )
							$alt = trim(strip_tags( $attachment->post_title )); // Finally, use the title

						$caption='';
						if($args['show_captions']) {
							$caption = trim(strip_tags( $attachment->post_excerpt ));
							if ( empty($caption) )
								$caption = trim(strip_tags( $attachment->post_title ));
						}							
						
				    $src=eventerra_img_resize($src_full[0], 'sliced-gallery-'.$n.'-'.$i.($args['hires']?'-hires':''), false);
				    if(!$src)
				    	$src=$src_full;
				    
				    if( get_option('eventerra_lazyload') == 'true' ) {
				    	$img='<img width="'.$src[1].'" height="'.$src[2].'" src="data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==" data-src="'.$src[0].'" alt="'.htmlspecialchars($alt).'" class="lazyload" />';
				    } else {
				    	$img='<img width="'.$src[1].'" height="'.$src[2].'" src="'.$src[0].'" alt="'.htmlspecialchars($alt).'"/>';
						}

				  	if($args['link_to'] == 'post') {
				  		$args['url_link']=get_permalink($attachment->ID);
				  	} elseif($args['link_to'] == 'custom_link') {
		  				if(isset($args['links'][$image_n]))
		  					$args['url_link']=$args['links'][$image_n];
		  				else
		  					$args['url_link']=false;	
		  			}

						$img=eventerra_hover_extras(
				    	$img.($caption!=''?'<span class="item-caption">'.$caption.'</span>':''),
				    	($use_link ? $src_full[0] : false),
				    	$args['url_link'],
				    	'rel="prettyPhoto[postgal_'.$gallery_unique_id.']"',
				    	$args['url_link_target']
				    );
				    
				    $out .=	'<div class="img-box img-'.$i.'">' . $img . '</div>';

						next($attachments);
						$images_left--;
						$image_n++;
						
						if($n==5&&$i==3)
							$out .= '<div class="clear"></div>';
					}
					$out .= '<div class="clear"></div></div>';
				}
				$row++;
			}
			
			$out .= '</div>';
		}
		
		return $out;
	}
}

/*************************************************************************************
 * Masonry Gallery
 *************************************************************************************/

if ( !function_exists( 'eventerra_gallery_masonry' ) ) {
	function eventerra_gallery_masonry($attachments, $args=array()) { 
		
		echo eventerra_get_gallery_masonry($attachments, $args);
		
	}
}

if ( !function_exists( 'eventerra_get_gallery_masonry' ) ) {
	function eventerra_get_gallery_masonry($attachments, $args=array()) { 
		
		$args = wp_parse_args( $args, array(
			'columns' => 3,
			'show_captions' => false,
			'custom_gallery_field_name' => false,
			'link_to' => 'file',
			'url_link' => false,
			'links' => false,
			'url_link_target' => false,
			'hires' => false,
		) );
	
		if(empty($attachments))
			return '';
			
		if(!is_array($attachments)) { // that means that passed not attachments array but post id
			$attachments=intval($attachments);
			if(!$attachments)
				return '';
			$attachments = eventerra_get_post_gallery_images($attachments, array('custom_gallery_field_name' => $args['custom_gallery_field_name']) );
		}
		
		if($args['url_link_target'] == '_blank')
			$args['url_link_target'] = ' target="_blank"';
		else
			$args['url_link_target']='';
			
		$args['columns']=intval($args['columns']);
		if(!$args['columns'])
			$args['columns']=3;
		elseif($args['columns'] > 9)
			$args['columns']=9;
		
		$out = '';
		
		if( !empty($attachments) ) {

			if($args['link_to'] == 'none' || $args['link_to'] == 'post' || $args['link_to'] == 'custom_link') {
				$use_link=false;
			} else {
				$use_link = !(get_option('eventerra_prettyphoto_lightbox') == 'disabled_no_action');
			}
			$gallery_unique_id=rand(0,getrandmax());
			
			$out .= '<div class="gallery-masonry'.($args['show_captions']?' gallery-with-captions':' gallery-without-captions').' gallery-columns-'.$args['columns'].'" data-gallery-columns="'.$args['columns'].'"><div class="items">';
			$i=0;
			foreach( $attachments as $attachment ) {
		    $src_full = wp_get_attachment_image_src( $attachment->ID, 'full' );
		    
				$alt=trim(strip_tags( get_post_meta($attachment->ID, '_wp_attachment_image_alt', true) ));
				if ( empty($alt) )
					$alt = trim(strip_tags( $attachment->post_excerpt )); // If not, Use the Caption
				if ( empty($alt) )
					$alt = trim(strip_tags( $attachment->post_title )); // Finally, use the title
				
				$caption='';
				if($args['show_captions']) {
					$caption = trim(strip_tags( $attachment->post_excerpt ));
					if ( empty($caption) )
						$caption = trim(strip_tags( $attachment->post_title ));
				}
				
		    $src=eventerra_img_resize($src_full[0], 'masonry-gallery-'.$args['columns'].($args['hires'] ? '-hires' : ''), false);
		    if(!$src)
		    	$src=$src_full;
		    
		    if( get_option('eventerra_lazyload') == 'true' ) {
		    	$img='<img height="'.$src[2].'" width="'.$src[1].'" src="'. EVENTERRA_TEMPLATE_DIR_URI .'/img/e.gif" data-src="'.$src[0].'" alt="'.htmlspecialchars($alt).'" class="lazyload" />';
		    } else {
		    	$img='<img height="'.$src[2].'" width="'.$src[1].'" src="'.$src[0].'" alt="'.htmlspecialchars($alt).'" />';
				}

		  	if($args['link_to'] == 'post') {
		  		$args['url_link']=get_permalink($attachment->ID);
		  	} elseif($args['link_to'] == 'custom_link') {
  				if(isset($args['links'][$i]))
  					$args['url_link']=$args['links'][$i];
  				else
  					$args['url_link']=false;	
  			}
		  			    
		    $img_html=eventerra_hover_extras(
		    	$img.($caption!=''?'<span class="item-caption">'.$caption.'</span>':''),
		    	($use_link ? $src_full[0] : false),
		    	$args['url_link'],
		    	'rel="prettyPhoto[postgal_'.$gallery_unique_id.']"',
				  $args['url_link_target']
		    );
		    
	    	$out .= 
	    		'<div class="item" rel="slide-'.$attachment->ID.'">'.
	    			$img_html.
	    		'</div>
	    	';
	    	
	    	$i++;
			}
			$out .= '</div><div class="clear"></div></div>';
		}

		wp_enqueue_script('om-isotope');
			
		return $out;
	}
}

/*************************************************************************************
 * Grid Gallery
 *************************************************************************************/

if ( !function_exists( 'eventerra_gallery_grid' ) ) {
	function eventerra_gallery_grid($attachments, $args=array()) { 
		
		echo eventerra_get_gallery_grid($attachments, $args);
		
	}
}

if ( !function_exists( 'eventerra_get_gallery_grid' ) ) {
	function eventerra_get_gallery_grid($attachments, $args=array()) { 
		
		$args = wp_parse_args( $args, array(
			'columns' => 3,
			'ratio' => '3:2',
			'show_captions' => false,
			'custom_gallery_field_name' => false,
			'link_to' => 'file',
			'url_link' => false,
			'links' => false,
			'url_link_target' => false,
			'hires' => false,
		) );
	
		if(empty($attachments))
			return '';
			
		if(!is_array($attachments)) { // that means that passed not attachments array but post id
			$attachments=intval($attachments);
			if(!$attachments)
				return '';
			$attachments = eventerra_get_post_gallery_images($attachments, array('custom_gallery_field_name' => $args['custom_gallery_field_name']) );
		}
		
		if($args['url_link_target'] == '_blank')
			$args['url_link_target'] = ' target="_blank"';
		else
			$args['url_link_target']='';
			
		$args['columns']=intval($args['columns']);
		if(!$args['columns'])
			$args['columns']=3;
		elseif($args['columns'] > 9)
			$args['columns']=9;
			
		$args['ratio']=explode(':',$args['ratio']);
		if(count($args['ratio']) < 2 || ! $args['ratio'][0] || ! $args['ratio'][1])
			$args['ratio']=3/2;
		else
			$args['ratio']=$args['ratio'][0] / $args['ratio'][1];
		
		global $EVENTERRA_THUMBNAIL_SIZES;
		
		$out = '';
		
		if( !empty($attachments) ) {

			if($args['link_to'] == 'none' || $args['link_to'] == 'post' || $args['link_to'] == 'custom_link') {
				$use_link=false;
			} else {
				$use_link = !(get_option('eventerra_prettyphoto_lightbox') == 'disabled_no_action');
			}
			$gallery_unique_id=rand(0,getrandmax());
			
			$out .= '<div class="gallery-grid'.($args['show_captions']?' gallery-with-captions':' gallery-without-captions').' gallery-columns-'.$args['columns'].'" data-gallery-columns="'.$args['columns'].'"><div class="items">';
			$i=0;
			foreach( $attachments as $attachment ) {
		    $src_full = wp_get_attachment_image_src( $attachment->ID, 'full' );
		    
				$alt=trim(strip_tags( get_post_meta($attachment->ID, '_wp_attachment_image_alt', true) ));
				if ( empty($alt) )
					$alt = trim(strip_tags( $attachment->post_excerpt )); // If not, Use the Caption
				if ( empty($alt) )
					$alt = trim(strip_tags( $attachment->post_title )); // Finally, use the title
				
				$caption='';
				if($args['show_captions']) {
					$caption = trim(strip_tags( $attachment->post_excerpt ));
					if ( empty($caption) )
						$caption = trim(strip_tags( $attachment->post_title ));
				}
				
				$size_postfix=$args['columns'].($args['hires'] ? '-hires' : '');
				$EVENTERRA_THUMBNAIL_SIZES['grid-gallery-'.$size_postfix]=$EVENTERRA_THUMBNAIL_SIZES['masonry-gallery-'.$size_postfix];
				$EVENTERRA_THUMBNAIL_SIZES['grid-gallery-'.$size_postfix]['height'] = $EVENTERRA_THUMBNAIL_SIZES['grid-gallery-'.$size_postfix]['width'] / $args['ratio'];
				$EVENTERRA_THUMBNAIL_SIZES['grid-gallery-'.$size_postfix]['crop'] = true;
				
		    $src=eventerra_img_resize($src_full[0], 'grid-gallery-'.$size_postfix, false);
		    if(!$src)
		    	$src=$src_full;
		    
		    if( get_option('eventerra_lazyload') == 'true' ) {
		    	$img='<img height="'.$src[2].'" width="'.$src[1].'" src="'. EVENTERRA_TEMPLATE_DIR_URI .'/img/e.gif" data-src="'.$src[0].'" alt="'.htmlspecialchars($alt).'" class="lazyload" />';
		    } else {
		    	$img='<img height="'.$src[2].'" width="'.$src[1].'" src="'.$src[0].'" alt="'.htmlspecialchars($alt).'" />';
				}

		  	if($args['link_to'] == 'post') {
		  		$args['url_link']=get_permalink($attachment->ID);
		  	} elseif($args['link_to'] == 'custom_link') {
  				if(isset($args['links'][$i]))
  					$args['url_link']=$args['links'][$i];
  				else
  					$args['url_link']=false;	
  			}
		  			    
		    $img_html=eventerra_hover_extras(
		    	$img.($caption!=''?'<span class="item-caption">'.$caption.'</span>':''),
		    	($use_link ? $src_full[0] : false),
		    	$args['url_link'],
		    	'rel="prettyPhoto[postgal_'.$gallery_unique_id.']"',
				  $args['url_link_target']
		    );
		    
	    	$out .= 
	    		'<div class="item" rel="slide-'.$attachment->ID.'">'.
	    			$img_html.
	    		'</div>
	    	';
	    	
	    	if(($i+1) % $args['columns'] == 0)
	    		$out .= '<div class="clear"></div>';
	    	
	    	$i++;
			}
			$out .= '</div><div class="clear"></div></div>';
		}
		
		return $out;
	}
}

/*************************************************************************************
 * Get Post Image
 *************************************************************************************/
/*
if ( !function_exists( 'eventerra_get_post_image' ) ) {
	function eventerra_get_post_image($post_id) { 
	
		$attachments = eventerra_get_post_gallery_images($post_id, array(
			'only_first' => true
		));

		if( !empty($attachments) ) {
			foreach( $attachments as $attachment ) {
				
		    $src = wp_get_attachment_image_src( $attachment->ID, 'full' );
		    $src['alt'] = ( empty($attachment->post_content) ) ? $attachment->post_title : $attachment->post_content;
		    return $src;
			}
		}
		
		return false;
	}
}
*/
/*************************************************************************************
 * Image resize on fly
 *************************************************************************************/

if ( !function_exists( 'eventerra_img_resize' ) ) {  
	function eventerra_img_resize($url,$thumb_size,$single=true) {
		global $EVENTERRA_THUMBNAIL_SIZES;
		
		if(isset($EVENTERRA_THUMBNAIL_SIZES[$thumb_size]) && function_exists('omaq_resize')) {
			return omaq_resize($url,$EVENTERRA_THUMBNAIL_SIZES[$thumb_size]['width'],$EVENTERRA_THUMBNAIL_SIZES[$thumb_size]['height'],$EVENTERRA_THUMBNAIL_SIZES[$thumb_size]['crop'],$single);
		} else {
			if($single)
				return $url;
			else
				return false;
		}
	}
}

if ( !function_exists( 'eventerra_img_custom_resize' ) ) {  
	function eventerra_img_custom_resize($url, $width, $height = null, $crop = null, $single=true) {
		if(function_exists('omaq_resize')) {
			return omaq_resize($url,$width,$height,$crop,$single);
		} else {
			if($single)
				return $url;
			else
				return false;
		}
	}
}

if ( !function_exists( 'eventerra_img_any_resize' ) ) {  
	function eventerra_img_any_resize($att_id, $thumb_size='full', $crop = null, $single=true) {
		global $_wp_additional_image_sizes;
		global $EVENTERRA_THUMBNAIL_SIZES;
		
		$no_resize=false;
		
		if(( isset( $_wp_additional_image_sizes[$thumb_size] ) && is_array( $_wp_additional_image_sizes[$thumb_size] ) ) || in_array( $thumb_size, array( 'thumbnail', 'thumb', 'medium', 'large', 'full' ) )) {
			
			$src=wp_get_attachment_image_src( $att_id, $thumb_size );
			if($single) {
				return $src[0];
			} else {
				return $src;
			}
			
		} elseif(isset($EVENTERRA_THUMBNAIL_SIZES[$thumb_size])) {
			
			$src=wp_get_attachment_image_src( $att_id, 'full' );
			return eventerra_img_resize($src[0], $thumb_size, $single);
			
		} elseif(preg_match_all( '/\d+/', $thumb_size, $thumb_matches )) {

			if ( isset( $thumb_matches[0] ) ) {
				$width=$height=false;
				if ( count( $thumb_matches[0] ) > 1 ) {
					$width = $thumb_matches[0][0];
					$height = $thumb_matches[0][1];
				} elseif ( count( $thumb_matches[0] ) == 1 ) {
					$width = $height = $thumb_matches[0][0];
				}
				if($width && $height) {
					$src=wp_get_attachment_image_src( $att_id, 'full' );
					return eventerra_img_custom_resize($src[0], $width, $height, $crop, $single);
				} else {
					$no_resize=true;
				}
			}

		} else {
			$no_resize=true;
		}
			
		if($no_resize) {
			if($single) {
				$src=wp_get_attachment_image_src( $att_id, 'full' );
				if($src) {
					return $src[0];
				} else {
				 	return false;
				}
			} else {
				return false;
			}
		}
		
	}
}

if ( !function_exists( 'eventerra_post_thumbnail' ) ) { 
	function eventerra_post_thumbnail($thumb_size, $args=array()) {
		
		echo eventerra_get_post_thumbnail($thumb_size, $args);
		
	}
}

if ( !function_exists( 'eventerra_get_post_thumbnail' ) ) { 
	function eventerra_get_post_thumbnail($thumb_size, $args=array()) {

    $args=wp_parse_args($args,array(
    	'single' => true,
    	'nolazyload' => false,
    	'post_id' => false,
    ));
		
		$html='';
		$full_image=false;
		$post_id=$args['post_id'] ? $args['post_id'] : get_the_ID();
		$post_thumbnail_id = get_post_thumbnail_id( $post_id );
		if($post_thumbnail_id) {
			
			$image = wp_get_attachment_image_src($post_thumbnail_id, 'full');
			if($image) {
				$full_image=$image;
				$src=$image[0];
				
				if(is_array($thumb_size)) {
					global $EVENTERRA_THUMBNAIL_SIZES;
					
					$image_resized=array();
					$def_src_set=false;
					foreach($thumb_size['srcset'] as $v) {
						$tmp=eventerra_img_resize($full_image[0], $v, false);
						if($tmp) {
							$image_resized[]=array('src' => $tmp[0], 'w' => $EVENTERRA_THUMBNAIL_SIZES[$v]['width']);
							if(!$def_src_set) {
								$src=$tmp[0];
								$def_src_set=true;
							}
						}
					}
				} else {
					$image_resized=eventerra_img_resize($full_image[0], $thumb_size, false);
					if($image_resized) {
						$src=$image_resized[0];
					}
				}
				
				$attachment = get_post($post_thumbnail_id);
				$attr = array(
					'src'   => $src,
					'class' => "attachment-".(is_array($thumb_size) ? $thumb_size['srcset'][0] : $thumb_size),
					'alt'   => trim(strip_tags( get_post_meta($post_thumbnail_id, '_wp_attachment_image_alt', true) )), // Use Alt field first
				);
				if ( empty($attr['alt']) )
					$attr['alt'] = trim(strip_tags( $attachment->post_excerpt )); // If not, Use the Caption
				if ( empty($attr['alt']) )
					$attr['alt'] = trim(strip_tags( $attachment->post_title )); // Finally, use the title
				
				if(is_array($thumb_size)) {
					$attr['srcset']=array();
					foreach($image_resized as $v) {
						$attr['srcset'][]=$v['src'].' '.$v['w'].'w';
					}
					$attr['srcset']=implode(', ',$attr['srcset']);
					
					if(isset($thumb_size['sizes'])) {
						if(preg_match_all('/{(.+?)}/', $thumb_size['sizes'], $m)) {
							foreach($m[0] as $k=>$v) {
								if(isset($EVENTERRA_THUMBNAIL_SIZES[$m[1][$k]])) {
									$thumb_size['sizes']=str_replace($v,$EVENTERRA_THUMBNAIL_SIZES[$m[1][$k]]['width'],$thumb_size['sizes']);
								}
							}
						}
						$attr['sizes']=$thumb_size['sizes'];
					}
				}
				
				if(get_option("eventerra_lazyload") == 'true' && $args['nolazyload'] != true) {
					$attr['data-src']=$attr['src'];
					$attr['src']='data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==';
					if(isset($attr['srcset'])) {
						$attr['data-srcset']=$attr['srcset'];
						unset($attr['srcset']);
					}
					if(isset($attr['sizes'])) {
						$attr['data-sizes']=$attr['sizes'];
						unset($attr['sizes']);
					}
					$attr['class'].=' lazyload';
				}

				$attr = apply_filters( 'wp_get_attachment_image_attributes', $attr, $attachment );
				$attr = array_map( 'esc_attr', $attr );
				$html = '<img';
				foreach ( $attr as $name => $value ) {
					$html .= " $name=" . '"' . $value . '"';
				}
				$html .= ' />';
			}
			
		}
		
		if($args['single']) {
			return $html;
		} else {
			return array(
				'html' => $html,
				'full_image' => $full_image,
			);
		}
			
	}
}

/*************************************************************************************
 * Hover Extras
 *************************************************************************************/

if ( !function_exists( 'eventerra_hover_extras' ) ) { 
	function eventerra_hover_extras($content, $zoom_link=false, $url_link=false, $zoom_link_attr='', $url_link_attr='') {
		
		$out='';
		
		if($zoom_link || $url_link ) {
			if($zoom_link && $url_link)
				$out.='<div class="hover-image-extra two-links">';
			else
				$out.='<div class="hover-image-extra single-link">';
			
				$out .= '<span class="back">'.$content.'</span>';
				$out .= '<span class="over">';
				if($zoom_link) {
					$out.='<a href="'.esc_url($zoom_link).'" class="link-zoom" '.$zoom_link_attr.'></a>';
				}
				if($url_link) {
					$out.='<a href="'.esc_url($url_link).'" class="link-url" '.$url_link_attr.'></a>';
				}
				$out.='</span>';
				
			$out.='</div>';
		} else {
			$out .= $content;
		}
		
		return $out;
		
	}
}

/*************************************************************************************
 * LazyLoad for inline images
 *************************************************************************************/

if ( !function_exists( 'eventerra_img_insert_lazyload' ) ) { 
	function eventerra_img_insert_lazyload($img) {
		if(strpos($img,'data-src=') === false) {
			$img = preg_replace( '/<img(.*?)src=/i', '<img$1src="data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==" data-src=', $img );
			$img = preg_replace( '/<img(.*?)srcset=/i', '<img$1data-srcset=', $img );
			$img = preg_replace( '/<img(.*?)sizes=/i', '<img$1data-sizes=', $img );
			
			// add the lazy class to the img element
			if ( preg_match( '/class=["\']/i', $img ) ) {
				$img = preg_replace( '/class=(["\'])(.*?)["\']/i', 'class=$1lazyload $2$1', $img );
			} else {
				$img = str_ireplace( '<img', '<img class="lazyload"', $img );
			}
		}
		return $img;
	}
}

if(get_option("eventerra_lazyload") == 'true') { 
	
	if ( !function_exists( 'eventerra_inline_lazyload' ) ) { 
		function eventerra_inline_lazyload($content) {
			
			$matches = array();
			preg_match_all( '/<img\s+.*?>/', $content, $matches );

			$search = array();
			$replace = array();
	
			foreach ( $matches[0] as $imgHTML ) {
				
				$replaceHTML=eventerra_img_insert_lazyload($imgHTML);
				
				if($replaceHTML != $imgHTML) {
					array_push( $search, $imgHTML );
					array_push( $replace, $replaceHTML );
				}
				
			}

							
			$content = str_replace( $search, $replace, $content);
			
			return $content;
		}
		add_filter('the_content', 'eventerra_inline_lazyload');
	}
	
}