<?php

add_filter('pre_get_posts','aitDirMainQuery');
function aitDirMainQuery($query) {
	global $aitThemeOptions;
	// only main query
	if($query->is_main_query() && !$query->is_admin){

		if(isset($_GET['dir-search']) && isset($_GET['s']) && isset($_GET['categories']) && isset($_GET['locations'])){

			$query->set('post_type','ait-dir-item');

			$taxquery = array();
			$taxquery['relation'] = 'AND';

			if(isset($_GET['categories']) && !empty($_GET['categories'])){
				$taxquery[] = array(
					'taxonomy' => 'ait-dir-item-category',
					'field' => 'id',
					'terms' => array($_GET['categories']),
					'include_children' => true
				);
			}
			if(isset($_GET['locations']) && !empty($_GET['locations'])){
				$taxquery[] = array(
					'taxonomy' => 'ait-dir-item-location',
					'field' => 'id',
					'terms' => array($_GET['locations']),
					'include_children' => true
				);
			}
			$query->set('tax_query',$taxquery);

			$num = (isset($GLOBALS['aitThemeOptions']->search->searchItemsPerPage)) ? $GLOBALS['aitThemeOptions']->search->searchItemsPerPage : 10;
			$query->set('posts_per_page',$num);

			// filter only items by geolocation
			if(isset($_GET['geo'])){
				$category = $_GET['categories'];
				$location = $_GET['locations'];
				$params = array(
					'post_type'			=> 'ait-dir-item',
					'nopaging'			=>	true,
					'post_status'		=> 'publish'
				);
				$taxquery = array();
				$taxquery['relation'] = 'AND';
				if($category != 0){
					$taxquery[] = array(
						'taxonomy' => 'ait-dir-item-category',
						'field' => 'id',
						'terms' => $category,
						'include_children' => true
					);
				}
				if($location != 0){
					$taxquery[] = array(
						'taxonomy' => 'ait-dir-item-location',
						'field' => 'id',
						'terms' => $location,
						'include_children' => true
					);
				}
				if($category != 0 || $location != 0){
					$params['tax_query'] = $taxquery;
				}
				if($query->get('s') != ''){
					$params['s'] = $query->get('s');
				}
				$itemsQuery = new WP_Query();
				$items = $itemsQuery->query($params);
				$notIn = array();
				// add item details
				foreach ($items as $key => $item) {
					// options
					$item->optionsDir = get_post_meta($item->ID, '_ait-dir-item', true);
					// filter radius
					if(!isPointInRadius(intval($_GET['geo-radius']), floatval($_GET['geo-lat']), floatval($_GET['geo-lng']), $item->optionsDir['gpsLatitude'], $item->optionsDir['gpsLongitude'])){
						$notIn[] = $item->ID;
					}
				}
				// filter
				$query->set('post__not_in',$notIn);
			}

			// filter only items with date and price
			if ((!empty($_GET['from'])) || (!empty($_GET['to'])) || (!empty($_GET['price']))) {
				$itemsWithOffers = getItemsWithOffers($_GET['from'], $_GET['to'], $_GET['price']);
				$GLOBALS['findedOffers'] = $itemsWithOffers;
				if (count($itemsWithOffers) > 0) {
					$query->set('post__in', array_keys($itemsWithOffers));
				} else {
					$query->set('tag','xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
				}
			// show offers in items
			} else if (isset($GLOBALS['aitThemeOptions']->search->showAllOffers)) {
				$GLOBALS['findedOffers'] = getAllItemsWithOffers();
			}

		}

		// pagination
		if (!empty($_GET['pagination'])) {
			$query->set('posts_per_page',$_GET['pagination']);
		} else {
			if (isset($_GET['dir-search'])) {
				$num = (isset($aitThemeOptions->search->searchItemsPerPage)) ? $aitThemeOptions->search->searchItemsPerPage : 9;
				$query->set('posts_per_page',$num);
			}
			if (isset($query->query_vars["ait-dir-item-category"])) {
				$num = (isset($aitThemeOptions->directory->categoryItemsPerPage)) ? $aitThemeOptions->directory->categoryItemsPerPage : 10;
				$query->set('posts_per_page',$num);
			}
			if (isset($query->query_vars["ait-dir-item-location"])) {
				$num = (isset($aitThemeOptions->directory->locationItemsPerPage)) ? $aitThemeOptions->directory->locationItemsPerPage : 10;
				$query->set('posts_per_page',$num);
			}
		}

	}
	return $query;
}

/**
 * get items from DB
 * @param  integer $category category ID
 * @param  integer $location location ID
 * @param  string  $search   search keyword
 * @param  array   $radius   (radius in km, lat, lon)
 * @param  string  $from     date from
 * @param  string  $to       date to
 * @param  string  $price    maximal price
 * @return array             items
 */
function getItems($category = 0, $location = 0, $search = '', $radius = array(), $from = 0, $to = 0, $price = 0) {

	$params = array(
		'post_type'			=> 'ait-dir-item',
		'nopaging'			=>	true,
		'post_status'		=> 'publish'
	);

	// if FROM or TO or PRICE is defined then find item's offers
	if ((!empty($from)) || (!empty($to)) || (!empty($price))) {
		$itemsWithOffers = getItemsWithOffers($from, $to, $price);
		if (count($itemsWithOffers) > 0) {
			$params['post__in'] = array_keys($itemsWithOffers);
		} else {
			return array();
		}
	}

	$taxquery = array();
	$taxquery['relation'] = 'AND';
	if($category != 0){
		$taxquery[] = array(
			'taxonomy' => 'ait-dir-item-category',
			'field' => 'id',
			'terms' => $category,
			'include_children' => true
		);
	}
	if($location != 0){
		$taxquery[] = array(
			'taxonomy' => 'ait-dir-item-location',
			'field' => 'id',
			'terms' => $location,
			'include_children' => true
		);
	}
	if($category != 0 || $location != 0){
		$params['tax_query'] = $taxquery;
	}

	if($search != ''){
		$params['s'] = $search;
	}

	$itemsQuery = new WP_Query();
	$items = $itemsQuery->query($params);

	// add item details
	foreach ($items as $key => $item) {
		// options
		$item->optionsDir = get_post_meta($item->ID, '_ait-dir-item', true);
		// filter radius
		if(!empty($radius) && !isPointInRadius($radius[0], $radius[1], $radius[2], $item->optionsDir['gpsLatitude'], $item->optionsDir['gpsLongitude'])){
			unset($items[$key]);
		} else {
			// link
			$item->link = get_permalink($item->ID);
			// thumbnail
			$image = wp_get_attachment_image_src( get_post_thumbnail_id($item->ID), 'full' );
			if($image !== false){
				$item->thumbnailDir = $image[0];
			} else {
				$item->thumbnailDir = $GLOBALS['aitThemeOptions']->directory->defaultItemImage;
			}
			// marker
			$terms = wp_get_post_terms($item->ID, 'ait-dir-item-category');
			$termMarker = $GLOBALS['aitThemeOptions']->directoryMap->defaultMapMarkerImage;
			if(isset($terms[0])){
				$termMarker = getCategoryMeta("marker", intval($terms[0]->term_id));
			}
			$item->marker = $termMarker;
			// excerpt
			$item->excerptDir = aitGetPostExcerpt($item->post_excerpt,$item->post_content);
			// package class
			$item->packageClass = getItemPackageClass($item->post_author);
			// rating
			$item->rating = get_post_meta( $item->ID, 'rating', true );
		}
	}

	return $items;

}

// AJAX get items
add_action( 'wp_ajax_get_items', 'getItemsAjax' );
add_action( 'wp_ajax_nopriv_get_items', 'getItemsAjax' );
function getItemsAjax() {

	global $aitThemeOptions;

	$category = $_POST['category'];
	$location = $_POST['location'];
	$search = $_POST['search'];

	$from = (empty($_POST['from'])) ? 0 : $_POST['from'];
	$to = (empty($_POST['to'])) ? 0 : $_POST['to'];
	$price = (empty($_POST['price'])) ? 0 : $_POST['price'];

	$radius = (empty($_POST['radius'])) ? array() : $_POST['radius'];

	$items = getItems($category,$location,$search,$radius,$from,$to,$price);

	foreach ($items as $item) {
		$item->timthumbUrl = (isset($item->thumbnailDir)) ? AitImageResizer::resize($item->thumbnailDir, array('w' => 76, 'h' => 76)) : '';
		$item->timthumbUrlDetail = (isset($item->thumbnailDir)) ? AitImageResizer::resize($item->thumbnailDir, array('w' => 210, 'h' => 115)) : '';
	}

	$output = json_encode($items);
	// response output
	header( "Content-Type: application/json" );
	echo $output;
	exit;

}

// AJAX get items content - html
add_action( 'wp_ajax_get_items_content', 'getItemsAjaxContent' );
add_action( 'wp_ajax_nopriv_get_items_content', 'getItemsAjaxContent' );
function getItemsAjaxContent() {


}

$indentationChars = (isset($GLOBALS['aitThemeOptions']->search->hierarchicalIndentation) && ($GLOBALS['aitThemeOptions']->search->hierarchicalIndentation == 'space') ) ? '&nbsp;&nbsp;' : '-&nbsp;';
function aitGenerateHirerarchicalAutocomplete($categories, $in = 0) {
	global $indentationChars;
	$return = '';
	foreach ($categories as $cat) {
		$return .= '{ value: "' . $cat->term_id . '" , label: "';
		$indentation = '';
		for ($i=0; $i < $in; $i++) {
			$indentation .= $indentationChars;
		}
		$return .= $indentation . htmlspecialchars($cat->name, ENT_QUOTES, 'UTF-8', false) . '" },';
		if (!empty($cat->children)) {
			$return .= aitGenerateHirerarchicalAutocomplete($cat->children, $in + 1);
		}
	}
	return $return;
}

function aitSortTermsHierarchicaly(Array &$cats, Array &$into, $parentId = 0) {
	foreach ($cats as $i => $cat) {
		if ($cat->parent == $parentId) {
			$into[$cat->term_id] = $cat;
			unset($cats[$i]);
		}
	}
	foreach ($into as $topCat) {
		$topCat->children = array();
		aitSortTermsHierarchicaly($cats, $topCat->children, $topCat->term_id);
	}
}

function getDirItemsDetails($items) {
	foreach ($items as $item) {
		$item->link = get_permalink($item->ID);
		$image = wp_get_attachment_image_src( get_post_thumbnail_id($item->ID), 'full' );
		if($image !== false){
			$item->thumbnailDir = $image[0];
		} else {
			$item->thumbnailDir = $GLOBALS['aitThemeOptions']->directory->defaultItemImage;
		}
		$item->optionsDir = get_post_meta($item->ID, '_ait-dir-item', true);
		$item->excerptDir = aitGetPostExcerpt($item->post_excerpt,$item->post_content);
		$item->packageClass = getItemPackageClass($item->post_author);
		$item->rating = get_post_meta( $item->ID, 'rating', true );
	}
	return $items;
}

function isDirectoryUser($userToTest = null) {
	global $current_user;
	$user = (isset($userToTest)) ? $userToTest : $current_user;
	if( isset( $user->roles ) && is_array( $user->roles ) ) {
		if( in_array('directory_1', $user->roles) || in_array('directory_2', $user->roles) || in_array('directory_3', $user->roles) || in_array('directory_4', $user->roles) || in_array('directory_5', $user->roles) ) {
			return true;
		} else {
			return false;
		}
	} else {
		return false;
	}
}

/**
 * Maps functions
 **/

function isPointInRadius($radiusInKm, $cenLat, $cenLng, $lat, $lng) {
	$radiusInKm = intval($radiusInKm);
	$cenLat = floatval($cenLat);
	$cenLng = floatval($cenLng);
	$lat = floatval($lat);
	$lng = floatval($lng);
	$distance = ( 6371 * acos( cos( deg2rad($cenLat) ) * cos( deg2rad( $lat ) ) * cos( deg2rad( $lng ) - deg2rad($cenLng) ) + sin( deg2rad($cenLat) ) * sin( deg2rad( $lat ) ) ) );
	if($distance <= $radiusInKm){
		return true;
	} else {
		return false;
	}
}

function movePointByMeters($lat, $lng, $top, $left) {
	// Earth’s radius, sphere
	$R = 6378137;

	// Coordinate offsets in radians
	$dLat = floatval($top) / $R;
	$dLng = floatval($left) / ( $R * cos(pi() * floatval($lat) / 180) );

	// OffsetPosition, decimal degrees
	$nlat = floatval($lat) + ( $dLat * 180 / pi() );
	$nlng = floatval($lng) + ( $dLng * 180 / pi() );

	return array( 'lat' => $nlat, 'lng' => $nlng);
}

function parseMapOptions($adminOptions) {
	$options = array();

	$options['draggable'] = (isset($adminOptions->draggable)) ? "true" : "false";
	$options['mapTypeControl'] = (isset($adminOptions->mapTypeControl)) ? "true" : "false";
	$options['mapTypeId'] = 'google.maps.MapTypeId.'.$adminOptions->mapTypeId;
	$options['scrollwheel'] = (isset($adminOptions->scrollwheel)) ? "true" : "false";
	$options['panControl'] = (isset($adminOptions->panControl)) ? "true" : "false";
	$options['rotateControl'] = (isset($adminOptions->rotateControl)) ? "true" : "false";
	$options['scaleControl'] = (isset($adminOptions->scaleControl)) ? "true" : "false";
	$options['streetViewControl'] = (isset($adminOptions->streetViewControl)) ? "true" : "false";
	$options['zoomControl'] = (isset($adminOptions->zoomControl)) ? "true" : "false";

	return $options;
}

$aitCategoryMeta = array();
function getCategoryMeta( $what, $categoryID ) {
	global $aitCategoryMeta, $wpdb;

	// get cache = all values
	if(empty($aitCategoryMeta)){
		$results = $wpdb->get_results( "SELECT * FROM ".$wpdb->options." WHERE option_name LIKE 'ait\_dir\_item\_category\_%'" );
		foreach ($results as $r) {
			preg_match('!\d+!', $r->option_name, $matches);
			if(isset($matches[0])) {
				$id = (int)$matches[0];
				if(!isset($aitCategoryMeta[$id])) {
					$aitCategoryMeta[$id] = array();
				}
				if(strpos($r->option_name,'icon') !== false){
					$aitCategoryMeta[$id]['icon'] = $r->option_value;
				} else if(strpos($r->option_name,'marker') !== false){
					$aitCategoryMeta[$id]['marker'] = $r->option_value;
				} else {
					$aitCategoryMeta[$id]['excerpt'] = $r->option_value;
				}
			}
		}
	}

	switch ($what) {
		case 'icon':
			$anc = get_ancestors( $categoryID, 'ait-dir-item-category' );
			$icon = isset($aitCategoryMeta[$categoryID]) ? $aitCategoryMeta[$categoryID]['icon'] : '';
			if(empty($icon)){
				foreach ($anc as $value) {
					if(!empty($aitCategoryMeta[$value]['icon'])){
						$icon = $aitCategoryMeta[$value]['icon'];
						break;
					}
				}
				if(empty($icon)){
					$icon = $GLOBALS['aitThemeOptions']->directory->defaultCategoryIcon;
				}
			}
			return $icon;
		case 'marker':
			$anc = get_ancestors( $categoryID, 'ait-dir-item-category' );
			$marker = isset($aitCategoryMeta[$categoryID]) ? $aitCategoryMeta[$categoryID]['marker'] : '';
			if(empty($marker)){
				foreach ($anc as $value) {
					if(!empty($aitCategoryMeta[$value]['marker'])){
						$marker = $aitCategoryMeta[$value]['marker'];
						break;
					}
				}
				if(empty($marker)){
					$marker = $GLOBALS['aitThemeOptions']->directoryMap->defaultMapMarkerImage;
				}
			}
			return $marker;
		case 'excerpt':
			$excerpt = '';
			if(isset($aitCategoryMeta[$categoryID]['excerpt'])){
				$excerpt = $aitCategoryMeta[$categoryID]['excerpt'];
			}
			return $excerpt;
		default:
			break;
	}
}

$aitLocationMeta = array();
function getLocationMeta( $what, $categoryID ) {
	global $aitLocationMeta, $wpdb;

	// get cache = all values
	if(empty($aitLocationMeta)){
		$results = $wpdb->get_results( "SELECT * FROM ".$wpdb->options." WHERE option_name LIKE 'ait\_dir\_item\_location\_%'" );
		foreach ($results as $r) {
			preg_match('!\d+!', $r->option_name, $matches);
			if(isset($matches[0])) {
				$id = (int)$matches[0];
				if(!isset($aitLocationMeta[$id])) {
					$aitLocationMeta[$id] = array();
				}
				if(strpos($r->option_name,'icon') !== false){
					$aitLocationMeta[$id]['icon'] = $r->option_value;
				} else {
					$aitLocationMeta[$id]['excerpt'] = $r->option_value;
				}
			}
		}
	}

	switch ($what) {
		case 'icon':
			$anc = get_ancestors( $categoryID, 'ait-dir-item-location' );
			$icon = (isset($aitLocationMeta[$categoryID]['icon'])) ? $aitLocationMeta[$categoryID]['icon'] : '';
			if(empty($icon)){
				foreach ($anc as $value) {
					if(!empty($aitLocationMeta[$value]['icon'])){
						$icon = $aitLocationMeta[$value]['icon'];
						break;
					}
				}
				if(empty($icon)){
					$icon = (isset($GLOBALS['aitThemeOptions']->directory->defaultLocationIcon)) ? $GLOBALS['aitThemeOptions']->directory->defaultLocationIcon : '';
				}
			}
			return $icon;
		case 'excerpt':
			$excerpt = '';
			if(isset($aitLocationMeta[$categoryID]['excerpt'])){
				$excerpt = $aitLocationMeta[$categoryID]['excerpt'];
			}
			return $excerpt;
		default:
			break;
	}
}

/**
 * Get package class for item
 * @param  int $id author id
 * @return string     class
 */
function getItemPackageClass( $authorId ) {
	$user = new WP_User( $authorId );
	if(isset($user->roles[0])){
		return $user->roles[0];
	} else {
		return null;
	}
}

// Get manually excerpt or automatic excerpt for wp post
function aitGetPostExcerpt($excerpt, $content) {
	$newExcerpt = '';
	$trimExcerpt = trim($excerpt);
	if(empty($trimExcerpt)){
		$exc = substr($content, 0, 300);
		$pos = strrpos($exc, " ");
		$newExcerpt = substr($exc, 0, ($pos ? $pos : -1)) . "...";
	} else {
		$newExcerpt = $excerpt;
	}
	return $newExcerpt;
}

// Change author custom post type ait-dir-item fix
add_filter('wp_dropdown_users', 'aitChangeAuthorForItems');
function aitChangeAuthorForItems($output) {
	global $post;
	// Doing it only for the custom post type
	if(!empty($post) && $post->post_type == 'ait-dir-item') {
		$users = array();
		$users[0] = get_users(array('role'=>'administrator'));
		$users[1] = get_users(array('role'=>'directory_1'));
		$users[2] = get_users(array('role'=>'directory_2'));
		$users[3] = get_users(array('role'=>'directory_3'));
		$users[4] = get_users(array('role'=>'directory_4'));
		$users[5] = get_users(array('role'=>'directory_5'));

		$output = "<select id='post_author_override' name='post_author_override' class=''>";
		foreach($users as $userGroup) {
			foreach ($userGroup as $user) {
				$selected = ($user->ID == intval($post->post_author)) ? " selected='selected'" : "";
				$output .= "<option".$selected." value='".$user->ID."'>".$user->user_login."</option>";
			}
		}
		$output .= "</select>";
	}
	return $output;
}

/*
 * Contact owner functionality
 */
add_action('wp_ajax_nopriv_ait_contact_owner', 'aitDirContactOwner');
add_action('wp_ajax_ait_contact_owner', 'aitDirContactOwner');
function aitDirContactOwner() {

	// Check for nonce security
	$nonce = $_POST['nonce'];
	if ( ! wp_verify_nonce( $nonce, 'ajax-nonce' ) ) {
		_e( 'Bad nonce', 'ait' );
		exit();
	}

	if ((!empty($_POST['name'])) && (!empty($_POST['from'])) && (!empty($_POST['to'])) && (!empty($_POST['subject'])) && (!empty($_POST['message']))) {
		$headers = 'From: "' . $_POST['name'] . '" <' . $_POST['from'] . '>' . "\r\n";
		$result = wp_mail( strip_tags($_POST['to']), strip_tags($_POST['subject']), strip_tags($_POST['message']), $headers );
		if (!$result) {
			_e( 'Error with sending email', 'ait' );
			exit();
		}
	} else {
		_e( 'Please fill out email, subject and message', 'ait' );
		exit();
	}

	echo "success";
	exit();

}

function aitGetSpecialOffers($count = 5) {

	$offers = array();
	$args = array(
		'post_type' => 'ait-dir-item',
		'post_status' => 'publish',
		'nopaging' => true,
		'orderby' => 'rand'
	);
	$query = new WP_Query($args);
	$all = $query->posts;

	$i = 0;
	$j = 0;
	do {
		$options = get_post_meta($all[$i]->ID,'_ait-dir-item',true);
		if (isset($options['specialActive'])) {
			$offers[$i] = $all[$i];
			$offers[$i]->link = get_permalink($all[$i]->ID);
			$offers[$i]->options = $options;
			$j++;
		}
		$i++;
	} while (($i < count($all)) && ($j < $count));

	return $offers;

}

function aitGetBestPlaces($count = 3) {

	$items = array();
	$args = array(
		'post_type' => 'ait-dir-item',
		'post_status' => 'publish',
		'posts_per_page' => $count,
		'meta_key' => 'rating_full',
		'orderby' => 'meta_value_num',
		'order' => 'DESC'
	);
	$query = new WP_Query($args);
	$items = $query->posts;

	return getDirItemsDetails($items);

}

function aitGetRecentPlaces($count = 3) {

	$items = array();
	$args = array(
		'post_type' => 'ait-dir-item',
		'post_status' => 'publish',
		'posts_per_page' => $count,
		'orderby' => 'date',
		'order' => 'DESC'
	);
	$query = new WP_Query($args);
	$items = $query->posts;

	return getDirItemsDetails($items);

}

function aitGetPeopleRatings($count = 5) {

	$items = array();
	$args = array(
		'post_type' => 'ait-rating',
		'post_status' => 'publish',
		'posts_per_page' => $count,
		'orderby' => 'rand'
	);
	$query = new WP_Query($args);
	$items = $query->posts;

	$max = (isset($GLOBALS['aitThemeOptions']->rating->starsCount)) ? intval($GLOBALS['aitThemeOptions']->rating->starsCount) : 5;

	foreach ($items as $item) {
		$item->rating = array();
		$item->rating['val'] = get_post_meta($item->ID,'rating_mean_rounded',true);
		$item->rating['max'] = $max;

		$itemId = get_post_meta($item->ID,'post_id',true);
		$item->for = get_post($itemId);
	}

	return $items;

}

// Is User shortcode
function aitDirShortcodeIsUser( $params, $content = null) {
	extract( shortcode_atts( array(
		'role' => ''
	), $params ) );

	$current_user = wp_get_current_user();
	if (!empty($role)) {
		if (in_array($role, $current_user->roles)) {
			echo $content;
		}
	} else {
		if ( 0 != $current_user->ID ) {
			echo $content;
		}
	}

}
add_shortcode( 'is-user', 'aitDirShortcodeIsUser' );

// Is Guest shortcode
function aitDirShortcodeIsGuest( $params, $content = null) {
	extract( shortcode_atts( array(
	), $params ) );

	$current_user = wp_get_current_user();
	if ( 0 == $current_user->ID ) {
	    echo $content;
	}
}
add_shortcode( 'is-guest', 'aitDirShortcodeIsGuest' );


// ==================================================
// TourOperator functions
// --------------------------------------------------

/**
 * get item offers by price and date
 * @param  date $from  YYYYmmdd
 * @param  date $to  YYYYmmdd
 * @param  integer $price offer's price
 * @return array of arrays  items with offers
 */
function getItemsWithOffers($from = 0, $to = 0, $price = 0) {

	$items = array();
	$params = array(
		'post_type'			=> 'ait-dir-item-offer',
		'nopaging'			=>	true,
		'post_status'		=> 'publish',
		'meta_query'		=> array()
	);
	if(!empty($price)) {
		$params['meta_query'][] = array(
			'key' => 'ait_date_price',
			'value' => $price,
			'compare' => '<=',
			'type' => 'DECIMAL'
		);
	}
	if(!empty($from)) {
		$date = parseDatepickerDate($from);
		$params['meta_query'][] = array(
			'key' => 'ait_date_from',
			'value' => $date,
			'compare' => '>=',
			'type' => 'DATE'
		);
	}
	if(!empty($to)) {
		$date = parseDatepickerDate($to);
		$params['meta_query'][] = array(
			'key' => 'ait_date_to',
			'value' => $date,
			'compare' => '<=',
			'type' => 'DATE'
		);
	}

	$query = new WP_Query($params);
	$currency = (isset($GLOBALS['aitThemeOptions']->directory->currency)) ? $GLOBALS['aitThemeOptions']->directory->currency : '';
	$dateFormat = get_option( 'date_format', 'F j, Y' );
	foreach ($query->posts as $offer) {
		$itemId = get_post_meta( $offer->ID, 'ait_item_id', true );

		$offer->options = get_post_meta( $offer->ID, '_ait-dir-item-offer', true );
		$offer->from = date($dateFormat,strtotime($offer->options['from']));
		$offer->to = date($dateFormat,strtotime($offer->options['to']));
		$offer->price = $currency . ' ' . $offer->options['price'];
		$offer->capacity = (isset($offer->options['capacity'])) ? intval($offer->options['capacity']) : 0;

		if(!isset($items[$itemId])){
			$items[$itemId] = array();
		}
		$items[$itemId][] = $offer;
	}

	return $items;

}

function getAllItemsWithOffers() {

	$items = array();
	$params = array(
		'post_type'			=> 'ait-dir-item-offer',
		'nopaging'			=>	true,
		'post_status'		=> 'publish'
	);

	$query = new WP_Query($params);
	$currency = (isset($GLOBALS['aitThemeOptions']->directory->currency)) ? $GLOBALS['aitThemeOptions']->directory->currency : '';
	$dateFormat = get_option( 'date_format', 'F j, Y' );
	foreach ($query->posts as $offer) {
		$itemId = get_post_meta( $offer->ID, 'ait_item_id', true );

		$offer->options = get_post_meta( $offer->ID, '_ait-dir-item-offer', true );
		$offer->from = date($dateFormat,strtotime($offer->options['from']));
		$offer->to = date($dateFormat,strtotime($offer->options['to']));
		$offer->price = $currency . ' ' . $offer->options['price'];
		$offer->capacity = (isset($offer->options['capacity'])) ? intval($offer->options['capacity']) : 0;

		if(!isset($items[$itemId])){
			$items[$itemId] = array();
		}
		$items[$itemId][] = $offer;
	}

	return $items;

}

function parseDatepickerDate($date) {

	// get format from admin settings
	$format = (isset($GLOBALS['aitThemeOptions']->directory->dateFormat)) ? $GLOBALS['aitThemeOptions']->directory->dateFormat : "mm/dd/y";
	$firstMonth = (substr($format, 0,1) == "m") ? true : false;
	$delimiter = substr($format, 2, 1);
	$newDate = explode($delimiter, $date);
	$newDateString = ($firstMonth) ? "20".$newDate[2].$newDate[0].$newDate[1] : "20".$newDate[2].$newDate[1].$newDate[0];
	return $newDateString;

}

function getTourPrice($tourId, $withCurrency = true) {

	$lowestPrice = false;
	$offers = getTourOffers($tourId);
	// first get price from special offers
	foreach ($offers as $offer) {
		if ((isset($offer->options['special'])) && ((!$lowestPrice) || floatval($offer->options['price']) < $lowestPrice)) {
			$lowestPrice = floatval($offer->options['price']);
		}
	}
	// if special price isn't available then get price from normal offers
	if (!$lowestPrice) {
		foreach ($offers as $offer) {
			if ((!$lowestPrice) || (floatval($offer->options['price']) < $lowestPrice)) {
				$lowestPrice = floatval($offer->options['price']);
			}
		}
	}
	if (!$lowestPrice) return false;

	if ($withCurrency) {
		$currency = (isset($GLOBALS['aitThemeOptions']->directory->currency)) ? $GLOBALS['aitThemeOptions']->directory->currency : '';
		return $currency . ' ' . $lowestPrice;
	} else {
		return $lowestPrice;
	}

}

function getTourOffers($tourId) {

	global $aitThemeOptions;


	$params = array(
		'post_type'			=> 'ait-dir-item-offer',
		'nopaging'			=>	true,
		'post_status'		=> 'publish',
		'meta_query'		=> array(
			array(
				'key' => 'ait_item_id',
				'value' => $tourId,
				'compare' => '='
			)
		)
	);
	$query = new WP_Query($params);
	$currency = (isset($aitThemeOptions->directory->currency)) ? $aitThemeOptions->directory->currency : '';
	$dateFormat = get_option( 'date_format', 'F j, Y' );
	foreach ($query->posts as $offer) {
		$offer->options = get_post_meta( $offer->ID, '_ait-dir-item-offer', true );
		$offer->from = date($dateFormat,strtotime($offer->options['from']));
		$offer->to = date($dateFormat,strtotime($offer->options['to']));
		$offer->price = $currency . ' ' . $offer->options['price'];
		$offer->capacity = (isset($offer->options['capacity'])) ? intval($offer->options['capacity']) : 0;
	}
	return $query->posts;

}