<?php

$latteParams['post'] = WpLatte::createPostEntity(
	$GLOBALS['wp_query']->post,
	array(
		'meta' => $GLOBALS['pageOptions'],
	)
);

/* SLIDERS :: START */
$sliderOptions = aitGetOptions($latteParams['post'],'slider');
$themeOptions = $latteParams['themeOptions'];

$term = '';
$term2 = '';
$term3 = '';
$term4 = '';
$termtax = '';
$termtax2 = '';
$termtax3 = '';
$termtax4 = '';
if(isset($sliderOptions->sliderType)){
	$term = $wpdb->get_results( "SELECT * FROM `".$wpdb->prefix."terms` WHERE `slug` LIKE '".$sliderOptions->sliderType."'" );
}
if(isset($sliderOptions->sliderType2)){
	$term2 = $wpdb->get_results( "SELECT * FROM `".$wpdb->prefix."terms` WHERE `slug` LIKE '".$sliderOptions->sliderType2."'" );
}
if(isset($sliderOptions->sliderType3)){
	$term3 = $wpdb->get_results( "SELECT * FROM `".$wpdb->prefix."terms` WHERE `slug` LIKE '".$sliderOptions->sliderType3."'" );
}
if(isset($sliderOptions->sliderType4)){
	$term4 = $wpdb->get_results( "SELECT * FROM `".$wpdb->prefix."terms` WHERE `slug` LIKE '".$sliderOptions->sliderType4."'" );
}
if(!empty($term)){
	$termtax = $wpdb->get_results( "SELECT * FROM `".$wpdb->prefix."term_taxonomy` WHERE `term_id` LIKE '".$term[0]->term_id."'" );
}
if(!empty($term2)){
	$termtax2 = $wpdb->get_results( "SELECT * FROM `".$wpdb->prefix."term_taxonomy` WHERE `term_id` LIKE '".$term2[0]->term_id."'" );
}
if(!empty($term3)){
	$termtax3 = $wpdb->get_results( "SELECT * FROM `".$wpdb->prefix."term_taxonomy` WHERE `term_id` LIKE '".$term3[0]->term_id."'" );
}
if(!empty($term4)){
	$termtax4 = $wpdb->get_results( "SELECT * FROM `".$wpdb->prefix."term_taxonomy` WHERE `term_id` LIKE '".$term4[0]->term_id."'" );
}

$latteParams['name'] = $term;
$latteParams['name2'] = $term2;
$latteParams['name3'] = $term3;
$latteParams['name4'] = $term4;

$latteParams['desc'] = $termtax;
$latteParams['desc2'] = $termtax2;
$latteParams['desc3'] = $termtax3;
$latteParams['desc4'] = $termtax4;

$termtaxGlob = '';
$termtaxGlob2 = '';
$termtaxGlob3 = '';
$termtaxGlob4 = '';

$termGlob = $wpdb->get_results( "SELECT * FROM `".$wpdb->prefix."terms` WHERE `slug` LIKE '".$themeOptions->header->sliderType."'" );
$termGlob2 = $wpdb->get_results( "SELECT * FROM `".$wpdb->prefix."terms` WHERE `slug` LIKE '".$themeOptions->header->sliderType2."'" );
$termGlob3 = $wpdb->get_results( "SELECT * FROM `".$wpdb->prefix."terms` WHERE `slug` LIKE '".$themeOptions->header->sliderType3."'" );
$termGlob4 = $wpdb->get_results( "SELECT * FROM `".$wpdb->prefix."terms` WHERE `slug` LIKE '".$themeOptions->header->sliderType4."'" );
if(!empty($termGlob)){
	$termtaxGlob = $wpdb->get_results( "SELECT * FROM `".$wpdb->prefix."term_taxonomy` WHERE `term_id` LIKE '".$termGlob[0]->term_id."'" );
}
if(!empty($termGlob2)){
	$termtaxGlob2 = $wpdb->get_results( "SELECT * FROM `".$wpdb->prefix."term_taxonomy` WHERE `term_id` LIKE '".$termGlob2[0]->term_id."'" );
}
if(!empty($termGlob3)){
	$termtaxGlob3 = $wpdb->get_results( "SELECT * FROM `".$wpdb->prefix."term_taxonomy` WHERE `term_id` LIKE '".$termGlob3[0]->term_id."'" );
}
if(!empty($termGlob4)){
	$termtaxGlob4 = $wpdb->get_results( "SELECT * FROM `".$wpdb->prefix."term_taxonomy` WHERE `term_id` LIKE '".$termGlob4[0]->term_id."'" );
}

$latteParams['nameGlob'] = $termGlob;
$latteParams['nameGlob2'] = $termGlob2;
$latteParams['nameGlob3'] = $termGlob3;
$latteParams['nameGlob4'] = $termGlob4;

$latteParams['descGlob'] = $termtaxGlob;
$latteParams['descGlob2'] = $termtaxGlob2;
$latteParams['descGlob3'] = $termtaxGlob3;
$latteParams['descGlob4'] = $termtaxGlob4;
/* SLIDERS :: END */

/* HEADER HEIGHT :: START */
$data = $wpdb->get_results( "SELECT * FROM `".$wpdb->prefix."postmeta` WHERE `meta_key` LIKE '_ait_slider_creator'" );
$maxHeight = 0;
foreach($data as $image){
	$imageData = unserialize($image->meta_value);
	if(!empty($imageData['topImage'])){
		if ($imageSize[$imageData['topImage']] = @getimagesize($imageData['topImage'])) {
			$imageHeight = $imageSize[$imageData['topImage']][1];
			if($imageHeight > $maxHeight){
				$maxHeight = $imageHeight;
			}
		} else {
			$src = THEME_DIR . "/" . substr($imageData['topImage'], strlen(THEME_URL) + 1);
			if(!is_file($src)){
				$u = wp_upload_dir();
				$baseUrl = $u['baseurl'];
				$baseDir = $u['basedir'];
				$src = $baseDir . "/" . substr($imageData['topImage'], strlen($baseUrl) + 1);
			}

			if(is_file($src)){
				$imageSize[$imageData['topImage']] = @getimagesize($src);
				$imageHeight = $imageSize[$imageData['topImage']][1];

				if($imageHeight > $maxHeight){
					$maxHeight = $imageHeight;
				}
			}
		}
	}
}
if($maxHeight == 0){$maxHeight = 420;}

$latteParams['imageSize'] = $maxHeight;
/* HEADER HEIGHT :: END */

$latteParams['post']->classes = implode(' ', get_post_class());

WPLatte::createTemplate(basename(__FILE__, '.php'), $latteParams)->render();
