<?php // Custom Post Types

function post_type_portfolio() {
	
	require(ghostpool_inc . 'options.php');
	
	/*************************** Portfolio Post Type ***************************/
	
	register_post_type('portfolio', array(
		'labels' => array('name' => __('Portfolio Items', 'gp_lang'), 'singular_label' => __('Portfolio Item', 'gp_lang'), 'add_new_item' => __('Add Portfolio Item', 'gp_lang'), 'edit_item' => __('Edit Portfolio Item', 'gp_lang'), 'search_items' => __( 'Search Portfolio Items', 'gp_lang')),
		'public' => true,
		'show_ui' => true,
		'_builtin' => false,
		'_edit_link' => 'post.php?post=%d',
		'capability_type' => 'post',
		'hierarchical' => false,
		'rewrite' => array('slug' => $theme_portfolio_slug),
		'menu_position' => 20,
		'with_front' => true,
		'supports' => array('title', 'editor', 'comments', 'author', 'custom-fields', 'thumbnail'),
		'taxonomies' => array('post_tag')
	));
	
	
	/*************************** Portfolio Categories ***************************/
	
	register_taxonomy('portfolio_categories', 'portfolio', array('hierarchical' => true, 'labels' => array('name' => __('Portfolio Categories', 'gp_lang'), 'singular_label' => __('Portfolio Category', 'gp_lang'), 'add_new_item' => __('Add Portfolio Category', 'gp_lang'), 'search_items' => __('Search Portfolio Categories', 'gp_lang')), 'rewrite' => array('slug' => $theme_portfolio_cat_slug)));

	
	/*************************** Portfolio Item Columns ***************************/
	
	add_filter("manage_edit-portfolio_columns", "portfolio_edit_columns");
	add_action("manage_posts_custom_column",  "portfolio_custom_columns");
	
	function portfolio_edit_columns($columns){
			$columns = array(
				"cb" => "<input type=\"checkbox\" />",
				"title" => __('Title', 'gp_lang'),
				"portfolio_desc" => __('Description', 'gp_lang'),	
				"portfolio_categories" => __('Categories', 'gp_lang'),
				"portfolio_image" => __('Image', 'gp_lang'),				
				"date" => __('Date', 'gp_lang')
			);
	
			return $columns;
	}
	
	function portfolio_custom_columns($column){
			global $post;
			require(ghostpool_inc . 'options.php');
			switch ($column)
			{
				case "portfolio_desc":
					echo excerpt(10);
					break;
				case "portfolio_categories":
					echo get_the_term_list($post->ID, 'portfolio_categories', '', ', ', '');
					break;
				case "portfolio_image":		
															
					$args = array('post_type' => 'attachment', 'post_mime_type' => 'image', 'post_parent' => $post->ID, 'numberposts' => 1, 'orderby' => 'menu_order', 'order' => 'ASC');
					
					$attachments = get_children($args); 
						
					if(has_post_thumbnail() OR $attachments) {
					
						echo '<img src="';					
						if($theme_timthumb == "0") {
							echo get_template_directory_uri().'/lib/scripts/timthumb.php?src=';
						}					
							
						if(has_post_thumbnail()) {
								
							$image = wp_get_attachment_image_src(get_post_thumbnail_id(), 'full');
							echo $image[0];
							
						} elseif ($attachments) { 
						
							foreach ($attachments as $attachment) {
								echo wp_get_attachment_url($attachment->ID);
							}
							
						}
						
						if($theme_timthumb == "0") {
							echo '&amp;h=0&amp;w=100&amp;zc=1';
						}
						echo '" alt="" width="100" />';	
						
					}
					
					break;					
			}
	}
}
	
add_action('init', 'post_type_portfolio');


/*************************** Portfolio Slide Fields ***************************/

function ghostpool_attachment_fields_to_edit($form_fields, $post) {
	$form_fields["ghostpool_slide_video"] = array(
		"label" => __('Slide Video', 'gp_lang'),
		"input" => "text",
		"value" => get_post_meta($post->ID, "_ghostpool_slide_video", true),
		"helps" => __('The URL of your video or audio file. YouTube, Vimeo, FLV, MP4 and MP3 formats accepted.', 'gp_lang'),
	);
	$form_fields["ghostpool_webm_mp4_slide_video"] = array(
		"label" => __('HTML5 Video URL (Safari/Chrome)', 'gp_lang'),
		"input" => "text",
		"value" => get_post_meta($post->ID, "_ghostpool_webm_mp4_slide_video", true),
		"helps" => __('Enter your HTML5 video URL for Safari/Chrome (WEBM/MP4).', 'gp_lang'),
	);	
	$form_fields["ghostpool_ogg_slide_video"] = array(
		"label" => __('HTML5 Video URL (FireFox/Opera)', 'gp_lang'),
		"input" => "text",
		"value" => get_post_meta($post->ID, "_ghostpool_ogg_slide_video", true),
		"helps" => __('Enter your HTML5 video URL for FireFox/Opera (OGG/OGV).', 'gp_lang'),
	);		
	$form_fields["ghostpool_slide_image_width"] = array(
		"label" => __('Cropped Slide Image Width', 'gp_lang'),
		"input" => "text",
		"value" => get_post_meta($post->ID, "_ghostpool_slide_image_width", true),
		"helps" => __('The width to crop the image to (px).', 'gp_lang'),
	);	
	$form_fields["ghostpool_slide_image_height"] = array(
		"label" => __('Cropped Slide Image Height', 'gp_lang'),
		"input" => "text",
		"value" => get_post_meta($post->ID, "_ghostpool_slide_image_height", true),
		"helps" => __('The height to crop the image to (px).', 'gp_lang'),
	);	
   return $form_fields;
}
add_filter("attachment_fields_to_edit", "ghostpool_attachment_fields_to_edit", null, 2);

function ghostpool_attachment_fields_to_save($post, $attachment) {
	if( isset($attachment['ghostpool_slide_video']) ){
		update_post_meta($post['ID'], '_ghostpool_slide_video', $attachment['ghostpool_slide_video']);
	}
	if( isset($attachment['ghostpool_webm_mp4_slide_video']) ){
		update_post_meta($post['ID'], '_ghostpool_webm_mp4_slide_video', $attachment['ghostpool_webm_mp4_slide_video']);
	}	
	if( isset($attachment['ghostpool_ogg_slide_video']) ){
		update_post_meta($post['ID'], '_ghostpool_ogg_slide_video', $attachment['ghostpool_ogg_slide_video']);
	}	
	if( isset($attachment['ghostpool_slide_image_width']) ){
		update_post_meta($post['ID'], '_ghostpool_slide_image_width', $attachment['ghostpool_slide_image_width']);
	}	
	if( isset($attachment['ghostpool_slide_image_height']) ){
		update_post_meta($post['ID'], '_ghostpool_slide_image_height', $attachment['ghostpool_slide_image_height']);
	}				
	return $post;
}
add_filter("attachment_fields_to_save", "ghostpool_attachment_fields_to_save", null , 2);


?>