<?php 

add_action('dslc_hook_register_modules',
    create_function('', 'return dslc_register_module( "Ainex_Projects" );')
);

class Ainex_Projects extends DSLC_Module {
 		
 	// Module Attributes
    var $module_id = 'Ainex_Projects';
    var $module_title = 'Projects';
    var $module_icon = 'th';
    var $module_category = 'Ainex - Posts';
 
 	// Module Options
    function options() { 
		$filter_choice = '';
		$filter_choices[] = array(
			'label' => 'Projects - Hide Filters',
			'value' => 'none'
		);
		
		$pagination_choices = '';
		$pagination_choices[] = array(
			'label' => 'Hide',
			'value' => 'none'
		);
		$pagination_choices[] = array(
			'label' => 'Show',
			'value' => 'show'
		);

		// Get categories
		$cats = get_categories('taxonomy=dslc_projects_cats');
		$cats_choices = array();

		// Generate usable array of categories
		foreach ( $cats as $cat ) {
			$cats_choices[] = array(
				'label' => $cat->name,
				'value' => $cat->cat_ID
			);
			$filter_choices[] = array(
				'label' => $cat->name,
				'value' => $cat->cat_ID
			);
		}
		
    	// The options array
    	$dslc_options = array(
			
			array(
				'label' => 'Layout',
				'id' => 'layout',
				'std' => '4',
				'type' => 'select',
				'choices' => array(
					array(
						'label' => '4 columns',
						'value' => '4'
					),
					array(
						'label' => '3 columns',
						'value' => '3'
					),
					array(
						'label' => '2 columns',
						'value' => '2'
					)
				),
			),
           	array(
    			'label' => 'Posts Per Page',
    			'id' => 'amount',
    			'std' => '12',
    			'type' => 'text',
    		),
    		array(
				'label' => 'Filter',
				'id' => 'filter-layout',
				'std' => '1',
				'type' => 'select',
				'choices' => array(
					array(
						'label' => 'Yes',
						'value' => '1'
					),
					array(
						'label' => 'No',
						'value' => '0'
					)
				)
			),
    		array(
    			'label' => 'Hide Pagination Buttons?',
    			'id' => 'pagination',
    			'std' => 'none',
    			'type' => 'select',
    			'choices' => $pagination_choices
    		),
    		array(
    			'label' => 'Show Which Categories?',
    			'id' => 'categories',
    			'std' => '',
    			'type' => 'checkbox',
    			'choices' => $cats_choices
    		),
    		array(
    			'label' => 'Order By',
    			'id' => 'orderby',
    			'std' => 'date',
    			'type' => 'select',
    			'choices' => array(
    				array(
    					'label' => 'Publish Date',
    					'value' => 'date'
    				),
    				array(
    					'label' => 'Modified Date',
    					'value' => 'modified'
    				),
    				array(
    					'label' => 'Random',
    					'value' => 'rand'
    				),
    				array(
    					'label' => 'Alphabetic',
    					'value' => 'title'
    				),
    				array(
    					'label' => 'Comment Count',
    					'value' => 'comment_count'
    				),
    			)
    		),
    		array(
    			'label' => 'Order',
    			'id' => 'order',
    			'std' => 'DESC',
    			'type' => 'select',
    			'choices' => array(
    				array(
    					'label' => 'Ascending',
    					'value' => 'ASC'
    				),
    				array(
    					'label' => 'Descending',
    					'value' => 'DESC'
    				)
    			)
    		),
    		array(
    			'label' => 'Offset',
    			'id' => 'offset',
    			'std' => '0',
    			'type' => 'text',
    		),
    	);

        $dslc_options = array_merge( $dslc_options, $this->shared_options('animation_options') );

    	// Return the array
    	return apply_filters( 'dslc_module_options', $dslc_options, $this->module_id );

    }
 
 	// Module Output
    function output( $options ) {

    	global $dslc_active;
    	
		if ( $dslc_active && is_user_logged_in() && current_user_can( DS_LIVE_COMPOSER_CAPABILITY ) )
			$dslc_is_admin = true;
		else
			$dslc_is_admin = false;

		$this->module_start( $options );
		
		// Fix for pagination
		if( is_front_page() ) { 
			$paged = ( get_query_var( 'page' ) ) ? get_query_var( 'page' ) : 1; 
		} else { 
			$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1; 
		}
		
		// General args
		$args = array(
			'paged' => $paged, 
			'post_type' => 'dslc_projects',
			'posts_per_page' => $options['amount'],
			'order' => $options['order'],
			'orderby' => $options['orderby'],
			'offset' => $options['offset']
		);

		// Category args
		if ( isset( $options['categories'] ) && $options['categories'] != '' ) {
			$terms = explode( ' ', $options['categories']);
			$args['tax_query'] = array(
				array(
					'taxonomy' => 'dslc_projects_cats',
					'field' => 'id',
					'terms' => $terms,
					'operator' => 'IN',
				)
			);
		}

		// Do the query
		$block_query = new WP_Query( $args );
		?>

		<?php global $ainex ?>

		<?php if ($options['filter-layout']==1): ?>
            <div class="portfolio_page">
                <div class="portfolioFilter">
                    <a href="#" data-filter="*" class="current">All</a>
                    <?php
                        $filters = get_categories('taxonomy=dslc_projects_cats');
                        foreach( $filters as $filter ){
                            echo ' <a href="#" data-filter=".'. $filter->slug .'">'. $filter->name .'</a>';
                        }
                    ?>
                </div>
                <div class="clearfix mar_top5"></div>
                <div class="portfolioContainer">
                    <?php
                        $dem = 1;
                        if ( $block_query->have_posts() ) : while ( $block_query->have_posts() ) : $block_query->the_post();
                            global $post;
                            $post_id  = (int)get_post_thumbnail_id( );
                            $port_img = wp_get_attachment_image_src( $post_id, 'large' );
                            $type     = get_post_meta( $post->ID, "ainex_portfolio_type", true );
                            switch ($type) {
                                case 'image':
                                    $port_link  = get_post_meta( $post->ID, "ainex_portfolio_image", true );
                                    $clas_audio = '';
                                break;

                                case 'slider':
                                    $clas_audio = '';
                                    $port_link  = $port_img[0];
                                break;

                                case 'video':
                                    $port_link  = get_post_meta( $post->ID, "ainex_portfolio_video", true );
                                    $clas_audio = '';
                                break;

                                case 'soundcloud':
                                    $link_audio = get_post_meta( $post->ID, "ainex_portfolio_soundcloud", true );
                                    $clas_audio = ' fancybox.iframe';
                                    $port_link  = 'http://w.soundcloud.com/player/?url='. $link_audio .'&show_artwork=true';
                                break;

                                default:
                                    $port_link  = $port_img[0];
                                    $clas_audio = '';
                                break;
                            }

                            $taxonomy = 'dslc_projects_cats';
                            $post_id  = (int)get_the_id();

                            $post_terms = wp_get_object_terms( $post_id, $taxonomy, array( 'fields' => 'ids' ) );

                            if ( !empty( $post_terms ) && !is_wp_error( $post_terms ) ) {
                            $term_ids = implode( ',' , $post_terms );

                            $args = array(
                                'orderby'                  => 'name',
                                'order'                    => 'ASC',
                                'include'                  => $term_ids,
                                'taxonomy'                 => $taxonomy,
                            );

                            $categories = get_categories( $args );
                            $cats_slug  = array();
                                foreach ( $categories as $category ) {
                                  $cats_slug[] = $category->slug;
                                }
                            }

                        ?>
                            <div class="<?php echo implode(" ", $cats_slug); ?>">
                                <a class="fancybox fancybox-media<?php echo $clas_audio; ?>" href="<?php echo $port_link; ?>" data-fancybox-group="gallery" title="<?php the_title( ); ?>">
                                    <div class="imgWrap">
                                        <img src="<?php echo $port_img[0]; ?>" alt="" />
                                        <p class="imgDescription"><i class="icon-search icon-4x"></i></p>
                                        <h3><?php the_title( ); ?></h3>
                                    </div>
                                </a>
                            </div>
                    <?php
                        $dem++;
                        endwhile;
                        else :
                            /**
                             * Display no posts message if none are found.
                             */
                            get_template_part('loop/content','none');
                        endif;
                    ?>
                </div>

                <!-- isotope -->
                <script type="text/javascript">
                    jQuery(document).ready(function($) {
                        var container = $('.portfolioContainer');
                        container.isotope({
                            filter: '*',
                            animationOptions: {
                                duration: 750,
                                easing: 'linear',
                                queue: false
                            }
                        });
                        $('.portfolioFilter a').click(function(){
                            $('.portfolioFilter .current').removeClass('current');
                            $(this).addClass('current');
                            var selector = $(this).attr('data-filter');
                            container.isotope({
                                filter: selector,
                                animationOptions: {
                                    duration: 750,
                                    easing: 'linear',
                                    queue: false
                                }
                             });
                             return false;
                        });
                    });
                </script>
                <script type="text/javascript">
                    jQuery(document).ready(function($) {
                        /* Simple image gallery. Uses default settings */
                        $('.fancybox').fancybox();

                        /* media effects*/
                        $(document).ready(function() {
                            $('.fancybox-media').fancybox({
                                openEffect  : 'none',
                                closeEffect : 'none',
                                helpers : {
                                    media : {}
                                }
                            });
                        });
                        $(".fancybox.iframe").fancybox({
                            maxWidth    : 800,
                            maxHeight   : 600,
                            fitToView   : false,
                            width       : '70%',
                            height      : '70%',
                            autoSize    : false,
                            closeClick  : false,
                            openEffect  : 'none',
                            closeEffect : 'none'
                        });

                    });
                </script>
            </div>
        <?php else: ?>
            <div class="portfolio-wrapper">
                <?php
                    switch ($options['layout']) {
                        case '2':
                            $class_width = 'one_half';
                            $columns     = 2;
                        break;

                        case '3':
                            $class_width = 'one_third';
                            $columns     = 3;
                        break;
                        default:
                            $class_width = 'one_fourth';
                            $columns     = 4;
                        break;
                    }
                    $dem = 1;
                    if ( $block_query->have_posts() ) : while ( $block_query->have_posts() ) : $block_query->the_post();
                        global $post;
                        $post_id  = (int)get_post_thumbnail_id( );
                        $port_img = wp_get_attachment_image_src( $post_id, 'large' );
                        $type     = get_post_meta( $post->ID, "ainex_portfolio_type", true );
                        switch ($type) {
                            case 'image':
                                $port_link  = get_post_meta( $post->ID, "ainex_portfolio_image", true );
                                $clas_audio = '';
                            break;
                            case 'slider':
                                $clas_audio = '';
                                $port_link  = $port_img[0];
                            break;
                            case 'video':
                                $port_link  = get_post_meta( $post->ID, "ainex_portfolio_video", true );
                                $clas_audio = '';
                            break;
                            case 'soundcloud':
                                $link_audio = get_post_meta( $post->ID, "ainex_portfolio_soundcloud", true );
                                $clas_audio = ' fancybox.iframe';
                                $port_link  = 'http://w.soundcloud.com/player/?url='. $link_audio .'&show_artwork=true';
                            break;
                            default:
                                $port_link  = $port_img[0];
                                $clas_audio = '';
                            break;
                        }

                    ?>
                        <div class="<?php echo $class_width; ?> <?php if($dem%$columns==0){ echo 'last'; } ?>">
                            <div class="portfolio_image">
                                <i class="icon-search icon-2x"></i>
                                <a class="fancybox fancybox-media<?php echo $clas_audio; ?>" href="<?php echo $port_link; ?>" data-fancybox-group="gallery" title="<?php the_title( ); ?>">
                                    <img src="<?php echo $port_img[0]; ?>" alt="" />
                                </a>
                                <div class="title"><a href="<?php the_permalink(); ?>"><?php the_title( ); ?></a></div>
                            </div>
                        </div><!-- end section -->
                        <?php if ($dem%$columns==0): ?>
                            <div class="clearfix"></div>
                            <div class="mar_top5"></div>
                        <?php endif ?>
                <?php
                    $dem++;
                    endwhile;
                    else :
                        /**
                         * Display no posts message if none are found.
                         */
                        get_template_part('loop/content','none');
                    endif;
                ?>
            </div>
            <script type="text/javascript">
                jQuery(document).ready(function($) {
                    /* Simple image gallery. Uses default settings */
                    $('.fancybox').fancybox();

                    /* media effects*/
                    $(document).ready(function() {
                        $('.fancybox-media').fancybox({
                            openEffect  : 'none',
                            closeEffect : 'none',
                            helpers : {
                                media : {}
                            }
                        });
                    });
                    $(".fancybox.iframe").fancybox({
                        maxWidth    : 800,
                        maxHeight   : 600,
                        fitToView   : false,
                        width       : '70%',
                        height      : '70%',
                        autoSize    : false,
                        closeClick  : false,
                        openEffect  : 'none',
                        closeEffect : 'none'
                    });

                });
            </script>
        <?php endif ?>
        <div class="clearfix"></div>

		<?php
    	// REQUIRED
    	$this->module_end( $options );

    }
}