<?php

function cloudfw_portfolio($atts = array()) {
	
	extract(shortcode_atts(array(
		'id'				=> NULL,
		'from'				=> 'a category',
		'columns'			=> 3,
		'title'				=> NULL,
		'subtitle'			=> NULL,
		'height'			=> 260,
		'carousel'	 		=> FALSE,
		"forcing"	 		=> NULL,
		"limit"				=> -1
	), $atts));
	
	switch ($from){
		default:
		case 'a category':
			
			$categories = get_term($id, 'cat-portfolio');
			
			$args = array(
				'post_type'	 		=>	array('portfolio'),
				'post_status' 		=>	'publish',
				'cat-portfolio'		=>  $categories -> slug,
				'posts_per_page' 	=>  $limit,
				'orderby'    		=>  'menu_order ID',
				'order'      		=>  'ASC',
			);
					
			$portfolio_source = cloudfw_get_portfolio_posts($args);
		
		break;
		case 'selected posts':
			
			$id = explode(',',$id);
			
			$args = array(
				'post_type'	 		=>	array('portfolio'),
				'post_status' 		=>	'publish',
				'post__in'  		=>  $id,
				'posts_per_page' 	=>  $limit,
			);
					
			$portfolio_source = cloudfw_get_portfolio_posts($args);
			
			if (is_array($portfolio_source)) 
				$portfolio_source = sortArrayByArray($portfolio_source, $id);
			
		
		break;
		case 'a post gallery':
			
			if (empty($id)) $id = get_the_ID();
			
			$portfolio_source = cloudfw_get_portfolio_posts_from_a_post($id,$limit);
		
		break;
	}
	
	$i = 0;
	$width = cloudfw_get_image_width($columns);
	
	if ($carousel == '1' && !count($portfolio_source)>$columns) {$carousel == '0';}
	
	
	ob_start();
	
	$unique_id = 'portfolio-'.cloudfw_randomizer(3);
	
	if (is_array($portfolio_source)):
	
	if ($title && $subtitle) {$title .= "<span class=\"sub\">$subtitle</span>"; }
	if ($carousel == '1') echo '<div style="position:relative;"><h1 class="portfolio-title block-title">',$title ? $title: 'Please Insert A Title<span class="sub">When Using A Portfolio With Carousel Attribute</span>','</h1><div class="carousel-navigation"><a href="#previous" class="carousel-previous sprite" data-rel="'.$unique_id.'">Previous</a><a href="#next" class="carousel-next sprite" data-rel="'.$unique_id.'">Next</a></div></div>';
	
	echo '<div id="'.$unique_id.'" class="portfolio', $carousel == '1' ? ' carousel':NULL ,'" data-columns="'.$columns.'">';
	
		foreach ($portfolio_source as $post_id => $post_data) {$i++;
		
			
			if ($i%$columns == 0){
				if ($carousel !== '1') $class = ' last';
			}else{
				$class = NULL;
			}
			
			$image_width = 260;
			
			$lb_source = NULL;
			if (!empty($post_data["video"])) $lb_source = htmlentities(($post_data["video"]));
			if (!empty($post_data["custom_image"])) $lb_source = $post_data["custom_image"];
			if (empty($lb_source)) $lb_source = $post_data["full_image"];
				
			
			echo '
				<div class="cl portfolio-item unselectable cl_'.$columns.$class.'" style="height:'.$height.'px;">
			';
			
			if ($columns == 1) { echo '<div class="cl cl_2">'; }
					
			if($post_data["inlightbox"] == "lightbox") 
				echo '<a href="'.$lb_source.'" rel="prettyPhoto['.$unique_id.']" title="'.esc_attr($post_data["desc"]).'">'; 
			elseif($post_data["inlightbox"] == "inpage") 
				echo '<a href="'.$post_data["permalink"].'" title="'.esc_attr($post_data["desc"]).'" class="cloudfw_page" data-post-id="'.$post_id.'">'; 
			else 
				echo '<a href="'._isso($post_data["custom_url"], __url($post_data["custom_url"]), $post_data["permalink"]).'">';
			
			echo '<div class="shadow"></div><img src="'.cloudfw_thumbnail(array('src'=> $post_data["large_image"],'w'=> $width,'h'=> $height )).'" alt="" class="load" width="'.$width.'" height="'.$height.'" />';
			echo '
			
				<div class="portfolio-overlay" style="width:'.$width.'px;">
				
					<div class="portfolio-content text-center">
						<h4>'.$post_data["title"].'</h4>
						',$post_data["category"] ? '<div class="portfolio-content-cat">
							'.$post_data["category"].'
						</div>':NULL,'
						
						',$post_data["desc"] ? '<div class="portfolio-content-desc">
							'.$post_data["desc"].'
						</div>':NULL,'
						
					</div>
				
				</div>
			
			';
			
			
			echo '</a>';
			
			if ($columns == 1) { 

				$post_data["content"] = do_shortcode($post_data["content"]);
				echo '</div><div class="cl cl_2 last">',$post_data["content"] ? $post_data["content"] : $post_data["desc"],'</div><div class="cf"></div>'; 

			}
			
			echo '</div>';
			
		
	}

	echo '</div><div class="cf"></div>';
		
	endif; // is_array($portfolio_source)
	
	
	
	ob_end_flush;
	$out = ob_get_contents();
	ob_end_clean();
	
	return "[raw]{$out}[/raw]";
	
}



function cloudfw_get_portfolio_posts($args) {
	
	if (empty($args)) return false;
	
 		$posts = new WP_Query( $args );

        if( $posts->have_posts()) : while( $posts->have_posts() ) : $posts->the_post();
						
			$id = get_the_ID();
			$metas = get_post_meta($id, false);
			$thumbs = get_post_thumbnail_id($id);
		
			$thumbnail = wp_get_attachment_image_src( $thumbs, 'medium');
			$full_image = wp_get_attachment_image_src( $thumbs, 'full');
			$large_image = wp_get_attachment_image_src( $thumbs, 'large');
			
			
			$cat = wp_get_object_terms($id, 'cat-portfolio');
			unset($cats);
			
			foreach($cat as $cat_id => $cat_data){
				
				$cats .= $cat_data -> name.', ';
				
				}
				
			if (!empty($cats)) $cats = substr($cats,0,-2); 
			

			$out[$id] = array(
				
				'title' =>  __translate( get_the_title() ),
				'permalink' => __url( get_permalink() ),
				'content' => __translate( get_the_content() ),
				'category' =>  $cats,
				'thumbnail' => $thumbnail[0] ,
				'large_image' => $large_image[0],
				'full_image' => $full_image[0],
				'desc' => $metas[PFIX.'_port_desc'][0],
				'inlightbox' =>  $metas[PFIX.'_port_lightbox'][0],
				'video' =>  $metas[PFIX.'_port_video'][0],
				'custom_image' =>  $metas[PFIX.'_custom_image'][0],
				'custom_url' =>  $metas[PFIX.'_custom_url'][0]
	
			);		
			
		endwhile; endif;

	return $out;

}


function cloudfw_get_portfolio_posts_from_a_post($id, $limit = -1){
	
	$gallery = get_children( array('post_parent' => $id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order', 'numberposts' => $limit ) );

	if (is_array($gallery)):
	
	foreach ($gallery as $gal_id => $gal) {
		
		 $title	= 	$gal -> post_title;
		 $desc	= 	$gal -> post_content;
		
		 $thumbs = get_post_thumbnail_id($id);
		 
		 $thumbnail = wp_get_attachment_image_src( $gal_id, 'medium');
		 $full_image = wp_get_attachment_image_src( $gal_id, 'full');
		 $large_image = wp_get_attachment_image_src( $gal_id, 'large');
		 
		 $out[] = array(
				
				'title' =>  __translate( $title ),
				'permalink' => __url( get_permalink() ),
				'thumbnail' => $thumbnail[0] ,
				'large_image' => $large_image[0],
				'full_image' => $full_image[0],
				'desc' => $desc,
				'inlightbox' =>  'lightbox'
	
			);
		 
		}
	
	endif;
	
	return $out;

}

?>