<?php
function ait_tour_top( $params ) {
	extract( shortcode_atts( array (
		'type' => 'type',
		'cat' => '',
		'slug' => '',
		'title' => '',
		'content' => 200
	), $params ) );
	
	$queryParams = array(
		'post_type'     => 'ait-dir-item',
		'nopaging'      =>  true,
		'post_status'   => 'publish'
	);

	if (!empty($slug)) {
		$tax_type = ($type == 'destination') ? 'ait-dir-item-location' : 'ait-dir-item-category';
		$queryParams['tax_query'] = array(array(
			'taxonomy' => $tax_type,
			'field' => 'slug',
			'terms' => $slug
		));
	} else if (!empty($cat)) {
		$tax_type = ($type == 'destination') ? 'ait-dir-item-location' : 'ait-dir-item-category';
		$queryParams['tax_query'] = array(array(
			'taxonomy' => $tax_type,
			'field' => 'id',
			'terms' => $cat
		));
	}

	$query = new WP_Query($queryParams);

	if(count($query->posts) <= 0) return;

	$result = '<div class="ait-shortcode-items-top">';
	$result .= '<div class="title">'.$title.'</div>';
	$result .= '<div class="items-container"><ul class="items">';
	foreach ($query->posts as $post) {

		$result .= '<li class="item"><div class="item-container">';

		$result .= '<a href="'.get_permalink( $post->ID ).'">';

		$result .= '<div class="item-title">'.$post->post_title.'</div>';
		
		$price = getTourPrice( $post->ID );
		$priceDiv = (!empty($price)) ? '<div class="item-price"><span><span>'.$price.'</span></span></div>' : '' ;
		
		$thumbnail_id = get_post_thumbnail_id( $post->ID );
		$thumbnail_args = wp_get_attachment_image_src( $thumbnail_id, 'large' );
		if($thumbnail_args){
			$result .= '<div class="item-thumbnail"><img src="'.AitImageResizer::resize($thumbnail_args['0'], array("w" => 155, "h" => 103)).'" alt="" />'.$priceDiv.'</div>';
		}

		$post->rating = get_post_meta( $post->ID, 'rating', true );
		if($post->rating){
			$result .= '<div class="item-rating">';
			for($i = 1; $i <= $post->rating['max']; $i++){
				$result .= '<div class="star';
				if($i <= $post->rating['val']) { $result .= ' active'; }
				$result .= '"></div>';
			}
			$result .= '</div>';
		}

		$excerpt = aitGetPostExcerpt($post->post_excerpt,$post->post_content,100);
		$result .= '<div class="item-excerpt clear">'.$excerpt.'</div>';

		$result .= '</a>';

		$result .= '</div></li>';
	}
	$result .= '</ul></div></div>';
 
	return $result;
}
add_shortcode( "ait_tour_top", "ait_tour_top" );

function ait_tour_list( $params ) {
	extract( shortcode_atts( array (
		'type' => 'type',
		'cat' => '',
		'slug' => '',
		'count' => 5,
		'thumb_width' => 220,
		'thumb_height' => 123
	), $params ) );

	if(empty($cat) && empty($slug)) return;
	
	$queryParams = array(
		'post_type'     => 'ait-dir-item',
		'posts_per_page' => $count,
		'post_status'   => 'publish'
	);
	
	$tax_type = ($type == 'destination') ? 'ait-dir-item-location' : 'ait-dir-item-category';
	
	if (!empty($slug)) {
		$queryParams['tax_query'] = array(array(
			'taxonomy' => $tax_type,
			'field' => 'slug',
			'terms' => $slug
		));
		$term = get_term_by( 'slug', $slug, $tax_type );
	} else {
		$queryParams['tax_query'] = array(array(
			'taxonomy' => $tax_type,
			'field' => 'id',
			'terms' => $cat
		));
		$term = get_term_by( 'id', $cat, $tax_type );
	}

	$query = new WP_Query($queryParams);

	if(count($query->posts) <= 0) return;

	$result = '<div class="ait-shortcode-items-list">';

	$result .= '<a href="'.get_term_link( $term ).'">';

		$result .= '<div class="title">'.$term->name.'</div>';

		$thumbnail = get_option( 'ait_dir_item_category_'.$term->term_id.'_thumbnail', false );
		if($thumbnail){
			$result .= '<div class="thumbnail"><img src="'.AitImageResizer::resize($thumbnail, array("w" => $thumb_width, "h" => $thumb_height)).'" alt="" /></div>';
		}

	$result .= '</a>';

	$result .= '<div class="items">';
	foreach ($query->posts as $post) {

		$result .= '<div class="item">';
		$result .= '<a href="'.get_permalink( $post->ID ).'">';

			$result .= '<div class="item-title">'.$post->post_title.'</div>';
			
			$price = getTourPrice( $post->ID );
			if (!empty($price)) {
				$result .= '<div class="price">'.$price.'</div>';
			}

		$result .= '</a>';
		$result .= '</div>';
	}
	$result .= '</div></div>';

	return $result;
}
add_shortcode( "ait_tour_list", "ait_tour_list" );