<?php 
/** 
 * Displays the photos metabox on the gallery
 */
if ( !function_exists('osfa_gallery_photos_metabox') ) {
	function osfa_gallery_photos_metabox( $post ) {

		$images = get_posts( array(
	      'post_type' => 'attachment',
	      'posts_per_page' => -1,
	      'post_status' => null,
	      'post_parent' => $post->ID,
	      'orderby' => 'menu_order',
	      'order' => 'ASC'        
	    ) );
		?>

		<?php if ( $images ) : ?>
		<ul>
			<?php foreach ( $images as $image ) : ?>
			<li><?php echo wp_get_attachment_image( $image->ID, array(200, 200) ) ?></li>
			<?php endforeach ?>
		</ul>
		<?php endif ?>

		<div class="osfa_button_container">	
			<a title="<?php _e( 'Add Photos', 'hitched' ) ?>" class="thickbox button-primary" id="add_image" href="media-upload.php?post_id=<?php echo $post->ID ?>&amp;type=image&amp;TB_iframe=1&amp;width=640&amp;height=445"><?php _e( 'Add photos', 'hitched' ) ?></a>
		</div>
		<?php
	}
}

/**
 * Display the settings for the gallery 
 */
if ( !function_exists('osfa_gallery_settings_metabox') ) {
	function osfa_gallery_settings_metabox( $post ) {
		$gallery_size = get_post_meta( $post->ID, 'gallery_size', true );
			
		wp_nonce_field( 'hitched_gallery_settings', '_hitched_nonce' );	
		?>

		<p class="meta-options">
			<select name="gallery_size">
				<option value="large" <?php selected( $gallery_size, 'large' ) ?>><?php _e( 'Two columns', 'hitched' ) ?></option>
				<option value="medium" <?php selected( $gallery_size, 'medium' ) ?>><?php _e( 'Four columns', 'hitched' ) ?></option>
				<option value="thumbnails" <?php selected( $gallery_size, 'thumbnails' ) ?>><?php _e( 'Six columns', 'hitched' ) ?></option>
			</select>
		</p>

		<p class="meta-options">
			<label class="selectit" for="lightbox_slideshow">
				<input type="checkbox" id="lightbox_slideshow" value="on" name="lightbox_slideshow" <?php checked( get_post_meta( $post->ID, 'lightbox_slideshow', true ), 'on' ) ?> />
				<?php _e( 'Enable slideshow in lightbox', 'hitched' ) ?>
			</label>
		</p>

		<?php

	}	
}

/**
 * First panel
 */
if ( !function_exists('osfa_homepage_panel_1_metabox') ) {
	function osfa_homepage_panel_1_metabox( $post ) {
		// Nonce field for validation purposes
		wp_nonce_field( 'hitched_homepage_settings', '_hitched_nonce' );

		// Content editor
		wp_editor( $post->post_content, 'hitched_home_panel_1', array( 'textarea_name' => 'content' ) );
	}
}

/**
 * Second panel
 */
if ( !function_exists('osfa_homepage_panel_2_metabox') ) {
	function osfa_homepage_panel_2_metabox( $post ) {
		// Editor
		$content = get_post_meta( $post->ID, 'content_panel_two', true );		
		wp_editor( $content, 'hitched_home_panel_2', array( 'textarea_name' => 'content_panel_two' ) );
	}
}

/**
 * Carousel metabox
 */
if ( !function_exists('osfa_homepage_carousel_metabox') ) {
	function osfa_homepage_carousel_metabox( $post ) {
		$galleries = get_posts( array(
            'post_type' => 'gallery', 
            'post_status' => 'publish',
            'orderby' => 'title'
        ));

        $selected = get_post_meta( $post->ID, 'carousel_gallery_id', true );
		
		if ( count( $galleries ) ) : ?>

			<p><?php printf( 
				__( 'Select which gallery you would like to use for the carousel, or %screate a new gallery%s.', 'hitched' ),
				'<a href="'.admin_url( "post-new.php?post_type=gallery" ).'">',
				'</a>' 
				)
				?>			
			 </p>
			<select name="carousel_gallery_id">
	            <?php foreach ( $galleries as $gallery ) : ?>
	            <option value="<?php echo $gallery->ID ?>"<?php selected( $gallery->ID, $selected ) ?>><?php echo $gallery->post_title ?></option>
	            <?php endforeach ?>
	        </select>

        <?php else : ?>

        	<p><?php printf( '%s <a href="%s">%s</a>', 
	        	__( 'It looks like you haven\'t created any galleries yet. %sCreate one now%s', 'hitched'), 
	        	'<a href="'.admin_url( "post-new.php?post_type=gallery" ).'">',
	        	'</a>' 
	        	)
	        	?>
        	</p>

        <?php endif;
	}
}

/**
 * Slider metabox
 */
if ( !function_exists('osfa_homepage_slider_metabox') ) {
	function osfa_homepage_slider_metabox( $post ) {
		$galleries = get_posts( array(
            'post_type' => 'gallery', 
            'post_status' => 'publish',
            'orderby' => 'title'
        ));

        $selected = get_post_meta( $post->ID, 'slider_gallery_id', true );
		
		if ( count( $galleries ) ) : ?>

			<p><?php printf( 
				__( 'Select which gallery you would like to use for the homepage slider, or %screate a new gallery%s.', 'hitched' ),
				'<a href="'.admin_url( "post-new.php?post_type=gallery" ).'">',
	        	'</a>'
				)
				?>
			</p>

			<select name="slider_gallery_id">
	            <?php foreach ( $galleries as $gallery ) : ?>
	            <option value="<?php echo $gallery->ID ?>"<?php selected( $gallery->ID, $selected ) ?>><?php echo $gallery->post_title ?></option>
	            <?php endforeach ?>
	        </select>

        <?php else : ?>

			<p><?php printf( '%s <a href="%s">%s</a>', 
	        	__( 'It looks like you haven\'t created any galleries yet. %sCreate one now%s', 'hitched'), 
	        	'<a href="'.admin_url( "post-new.php?post_type=gallery" ).'">',
	        	'</a>' 
	        	)
	        	?>
        	</p>

        <?php endif;
	}
}