
<?php
global $NHP_Options;
global $data;
$is_full = ($NHP_Options->get("portfolio_single_style") == 'full');

get_template_part('templates/top','folio'); ?>

<section id="layout">

<div class="row">
    <div class="project one-photo clearing-container">


    <div class="<?php echo ($is_full) ? 'twelve' : 'eight'; ?> columns">

    <?php

    $folio_video = false;

        if (!post_password_required(get_the_id())) {
        $custom = get_post_custom($post->ID);

	    if (get_post_meta($post->ID, 'folio_vimeo_video_url', true)): ?>
		    <div class="flex-video widescreen">
			    <?php
			    $url = esc_html( get_post_meta($post->ID, 'folio_vimeo_video_url', true) );
			    echo wp_oembed_get( $url );
			    ?>
		    </div>
		    <?php $folio_video = true; endif;

	    if (get_post_meta($post->ID, 'folio_youtube_video_url', true)): ?>

		    <div class="flex-video widescreen">
			    <?php
			    $url = esc_html( get_post_meta($post->ID, 'folio_youtube_video_url', true) );
			    echo wp_oembed_get( $url );
			    ?>
		    </div>

	    <?php $folio_video = true; endif;

        $folio_self_hosted_video = get_post_meta($post->ID, 'self_hosted_video_files', true);

        if (is_array($folio_self_hosted_video)){
	        foreach($folio_self_hosted_video as $video_block => $video_array){?>

		        <div class="flex-video widescreen">
			        <video controls>
				        <source src="<?php echo $video_array['folio_self_hosted_mp4'];?>" type="video/mp4">
				        <source src="<?php echo $video_array['folio_self_hosted_webm'];?>" type="video/ogg">
			        </video>
			    </div>

	        <?php }

	        $folio_video = true;

        }

            echo '<div id="work-slider">';

		    if (metadata_exists('post', $post->ID, '_my_product_image_gallery')) {
			    $my_product_image_gallery = get_post_meta($post->ID, '_my_product_image_gallery', true);
		    } else {
			    // Backwards compat
			    $attachment_ids = get_posts('post_parent=' . $post->ID . '&numberposts=-1&post_type=attachment&orderby='.$NHP_Options->get("order_folio_images").'&order='.$NHP_Options->get("sort_order_folio_images").'&post_mime_type=image&fields=ids');
			    $attachment_ids = array_diff($attachment_ids, array(get_post_thumbnail_id()));
			    $my_product_image_gallery = implode(',', $attachment_ids);
		    }

		    $attachments = array_filter(explode(',', $my_product_image_gallery));

		    if ( $attachments ) {

			    if ( $NHP_Options->get("portfolio_single_slider") == 'full' ) {

				    foreach ( $attachments as $attachment_id ) {

					    echo '<div class="folio-images">';

					    echo '<a href="' . $image_attributes[0] . '" class="zoom" rel="prettyPhoto[pp_gal]">';

					    echo wp_get_attachment_image( $attachment_id, 'full' );

					    echo '</a></div>'; echo '  </ul></div>';
				    }
			    }
			    else {
				    echo '<div id="my-work-slider" class="loading"><ul class="slides">';

				    foreach ( $attachments as $attachment_id ) {

					    $image_attributes = wp_get_attachment_image_src( $attachment_id, 'full' ); // returns an array

					    $thumb_image = aq_resize( $image_attributes[0], 63, 63, true );


					    echo '<li data-thumb="' . $thumb_image . '">';

					    echo '<a href="' . $image_attributes[0] . '" class="zoom" rel="prettyPhoto[pp_gal]">';

					    echo wp_get_attachment_image( $attachment_id, 'full' );

					    echo '</a></li>';
				    }

			    }
		    }elseif (has_post_thumbnail()) {
                $thumb = get_post_thumbnail_id();
                $img_url = wp_get_attachment_url($thumb, 'full'); //get img URL
                $img_width = $is_full ? 1200 : 940;
                $img_height = $is_full ? 1200 : 999;
                $article_image = aq_resize($img_url, $img_width, $img_height, false); //resize & crop img


            if ($NHP_Options->get("portfolio_single_featured")) {
                if ((has_post_thumbnail()) && !$folio_video) { ?>

                    <img src="<?php echo $article_image ?>" alt="<?php the_title(); ?>" class="thumbnail"/>

                    <?php }
             }
            }

            $args = array(
                'order' => $NHP_Options->get("sort_order_folio_images"),
                'orderby' => $NHP_Options->get("order_folio_images"),
                'exclude' => get_post_thumbnail_id($post->ID),
                'post_type' => 'attachment',
                'post_parent' => $post->ID,
                'post_mime_type' => 'image',
                'post_status' => null,
                'numberposts' => -1,
            );
            $attachments = get_posts($args);
            $attachments_count = '0';
            if ($attachments) {
                foreach ($attachments as $attachment) {
                    $img_url = wp_get_attachment_url($attachment->ID); //get img URL

                    if ($NHP_Options->get("portfolio_single_slider") == 'full') {
                        $img_height = 9999;
                    } else {
                        $img_height = 1200;
                    }

                    $img_width = 1200;

                    $article_image = aq_resize($img_url, $img_width, $img_height, false); //resize & crop img
                    $attachments_count++;
                    ?>

                    <img src="<?php echo $article_image ?>" alt="<?php the_title(); ?>" class="thumb1"/>


                    <?php }   } ?>

</div></div>

    <div  class="folio-info <?php echo $is_full?'twelve':'four'; ?> columns">

        <?php while (have_posts()) : the_post(); ?>

        <h6 class="project-info">
            <?php if ($NHP_Options->get("folio_date") != '') {
                echo '<time>' . get_the_date() . '</time>';
            } ?>

            <span class="project-cat">

                <?php get_template_part('templates/folio', 'terms'); ?>

                <?php the_tags('<span class="delim">&nbsp;</span> <span class="tags">', ', ', '</span>'); ?>

			</span>
        </h6>

        <div class="entry-content">
            <?php the_content(); ?>
        </div>

        <ul class="person-list">

            <?php
            if(function_exists('get_field_object')){

                $fields = get_post_custom_keys( get_the_id() );
                foreach( $fields as $key=>$field ){
                    substr( $field, 0, 1);
                    if(substr( $field, 0, 1) == '_')
                        unset($fields[$key]);
                }

                $fields = array_flip($fields);
                foreach($fields as $key=>$field){
                    $fields[$key] = get_post_meta($post->ID,$key, true);
                }

                if( $fields ) {
                    foreach( $fields as $field_name => $value ) {

                        $field = get_field_object($field_name, false, array('load_value' => false));

                        if ($value) {

                            if ($field_name == 'website_link') {

                                echo '<li class="field-link"><a href="http://' . $value . '">';
                                echo $value;
                                echo '</a></li>';

                            } else {

                            }
                            if ($field['label'] != '') {
                                echo '<li><strong>';
                                echo $field['label'] . ':</strong> ';
                                echo $value;
                                echo '</li>';
                            }
                        }
                    }
                }} ?>
        </ul>
        <?php endwhile; ?>

        <?php

        if( $NHP_Options->get("post_share_button") ) { ; ?>

            <!--social share buttons start-->
            <?php get_template_part('templates/project','social'); ?>
            <!--social share ends-->
            <?php } ?>
        
    </div>


</div>


<?php } else the_content(); ?>


<div class="row">

    <?php get_template_part('templates/block','recent'); ?>

</div>

</section>

<?php if ($NHP_Options->get("portfolio_single_slider") != 'full') { ?>
	<script type="text/javascript">
		jQuery(document).ready(function () {
			var target_flexslider = jQuery('#my-work-slider');
			target_flexslider.flexslider({
				namespace: "my-work-",
				animation: "slide",
				controlNav: "thumbnails",
				smoothHeight: true,
				directionNav: false,

				start: function (slider) {
					slider.removeClass('loading');
				}

			});
		});

	</script>
<?php } ?>