<?
/**
 * Init Theme features
 */
function homePageThemeInit()
{
	$features= array('post-thumbnails', 'html5', 'title-tag', 'editor-style', 'widgets', 'menus');

	foreach ($features as $feature) {
		add_theme_support( $feature );
	}

	add_image_size( 'gallery', 1620, 810, true );

}

add_action( 'init', 'homePageThemeInit' );


/**
 * Add Scripts and Styles
 */
function homePageThemeScripts()
{
	// wp_enqueue_script( 'jQueryMobile', 'https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js', array( 'jquery' ), '3.3.5' );

	wp_enqueue_style( 'bootstrap', 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css', array(), '3.3.5' );
	wp_enqueue_script( 'bootstrap', 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js', array( 'jquery' ), '3.3.5' );


	wp_enqueue_script( 'owlCarousel', get_template_directory_uri() . '/js/owl.carousel.min.js', array( 'jquery' ), '2.0.0' );

	wp_enqueue_style( 'homepage', get_template_directory_uri() . '/style.css', array(), '1.0.0' );


	if( is_page_template( 'templates/frontpage.php' ) )
	{
		wp_enqueue_script( 'socialMenuScripts', get_template_directory_uri() . '/js/shareScript.js', array( 'jquery' ), '1.0.0' );
		wp_enqueue_style( 'homepageFrontpageStyle', get_template_directory_uri() . '/css/frontpage.css', array(), '1.0.0' );
		wp_enqueue_script( 'homepageFrontpageScript', get_template_directory_uri() . '/js/frontpage.js', array( 'jquery' ), '1.0.0', true );
		wp_enqueue_script( 'snapSVG', '//cdnjs.cloudflare.com/ajax/libs/snap.svg/0.3.0/snap.svg-min.js', array( 'jquery' ), '1.0.0', true );
	}

	wp_enqueue_script( 'google-Maps', '//maps.googleapis.com/maps/api/js?sensor=false', array( 'jquery' ), '1.0.0', false );
	wp_enqueue_script( 'g-maps-shortcode', get_template_directory_uri() . '/js/gMaps.js', array( 'jquery' ), '1.0.0', true );
	wp_enqueue_script( 'reference-gallery-shortcode', get_template_directory_uri() . '/js/referenceGallery.js', array( 'jquery' ), '1.0.0', true );
	wp_enqueue_script( 'contactForm7', get_template_directory_uri() . '/js/contactForm7.js', array( 'jquery' ), '1.0.0', true );
}
add_action( 'wp_enqueue_scripts', 'homePageThemeScripts' );

/**
 *
 */
function homePageAlterBodyClasses( $classes ) {
	$classes[] = wp_is_mobile() ? 'mobile' : 'desktop';

	return $classes;
}
add_filter( 'body_class', 'homePageAlterBodyClasses' );


function homePageGetMap($content)
{
	global $post;

	$map = '';
	$maps = get_field( "maps", $post->ID );

	if( $maps )
	{
		foreach ($maps as $key => $mapData)
		{
			unset( $maps[$key]['icon'] );
			if( $mapData['custom_icon'] )
			{
				$maps[$key]['icon'] = wp_get_attachment_image_src( $mapData['custom_icon'], 'full' );
			}
			else
			{
				$maps[$key]['icon'] = get_template_directory_uri().'/images/mapIcon.svg';
			}
			unset( $maps[$key]['custom_icon'] );
		}
		$map = '<div class="GMapWrapper row"><div id="postGMap'.$post->ID.'" class="GMap" data-settings="'.htmlspecialchars(json_encode($maps)).'"></div></div>';

	}

	return $map . $content;
}

// add_action( 'the_content', 'homePageGetMap' );
// add_action( 'the_post', 'homePageGetMap' );



function homePageGetBackground($template=false){
	global $post;

	$templateType = 'img';
	$background = get_field( "background_video", $post->ID );

	if( $background )
	{
		$background = [wp_get_attachment_url( $background )];
		$templateType = 'video';
	}
	else
	{
		$background = get_field( "background_img", $post->ID );
		if( !$background )
		{
			return null;
		}

		$background = wp_get_attachment_image_src( $background, 'full' );
	}

	if( !$template )
	{
		$template = $templateType == 'video' ?
			'<video class="background backgroundVideo" src="%1$s" loop></video>' :
			'<img class="background backgroundImage" src="%1$s" width="%2$d" height="%3$d">';
	}

	vprintf($template, $background);
}



function homepageGetMenuItems(){
	$frontpageMenuItems = wp_get_nav_menu_items( 'frontpageMenu', array(
		'order'						=> 'ASC',
		'orderby'					=> 'menu_order',
		'post_type'					=> 'nav_menu_item',
		'post_status'				=> 'publish',
		'output'					=> ARRAY_A,
		'output_key'				=> 'menu_order',
		'nopaging'					=> true,
		'update_post_term_cache'	=> false
	));
	$permanentLinks = array();

	echo '<ul id="frontpageMenu">';

	foreach ($frontpageMenuItems as $key => $item) {
		if( in_array('permanent_link', $item->classes) ){
			$permanentLinks[] = $item;
		}
		echo homepageGetMenuItem($item);
	}

	echo '</ul><div id="menuToggle"><img src="' . get_template_directory_uri() . '/images/menuIcons.svg"></div>';

	if( !empty($permanentLinks) ) {
		echo '<ul id="permanentLinks">';
		foreach ($permanentLinks as $key => $item) {
			echo homepageGetMenuItem($item);
		}
		echo '</ul>';
	}
}


function homepageGetMenuItem($item){
	$item->classes[] = 'item';
	$item->classes[] = $item->object .'-'.$item->object_id;
	$classes = implode(' ', array_unique($item->classes) );
	// var_dump($item);

	return '<li class="'.$classes.'" title="'.$item->title.'">
		<a href="#'.homepage_post_slug($item->object_id).'" data-id="#page-'.$item->object_id.'">'.$item->title.'</a>
	</li>';
}


function the_page_content_class(){
	global $post;
	$class = get_field( "page_width", $post->ID );

	return !$class ? 'container-fluid' : $class;
}

function homepage_post_slug($id) {
	$post_data = get_post($id, ARRAY_A);
	return $post_data['post_name'];
}



/**
 * Short codes
 */
function homePageGetMapShortCode( $atts ){
	return homePageGetMap('');
}
add_shortcode( 'gMap', 'homePageGetMapShortCode' );


function homePageGetReferenceGalleryShortCode( $atts ){
	global $wpdb;

	$query = "SELECT ID, post_title FROM $wpdb->posts WHERE post_type = 'references' AND post_status = 'publish'";

	$references = $wpdb->get_results($query, OBJECT);

	if( empty($references) )
	{
		return '';
	}

	$code = '<div class="referenceGallery">
			<a class="openGallery" href="#"><span>start gallery</span></a>
			<a class="closeGallery" href="#"><span>&nbsp;</span></a>
			<a class="prevGallery" href="#"><span>&nbsp;</span></a>
			<a class="nextGallery" href="#"><span>&nbsp;</span></a>
			<div class="referenceItems">
				<div class="referenceItemsWrapp">';

	foreach ($references as $ref) {
		$singleGallery = get_field( "gallery", $ref->ID );
		$video = get_field( "gallery_video", $ref->ID );
		$code .= '<div class="referenceItem">
				<div class="description">' . $ref->post_title . '</div>';
		if($singleGallery)
		{
			$code .= '<div class="thumb">'.get_the_post_thumbnail( $ref->ID, 'gallery' ).'</div>
				<div class="singleReferenceGallery">
					<div class="singleReferenceGalleryWrapp">';
			foreach ($singleGallery as $photo)
			{
				$src = !empty( $photo['sizes']['medium'] ) ? $photo['sizes']['medium'] : $photo['url'];
				$bigImgSrc = !empty( $photo['sizes']['gallery'] ) ? $photo['sizes']['gallery'] : $photo['url'];
				$code .= '<div class="item"><img src="'.$src.'" alt="'.$photo['title'].'" data-src-toggle="'.$bigImgSrc.'"></div>';
			}
			$code .= '</div>
				</div>';
		}
		else if( $video )
		{
			$code .= '<div class="thumb hasVideo"><div id="pageThumb'.$ref->ID.'" class="thumbVideo" data-video-id="'.$video.'">'.get_the_post_thumbnail( $ref->ID, 'gallery' ).'</div></div>';
		}
		$code .= '</div>';
	}

	$code .= '</div>
		</div>
		</div>';

	return $code;
}
add_shortcode( 'referenceGallery', 'homePageGetReferenceGalleryShortCode' );
