<?php
/**
 * Create some cool functions to use with the custom post types
 * @package Ultra WordPress Theme
 * @since 1.0
 * @author AJ Clarke : http://wpexplorer.com
 * @copyright Copyright (c) 2012, AJ Clarke
 * @link http://wpexplorer.com
 * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 */



/*************
* ALL
*************/


/*
 * Return Cropped Image based on shortcode parameters
 * @since 1.0
*/

function wpex_get_cropped_grid_image_url( $postid, $columns, $thumbnail_width, $thumbnail_height, $thumbnail_hard_crop ) {
	
	// Image Width
	if ( $thumbnail_width ) {
		$thumbnail_width = $thumbnail_width; // Width defined by shortcode parameter
	} else {
		if ( $columns == 2 || $columns == 1 ) {
			$thumbnail_width = '450';
		} elseif ( $columns == 3 )  {
			$thumbnail_width = '400';
		} elseif ( $columns == 4 )  {
			$thumbnail_width = '400';
		} else  {
			$thumbnail_width = '400'; // 5 column +
		}
	}
	
	// Image Height
	if ( $thumbnail_hard_crop == 'false' || $thumbnail_height == 9999 ) {
		$thumbnail_height = 9999;
		$thumbnail_hard_crop = false;
	} else {
		$thumbnail_hard_crop = true;
		if ( $thumbnail_height ) {
			$thumbnail_height = $thumbnail_height;
		} else {
			if ( $columns == 2 || $columns == 1 ) {
				$thumbnail_height = '340';
			} elseif ( $columns == 3 )  {
				$thumbnail_height = '300';
			}  elseif ( $columns == 4 )  {
				$thumbnail_height = '300';
			} else {
				$thumbnail_height = '300';
			}
			
		 }
	}
	
	// Return Resized Image
	return apply_filters('wpex_get_cropped_grid_image_url', aq_resize( wp_get_attachment_url( get_post_thumbnail_id( $postid ) ),  $thumbnail_width, $thumbnail_height, $thumbnail_hard_crop ) );

}



/*
 * Create Arrays of Post Type Categories
 * @since 1.0
*/


// Portfolio
function wpex_posttype_cats($post_type) {
	
	if ( !post_type_exists( $post_type ) ) return;
	
	$post_type_cats = array();
	$post_type_cats_obj = get_terms( $post_type .'_category' );
	$post_type_cats[''] = __('All','wpex');
	foreach ( $post_type_cats_obj as $post_type_cat ) {
		$post_type_cats[$post_type_cat->term_id] = $post_type_cat->name;
	}
	return $post_type_cats;	
}



// Staff


// 



/*************
* Portfolio
*************/


/*
 * Return URL for portfolio entries
 * @since 1.0
*/
function wpex_portfolio_url($postid, $lightbox_only) {
	
	// Custom fields
	$style = get_post_meta( $postid, 'wpex_portfolio_post_style', true );
	$redirect_url = get_post_meta( $postid, 'wpex_portfolio_post_redirect_url', true );
	$video_url = get_post_meta( $postid, 'wpex_portfolio_post_video', true );
	
	// Lighbox Only Links
	if ( $lightbox_only == 'true' ) {
		
		// Video Lightbox
		if ( $video_url ) {
			$wpex_portfolio_entry_url = $video_url;
		} else {
		// Image Lightbox
			$wpex_portfolio_entry_url = wp_get_attachment_url( get_post_thumbnail_id($postid) );
		}
		
	// Default Links
	} else {
	
		// Redirection
		if ( $style == 'Redirect' && $redirect_url !== '' ) {
			$wpex_portfolio_entry_url = $redirect_url;
			
		// Video Lightbox	
		} elseif ( $style == 'Lightbox' && $video_url !== '' ) {
			$wpex_portfolio_entry_url = $video_url;
			
		// Plain Lightbox
		} elseif ( $style == 'Lightbox' ) {
			$wpex_portfolio_entry_url = wp_get_attachment_url( get_post_thumbnail_id( $postid ) );
		
		// Default permalink
		} else {
			$wpex_portfolio_entry_url = get_permalink($postid);
		}
	
	}
	
	return apply_filters('wpex_portfolio_url', $wpex_portfolio_entry_url); // Apply Filters for easier child theme editing
	
}


/*
 * Return an icon for portfolio overlays
 * @since 1.0
*/
function wpex_portfolio_icon($postid, $lightbox_only) {	
	
	// Custom Fields
	$video = get_post_meta( $postid, 'wpex_portfolio_post_video', true );
	$post_style = get_post_meta( $postid, 'wpex_portfolio_post_style', true );	
	
	// Lightbox Only
	if ( $lightbox_only == 'true' ) {
		if ( $video !== '' ) {
			$icon = 'film';
		} else {
			$icon = 'search';
		}
	}
	
	// Default Entries
	else {
	
		// Video
		if ( $video !== '' ) {
			$icon = 'film';
		
		// Lightbox
		} elseif( $post_style == 'Lightbox' ) {
			$icon = 'search';
		
		// Redirect
		} elseif( $post_style == 'Redirect' ) {
			$icon = 'link';
		
		// Default
		} else {
			$icon = 'plus';
		}
	
	}
	
	return apply_filters('wpex_portfolio_icon', $icon); // Apply Filters for easier child theme editing
}




/*************
* Services
*************/


/*
 * Return URL for services entries
 * @since 1.0
*/
function wpex_service_url($postid) {
	if ( $postid ) {
		$services_entry_url = get_permalink( $postid );
		$redirect_url = get_post_meta( $postid, 'wpex_services_post_redirect_url', true );
		if ( $redirect_url ) {
			$services_entry_url = $redirect_url;
		}
		return $services_entry_url;
	}
}
	

/*
 * Return service icon
 * @since 1.0
*/
function wpex_service_icon($postid) {
	
	$font_icon = get_post_meta( $postid, 'wpex_services_icon_font', true );	
	$img_icon = get_post_meta( $postid, 'wpex_services_icon', true );
	$title = get_the_title($postid);
	
	$output = '';
	
	if ( $font_icon !== 'Select' && $font_icon !== '' ) {
		$output = '<div class="services-entry-font-icon"><span class="icon-'.$font_icon.'"></span></div>';
	} elseif ( $img_icon ) {
		$output = '<img class="services-entry-icon" src="'. $img_icon .'" alt="'. $title .'" />';
	}
	
	return $output;
}
